2008-11-04 Jan Michael Alonzo <jmalonzo@webkit.org>
[webkit/qt.git] / WebCore / storage / LocalStorageArea.cpp
blob56dc246ca18c5b03bb0ca28a6f83c29413239296
1 /*
2 * Copyright (C) 2008 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
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.
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.
26 #include "config.h"
27 #include "LocalStorageArea.h"
29 #include "CString.h"
30 #include "EventNames.h"
31 #include "Frame.h"
32 #include "FrameTree.h"
33 #include "LocalStorage.h"
34 #include "LocalStorageTask.h"
35 #include "LocalStorageThread.h"
36 #include "Page.h"
37 #include "PageGroup.h"
38 #include "PlatformString.h"
39 #include "SecurityOrigin.h"
40 #include "SQLiteStatement.h"
42 namespace WebCore {
44 // If the LocalStorageArea undergoes rapid changes, don't sync each change to disk.
45 // Instead, queue up a batch of items to sync and actually do the sync at the following interval.
46 static const double LocalStorageSyncInterval = 1.0;
48 LocalStorageArea::LocalStorageArea(SecurityOrigin* origin, LocalStorage* localStorage)
49 : StorageArea(origin)
50 , m_syncTimer(this, &LocalStorageArea::syncTimerFired)
51 , m_itemsCleared(false)
52 , m_finalSyncScheduled(false)
53 , m_localStorage(localStorage)
54 , m_clearItemsWhileSyncing(false)
55 , m_syncScheduled(false)
56 , m_importComplete(false)
58 ASSERT(m_localStorage);
60 if (!m_localStorage->scheduleImport(this))
61 m_importComplete = true;
64 LocalStorageArea::~LocalStorageArea()
66 ASSERT(!m_syncTimer.isActive());
69 void LocalStorageArea::scheduleFinalSync()
71 m_syncTimer.stop();
72 syncTimerFired(&m_syncTimer);
73 m_finalSyncScheduled = true;
76 unsigned LocalStorageArea::length() const
78 ASSERT(isMainThread());
80 if (m_importComplete)
81 return internalLength();
83 MutexLocker locker(m_importLock);
84 if (m_importComplete)
85 return internalLength();
87 while (!m_importComplete)
88 m_importCondition.wait(m_importLock);
89 ASSERT(m_importComplete);
91 return internalLength();
94 String LocalStorageArea::key(unsigned index, ExceptionCode& ec) const
96 ASSERT(isMainThread());
98 if (m_importComplete)
99 return internalKey(index, ec);
101 MutexLocker locker(m_importLock);
102 if (m_importComplete)
103 return internalKey(index, ec);
105 while (!m_importComplete)
106 m_importCondition.wait(m_importLock);
107 ASSERT(m_importComplete);
109 return internalKey(index, ec);
112 String LocalStorageArea::getItem(const String& key) const
114 ASSERT(isMainThread());
116 if (m_importComplete)
117 return internalGetItem(key);
119 MutexLocker locker(m_importLock);
120 if (m_importComplete)
121 return internalGetItem(key);
123 String item = internalGetItem(key);
124 if (!item.isNull())
125 return item;
127 while (!m_importComplete)
128 m_importCondition.wait(m_importLock);
129 ASSERT(m_importComplete);
131 return internalGetItem(key);
134 void LocalStorageArea::setItem(const String& key, const String& value, ExceptionCode& ec, Frame* frame)
136 ASSERT(isMainThread());
138 if (m_importComplete) {
139 internalSetItem(key, value, ec, frame);
140 return;
143 MutexLocker locker(m_importLock);
144 internalSetItem(key, value, ec, frame);
147 void LocalStorageArea::removeItem(const String& key, Frame* frame)
149 ASSERT(isMainThread());
151 if (m_importComplete) {
152 internalRemoveItem(key, frame);
153 return;
156 MutexLocker locker(m_importLock);
157 internalRemoveItem(key, frame);
160 bool LocalStorageArea::contains(const String& key) const
162 ASSERT(isMainThread());
164 if (m_importComplete)
165 return internalContains(key);
167 MutexLocker locker(m_importLock);
168 if (m_importComplete)
169 return internalContains(key);
171 bool contained = internalContains(key);
172 if (contained)
173 return true;
175 while (!m_importComplete)
176 m_importCondition.wait(m_importLock);
177 ASSERT(m_importComplete);
179 return internalContains(key);
182 void LocalStorageArea::itemChanged(const String& key, const String& oldValue, const String& newValue, Frame* sourceFrame)
184 ASSERT(isMainThread());
186 scheduleItemForSync(key, newValue);
187 dispatchStorageEvent(key, oldValue, newValue, sourceFrame);
190 void LocalStorageArea::itemRemoved(const String& key, const String& oldValue, Frame* sourceFrame)
192 ASSERT(isMainThread());
194 scheduleItemForSync(key, String());
195 dispatchStorageEvent(key, oldValue, String(), sourceFrame);
198 void LocalStorageArea::areaCleared(Frame* sourceFrame)
200 ASSERT(isMainThread());
202 scheduleClear();
203 dispatchStorageEvent(String(), String(), String(), sourceFrame);
206 void LocalStorageArea::dispatchStorageEvent(const String& key, const String& oldValue, const String& newValue, Frame* sourceFrame)
208 ASSERT(isMainThread());
210 Page* page = sourceFrame->page();
211 if (!page)
212 return;
214 // Need to copy all relevant frames from every page to a vector, since sending the event to one frame might mutate the frame tree
215 // of any given page in the group, or mutate the page group itself
216 Vector<RefPtr<Frame> > frames;
217 const HashSet<Page*>& pages = page->group().pages();
219 HashSet<Page*>::const_iterator end = pages.end();
220 for (HashSet<Page*>::const_iterator it = pages.begin(); it != end; ++it) {
221 for (Frame* frame = (*it)->mainFrame(); frame; frame = frame->tree()->traverseNext()) {
222 if (Document* document = frame->document())
223 if (document->securityOrigin()->equal(securityOrigin()))
224 frames.append(frame);
228 for (unsigned i = 0; i < frames.size(); ++i) {
229 if (HTMLElement* body = frames[i]->document()->body())
230 body->dispatchStorageEvent(eventNames().storageEvent, key, oldValue, newValue, sourceFrame);
234 void LocalStorageArea::scheduleItemForSync(const String& key, const String& value)
236 ASSERT(isMainThread());
237 ASSERT(!m_finalSyncScheduled);
239 m_changedItems.set(key, value);
240 if (!m_syncTimer.isActive())
241 m_syncTimer.startOneShot(LocalStorageSyncInterval);
244 void LocalStorageArea::scheduleClear()
246 ASSERT(isMainThread());
247 ASSERT(!m_finalSyncScheduled);
249 m_changedItems.clear();
250 m_itemsCleared = true;
251 if (!m_syncTimer.isActive())
252 m_syncTimer.startOneShot(LocalStorageSyncInterval);
255 void LocalStorageArea::syncTimerFired(Timer<LocalStorageArea>*)
257 ASSERT(isMainThread());
259 HashMap<String, String>::iterator it = m_changedItems.begin();
260 HashMap<String, String>::iterator end = m_changedItems.end();
263 MutexLocker locker(m_syncLock);
265 if (m_itemsCleared) {
266 m_itemsPendingSync.clear();
267 m_clearItemsWhileSyncing = true;
268 m_itemsCleared = false;
271 for (; it != end; ++it)
272 m_itemsPendingSync.set(it->first.copy(), it->second.copy());
274 if (!m_syncScheduled) {
275 m_syncScheduled = true;
276 m_localStorage->scheduleSync(this);
280 m_changedItems.clear();
283 void LocalStorageArea::performImport()
285 ASSERT(!isMainThread());
286 ASSERT(!m_database.isOpen());
288 String databaseFilename = m_localStorage->fullDatabaseFilename(securityOrigin());
290 if (databaseFilename.isEmpty()) {
291 LOG_ERROR("Filename for local storage database is empty - cannot open for persistent storage");
292 markImported();
293 return;
296 if (!m_database.open(databaseFilename)) {
297 LOG_ERROR("Failed to open database file %s for local storage", databaseFilename.utf8().data());
298 markImported();
299 return;
302 if (!m_database.executeCommand("CREATE TABLE IF NOT EXISTS ItemTable (key TEXT UNIQUE ON CONFLICT REPLACE, value TEXT NOT NULL ON CONFLICT FAIL)")) {
303 LOG_ERROR("Failed to create table ItemTable for local storage");
304 markImported();
305 return;
308 SQLiteStatement query(m_database, "SELECT key, value FROM ItemTable");
309 if (query.prepare() != SQLResultOk) {
310 LOG_ERROR("Unable to select items from ItemTable for local storage");
311 markImported();
312 return;
315 HashMap<String, String> itemMap;
317 int result = query.step();
318 while (result == SQLResultRow) {
319 itemMap.set(query.getColumnText(0), query.getColumnText(1));
320 result = query.step();
323 if (result != SQLResultDone) {
324 LOG_ERROR("Error reading items from ItemTable for local storage");
325 markImported();
326 return;
329 MutexLocker locker(m_importLock);
331 HashMap<String, String>::iterator it = itemMap.begin();
332 HashMap<String, String>::iterator end = itemMap.end();
334 for (; it != end; ++it)
335 importItem(it->first, it->second);
337 m_importComplete = true;
338 m_importCondition.signal();
341 void LocalStorageArea::markImported()
343 ASSERT(!isMainThread());
345 MutexLocker locker(m_importLock);
346 m_importComplete = true;
347 m_importCondition.signal();
350 void LocalStorageArea::performSync()
352 ASSERT(!isMainThread());
354 if (!m_database.isOpen())
355 return;
357 HashMap<String, String> items;
358 bool clearFirst = false;
360 MutexLocker locker(m_syncLock);
361 m_itemsPendingSync.swap(items);
362 clearFirst = m_clearItemsWhileSyncing;
363 m_clearItemsWhileSyncing = false;
364 m_syncScheduled = false;
367 // If the clear flag is marked, then we clear all items out before we write any new ones in
368 if (clearFirst) {
369 SQLiteStatement clear(m_database, "DELETE FROM ItemTable");
370 if (clear.prepare() != SQLResultOk) {
371 LOG_ERROR("Failed to prepare clear statement - cannot write to local storage database");
372 return;
375 int result = clear.step();
376 if (result != SQLResultDone) {
377 LOG_ERROR("Failed to clear all items in the local storage database - %i", result);
378 return;
382 SQLiteStatement insert(m_database, "INSERT INTO ItemTable VALUES (?, ?)");
383 if (insert.prepare() != SQLResultOk) {
384 LOG_ERROR("Failed to prepare insert statement - cannot write to local storage database");
385 return;
388 SQLiteStatement remove(m_database, "DELETE FROM ItemTable WHERE key=?");
389 if (remove.prepare() != SQLResultOk) {
390 LOG_ERROR("Failed to prepare delete statement - cannot write to local storage database");
391 return;
394 HashMap<String, String>::iterator end = items.end();
396 for (HashMap<String, String>::iterator it = items.begin(); it != end; ++it) {
397 // Based on the null-ness of the second argument, decide whether this is an insert or a delete
398 SQLiteStatement& query = it->second.isNull() ? remove : insert;
400 query.bindText(1, it->first);
402 // If the second argument is non-null, we're doing an insert, so bind it as the value.
403 if (!it->second.isNull())
404 query.bindText(2, it->second);
406 int result = query.step();
407 if (result != SQLResultDone) {
408 LOG_ERROR("Failed to update item in the local storage database - %i", result);
409 break;
412 query.reset();
416 } // namespace WebCore