<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[149615] trunk/Source</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.webkit.org/projects/webkit/changeset/149615">149615</a></dd>
<dt>Author</dt> <dd>andersca@apple.com</dd>
<dt>Date</dt> <dd>2013-05-06 11:10:10 -0700 (Mon, 06 May 2013)</dd>
</dl>

<h3>Log Message</h3>
<pre>Write storage changes to disk
https://bugs.webkit.org/show_bug.cgi?id=115660

Reviewed by Andreas Kling.

Source/WebCore:

Export symbols needed by WebKit2.

* WebCore.exp.in:

Source/WebKit2:

* UIProcess/Storage/LocalStorageDatabase.cpp:
(WebKit::LocalStorageDatabase::LocalStorageDatabase):
Initialize new member variables.

(WebKit::LocalStorageDatabase::tryToOpenDatabase):
Disable database threading checks.

(WebKit::LocalStorageDatabase::importItems):
Only import the items once.

(WebKit::LocalStorageDatabase::setItem):
Call itemDidChange.

(WebKit::LocalStorageDatabase::itemDidChange):
Record the change and schedule a database update.

(WebKit::LocalStorageDatabase::scheduleDatabaseUpdate):
Use WorkQueue::dispatchAfterDelay to schedule a datbase update.

(WebKit::LocalStorageDatabase::updateDatabase):
Write changes to disk. If there are more than 100 pending items, only write the first 100 and then schedule
another database update for the remaining items.

* UIProcess/Storage/LocalStorageDatabase.h:
Add new member variables.

* UIProcess/Storage/StorageManager.cpp:
(WebKit::StorageManager::StorageArea::setItem):
Call LocalStorageDatabase::setItem.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceWebCoreChangeLog">trunk/Source/WebCore/ChangeLog</a></li>
<li><a href="#trunkSourceWebCoreWebCoreexpin">trunk/Source/WebCore/WebCore.exp.in</a></li>
<li><a href="#trunkSourceWebKit2ChangeLog">trunk/Source/WebKit2/ChangeLog</a></li>
<li><a href="#trunkSourceWebKit2UIProcessStorageLocalStorageDatabasecpp">trunk/Source/WebKit2/UIProcess/Storage/LocalStorageDatabase.cpp</a></li>
<li><a href="#trunkSourceWebKit2UIProcessStorageLocalStorageDatabaseh">trunk/Source/WebKit2/UIProcess/Storage/LocalStorageDatabase.h</a></li>
<li><a href="#trunkSourceWebKit2UIProcessStorageStorageManagercpp">trunk/Source/WebKit2/UIProcess/Storage/StorageManager.cpp</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceWebCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/ChangeLog (149614 => 149615)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/ChangeLog        2013-05-06 18:05:34 UTC (rev 149614)
+++ trunk/Source/WebCore/ChangeLog        2013-05-06 18:10:10 UTC (rev 149615)
</span><span class="lines">@@ -1,3 +1,14 @@
</span><ins>+2013-05-06  Anders Carlsson  &lt;andersca@apple.com&gt;
+
+        Write storage changes to disk
+        https://bugs.webkit.org/show_bug.cgi?id=115660
+
+        Reviewed by Andreas Kling.
+
+        Export symbols needed by WebKit2.
+
+        * WebCore.exp.in:
+
</ins><span class="cx"> 2013-05-06  Tiancheng Jiang  &lt;tijiang@rim.com&gt;
</span><span class="cx"> 
</span><span class="cx">         [BlackBerry] Update HTML5 progress bar UX.
</span></span></pre></div>
<a id="trunkSourceWebCoreWebCoreexpin"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/WebCore.exp.in (149614 => 149615)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/WebCore.exp.in        2013-05-06 18:05:34 UTC (rev 149614)
+++ trunk/Source/WebCore/WebCore.exp.in        2013-05-06 18:10:10 UTC (rev 149615)
</span><span class="lines">@@ -470,7 +470,10 @@
</span><span class="cx"> __ZN7WebCore15SQLiteStatement21getColumnBlobAsStringEi
</span><span class="cx"> __ZN7WebCore15SQLiteStatement22isColumnDeclaredAsBlobEi
</span><span class="cx"> __ZN7WebCore15SQLiteStatement4stepEv
</span><ins>+__ZN7WebCore15SQLiteStatement5resetEv
</ins><span class="cx"> __ZN7WebCore15SQLiteStatement7prepareEv
</span><ins>+__ZN7WebCore15SQLiteStatement8bindBlobEiRKN3WTF6StringE
+__ZN7WebCore15SQLiteStatement8bindTextEiRKN3WTF6StringE
</ins><span class="cx"> __ZN7WebCore15SQLiteStatementC1ERNS_14SQLiteDatabaseERKN3WTF6StringE
</span><span class="cx"> __ZN7WebCore15SQLiteStatementD1Ev
</span><span class="cx"> __ZN7WebCore15ScrollAlignment17alignCenterAlwaysE
</span><span class="lines">@@ -2253,6 +2256,7 @@
</span><span class="cx"> #endif
</span><span class="cx"> 
</span><span class="cx"> #if !defined(NDEBUG)
</span><ins>+__ZN7WebCore14SQLiteDatabase22disableThreadingChecksEv
</ins><span class="cx"> __ZNK7WebCore7Element26fastAttributeLookupAllowedERKNS_13QualifiedNameE
</span><span class="cx"> #endif
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkSourceWebKit2ChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/ChangeLog (149614 => 149615)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/ChangeLog        2013-05-06 18:05:34 UTC (rev 149614)
+++ trunk/Source/WebKit2/ChangeLog        2013-05-06 18:10:10 UTC (rev 149615)
</span><span class="lines">@@ -1,3 +1,40 @@
</span><ins>+2013-05-06  Anders Carlsson  &lt;andersca@apple.com&gt;
+
+        Write storage changes to disk
+        https://bugs.webkit.org/show_bug.cgi?id=115660
+
+        Reviewed by Andreas Kling.
+
+        * UIProcess/Storage/LocalStorageDatabase.cpp:
+        (WebKit::LocalStorageDatabase::LocalStorageDatabase):
+        Initialize new member variables.
+
+        (WebKit::LocalStorageDatabase::tryToOpenDatabase):
+        Disable database threading checks.
+
+        (WebKit::LocalStorageDatabase::importItems):
+        Only import the items once.
+
+        (WebKit::LocalStorageDatabase::setItem):
+        Call itemDidChange.
+
+        (WebKit::LocalStorageDatabase::itemDidChange):
+        Record the change and schedule a database update.
+
+        (WebKit::LocalStorageDatabase::scheduleDatabaseUpdate):
+        Use WorkQueue::dispatchAfterDelay to schedule a datbase update.
+
+        (WebKit::LocalStorageDatabase::updateDatabase):
+        Write changes to disk. If there are more than 100 pending items, only write the first 100 and then schedule
+        another database update for the remaining items.
+
+        * UIProcess/Storage/LocalStorageDatabase.h:
+        Add new member variables.
+
+        * UIProcess/Storage/StorageManager.cpp:
+        (WebKit::StorageManager::StorageArea::setItem):
+        Call LocalStorageDatabase::setItem.
+
</ins><span class="cx"> 2013-05-06  Zan Dobersek  &lt;zdobersek@igalia.com&gt;
</span><span class="cx"> 
</span><span class="cx">         [WK2] Make the WebBatteryManagerProxy a supplement to the WebContext
</span></span></pre></div>
<a id="trunkSourceWebKit2UIProcessStorageLocalStorageDatabasecpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/UIProcess/Storage/LocalStorageDatabase.cpp (149614 => 149615)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/UIProcess/Storage/LocalStorageDatabase.cpp        2013-05-06 18:05:34 UTC (rev 149614)
+++ trunk/Source/WebKit2/UIProcess/Storage/LocalStorageDatabase.cpp        2013-05-06 18:10:10 UTC (rev 149615)
</span><span class="lines">@@ -37,6 +37,10 @@
</span><span class="cx"> 
</span><span class="cx"> using namespace WebCore;
</span><span class="cx"> 
</span><ins>+static const double databaseUpdateIntervalInSeconds = 1.0;
+
+static const int maximumItemsToUpdate = 100;
+
</ins><span class="cx"> namespace WebKit {
</span><span class="cx"> 
</span><span class="cx"> PassRefPtr&lt;LocalStorageDatabase&gt; LocalStorageDatabase::create(const String&amp; databaseFilename, PassRefPtr&lt;WorkQueue&gt; queue)
</span><span class="lines">@@ -48,6 +52,8 @@
</span><span class="cx">     : m_databaseFilename(databaseFilename)
</span><span class="cx">     , m_queue(queue)
</span><span class="cx">     , m_failedToOpenDatabase(false)
</span><ins>+    , m_didImportItems(false)
+    , m_didScheduleDatabaseUpdate(false)
</ins><span class="cx"> {
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="lines">@@ -83,6 +89,10 @@
</span><span class="cx">         return false;
</span><span class="cx">     }
</span><span class="cx"> 
</span><ins>+    // Since a WorkQueue isn't bound to a specific thread, we have to disable threading checks
+    // even though we never access the database from different threads simultaneously.
+    m_database.disableThreadingChecks();
+
</ins><span class="cx">     if (!migrateItemTableIfNeeded()) {
</span><span class="cx">         // We failed to migrate the item table. In order to avoid trying to migrate the table over and over,
</span><span class="cx">         // just delete it and start from scratch.
</span><span class="lines">@@ -138,9 +148,16 @@
</span><span class="cx"> 
</span><span class="cx"> void LocalStorageDatabase::importItems(StorageMap&amp; storageMap)
</span><span class="cx"> {
</span><ins>+    if (m_didImportItems)
+        return;
+
</ins><span class="cx">     // FIXME: If it can't import, then the default WebKit behavior should be that of private browsing,
</span><span class="cx">     // not silently ignoring it. https://bugs.webkit.org/show_bug.cgi?id=25894
</span><span class="cx"> 
</span><ins>+    // We set this to true even if we don't end up importing any items due to failure because
+    // there's really no good way to recover other than not importing anything.
+    m_didImportItems = true;
+
</ins><span class="cx">     openDatabase(SkipIfNonExistent);
</span><span class="cx">     if (!m_database.isOpen())
</span><span class="cx">         return;
</span><span class="lines">@@ -167,4 +184,87 @@
</span><span class="cx">     storageMap.importItems(items);
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+void LocalStorageDatabase::setItem(const String&amp; key, const String&amp; value)
+{
+    itemDidChange(key, value);
+}
+
+void LocalStorageDatabase::itemDidChange(const String&amp; key, const String&amp; value)
+{
+    m_changedItems.set(key, value);
+    scheduleDatabaseUpdate();
+}
+
+void LocalStorageDatabase::scheduleDatabaseUpdate()
+{
+    if (m_didScheduleDatabaseUpdate)
+        return;
+
+    m_didScheduleDatabaseUpdate = true;
+    m_queue-&gt;dispatchAfterDelay(bind(&amp;LocalStorageDatabase::updateDatabase, this), databaseUpdateIntervalInSeconds);
+}
+
+void LocalStorageDatabase::updateDatabase()
+{
+    ASSERT(m_didScheduleDatabaseUpdate);
+
+    m_didScheduleDatabaseUpdate = false;
+
+    // FIXME: Handle clearing.
+
+    HashMap&lt;String, String&gt; changedItems;
+    if (m_changedItems.size() &gt; maximumItemsToUpdate) {
+        for (int i = 0; i &lt; maximumItemsToUpdate; ++i) {
+            auto it = m_changedItems.begin();
+            changedItems.add(it-&gt;key, it-&gt;value);
+
+            m_changedItems.remove(it);
+        }
+
+        // Reschedule the update for the remaining items.
+        scheduleDatabaseUpdate();
+    } else {
+        // There are few enough changed items that we can just always write all of them.
+        m_changedItems.swap(changedItems);
+    }
+
+    ASSERT(changedItems.size() &lt;= maximumItemsToUpdate);
+
+    SQLiteStatement insertStatement(m_database, &quot;INSERT INTO ItemTable VALUES (?, ?)&quot;);
+    if (insertStatement.prepare() != SQLResultOk) {
+        LOG_ERROR(&quot;Failed to prepare insert statement - cannot write to local storage database&quot;);
+        return;
+    }
+
+    SQLiteStatement deleteStatement(m_database, &quot;DELETE FROM ItemTable WHERE key=?&quot;);
+    if (deleteStatement.prepare() != SQLResultOk) {
+        LOG_ERROR(&quot;Failed to prepare delete statement - cannot write to local storage database&quot;);
+        return;
+    }
+
+    SQLiteTransaction transaction(m_database);
+    transaction.begin();
+
+    for (auto it = changedItems.begin(), end = changedItems.end(); it != end; ++it) {
+        // A null value means that the key/value pair should be deleted.
+        SQLiteStatement&amp; statement = it-&gt;value.isNull() ? deleteStatement : insertStatement;
+
+        statement.bindText(1, it-&gt;key);
+
+        // If we're inserting a key/value pair, bind the value as well.
+        if (!it-&gt;value.isNull())
+            statement.bindBlob(2, it-&gt;value);
+
+        int result = statement.step();
+        if (result != SQLResultDone) {
+            LOG_ERROR(&quot;Failed to update item in the local storage database - %i&quot;, result);
+            break;
+        }
+
+        statement.reset();
+    }
+
+    transaction.commit();
+}
+
</ins><span class="cx"> } // namespace WebKit
</span></span></pre></div>
<a id="trunkSourceWebKit2UIProcessStorageLocalStorageDatabaseh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/UIProcess/Storage/LocalStorageDatabase.h (149614 => 149615)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/UIProcess/Storage/LocalStorageDatabase.h        2013-05-06 18:05:34 UTC (rev 149614)
+++ trunk/Source/WebKit2/UIProcess/Storage/LocalStorageDatabase.h        2013-05-06 18:10:10 UTC (rev 149615)
</span><span class="lines">@@ -28,6 +28,7 @@
</span><span class="cx"> 
</span><span class="cx"> #include &lt;WebCore/SQLiteDatabase.h&gt;
</span><span class="cx"> #include &lt;wtf/Forward.h&gt;
</span><ins>+#include &lt;wtf/HashMap.h&gt;
</ins><span class="cx"> #include &lt;wtf/RefPtr.h&gt;
</span><span class="cx"> #include &lt;wtf/ThreadSafeRefCounted.h&gt;
</span><span class="cx"> 
</span><span class="lines">@@ -47,6 +48,8 @@
</span><span class="cx">     // Will block until the import is complete.
</span><span class="cx">     void importItems(WebCore::StorageMap&amp;);
</span><span class="cx"> 
</span><ins>+    void setItem(const String&amp; key, const String&amp; value);
+
</ins><span class="cx"> private:
</span><span class="cx">     LocalStorageDatabase(const String&amp; databaseFilename, PassRefPtr&lt;WorkQueue&gt;);
</span><span class="cx"> 
</span><span class="lines">@@ -59,13 +62,20 @@
</span><span class="cx"> 
</span><span class="cx">     bool migrateItemTableIfNeeded();
</span><span class="cx"> 
</span><del>-    void performImport();
</del><ins>+    void itemDidChange(const String&amp; key, const String&amp; value);
</ins><span class="cx"> 
</span><ins>+    void scheduleDatabaseUpdate();
+    void updateDatabase();
+
</ins><span class="cx">     String m_databaseFilename;
</span><span class="cx">     RefPtr&lt;WorkQueue&gt; m_queue;
</span><span class="cx"> 
</span><span class="cx">     WebCore::SQLiteDatabase m_database;
</span><span class="cx">     bool m_failedToOpenDatabase;
</span><ins>+    bool m_didImportItems;
+
+    bool m_didScheduleDatabaseUpdate;
+    HashMap&lt;String, String&gt; m_changedItems;
</ins><span class="cx"> };
</span><span class="cx"> 
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkSourceWebKit2UIProcessStorageStorageManagercpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/UIProcess/Storage/StorageManager.cpp (149614 => 149615)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/UIProcess/Storage/StorageManager.cpp        2013-05-06 18:05:34 UTC (rev 149614)
+++ trunk/Source/WebKit2/UIProcess/Storage/StorageManager.cpp        2013-05-06 18:10:10 UTC (rev 149615)
</span><span class="lines">@@ -153,8 +153,13 @@
</span><span class="cx">     if (newStorageMap)
</span><span class="cx">         m_storageMap = newStorageMap.release();
</span><span class="cx"> 
</span><del>-    if (!quotaException)
-        dispatchEvents(sourceConnection, sourceStorageAreaID, key, oldValue, value, urlString);
</del><ins>+    if (quotaException)
+        return;
+
+    if (m_localStorageDatabase)
+        m_localStorageDatabase-&gt;setItem(key, value);
+
+    dispatchEvents(sourceConnection, sourceStorageAreaID, key, oldValue, value, urlString);
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> void StorageManager::StorageArea::removeItem(CoreIPC::Connection* sourceConnection, uint64_t sourceStorageAreaID, const String&amp; key, const String&amp; urlString)
</span></span></pre>
</div>
</div>

</body>
</html>