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;
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 switch (storageType) {
56 case LOCAL_STORAGE:
57 return getLocalStorage(page.getUrl());
58
59 case SESSION_STORAGE:
60 return getSessionStorage(page.getEnclosingWindow());
61
62 default:
63 return null;
64 }
65 }
66
67 /**
68 * Gets the local storage (map).
69 * @param url the page origin
70 * @return the store
71 */
72 public Map<String, String> getLocalStorage(final URL url) {
73 synchronized (localStorage_) {
74 final String key = url.getProtocol() + "://" + url.getHost();
75 return localStorage_.computeIfAbsent(key, k -> new LinkedHashMap<>());
76 }
77 }
78
79 /**
80 * Gets the local storage (map).
81 * @param webWindow the window
82 * @return the store
83 */
84 public Map<String, String> getSessionStorage(final WebWindow webWindow) {
85 synchronized (sessionStorage_) {
86 final WebWindow topWindow = webWindow.getTopWindow();
87 final String key = Integer.toHexString(topWindow.hashCode());
88 return sessionStorage_.computeIfAbsent(key, k -> new LinkedHashMap<>());
89 }
90 }
91
92 private void readObject(final ObjectInputStream ois) throws ClassNotFoundException, IOException {
93 ois.defaultReadObject();
94
95 sessionStorage_ = new HashMap<>();
96 }
97 }