View Javadoc
1   /*
2    * Copyright (c) 2002-2017 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  package com.gargoylesoftware.htmlunit.javascript.host;
16  
17  import static com.gargoylesoftware.htmlunit.BrowserVersionFeatures.JS_STORAGE_GET_FROM_ITEMS;
18  import static com.gargoylesoftware.htmlunit.BrowserVersionFeatures.JS_STORAGE_PRESERVED_INCLUDED;
19  import static com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.CHROME;
20  import static com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.EDGE;
21  import static com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.FF;
22  
23  import java.util.Arrays;
24  import java.util.List;
25  import java.util.Map;
26  
27  import com.gargoylesoftware.htmlunit.javascript.SimpleScriptable;
28  import com.gargoylesoftware.htmlunit.javascript.configuration.JsxClass;
29  import com.gargoylesoftware.htmlunit.javascript.configuration.JsxConstructor;
30  import com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction;
31  import com.gargoylesoftware.htmlunit.javascript.configuration.JsxGetter;
32  
33  import net.sourceforge.htmlunit.corejs.javascript.Context;
34  import net.sourceforge.htmlunit.corejs.javascript.Scriptable;
35  
36  /**
37   * The JavaScript object that represents a Storage.
38   *
39   * @author Ahmed Ashour
40   * @author Marc Guillemot
41   */
42  @JsxClass
43  public class Storage extends SimpleScriptable {
44  
45      private static List<String> RESERVED_NAMES_ = Arrays.asList("clear", "key", "getItem", "length", "removeItem",
46          "setItem", "constructor", "toString", "toLocaleString", "valueOf", "hasOwnProperty", "propertyIsEnumerable",
47          "isPrototypeOf", "__defineGetter__", "__defineSetter__", "__lookupGetter__", "__lookupSetter__");
48  
49      private final Map<String, String> store_;
50  
51      /**
52       * Public default constructor only for the prototype.
53       */
54      @JsxConstructor({CHROME, FF, EDGE})
55      public Storage() {
56          store_ = null;
57      }
58  
59      /**
60       * Constructor.
61       * @param window the parent scope
62       * @param store the storage itself
63       */
64      public Storage(final Window window, final Map<String, String> store) {
65          store_ = store;
66          setParentScope(window);
67          setPrototype(window.getPrototype(Storage.class));
68      }
69  
70      /**
71       * {@inheritDoc}
72       */
73      @Override
74      public void put(final String name, final Scriptable start, final Object value) {
75          final boolean isReserved = RESERVED_NAMES_.contains(name);
76          if (store_ == null || isReserved) {
77              super.put(name, start, value);
78          }
79          if (store_ != null && (!isReserved || getBrowserVersion().hasFeature(JS_STORAGE_PRESERVED_INCLUDED))) {
80              setItem(name, Context.toString(value));
81          }
82      }
83  
84      /**
85       * {@inheritDoc}
86       */
87      @Override
88      public Object get(final String name, final Scriptable start) {
89          if (store_ == null
90                  || (RESERVED_NAMES_.contains(name) && !getBrowserVersion().hasFeature(JS_STORAGE_GET_FROM_ITEMS))) {
91              return super.get(name, start);
92          }
93          final Object value = getItem(name);
94          if (value != null) {
95              return value;
96          }
97          return super.get(name, start);
98      }
99  
100     /**
101      * Returns the length property.
102      * @return the length property
103      */
104     @JsxGetter
105     public int getLength() {
106         return getMap().size();
107     }
108 
109     /**
110      * Removes the specified key.
111      * @param key the item key
112      */
113     @JsxFunction
114     public void removeItem(final String key) {
115         getMap().remove(key);
116     }
117 
118     /**
119      * Returns the key of the specified index.
120      * @param index the index
121      * @return the key
122      */
123     @JsxFunction
124     public String key(final int index) {
125         int counter = 0;
126         for (final String key : getMap().keySet()) {
127             if (counter++ == index) {
128                 return key;
129             }
130         }
131         return null;
132     }
133 
134     private Map<String, String> getMap() {
135         return store_;
136     }
137 
138     /**
139      * Returns the value of the specified key.
140      * @param key the item key
141      * @return the value
142      */
143     @JsxFunction
144     public Object getItem(final String key) {
145         return getMap().get(key);
146     }
147 
148     /**
149      * Sets the item value.
150      * @param key the item key
151      * @param data the value
152      */
153     @JsxFunction
154     public void setItem(final String key, final String data) {
155         getMap().put(key, data);
156     }
157 
158     /**
159      * Clears all items.
160      */
161     @JsxFunction
162     public void clear() {
163         getMap().clear();
164     }
165 }