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#ifndef StorageAreaMap_h
27#define StorageAreaMap_h
28
29#include "MessageReceiver.h"
30#include <WebCore/SecurityOrigin.h>
31#include <WebCore/StorageArea.h>
32#include <wtf/Forward.h>
33#include <wtf/HashCountedSet.h>
34#include <wtf/PassRefPtr.h>
35#include <wtf/RefCounted.h>
36
37namespace WebCore {
38class SecurityOrigin;
39class StorageMap;
40}
41
42namespace WebKit {
43
44class StorageAreaImpl;
45class StorageNamespaceImpl;
46
47class StorageAreaMap : public RefCounted<StorageAreaMap>, private IPC::MessageReceiver {
48public:
49    static PassRefPtr<StorageAreaMap> create(StorageNamespaceImpl*, PassRefPtr<WebCore::SecurityOrigin>);
50    ~StorageAreaMap();
51
52    WebCore::StorageType storageType() const { return m_storageType; }
53
54    unsigned length();
55    String key(unsigned index);
56    String item(const String& key);
57    void setItem(WebCore::Frame* sourceFrame, StorageAreaImpl* sourceArea, const String& key, const String& value, bool& quotaException);
58    void removeItem(WebCore::Frame* sourceFrame, StorageAreaImpl* sourceArea, const String& key);
59    void clear(WebCore::Frame* sourceFrame, StorageAreaImpl* sourceArea);
60    bool contains(const String& key);
61
62private:
63    StorageAreaMap(StorageNamespaceImpl*, PassRefPtr<WebCore::SecurityOrigin>);
64
65    // IPC::MessageReceiver
66    virtual void didReceiveMessage(IPC::Connection*, IPC::MessageDecoder&) override;
67
68    void didGetValues(uint64_t storageMapSeed);
69    void didSetItem(uint64_t storageMapSeed, const String& key, bool quotaError);
70    void didRemoveItem(uint64_t storageMapSeed, const String& key);
71    void didClear(uint64_t storageMapSeed);
72
73    void dispatchStorageEvent(uint64_t sourceStorageAreaID, const String& key, const String& oldValue, const String& newValue, const String& urlString);
74    void clearCache();
75
76    void resetValues();
77    void loadValuesIfNeeded();
78
79    bool shouldApplyChangeForKey(const String& key) const;
80    void applyChange(const String& key, const String& newValue);
81
82    void dispatchSessionStorageEvent(uint64_t sourceStorageAreaID, const String& key, const String& oldValue, const String& newValue, const String& urlString);
83    void dispatchLocalStorageEvent(uint64_t sourceStorageAreaID, const String& key, const String& oldValue, const String& newValue, const String& urlString);
84
85    uint64_t m_storageMapID;
86
87    WebCore::StorageType m_storageType;
88    uint64_t m_storageNamespaceID;
89    unsigned m_quotaInBytes;
90    RefPtr<WebCore::SecurityOrigin> m_securityOrigin;
91
92    RefPtr<WebCore::StorageMap> m_storageMap;
93
94    uint64_t m_currentSeed;
95    bool m_hasPendingClear;
96    bool m_hasPendingGetValues;
97    HashCountedSet<String> m_pendingValueChanges;
98};
99
100} // namespace WebKit
101
102#endif // StorageAreaMap_h
103