• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008, 2009 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. ``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.
24  */
25 
26 #include "config.h"
27 #include "StorageAreaSync.h"
28 
29 #if ENABLE(DOM_STORAGE)
30 
31 #include "CString.h"
32 #include "EventNames.h"
33 #include "HTMLElement.h"
34 #include "SQLiteStatement.h"
35 #include "StorageAreaImpl.h"
36 #include "StorageSyncManager.h"
37 #include "SuddenTermination.h"
38 
39 namespace WebCore {
40 
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;
44 
create(PassRefPtr<StorageSyncManager> storageSyncManager,PassRefPtr<StorageAreaImpl> storageArea)45 PassRefPtr<StorageAreaSync> StorageAreaSync::create(PassRefPtr<StorageSyncManager> storageSyncManager, PassRefPtr<StorageAreaImpl> storageArea)
46 {
47     return adoptRef(new StorageAreaSync(storageSyncManager, storageArea));
48 }
49 
StorageAreaSync(PassRefPtr<StorageSyncManager> storageSyncManager,PassRefPtr<StorageAreaImpl> storageArea)50 StorageAreaSync::StorageAreaSync(PassRefPtr<StorageSyncManager> storageSyncManager, PassRefPtr<StorageAreaImpl> 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)
59 {
60     ASSERT(m_storageArea);
61     ASSERT(m_syncManager);
62 
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;
67 }
68 
~StorageAreaSync()69 StorageAreaSync::~StorageAreaSync()
70 {
71     ASSERT(!m_syncTimer.isActive());
72 }
73 
scheduleFinalSync()74 void StorageAreaSync::scheduleFinalSync()
75 {
76     ASSERT(isMainThread());
77     // FIXME: We do this to avoid races, but it'd be better to make things safe without blocking.
78     blockUntilImportComplete();
79 
80     if (m_syncTimer.isActive())
81         m_syncTimer.stop();
82     else {
83         // The following is balanced by the call to enableSuddenTermination in the
84         // syncTimerFired function.
85         disableSuddenTermination();
86     }
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;
91 }
92 
scheduleItemForSync(const String & key,const String & value)93 void StorageAreaSync::scheduleItemForSync(const String& key, const String& value)
94 {
95     ASSERT(isMainThread());
96     ASSERT(!m_finalSyncScheduled);
97 
98     m_changedItems.set(key, value);
99     if (!m_syncTimer.isActive()) {
100         m_syncTimer.startOneShot(StorageSyncInterval);
101 
102         // The following is balanced by the call to enableSuddenTermination in the
103         // syncTimerFired function.
104         disableSuddenTermination();
105     }
106 }
107 
scheduleClear()108 void StorageAreaSync::scheduleClear()
109 {
110     ASSERT(isMainThread());
111     ASSERT(!m_finalSyncScheduled);
112 
113     m_changedItems.clear();
114     m_itemsCleared = true;
115     if (!m_syncTimer.isActive()) {
116         m_syncTimer.startOneShot(StorageSyncInterval);
117 
118         // The following is balanced by the call to enableSuddenTermination in the
119         // syncTimerFired function.
120         disableSuddenTermination();
121     }
122 }
123 
syncTimerFired(Timer<StorageAreaSync> *)124 void StorageAreaSync::syncTimerFired(Timer<StorageAreaSync>*)
125 {
126     ASSERT(isMainThread());
127 
128     HashMap<String, String>::iterator it = m_changedItems.begin();
129     HashMap<String, String>::iterator end = m_changedItems.end();
130 
131     {
132         MutexLocker locker(m_syncLock);
133 
134         if (m_itemsCleared) {
135             m_itemsPendingSync.clear();
136             m_clearItemsWhileSyncing = true;
137             m_itemsCleared = false;
138         }
139 
140         for (; it != end; ++it)
141             m_itemsPendingSync.set(it->first.copy(), it->second.copy());
142 
143         if (!m_syncScheduled) {
144             m_syncScheduled = true;
145 
146             // The following is balanced by the call to enableSuddenTermination in the
147             // performSync function.
148             disableSuddenTermination();
149 
150             m_syncManager->scheduleSync(this);
151         }
152     }
153 
154     // The following is balanced by the calls to disableSuddenTermination in the
155     // scheduleItemForSync, scheduleClear, and scheduleFinalSync functions.
156     enableSuddenTermination();
157 
158     m_changedItems.clear();
159 }
160 
performImport()161 void StorageAreaSync::performImport()
162 {
163     ASSERT(!isMainThread());
164     ASSERT(!m_database.isOpen());
165 
166     String databaseFilename = m_syncManager->fullDatabaseFilename(m_storageArea->securityOrigin());
167 
168     if (databaseFilename.isEmpty()) {
169         LOG_ERROR("Filename for local storage database is empty - cannot open for persistent storage");
170         markImported();
171         return;
172     }
173 
174     if (!m_database.open(databaseFilename)) {
175         LOG_ERROR("Failed to open database file %s for local storage", databaseFilename.utf8().data());
176         markImported();
177         return;
178     }
179 
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");
182         markImported();
183         return;
184     }
185 
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");
189         markImported();
190         return;
191     }
192 
193     HashMap<String, String> itemMap;
194 
195     int result = query.step();
196     while (result == SQLResultRow) {
197         itemMap.set(query.getColumnText(0), query.getColumnText(1));
198         result = query.step();
199     }
200 
201     if (result != SQLResultDone) {
202         LOG_ERROR("Error reading items from ItemTable for local storage");
203         markImported();
204         return;
205     }
206 
207     MutexLocker locker(m_importLock);
208 
209     HashMap<String, String>::iterator it = itemMap.begin();
210     HashMap<String, String>::iterator end = itemMap.end();
211 
212     for (; it != end; ++it)
213         m_storageArea->importItem(it->first, it->second);
214 
215     // Break the (ref count) cycle.
216     m_storageArea = 0;
217     m_importComplete = true;
218     m_importCondition.signal();
219 }
220 
markImported()221 void StorageAreaSync::markImported()
222 {
223     ASSERT(!isMainThread());
224 
225     MutexLocker locker(m_importLock);
226     // Break the (ref count) cycle.
227     m_storageArea = 0;
228     m_importComplete = true;
229     m_importCondition.signal();
230 }
231 
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
238 // job first.
blockUntilImportComplete() const239 void StorageAreaSync::blockUntilImportComplete() const
240 {
241     ASSERT(isMainThread());
242 
243     // Fast path to avoid locking.
244     if (m_importComplete)
245         return;
246 
247     MutexLocker locker(m_importLock);
248     while (!m_importComplete)
249         m_importCondition.wait(m_importLock);
250     ASSERT(m_importComplete);
251     ASSERT(!m_storageArea);
252 }
253 
sync(bool clearItems,const HashMap<String,String> & items)254 void StorageAreaSync::sync(bool clearItems, const HashMap<String, String>& items)
255 {
256     ASSERT(!isMainThread());
257 
258     if (!m_database.isOpen())
259         return;
260 
261     // If the clear flag is set, then we clear all items out before we write any new ones in.
262     if (clearItems) {
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");
266             return;
267         }
268 
269         int result = clear.step();
270         if (result != SQLResultDone) {
271             LOG_ERROR("Failed to clear all items in the local storage database - %i", result);
272             return;
273         }
274     }
275 
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");
279         return;
280     }
281 
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");
285         return;
286     }
287 
288     HashMap<String, String>::const_iterator end = items.end();
289 
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;
293 
294         query.bindText(1, it->first);
295 
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);
299 
300         int result = query.step();
301         if (result != SQLResultDone) {
302             LOG_ERROR("Failed to update item in the local storage database - %i", result);
303             break;
304         }
305 
306         query.reset();
307     }
308 }
309 
performSync()310 void StorageAreaSync::performSync()
311 {
312     ASSERT(!isMainThread());
313 
314     bool clearItems;
315     HashMap<String, String> items;
316     {
317         MutexLocker locker(m_syncLock);
318 
319         ASSERT(m_syncScheduled);
320 
321         clearItems = m_clearItemsWhileSyncing;
322         m_itemsPendingSync.swap(items);
323 
324         m_clearItemsWhileSyncing = false;
325         m_syncScheduled = false;
326     }
327 
328     sync(clearItems, items);
329 
330     // The following is balanced by the call to disableSuddenTermination in the
331     // syncTimerFired function.
332     enableSuddenTermination();
333 }
334 
335 } // namespace WebCore
336 
337 #endif // ENABLE(DOM_STORAGE)
338 
339