1 /*
2 * Copyright (c) 2002-2026 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;
16
17 import java.io.IOException;
18 import java.io.ObjectInputStream;
19 import java.io.Serializable;
20 import java.net.URL;
21 import java.util.HashMap;
22 import java.util.LinkedHashMap;
23 import java.util.Map;
24
25 /**
26 * Holder for different types of storages.
27 * <p><span style="color:red">Experimental API: May be changed in next release!</span></p>
28 *
29 * @author Ahmed Ashour
30 * @author Ronald Brill
31 * @author Marc Guillemot
32 */
33 public class StorageHolder implements Serializable {
34
35 /**
36 * Type for Storage.
37 */
38 public enum Type {
39 /** The type for window.localStorage. */
40 LOCAL_STORAGE,
41 /** The type for window.sessionStorage. */
42 SESSION_STORAGE
43 }
44
45 private final Map<String, Map<String, String>> localStorage_ = new HashMap<>();
46 private transient Map<String, Map<String, String>> sessionStorage_ = new HashMap<>();
47
48 /**
49 * Gets the store of the give type for the page.
50 * @param storageType the type
51 * @param page the page
52 * @return the store
53 */
54 public Map<String, String> getStore(final Type storageType, final Page page) {
55 return switch (storageType) {
56 case LOCAL_STORAGE -> getLocalStorage(page.getUrl());
57 case SESSION_STORAGE -> getSessionStorage(page.getEnclosingWindow());
58 };
59 }
60
61 /**
62 * Gets the local storage (map).
63 * @param url the page origin
64 * @return the store
65 */
66 public Map<String, String> getLocalStorage(final URL url) {
67 synchronized (localStorage_) {
68 final String key = url.getProtocol() + "://" + url.getHost();
69 return localStorage_.computeIfAbsent(key, k -> new LinkedHashMap<>());
70 }
71 }
72
73 /**
74 * Gets the local storage (map).
75 * @param webWindow the window
76 * @return the store
77 */
78 public Map<String, String> getSessionStorage(final WebWindow webWindow) {
79 synchronized (sessionStorage_) {
80 final WebWindow topWindow = webWindow.getTopWindow();
81 final String key = Integer.toHexString(topWindow.hashCode());
82 return sessionStorage_.computeIfAbsent(key, k -> new LinkedHashMap<>());
83 }
84 }
85
86 private void readObject(final ObjectInputStream ois) throws ClassNotFoundException, IOException {
87 ois.defaultReadObject();
88
89 sessionStorage_ = new HashMap<>();
90 }
91 }