1 | /* |
2 | * Copyright (C) 2007, 2008, 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 | * |
8 | * 1. Redistributions of source code must retain the above copyright |
9 | * notice, this list of conditions and the following disclaimer. |
10 | * 2. Redistributions in binary form must reproduce the above copyright |
11 | * notice, this list of conditions and the following disclaimer in the |
12 | * documentation and/or other materials provided with the distribution. |
13 | * 3. Neither the name of Apple Inc. ("Apple") nor the names of |
14 | * its contributors may be used to endorse or promote products derived |
15 | * from this software without specific prior written permission. |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY |
18 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
19 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
20 | * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY |
21 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
22 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
23 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
24 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
28 | |
29 | #pragma once |
30 | |
31 | #include "DatabaseDetails.h" |
32 | #include "ExceptionOr.h" |
33 | #include "SQLiteDatabase.h" |
34 | #include "SecurityOriginData.h" |
35 | #include "SecurityOriginHash.h" |
36 | #include <wtf/HashCountedSet.h> |
37 | #include <wtf/HashMap.h> |
38 | #include <wtf/HashSet.h> |
39 | #include <wtf/WallTime.h> |
40 | #include <wtf/text/StringHash.h> |
41 | |
42 | namespace WebCore { |
43 | |
44 | class Database; |
45 | class DatabaseContext; |
46 | class DatabaseManagerClient; |
47 | class OriginLock; |
48 | class SecurityOrigin; |
49 | struct SecurityOriginData; |
50 | |
51 | enum class CurrentQueryBehavior { Interrupt, RunToCompletion }; |
52 | |
53 | class DatabaseTracker { |
54 | WTF_MAKE_NONCOPYABLE(DatabaseTracker); WTF_MAKE_FAST_ALLOCATED; |
55 | public: |
56 | // FIXME: This is a hack so we can easily delete databases from the UI process in WebKit2. |
57 | WEBCORE_EXPORT static std::unique_ptr<DatabaseTracker> trackerWithDatabasePath(const String& databasePath); |
58 | |
59 | static void initializeTracker(const String& databasePath); |
60 | |
61 | WEBCORE_EXPORT static DatabaseTracker& singleton(); |
62 | WEBCORE_EXPORT static bool isInitialized(); |
63 | // This singleton will potentially be used from multiple worker threads and the page's context thread simultaneously. To keep this safe, it's |
64 | // currently using 4 locks. In order to avoid deadlock when taking multiple locks, you must take them in the correct order: |
65 | // m_databaseGuard before quotaManager if both locks are needed. |
66 | // m_openDatabaseMapGuard before quotaManager if both locks are needed. |
67 | // m_databaseGuard and m_openDatabaseMapGuard currently don't overlap. |
68 | // notificationMutex() is currently independent of the other locks. |
69 | |
70 | ExceptionOr<void> canEstablishDatabase(DatabaseContext&, const String& name, unsigned long long estimatedSize); |
71 | ExceptionOr<void> retryCanEstablishDatabase(DatabaseContext&, const String& name, unsigned long long estimatedSize); |
72 | |
73 | void setDatabaseDetails(const SecurityOriginData&, const String& name, const String& displayName, unsigned long long estimatedSize); |
74 | WEBCORE_EXPORT String fullPathForDatabase(const SecurityOriginData&, const String& name, bool createIfDoesNotExist); |
75 | |
76 | Vector<Ref<Database>> openDatabases(); |
77 | void addOpenDatabase(Database&); |
78 | void removeOpenDatabase(Database&); |
79 | |
80 | unsigned long long maximumSize(Database&); |
81 | |
82 | WEBCORE_EXPORT void closeAllDatabases(CurrentQueryBehavior = CurrentQueryBehavior::RunToCompletion); |
83 | |
84 | WEBCORE_EXPORT Vector<SecurityOriginData> origins(); |
85 | WEBCORE_EXPORT Vector<String> databaseNames(const SecurityOriginData&); |
86 | |
87 | DatabaseDetails detailsForNameAndOrigin(const String&, const SecurityOriginData&); |
88 | |
89 | WEBCORE_EXPORT unsigned long long usage(const SecurityOriginData&); |
90 | WEBCORE_EXPORT unsigned long long quota(const SecurityOriginData&); |
91 | WEBCORE_EXPORT void setQuota(const SecurityOriginData&, unsigned long long); |
92 | RefPtr<OriginLock> originLockFor(const SecurityOriginData&); |
93 | |
94 | WEBCORE_EXPORT void deleteAllDatabasesImmediately(); |
95 | WEBCORE_EXPORT void deleteDatabasesModifiedSince(WallTime); |
96 | WEBCORE_EXPORT bool deleteOrigin(const SecurityOriginData&); |
97 | WEBCORE_EXPORT bool deleteDatabase(const SecurityOriginData&, const String& name); |
98 | |
99 | #if PLATFORM(IOS_FAMILY) |
100 | WEBCORE_EXPORT void removeDeletedOpenedDatabases(); |
101 | WEBCORE_EXPORT static bool deleteDatabaseFileIfEmpty(const String&); |
102 | |
103 | // MobileSafari will grab this mutex on the main thread before dispatching the task to |
104 | // clean up zero byte database files. Any operations to open new database will have to |
105 | // wait for that task to finish by waiting on this mutex. |
106 | static Lock& openDatabaseMutex(); |
107 | |
108 | WEBCORE_EXPORT static void emptyDatabaseFilesRemovalTaskWillBeScheduled(); |
109 | WEBCORE_EXPORT static void emptyDatabaseFilesRemovalTaskDidFinish(); |
110 | #endif |
111 | |
112 | void setClient(DatabaseManagerClient*); |
113 | |
114 | // From a secondary thread, must be thread safe with its data |
115 | void scheduleNotifyDatabaseChanged(const SecurityOriginData&, const String& name); |
116 | |
117 | void doneCreatingDatabase(Database&); |
118 | |
119 | private: |
120 | explicit DatabaseTracker(const String& databasePath); |
121 | |
122 | ExceptionOr<void> hasAdequateQuotaForOrigin(const SecurityOriginData&, unsigned long long estimatedSize); |
123 | |
124 | bool hasEntryForOriginNoLock(const SecurityOriginData&); |
125 | String fullPathForDatabaseNoLock(const SecurityOriginData&, const String& name, bool createIfDoesNotExist); |
126 | Vector<String> databaseNamesNoLock(const SecurityOriginData&); |
127 | unsigned long long quotaNoLock(const SecurityOriginData&); |
128 | |
129 | String trackerDatabasePath() const; |
130 | |
131 | enum TrackerCreationAction { |
132 | DontCreateIfDoesNotExist, |
133 | CreateIfDoesNotExist |
134 | }; |
135 | void openTrackerDatabase(TrackerCreationAction); |
136 | |
137 | String originPath(const SecurityOriginData&) const; |
138 | |
139 | bool hasEntryForDatabase(const SecurityOriginData&, const String& databaseIdentifier); |
140 | |
141 | bool addDatabase(const SecurityOriginData&, const String& name, const String& path); |
142 | |
143 | enum class DeletionMode { |
144 | Immediate, |
145 | #if PLATFORM(IOS_FAMILY) |
146 | // Deferred deletion is currently only supported on iOS |
147 | // (see removeDeletedOpenedDatabases etc, above). |
148 | Deferred, |
149 | Default = Deferred |
150 | #else |
151 | Default = Immediate |
152 | #endif |
153 | }; |
154 | |
155 | bool deleteOrigin(const SecurityOriginData&, DeletionMode); |
156 | bool deleteDatabaseFile(const SecurityOriginData&, const String& name, DeletionMode); |
157 | |
158 | void deleteOriginLockFor(const SecurityOriginData&); |
159 | |
160 | using DatabaseSet = HashSet<Database*>; |
161 | using DatabaseNameMap = HashMap<String, DatabaseSet*>; |
162 | using DatabaseOriginMap = HashMap<SecurityOriginData, DatabaseNameMap*>; |
163 | |
164 | Lock m_openDatabaseMapGuard; |
165 | mutable std::unique_ptr<DatabaseOriginMap> m_openDatabaseMap; |
166 | |
167 | // This lock protects m_database, m_originLockMap, m_databaseDirectoryPath, m_originsBeingDeleted, m_beingCreated, and m_beingDeleted. |
168 | Lock m_databaseGuard; |
169 | SQLiteDatabase m_database; |
170 | |
171 | using OriginLockMap = HashMap<String, RefPtr<OriginLock>>; |
172 | OriginLockMap m_originLockMap; |
173 | |
174 | String m_databaseDirectoryPath; |
175 | |
176 | DatabaseManagerClient* m_client { nullptr }; |
177 | |
178 | HashMap<SecurityOriginData, std::unique_ptr<HashCountedSet<String>>> m_beingCreated; |
179 | HashMap<SecurityOriginData, std::unique_ptr<HashSet<String>>> m_beingDeleted; |
180 | HashSet<SecurityOriginData> m_originsBeingDeleted; |
181 | bool isDeletingDatabaseOrOriginFor(const SecurityOriginData&, const String& name); |
182 | void recordCreatingDatabase(const SecurityOriginData&, const String& name); |
183 | void doneCreatingDatabase(const SecurityOriginData&, const String& name); |
184 | bool creatingDatabase(const SecurityOriginData&, const String& name); |
185 | bool canDeleteDatabase(const SecurityOriginData&, const String& name); |
186 | void recordDeletingDatabase(const SecurityOriginData&, const String& name); |
187 | void doneDeletingDatabase(const SecurityOriginData&, const String& name); |
188 | bool isDeletingDatabase(const SecurityOriginData&, const String& name); |
189 | bool canDeleteOrigin(const SecurityOriginData&); |
190 | bool isDeletingOrigin(const SecurityOriginData&); |
191 | void recordDeletingOrigin(const SecurityOriginData&); |
192 | void doneDeletingOrigin(const SecurityOriginData&); |
193 | |
194 | static void scheduleForNotification(); |
195 | static void notifyDatabasesChanged(); |
196 | }; |
197 | |
198 | } // namespace WebCore |
199 | |