View Javadoc
1   /*
2    * Copyright (c) 2002-2025 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    * https://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 org.htmlunit.javascript.host;
16  
17  import static org.htmlunit.BrowserVersionFeatures.JS_STORAGE_PRESERVED_INCLUDED;
18  
19  import java.util.Arrays;
20  import java.util.HashSet;
21  import java.util.Map;
22  
23  import org.htmlunit.corejs.javascript.Scriptable;
24  import org.htmlunit.javascript.HtmlUnitScriptable;
25  import org.htmlunit.javascript.JavaScriptEngine;
26  import org.htmlunit.javascript.configuration.JsxClass;
27  import org.htmlunit.javascript.configuration.JsxConstructor;
28  import org.htmlunit.javascript.configuration.JsxFunction;
29  import org.htmlunit.javascript.configuration.JsxGetter;
30  import org.w3c.dom.DOMException;
31  
32  /**
33   * The JavaScript object that represents a Storage.
34   *
35   * @author Ahmed Ashour
36   * @author Marc Guillemot
37   * @author Ronald Brill
38   */
39  @JsxClass
40  public class Storage extends HtmlUnitScriptable {
41  
42      private static final HashSet<String> RESERVED_NAMES_ = new HashSet<>(Arrays.asList(
43          "clear", "key", "getItem", "length", "removeItem",
44          "setItem", "constructor", "toString", "toLocaleString", "valueOf", "hasOwnProperty", "propertyIsEnumerable",
45          "isPrototypeOf", "__defineGetter__", "__defineSetter__", "__lookupGetter__", "__lookupSetter__"));
46  
47      private static final long STORE_SIZE_KIMIT = 5_200_000;
48  
49      private final Map<String, String> store_;
50      private long storeSize_;
51  
52      /**
53       * Public default constructor only for the prototype.
54       */
55      public Storage() {
56          super();
57          store_ = null;
58      }
59  
60      /**
61       * JavaScript constructor.
62       */
63      @JsxConstructor
64      public void jsConstructor() {
65          // nothing to do
66      }
67  
68      /**
69       * Constructor.
70       * @param window the parent scope
71       * @param store the storage itself
72       */
73      public Storage(final Window window, final Map<String, String> store) {
74          super();
75          store_ = store;
76          storeSize_ = 0L;
77          setParentScope(window);
78          setPrototype(window.getPrototype(Storage.class));
79      }
80  
81      /**
82       * {@inheritDoc}
83       */
84      @Override
85      public void put(final String name, final Scriptable start, final Object value) {
86          final boolean isReserved = RESERVED_NAMES_.contains(name);
87          if (store_ == null || isReserved) {
88              super.put(name, start, value);
89          }
90          if (store_ != null && (!isReserved || getBrowserVersion().hasFeature(JS_STORAGE_PRESERVED_INCLUDED))) {
91              setItem(name, JavaScriptEngine.toString(value));
92          }
93      }
94  
95      /**
96       * {@inheritDoc}
97       */
98      @Override
99      public Object get(final String name, final Scriptable start) {
100         if (store_ == null || RESERVED_NAMES_.contains(name)) {
101             return super.get(name, start);
102         }
103         final Object value = getItem(name);
104         if (value != null) {
105             return value;
106         }
107         return super.get(name, start);
108     }
109 
110     /**
111      * Returns the length property.
112      * @return the length property
113      */
114     @JsxGetter
115     public int getLength() {
116         return store_.size();
117     }
118 
119     /**
120      * Removes the specified key.
121      * @param key the item key
122      */
123     @JsxFunction
124     public void removeItem(final String key) {
125         final String removed = store_.remove(key);
126         if (removed != null) {
127             storeSize_ -= removed.length();
128         }
129     }
130 
131     /**
132      * Returns the key of the specified index.
133      * @param index the index
134      * @return the key
135      */
136     @JsxFunction
137     public String key(final int index) {
138         int counter = 0;
139         for (final String key : store_.keySet()) {
140             if (counter++ == index) {
141                 return key;
142             }
143         }
144         return null;
145     }
146 
147     /**
148      * Returns the value of the specified key.
149      * @param key the item key
150      * @return the value
151      */
152     @JsxFunction
153     public Object getItem(final String key) {
154         return store_.get(key);
155     }
156 
157     /**
158      * Sets the item value.
159      * @param key the item key
160      * @param data the value
161      */
162     @JsxFunction
163     public void setItem(final String key, final String data) {
164         final long storeSize = storeSize_ + data.length();
165         if (storeSize > STORE_SIZE_KIMIT) {
166             throw JavaScriptEngine.throwAsScriptRuntimeEx(
167                     new DOMException((short) 22, "QuotaExceededError: Failed to execute 'setItem' on 'Storage': "
168                             + "Setting the value of '" + key + "' exceeded the quota."));
169         }
170         storeSize_ = storeSize;
171         store_.put(key, data);
172     }
173 
174     /**
175      * Clears all items.
176      */
177     @JsxFunction
178     public void clear() {
179         store_.clear();
180         storeSize_ = 0;
181     }
182 }