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 "SuddenTermination.h"
40 // If the StorageArea undergoes rapid changes, don't sync each change to disk.
41 // Instead, queue up a batch of items to sync and actually do the sync at the following interval.
42 static const double StorageSyncInterval = 1.0;
44 PassRefPtr<StorageAreaSync> StorageAreaSync::create(PassRefPtr<StorageSyncManager> storageSyncManager, PassRefPtr<StorageArea> storageArea)
46 return adoptRef(new StorageAreaSync(storageSyncManager, storageArea));
49 StorageAreaSync::StorageAreaSync(PassRefPtr<StorageSyncManager> storageSyncManager, PassRefPtr<StorageArea> storageArea)
50 : m_syncTimer(this, &StorageAreaSync::syncTimerFired)
51 , m_itemsCleared(false)
52 , m_finalSyncScheduled(false)
53 , m_storageArea(storageArea)
54 , m_syncManager(storageSyncManager)
55 , m_clearItemsWhileSyncing(false)
56 , m_syncScheduled(false)
57 , m_importComplete(false)
59 ASSERT(m_storageArea);
60 ASSERT(m_syncManager);
62 // FIXME: If it can't import, then the default WebKit behavior should be that of private browsing,
63 // not silently ignoring it. https://bugs.webkit.org/show_bug.cgi?id=25894
64 if (!m_syncManager->scheduleImport(this))
65 m_importComplete = true;
69 StorageAreaSync::~StorageAreaSync()
71 ASSERT(!m_syncTimer.isActive());
75 void StorageAreaSync::scheduleFinalSync()
77 ASSERT(isMainThread());
78 // FIXME: We do this to avoid races, but it'd be better to make things safe without blocking.
79 blockUntilImportComplete();
81 if (m_syncTimer.isActive())
84 // The following is balanced by the call to enableSuddenTermination in the
85 // syncTimerFired function.
86 disableSuddenTermination();
88 // FIXME: This is synchronous. We should do it on the background process, but
89 // we should do it safely.
90 syncTimerFired(&m_syncTimer);
91 m_finalSyncScheduled = true;
94 void StorageAreaSync::scheduleItemForSync(const String& key, const String& value)
96 ASSERT(isMainThread());
97 ASSERT(!m_finalSyncScheduled);
99 m_changedItems.set(key, value);
100 if (!m_syncTimer.isActive()) {
101 m_syncTimer.startOneShot(StorageSyncInterval);
103 // The following is balanced by the call to enableSuddenTermination in the
104 // syncTimerFired function.
105 disableSuddenTermination();
109 void StorageAreaSync::scheduleClear()
111 ASSERT(isMainThread());
112 ASSERT(!m_finalSyncScheduled);
114 m_changedItems.clear();
115 m_itemsCleared = true;
116 if (!m_syncTimer.isActive()) {
117 m_syncTimer.startOneShot(StorageSyncInterval);
119 // The following is balanced by the call to enableSuddenTermination in the
120 // syncTimerFired function.
121 disableSuddenTermination();
125 void StorageAreaSync::syncTimerFired(Timer<StorageAreaSync>*)
127 ASSERT(isMainThread());
129 HashMap<String, String>::iterator it = m_changedItems.begin();
130 HashMap<String, String>::iterator end = m_changedItems.end();
133 MutexLocker locker(m_syncLock);
135 if (m_itemsCleared) {
136 m_itemsPendingSync.clear();
137 m_clearItemsWhileSyncing = true;
138 m_itemsCleared = false;
141 for (; it != end; ++it)
142 m_itemsPendingSync.set(it->first.copy(), it->second.copy());
144 if (!m_syncScheduled) {
145 m_syncScheduled = true;
147 // The following is balanced by the call to enableSuddenTermination in the
148 // performSync function.
149 disableSuddenTermination();
151 m_syncManager->scheduleSync(this);
155 // The following is balanced by the calls to disableSuddenTermination in the
156 // scheduleItemForSync, scheduleClear, and scheduleFinalSync functions.
157 enableSuddenTermination();
159 m_changedItems.clear();
162 void StorageAreaSync::performImport()
164 ASSERT(!isMainThread());
165 ASSERT(!m_database.isOpen());
167 String databaseFilename = m_syncManager->fullDatabaseFilename(m_storageArea->securityOrigin());
169 if (databaseFilename.isEmpty()) {
170 LOG_ERROR("Filename for local storage database is empty - cannot open for persistent storage");
175 if (!m_database.open(databaseFilename)) {
176 LOG_ERROR("Failed to open database file %s for local storage", databaseFilename.utf8().data());
181 if (!m_database.executeCommand("CREATE TABLE IF NOT EXISTS ItemTable (key TEXT UNIQUE ON CONFLICT REPLACE, value TEXT NOT NULL ON CONFLICT FAIL)")) {
182 LOG_ERROR("Failed to create table ItemTable for local storage");
187 SQLiteStatement query(m_database, "SELECT key, value FROM ItemTable");
188 if (query.prepare() != SQLResultOk) {
189 LOG_ERROR("Unable to select items from ItemTable for local storage");
194 HashMap<String, String> itemMap;
196 int result = query.step();
197 while (result == SQLResultRow) {
198 itemMap.set(query.getColumnText(0), query.getColumnText(1));
199 result = query.step();
202 if (result != SQLResultDone) {
203 LOG_ERROR("Error reading items from ItemTable for local storage");
208 MutexLocker locker(m_importLock);
210 HashMap<String, String>::iterator it = itemMap.begin();
211 HashMap<String, String>::iterator end = itemMap.end();
213 for (; it != end; ++it)
214 m_storageArea->importItem(it->first, it->second);
216 // Break the (ref count) cycle.
218 m_importComplete = true;
219 m_importCondition.signal();
222 void StorageAreaSync::markImported()
224 ASSERT(!isMainThread());
226 MutexLocker locker(m_importLock);
227 // Break the (ref count) cycle.
229 m_importComplete = true;
230 m_importCondition.signal();
233 // FIXME: In the future, we should allow use of StorageAreas while it's importing (when safe to do so).
234 // Blocking everything until the import is complete is by far the simplest and safest thing to do, but
235 // there is certainly room for safe optimization: Key/length will never be able to make use of such an
236 // optimization (since the order of iteration can change as items are being added). Get can return any
237 // item currently in the map. Get/remove can work whether or not it's in the map, but we'll need a list
238 // of items the import should not overwrite. Clear can also work, but it'll need to kill the import
240 void StorageAreaSync::blockUntilImportComplete() const
242 ASSERT(isMainThread());
244 // Fast path to avoid locking.
245 if (m_importComplete)
248 MutexLocker locker(m_importLock);
249 while (!m_importComplete)
250 m_importCondition.wait(m_importLock);
251 ASSERT(m_importComplete);
252 ASSERT(!m_storageArea);
255 void StorageAreaSync::sync(bool clearItems, const HashMap<String, String>& items)
257 ASSERT(!isMainThread());
259 if (!m_database.isOpen())
262 // If the clear flag is set, then we clear all items out before we write any new ones in.
264 SQLiteStatement clear(m_database, "DELETE FROM ItemTable");
265 if (clear.prepare() != SQLResultOk) {
266 LOG_ERROR("Failed to prepare clear statement - cannot write to local storage database");
270 int result = clear.step();
271 if (result != SQLResultDone) {
272 LOG_ERROR("Failed to clear all items in the local storage database - %i", result);
277 SQLiteStatement insert(m_database, "INSERT INTO ItemTable VALUES (?, ?)");
278 if (insert.prepare() != SQLResultOk) {
279 LOG_ERROR("Failed to prepare insert statement - cannot write to local storage database");
283 SQLiteStatement remove(m_database, "DELETE FROM ItemTable WHERE key=?");
284 if (remove.prepare() != SQLResultOk) {
285 LOG_ERROR("Failed to prepare delete statement - cannot write to local storage database");
289 HashMap<String, String>::const_iterator end = items.end();
291 for (HashMap<String, String>::const_iterator it = items.begin(); it != end; ++it) {
292 // Based on the null-ness of the second argument, decide whether this is an insert or a delete.
293 SQLiteStatement& query = it->second.isNull() ? remove : insert;
295 query.bindText(1, it->first);
297 // If the second argument is non-null, we're doing an insert, so bind it as the value.
298 if (!it->second.isNull())
299 query.bindText(2, it->second);
301 int result = query.step();
302 if (result != SQLResultDone) {
303 LOG_ERROR("Failed to update item in the local storage database - %i", result);
311 void StorageAreaSync::performSync()
313 ASSERT(!isMainThread());
316 HashMap<String, String> items;
318 MutexLocker locker(m_syncLock);
320 ASSERT(m_syncScheduled);
322 clearItems = m_clearItemsWhileSyncing;
323 m_itemsPendingSync.swap(items);
325 m_clearItemsWhileSyncing = false;
326 m_syncScheduled = false;
329 sync(clearItems, items);
331 // The following is balanced by the call to disableSuddenTermination in the
332 // syncTimerFired function.
333 enableSuddenTermination();
336 } // namespace WebCore
338 #endif // ENABLE(DOM_STORAGE)