2 * Copyright (C) 2008, 2009 Apple Inc. All Rights Reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
27 #include "StorageAreaSync.h"
29 #if ENABLE(DOM_STORAGE)
32 #include "EventNames.h"
33 #include "HTMLElement.h"
34 #include "SQLiteStatement.h"
35 #include "StorageArea.h"
36 #include "StorageSyncManager.h"
37 #include "SuddenTermination.h"
41 // If the StorageArea undergoes rapid changes, don't sync each change to disk.
42 // Instead, queue up a batch of items to sync and actually do the sync at the following interval.
43 static const double StorageSyncInterval = 1.0;
45 PassRefPtr<StorageAreaSync> StorageAreaSync::create(PassRefPtr<StorageSyncManager> storageSyncManager, PassRefPtr<StorageArea> storageArea)
47 return adoptRef(new StorageAreaSync(storageSyncManager, storageArea));
50 StorageAreaSync::StorageAreaSync(PassRefPtr<StorageSyncManager> storageSyncManager, PassRefPtr<StorageArea> storageArea)
51 : m_syncTimer(this, &StorageAreaSync::syncTimerFired)
52 , m_itemsCleared(false)
53 , m_finalSyncScheduled(false)
54 , m_storageArea(storageArea)
55 , m_syncManager(storageSyncManager)
56 , m_clearItemsWhileSyncing(false)
57 , m_syncScheduled(false)
58 , m_importComplete(false)
60 ASSERT(m_storageArea);
61 ASSERT(m_syncManager);
63 // FIXME: If it can't import, then the default WebKit behavior should be that of private browsing,
64 // not silently ignoring it. https://bugs.webkit.org/show_bug.cgi?id=25894
65 if (!m_syncManager->scheduleImport(this))
66 m_importComplete = true;
69 StorageAreaSync::~StorageAreaSync()
71 ASSERT(!m_syncTimer.isActive());
74 void StorageAreaSync::scheduleFinalSync()
76 ASSERT(isMainThread());
77 // FIXME: We do this to avoid races, but it'd be better to make things safe without blocking.
78 blockUntilImportComplete();
80 if (m_syncTimer.isActive())
83 // The following is balanced by the call to enableSuddenTermination in the
84 // syncTimerFired function.
85 disableSuddenTermination();
87 // FIXME: This is synchronous. We should do it on the background process, but
88 // we should do it safely.
89 syncTimerFired(&m_syncTimer);
90 m_finalSyncScheduled = true;
93 void StorageAreaSync::scheduleItemForSync(const String& key, const String& value)
95 ASSERT(isMainThread());
96 ASSERT(!m_finalSyncScheduled);
98 m_changedItems.set(key, value);
99 if (!m_syncTimer.isActive()) {
100 m_syncTimer.startOneShot(StorageSyncInterval);
102 // The following is balanced by the call to enableSuddenTermination in the
103 // syncTimerFired function.
104 disableSuddenTermination();
108 void StorageAreaSync::scheduleClear()
110 ASSERT(isMainThread());
111 ASSERT(!m_finalSyncScheduled);
113 m_changedItems.clear();
114 m_itemsCleared = true;
115 if (!m_syncTimer.isActive()) {
116 m_syncTimer.startOneShot(StorageSyncInterval);
118 // The following is balanced by the call to enableSuddenTermination in the
119 // syncTimerFired function.
120 disableSuddenTermination();
124 void StorageAreaSync::syncTimerFired(Timer<StorageAreaSync>*)
126 ASSERT(isMainThread());
128 HashMap<String, String>::iterator it = m_changedItems.begin();
129 HashMap<String, String>::iterator end = m_changedItems.end();
132 MutexLocker locker(m_syncLock);
134 if (m_itemsCleared) {
135 m_itemsPendingSync.clear();
136 m_clearItemsWhileSyncing = true;
137 m_itemsCleared = false;
140 for (; it != end; ++it)
141 m_itemsPendingSync.set(it->first.copy(), it->second.copy());
143 if (!m_syncScheduled) {
144 m_syncScheduled = true;
146 // The following is balanced by the call to enableSuddenTermination in the
147 // performSync function.
148 disableSuddenTermination();
150 m_syncManager->scheduleSync(this);
154 // The following is balanced by the calls to disableSuddenTermination in the
155 // scheduleItemForSync, scheduleClear, and scheduleFinalSync functions.
156 enableSuddenTermination();
158 m_changedItems.clear();
161 void StorageAreaSync::performImport()
163 ASSERT(!isMainThread());
164 ASSERT(!m_database.isOpen());
166 String databaseFilename = m_syncManager->fullDatabaseFilename(m_storageArea->securityOrigin());
168 if (databaseFilename.isEmpty()) {
169 LOG_ERROR("Filename for local storage database is empty - cannot open for persistent storage");
174 if (!m_database.open(databaseFilename)) {
175 LOG_ERROR("Failed to open database file %s for local storage", databaseFilename.utf8().data());
180 if (!m_database.executeCommand("CREATE TABLE IF NOT EXISTS ItemTable (key TEXT UNIQUE ON CONFLICT REPLACE, value TEXT NOT NULL ON CONFLICT FAIL)")) {
181 LOG_ERROR("Failed to create table ItemTable for local storage");
186 SQLiteStatement query(m_database, "SELECT key, value FROM ItemTable");
187 if (query.prepare() != SQLResultOk) {
188 LOG_ERROR("Unable to select items from ItemTable for local storage");
193 HashMap<String, String> itemMap;
195 int result = query.step();
196 while (result == SQLResultRow) {
197 itemMap.set(query.getColumnText(0), query.getColumnText(1));
198 result = query.step();
201 if (result != SQLResultDone) {
202 LOG_ERROR("Error reading items from ItemTable for local storage");
207 MutexLocker locker(m_importLock);
209 HashMap<String, String>::iterator it = itemMap.begin();
210 HashMap<String, String>::iterator end = itemMap.end();
212 for (; it != end; ++it)
213 m_storageArea->importItem(it->first, it->second);
215 // Break the (ref count) cycle.
217 m_importComplete = true;
218 m_importCondition.signal();
221 void StorageAreaSync::markImported()
223 ASSERT(!isMainThread());
225 MutexLocker locker(m_importLock);
226 // Break the (ref count) cycle.
228 m_importComplete = true;
229 m_importCondition.signal();
232 // FIXME: In the future, we should allow use of StorageAreas while it's importing (when safe to do so).
233 // Blocking everything until the import is complete is by far the simplest and safest thing to do, but
234 // there is certainly room for safe optimization: Key/length will never be able to make use of such an
235 // optimization (since the order of iteration can change as items are being added). Get can return any
236 // item currently in the map. Get/remove can work whether or not it's in the map, but we'll need a list
237 // of items the import should not overwrite. Clear can also work, but it'll need to kill the import
239 void StorageAreaSync::blockUntilImportComplete() const
241 ASSERT(isMainThread());
243 // Fast path to avoid locking.
244 if (m_importComplete)
247 MutexLocker locker(m_importLock);
248 while (!m_importComplete)
249 m_importCondition.wait(m_importLock);
250 ASSERT(m_importComplete);
251 ASSERT(!m_storageArea);
254 void StorageAreaSync::sync(bool clearItems, const HashMap<String, String>& items)
256 ASSERT(!isMainThread());
258 if (!m_database.isOpen())
261 // If the clear flag is set, then we clear all items out before we write any new ones in.
263 SQLiteStatement clear(m_database, "DELETE FROM ItemTable");
264 if (clear.prepare() != SQLResultOk) {
265 LOG_ERROR("Failed to prepare clear statement - cannot write to local storage database");
269 int result = clear.step();
270 if (result != SQLResultDone) {
271 LOG_ERROR("Failed to clear all items in the local storage database - %i", result);
276 SQLiteStatement insert(m_database, "INSERT INTO ItemTable VALUES (?, ?)");
277 if (insert.prepare() != SQLResultOk) {
278 LOG_ERROR("Failed to prepare insert statement - cannot write to local storage database");
282 SQLiteStatement remove(m_database, "DELETE FROM ItemTable WHERE key=?");
283 if (remove.prepare() != SQLResultOk) {
284 LOG_ERROR("Failed to prepare delete statement - cannot write to local storage database");
288 HashMap<String, String>::const_iterator end = items.end();
290 for (HashMap<String, String>::const_iterator it = items.begin(); it != end; ++it) {
291 // Based on the null-ness of the second argument, decide whether this is an insert or a delete.
292 SQLiteStatement& query = it->second.isNull() ? remove : insert;
294 query.bindText(1, it->first);
296 // If the second argument is non-null, we're doing an insert, so bind it as the value.
297 if (!it->second.isNull())
298 query.bindText(2, it->second);
300 int result = query.step();
301 if (result != SQLResultDone) {
302 LOG_ERROR("Failed to update item in the local storage database - %i", result);
310 void StorageAreaSync::performSync()
312 ASSERT(!isMainThread());
315 HashMap<String, String> items;
317 MutexLocker locker(m_syncLock);
319 ASSERT(m_syncScheduled);
321 clearItems = m_clearItemsWhileSyncing;
322 m_itemsPendingSync.swap(items);
324 m_clearItemsWhileSyncing = false;
325 m_syncScheduled = false;
328 sync(clearItems, items);
330 // The following is balanced by the call to disableSuddenTermination in the
331 // syncTimerFired function.
332 enableSuddenTermination();
335 } // namespace WebCore
337 #endif // ENABLE(DOM_STORAGE)