1/*
2 * Copyright (C) 2013 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#pragma once
27
28#include "MessageReceiver.h"
29#include <WebCore/SecurityOrigin.h>
30#include <WebCore/StorageArea.h>
31#include <wtf/Forward.h>
32#include <wtf/HashCountedSet.h>
33#include <wtf/RefCounted.h>
34#include <wtf/RefPtr.h>
35
36namespace WebCore {
37class SecurityOrigin;
38class StorageMap;
39}
40
41namespace WebKit {
42
43class StorageAreaImpl;
44class StorageNamespaceImpl;
45
46class StorageAreaMap : public RefCounted<StorageAreaMap>, private IPC::MessageReceiver {
47public:
48 static Ref<StorageAreaMap> create(StorageNamespaceImpl*, Ref<WebCore::SecurityOrigin>&&);
49 ~StorageAreaMap();
50
51 WebCore::StorageType storageType() const { return m_storageType; }
52
53 unsigned length();
54 String key(unsigned index);
55 String item(const String& key);
56 void setItem(WebCore::Frame* sourceFrame, StorageAreaImpl* sourceArea, const String& key, const String& value, bool& quotaException);
57 void removeItem(WebCore::Frame* sourceFrame, StorageAreaImpl* sourceArea, const String& key);
58 void clear(WebCore::Frame* sourceFrame, StorageAreaImpl* sourceArea);
59 bool contains(const String& key);
60
61 // IPC::MessageReceiver
62 void didReceiveMessage(IPC::Connection&, IPC::Decoder&) override;
63
64 const WebCore::SecurityOrigin& securityOrigin() const { return m_securityOrigin.get(); }
65
66 void connect();
67 void disconnect();
68 uint64_t identifier() const { return m_storageMapID; }
69
70private:
71 StorageAreaMap(StorageNamespaceImpl*, Ref<WebCore::SecurityOrigin>&&);
72
73 void didGetValues(uint64_t storageMapSeed);
74 void didSetItem(uint64_t storageMapSeed, const String& key, bool quotaError);
75 void didRemoveItem(uint64_t storageMapSeed, const String& key);
76 void didClear(uint64_t storageMapSeed);
77
78 void dispatchStorageEvent(uint64_t sourceStorageAreaID, const String& key, const String& oldValue, const String& newValue, const String& urlString);
79 void clearCache();
80
81 void resetValues();
82 void loadValuesIfNeeded();
83
84 bool shouldApplyChangeForKey(const String& key) const;
85 void applyChange(const String& key, const String& newValue);
86
87 void dispatchSessionStorageEvent(uint64_t sourceStorageAreaID, const String& key, const String& oldValue, const String& newValue, const String& urlString);
88 void dispatchLocalStorageEvent(uint64_t sourceStorageAreaID, const String& key, const String& oldValue, const String& newValue, const String& urlString);
89
90 Ref<StorageNamespaceImpl> m_storageNamespace;
91
92 uint64_t m_storageMapID;
93
94 WebCore::StorageType m_storageType;
95 uint64_t m_storageNamespaceID;
96 unsigned m_quotaInBytes;
97 Ref<WebCore::SecurityOrigin> m_securityOrigin;
98
99 RefPtr<WebCore::StorageMap> m_storageMap;
100
101 uint64_t m_currentSeed;
102 bool m_hasPendingClear;
103 bool m_hasPendingGetValues;
104 HashCountedSet<String> m_pendingValueChanges;
105
106 bool m_isDisconnected { true };
107};
108
109} // namespace WebKit
110