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 | #include "config.h" |
27 | #include "LocalStorageDatabase.h" |
28 | |
29 | #include "LocalStorageDatabaseTracker.h" |
30 | #include <WebCore/SQLiteStatement.h> |
31 | #include <WebCore/SQLiteTransaction.h> |
32 | #include <WebCore/SecurityOrigin.h> |
33 | #include <WebCore/StorageMap.h> |
34 | #include <WebCore/SuddenTermination.h> |
35 | #include <wtf/FileSystem.h> |
36 | #include <wtf/RefPtr.h> |
37 | #include <wtf/WorkQueue.h> |
38 | #include <wtf/text/StringHash.h> |
39 | #include <wtf/text/WTFString.h> |
40 | |
41 | static const auto databaseUpdateInterval = 1_s; |
42 | |
43 | static const int maximumItemsToUpdate = 100; |
44 | |
45 | namespace WebKit { |
46 | using namespace WebCore; |
47 | |
48 | Ref<LocalStorageDatabase> LocalStorageDatabase::create(Ref<WorkQueue>&& queue, Ref<LocalStorageDatabaseTracker>&& tracker, const SecurityOriginData& securityOrigin) |
49 | { |
50 | return adoptRef(*new LocalStorageDatabase(WTFMove(queue), WTFMove(tracker), securityOrigin)); |
51 | } |
52 | |
53 | LocalStorageDatabase::LocalStorageDatabase(Ref<WorkQueue>&& queue, Ref<LocalStorageDatabaseTracker>&& tracker, const SecurityOriginData& securityOrigin) |
54 | : m_queue(WTFMove(queue)) |
55 | , m_tracker(WTFMove(tracker)) |
56 | , m_securityOrigin(securityOrigin) |
57 | , m_databasePath(m_tracker->databasePath(m_securityOrigin)) |
58 | , m_failedToOpenDatabase(false) |
59 | , m_didImportItems(false) |
60 | , m_isClosed(false) |
61 | , m_didScheduleDatabaseUpdate(false) |
62 | , m_shouldClearItems(false) |
63 | { |
64 | } |
65 | |
66 | LocalStorageDatabase::~LocalStorageDatabase() |
67 | { |
68 | ASSERT(m_isClosed); |
69 | } |
70 | |
71 | void LocalStorageDatabase::openDatabase(DatabaseOpeningStrategy openingStrategy) |
72 | { |
73 | ASSERT(!m_database.isOpen()); |
74 | ASSERT(!m_failedToOpenDatabase); |
75 | |
76 | if (!tryToOpenDatabase(openingStrategy)) { |
77 | m_failedToOpenDatabase = true; |
78 | return; |
79 | } |
80 | |
81 | if (m_database.isOpen()) |
82 | m_tracker->didOpenDatabaseWithOrigin(m_securityOrigin); |
83 | } |
84 | |
85 | bool LocalStorageDatabase::tryToOpenDatabase(DatabaseOpeningStrategy openingStrategy) |
86 | { |
87 | ASSERT(!RunLoop::isMain()); |
88 | if (!FileSystem::fileExists(m_databasePath) && openingStrategy == SkipIfNonExistent) |
89 | return true; |
90 | |
91 | if (m_databasePath.isEmpty()) { |
92 | LOG_ERROR("Filename for local storage database is empty - cannot open for persistent storage" ); |
93 | return false; |
94 | } |
95 | |
96 | if (!m_database.open(m_databasePath)) { |
97 | LOG_ERROR("Failed to open database file %s for local storage" , m_databasePath.utf8().data()); |
98 | return false; |
99 | } |
100 | |
101 | // Since a WorkQueue isn't bound to a specific thread, we have to disable threading checks |
102 | // even though we never access the database from different threads simultaneously. |
103 | m_database.disableThreadingChecks(); |
104 | |
105 | if (!migrateItemTableIfNeeded()) { |
106 | // We failed to migrate the item table. In order to avoid trying to migrate the table over and over, |
107 | // just delete it and start from scratch. |
108 | if (!m_database.executeCommand("DROP TABLE ItemTable" )) |
109 | LOG_ERROR("Failed to delete table ItemTable for local storage" ); |
110 | } |
111 | |
112 | if (!m_database.executeCommand("CREATE TABLE IF NOT EXISTS ItemTable (key TEXT UNIQUE ON CONFLICT REPLACE, value BLOB NOT NULL ON CONFLICT FAIL)" )) { |
113 | LOG_ERROR("Failed to create table ItemTable for local storage" ); |
114 | return false; |
115 | } |
116 | |
117 | return true; |
118 | } |
119 | |
120 | bool LocalStorageDatabase::migrateItemTableIfNeeded() |
121 | { |
122 | if (!m_database.tableExists("ItemTable" )) |
123 | return true; |
124 | |
125 | SQLiteStatement query(m_database, "SELECT value FROM ItemTable LIMIT 1" ); |
126 | |
127 | // This query isn't ever executed, it's just used to check the column type. |
128 | if (query.isColumnDeclaredAsBlob(0)) |
129 | return true; |
130 | |
131 | // Create a new table with the right type, copy all the data over to it and then replace the new table with the old table. |
132 | static const char* commands[] = { |
133 | "DROP TABLE IF EXISTS ItemTable2" , |
134 | "CREATE TABLE ItemTable2 (key TEXT UNIQUE ON CONFLICT REPLACE, value BLOB NOT NULL ON CONFLICT FAIL)" , |
135 | "INSERT INTO ItemTable2 SELECT * from ItemTable" , |
136 | "DROP TABLE ItemTable" , |
137 | "ALTER TABLE ItemTable2 RENAME TO ItemTable" , |
138 | 0, |
139 | }; |
140 | |
141 | SQLiteTransaction transaction(m_database, false); |
142 | transaction.begin(); |
143 | |
144 | for (size_t i = 0; commands[i]; ++i) { |
145 | if (m_database.executeCommand(commands[i])) |
146 | continue; |
147 | |
148 | LOG_ERROR("Failed to migrate table ItemTable for local storage when executing: %s" , commands[i]); |
149 | transaction.rollback(); |
150 | |
151 | return false; |
152 | } |
153 | |
154 | transaction.commit(); |
155 | return true; |
156 | } |
157 | |
158 | void LocalStorageDatabase::importItems(StorageMap& storageMap) |
159 | { |
160 | if (m_didImportItems) |
161 | return; |
162 | |
163 | // FIXME: If it can't import, then the default WebKit behavior should be that of private browsing, |
164 | // not silently ignoring it. https://bugs.webkit.org/show_bug.cgi?id=25894 |
165 | |
166 | // We set this to true even if we don't end up importing any items due to failure because |
167 | // there's really no good way to recover other than not importing anything. |
168 | m_didImportItems = true; |
169 | |
170 | openDatabase(SkipIfNonExistent); |
171 | if (!m_database.isOpen()) |
172 | return; |
173 | |
174 | SQLiteStatement query(m_database, "SELECT key, value FROM ItemTable" ); |
175 | if (query.prepare() != SQLITE_OK) { |
176 | LOG_ERROR("Unable to select items from ItemTable for local storage" ); |
177 | return; |
178 | } |
179 | |
180 | HashMap<String, String> items; |
181 | |
182 | int result = query.step(); |
183 | while (result == SQLITE_ROW) { |
184 | String key = query.getColumnText(0); |
185 | String value = query.getColumnBlobAsString(1); |
186 | if (!key.isNull() && !value.isNull()) |
187 | items.set(key, value); |
188 | result = query.step(); |
189 | } |
190 | |
191 | if (result != SQLITE_DONE) { |
192 | LOG_ERROR("Error reading items from ItemTable for local storage" ); |
193 | return; |
194 | } |
195 | |
196 | storageMap.importItems(items); |
197 | } |
198 | |
199 | void LocalStorageDatabase::setItem(const String& key, const String& value) |
200 | { |
201 | itemDidChange(key, value); |
202 | } |
203 | |
204 | void LocalStorageDatabase::removeItem(const String& key) |
205 | { |
206 | itemDidChange(key, String()); |
207 | } |
208 | |
209 | void LocalStorageDatabase::clear() |
210 | { |
211 | m_changedItems.clear(); |
212 | m_shouldClearItems = true; |
213 | |
214 | scheduleDatabaseUpdate(); |
215 | } |
216 | |
217 | void LocalStorageDatabase::close() |
218 | { |
219 | ASSERT(!m_isClosed); |
220 | m_isClosed = true; |
221 | |
222 | if (m_didScheduleDatabaseUpdate) { |
223 | updateDatabaseWithChangedItems(m_changedItems); |
224 | m_changedItems.clear(); |
225 | } |
226 | |
227 | bool isEmpty = databaseIsEmpty(); |
228 | |
229 | if (m_database.isOpen()) |
230 | m_database.close(); |
231 | |
232 | if (isEmpty) |
233 | m_tracker->deleteDatabaseWithOrigin(m_securityOrigin); |
234 | } |
235 | |
236 | void LocalStorageDatabase::itemDidChange(const String& key, const String& value) |
237 | { |
238 | m_changedItems.set(key, value); |
239 | scheduleDatabaseUpdate(); |
240 | } |
241 | |
242 | void LocalStorageDatabase::scheduleDatabaseUpdate() |
243 | { |
244 | if (m_didScheduleDatabaseUpdate) |
245 | return; |
246 | |
247 | if (!m_disableSuddenTerminationWhileWritingToLocalStorage) |
248 | m_disableSuddenTerminationWhileWritingToLocalStorage = std::make_unique<SuddenTerminationDisabler>(); |
249 | |
250 | m_didScheduleDatabaseUpdate = true; |
251 | |
252 | m_queue->dispatch([protectedThis = makeRef(*this)] { |
253 | protectedThis->updateDatabase(); |
254 | }); |
255 | } |
256 | |
257 | void LocalStorageDatabase::updateDatabase() |
258 | { |
259 | if (m_isClosed) |
260 | return; |
261 | |
262 | ASSERT(m_didScheduleDatabaseUpdate); |
263 | m_didScheduleDatabaseUpdate = false; |
264 | |
265 | HashMap<String, String> changedItems; |
266 | if (m_changedItems.size() <= maximumItemsToUpdate) { |
267 | // There are few enough changed items that we can just always write all of them. |
268 | m_changedItems.swap(changedItems); |
269 | updateDatabaseWithChangedItems(changedItems); |
270 | m_disableSuddenTerminationWhileWritingToLocalStorage = nullptr; |
271 | } else { |
272 | for (int i = 0; i < maximumItemsToUpdate; ++i) { |
273 | auto it = m_changedItems.begin(); |
274 | changedItems.add(it->key, it->value); |
275 | |
276 | m_changedItems.remove(it); |
277 | } |
278 | |
279 | ASSERT(changedItems.size() <= maximumItemsToUpdate); |
280 | |
281 | // Reschedule the update for the remaining items. |
282 | scheduleDatabaseUpdate(); |
283 | updateDatabaseWithChangedItems(changedItems); |
284 | } |
285 | } |
286 | |
287 | void LocalStorageDatabase::updateDatabaseWithChangedItems(const HashMap<String, String>& changedItems) |
288 | { |
289 | if (!m_database.isOpen()) |
290 | openDatabase(CreateIfNonExistent); |
291 | if (!m_database.isOpen()) |
292 | return; |
293 | |
294 | if (m_shouldClearItems) { |
295 | m_shouldClearItems = false; |
296 | |
297 | SQLiteStatement clearStatement(m_database, "DELETE FROM ItemTable" ); |
298 | if (clearStatement.prepare() != SQLITE_OK) { |
299 | LOG_ERROR("Failed to prepare clear statement - cannot write to local storage database" ); |
300 | return; |
301 | } |
302 | |
303 | int result = clearStatement.step(); |
304 | if (result != SQLITE_DONE) { |
305 | LOG_ERROR("Failed to clear all items in the local storage database - %i" , result); |
306 | return; |
307 | } |
308 | } |
309 | |
310 | SQLiteStatement insertStatement(m_database, "INSERT INTO ItemTable VALUES (?, ?)" ); |
311 | if (insertStatement.prepare() != SQLITE_OK) { |
312 | LOG_ERROR("Failed to prepare insert statement - cannot write to local storage database" ); |
313 | return; |
314 | } |
315 | |
316 | SQLiteStatement deleteStatement(m_database, "DELETE FROM ItemTable WHERE key=?" ); |
317 | if (deleteStatement.prepare() != SQLITE_OK) { |
318 | LOG_ERROR("Failed to prepare delete statement - cannot write to local storage database" ); |
319 | return; |
320 | } |
321 | |
322 | SQLiteTransaction transaction(m_database); |
323 | transaction.begin(); |
324 | |
325 | for (auto it = changedItems.begin(), end = changedItems.end(); it != end; ++it) { |
326 | // A null value means that the key/value pair should be deleted. |
327 | SQLiteStatement& statement = it->value.isNull() ? deleteStatement : insertStatement; |
328 | |
329 | statement.bindText(1, it->key); |
330 | |
331 | // If we're inserting a key/value pair, bind the value as well. |
332 | if (!it->value.isNull()) |
333 | statement.bindBlob(2, it->value); |
334 | |
335 | int result = statement.step(); |
336 | if (result != SQLITE_DONE) { |
337 | LOG_ERROR("Failed to update item in the local storage database - %i" , result); |
338 | break; |
339 | } |
340 | |
341 | statement.reset(); |
342 | } |
343 | |
344 | transaction.commit(); |
345 | } |
346 | |
347 | bool LocalStorageDatabase::databaseIsEmpty() |
348 | { |
349 | if (!m_database.isOpen()) |
350 | return false; |
351 | |
352 | SQLiteStatement query(m_database, "SELECT COUNT(*) FROM ItemTable" ); |
353 | if (query.prepare() != SQLITE_OK) { |
354 | LOG_ERROR("Unable to count number of rows in ItemTable for local storage" ); |
355 | return false; |
356 | } |
357 | |
358 | int result = query.step(); |
359 | if (result != SQLITE_ROW) { |
360 | LOG_ERROR("No results when counting number of rows in ItemTable for local storage" ); |
361 | return false; |
362 | } |
363 | |
364 | return !query.getColumnInt(0); |
365 | } |
366 | |
367 | } // namespace WebKit |
368 | |