1/*
2 * Copyright (C) 2008 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. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#ifndef StorageAreaImpl_h
27#define StorageAreaImpl_h
28
29#include "StorageArea.h"
30#include "Timer.h"
31
32#include <wtf/HashMap.h>
33#include <wtf/PassRefPtr.h>
34#include <wtf/RefPtr.h>
35
36namespace WebCore {
37
38class SecurityOrigin;
39class StorageMap;
40class StorageAreaSync;
41
42class StorageAreaImpl : public StorageArea {
43public:
44    static PassRefPtr<StorageAreaImpl> create(StorageType, PassRefPtr<SecurityOrigin>, PassRefPtr<StorageSyncManager>, unsigned quota);
45    virtual ~StorageAreaImpl();
46
47    virtual unsigned length() override;
48    virtual String key(unsigned index) override;
49    virtual String item(const String& key) override;
50    virtual void setItem(Frame* sourceFrame, const String& key, const String& value, bool& quotaException) override;
51    virtual void removeItem(Frame* sourceFrame, const String& key) override;
52    virtual void clear(Frame* sourceFrame) override;
53    virtual bool contains(const String& key) override;
54
55    virtual bool canAccessStorage(Frame* sourceFrame) override;
56    virtual StorageType storageType() const override;
57
58    virtual size_t memoryBytesUsedByCache() override;
59
60    virtual void incrementAccessCount();
61    virtual void decrementAccessCount();
62    virtual void closeDatabaseIfIdle();
63
64    PassRefPtr<StorageAreaImpl> copy();
65    void close();
66
67    // Only called from a background thread.
68    void importItems(const HashMap<String, String>& items);
69
70    // Used to clear a StorageArea and close db before backing db file is deleted.
71    void clearForOriginDeletion();
72
73    void sync();
74
75private:
76    StorageAreaImpl(StorageType, PassRefPtr<SecurityOrigin>, PassRefPtr<StorageSyncManager>, unsigned quota);
77    explicit StorageAreaImpl(StorageAreaImpl*);
78
79    void blockUntilImportComplete() const;
80    void closeDatabaseTimerFired(Timer<StorageAreaImpl>*);
81
82    void dispatchStorageEvent(const String& key, const String& oldValue, const String& newValue, Frame* sourceFrame);
83
84    StorageType m_storageType;
85    RefPtr<SecurityOrigin> m_securityOrigin;
86    RefPtr<StorageMap> m_storageMap;
87
88    RefPtr<StorageAreaSync> m_storageAreaSync;
89    RefPtr<StorageSyncManager> m_storageSyncManager;
90
91#ifndef NDEBUG
92    bool m_isShutdown;
93#endif
94    unsigned m_accessCount;
95    Timer<StorageAreaImpl> m_closeDatabaseTimer;
96};
97
98} // namespace WebCore
99
100#endif // StorageAreaImpl_h
101