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 const WebCore::SecurityOrigin& securityOrigin() const { return m_securityOrigin.get(); }
62
63private:
64 StorageAreaMap(StorageNamespaceImpl*, Ref<WebCore::SecurityOrigin>&&);
65
66 // IPC::MessageReceiver
67 void didReceiveMessage(IPC::Connection&, IPC::Decoder&) override;
68
69 void didGetValues(uint64_t storageMapSeed);
70 void didSetItem(uint64_t storageMapSeed, const String& key, bool quotaError);
71 void didRemoveItem(uint64_t storageMapSeed, const String& key);
72 void didClear(uint64_t storageMapSeed);
73
74 void dispatchStorageEvent(uint64_t sourceStorageAreaID, const String& key, const String& oldValue, const String& newValue, const String& urlString);
75 void clearCache();
76
77 void resetValues();
78 void loadValuesIfNeeded();
79
80 bool shouldApplyChangeForKey(const String& key) const;
81 void applyChange(const String& key, const String& newValue);
82
83 void dispatchSessionStorageEvent(uint64_t sourceStorageAreaID, const String& key, const String& oldValue, const String& newValue, const String& urlString);
84 void dispatchLocalStorageEvent(uint64_t sourceStorageAreaID, const String& key, const String& oldValue, const String& newValue, const String& urlString);
85
86 Ref<StorageNamespaceImpl> m_storageNamespace;
87
88 uint64_t m_storageMapID;
89
90 WebCore::StorageType m_storageType;
91 uint64_t m_storageNamespaceID;
92 unsigned m_quotaInBytes;
93 Ref<WebCore::SecurityOrigin> m_securityOrigin;
94
95 RefPtr<WebCore::StorageMap> m_storageMap;
96
97 uint64_t m_currentSeed;
98 bool m_hasPendingClear;
99 bool m_hasPendingGetValues;
100 HashCountedSet<String> m_pendingValueChanges;
101};
102
103} // namespace WebKit
104