1 | /* |
2 | * Copyright (C) 2008, 2009, 2010, 2013, 2019 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 <WebCore/SQLiteDatabase.h> |
29 | #include <WebCore/SecurityOriginData.h> |
30 | #include <wtf/Forward.h> |
31 | #include <wtf/HashMap.h> |
32 | #include <wtf/RefPtr.h> |
33 | #include <wtf/ThreadSafeRefCounted.h> |
34 | #include <wtf/WorkQueue.h> |
35 | |
36 | namespace WebCore { |
37 | class SecurityOrigin; |
38 | class StorageMap; |
39 | class SuddenTerminationDisabler; |
40 | } |
41 | |
42 | namespace WebKit { |
43 | |
44 | class LocalStorageDatabaseTracker; |
45 | |
46 | class LocalStorageDatabase : public ThreadSafeRefCounted<LocalStorageDatabase> { |
47 | public: |
48 | static Ref<LocalStorageDatabase> create(Ref<WorkQueue>&&, Ref<LocalStorageDatabaseTracker>&&, const WebCore::SecurityOriginData&); |
49 | ~LocalStorageDatabase(); |
50 | |
51 | // Will block until the import is complete. |
52 | void importItems(WebCore::StorageMap&); |
53 | |
54 | void setItem(const String& key, const String& value); |
55 | void removeItem(const String& key); |
56 | void clear(); |
57 | |
58 | // Will block until all pending changes have been written to disk. |
59 | void close(); |
60 | |
61 | private: |
62 | LocalStorageDatabase(Ref<WorkQueue>&&, Ref<LocalStorageDatabaseTracker>&&, const WebCore::SecurityOriginData&); |
63 | |
64 | enum DatabaseOpeningStrategy { |
65 | CreateIfNonExistent, |
66 | SkipIfNonExistent |
67 | }; |
68 | bool tryToOpenDatabase(DatabaseOpeningStrategy); |
69 | void openDatabase(DatabaseOpeningStrategy); |
70 | |
71 | bool migrateItemTableIfNeeded(); |
72 | |
73 | void itemDidChange(const String& key, const String& value); |
74 | |
75 | void scheduleDatabaseUpdate(); |
76 | void updateDatabase(); |
77 | void updateDatabaseWithChangedItems(const HashMap<String, String>&); |
78 | |
79 | bool databaseIsEmpty(); |
80 | |
81 | Ref<WorkQueue> m_queue; |
82 | Ref<LocalStorageDatabaseTracker> m_tracker; |
83 | WebCore::SecurityOriginData m_securityOrigin; |
84 | |
85 | String m_databasePath; |
86 | WebCore::SQLiteDatabase m_database; |
87 | bool m_failedToOpenDatabase; |
88 | bool m_didImportItems; |
89 | bool m_isClosed; |
90 | |
91 | bool m_didScheduleDatabaseUpdate; |
92 | bool m_shouldClearItems; |
93 | HashMap<String, String> m_changedItems; |
94 | |
95 | std::unique_ptr<WebCore::SuddenTerminationDisabler> m_disableSuddenTerminationWhileWritingToLocalStorage; |
96 | }; |
97 | |
98 | |
99 | } // namespace WebKit |
100 | |