Title: [201232] trunk
Revision
201232
Author
beid...@apple.com
Date
2016-05-20 16:15:15 -0700 (Fri, 20 May 2016)

Log Message

Modern IDB: Properly handle blobs in Workers.
https://bugs.webkit.org/show_bug.cgi?id=157947

Reviewed by Alex Christensen.

Source/WebCore:

Test: storage/indexeddb/modern/blob-simple-workers.html

* Modules/indexeddb/IDBTransaction.cpp:
(WebCore::IDBTransaction::putOrAddOnServer): Use writeBlobsToDiskForIndexedDBSynchronously from
  background threads instead of the asynchronous form.

Add ability to set an existing empty IDBValue to be an isolated copy of a different IDBValue:
* Modules/indexeddb/IDBValue.cpp:
(WebCore::IDBValue::setAsIsolatedCopy):
(WebCore::IDBValue::isolatedCopy):
* Modules/indexeddb/IDBValue.h:

Add a method - only to be called from a non-main thread - that synchronously writes blobs to disk:
* bindings/js/SerializedScriptValue.cpp:
(WebCore::SerializedScriptValue::writeBlobsToDiskForIndexedDBSynchronously):
* bindings/js/SerializedScriptValue.h:

LayoutTests:

* storage/indexeddb/modern/blob-simple-workers-expected.txt: Added.
* storage/indexeddb/modern/blob-simple-workers.html: Added.
* storage/indexeddb/modern/resources/blob-simple-workers.js: Added.

Modified Paths

Added Paths

Diff

Modified: trunk/LayoutTests/ChangeLog (201231 => 201232)


--- trunk/LayoutTests/ChangeLog	2016-05-20 22:53:35 UTC (rev 201231)
+++ trunk/LayoutTests/ChangeLog	2016-05-20 23:15:15 UTC (rev 201232)
@@ -1,3 +1,14 @@
+2016-05-20  Brady Eidson  <beid...@apple.com>
+
+        Modern IDB: Properly handle blobs in Workers.
+        https://bugs.webkit.org/show_bug.cgi?id=157947
+
+        Reviewed by Alex Christensen.
+
+        * storage/indexeddb/modern/blob-simple-workers-expected.txt: Added.
+        * storage/indexeddb/modern/blob-simple-workers.html: Added.
+        * storage/indexeddb/modern/resources/blob-simple-workers.js: Added.
+        
 2016-05-20  Myles C. Maxfield  <mmaxfi...@apple.com>
 
         [Cocoa] REGRESSION(r184899): Ascent adjustments are applied to web fonts

Added: trunk/LayoutTests/storage/indexeddb/modern/blob-simple-workers-expected.txt (0 => 201232)


--- trunk/LayoutTests/storage/indexeddb/modern/blob-simple-workers-expected.txt	                        (rev 0)
+++ trunk/LayoutTests/storage/indexeddb/modern/blob-simple-workers-expected.txt	2016-05-20 23:15:15 UTC (rev 201232)
@@ -0,0 +1,48 @@
+[Worker] This tests basic operations putting blobs into an object store and then retrieving them.
+
+On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".
+
+
+Starting worker: resources/blob-simple-workers.js
+[Worker] indexedDB = self.indexedDB || self.webkitIndexedDB || self.mozIndexedDB || self.msIndexedDB || self.OIndexedDB;
+[Worker] 
+[Worker] indexedDB.deleteDatabase(dbname)
+[Worker] indexedDB.open(dbname)
+[Worker] Initial upgrade needed: Old version - 0 New version - 1
+[Worker] Let's create a blob and store it in IndexedDB twice.
+[Worker] Added blob to database once
+[Worker] Added blob to database twice
+[Worker] Let's retrieve the blob again and verify the contents is the same.
+[Worker] Got blob from database
+[Worker] Correct text: true
+[Worker] Let's retrieve it again, create an object URL for the blob, load it via an XMLHttpRequest, and verify the contents are the same.
+[Worker] Got blob from database
+[Worker] Correct responseText: true
+[Worker] Retrieve both blob entries from the database and verify contents.
+[Worker] Correct text on first value: true
+[Worker] Correct text on second value: true
+[Worker] Got item from cursor
+[Worker] Got item from cursor
+[Worker] Finished cursor
+[Worker] Got right number of items: true
+[Worker] Correct text: true
+[Worker] Retrieve blobs from database via index and verify contents.
+[Worker] Got blob from database
+[Worker] Correct text: true
+[Worker] Got item from cursor
+[Worker] Got item from cursor
+[Worker] Finished cursor
+[Worker] Got right number of items: true
+[Worker] Correct text: true
+[Worker] Correct text: true
+[Worker] Slice the the retrieved blob and verify its contents.
+[Worker] Correct text: true
+[Worker] Store a blob back in the database, and keep holding on to the blob, verifying that it still can be read.
+[Worker] Got blob from database
+[Worker] Stored blob back into database
+[Worker] Correct text: true
+[Worker] Correct responseText: true
+PASS successfullyParsed is true
+
+TEST COMPLETE
+

Added: trunk/LayoutTests/storage/indexeddb/modern/blob-simple-workers.html (0 => 201232)


--- trunk/LayoutTests/storage/indexeddb/modern/blob-simple-workers.html	                        (rev 0)
+++ trunk/LayoutTests/storage/indexeddb/modern/blob-simple-workers.html	2016-05-20 23:15:15 UTC (rev 201232)
@@ -0,0 +1,12 @@
+<html>
+<head>
+<script src=""
+<script src=""
+</head>
+<body>
+<script>
+if (window.internals)
+    window.internals.settings.setIndexedDBWorkersEnabled(true);
+worker = startWorker('resources/blob-simple-workers.js');</script>
+</body>
+</html>

Added: trunk/LayoutTests/storage/indexeddb/modern/resources/blob-simple-workers.js (0 => 201232)


--- trunk/LayoutTests/storage/indexeddb/modern/resources/blob-simple-workers.js	                        (rev 0)
+++ trunk/LayoutTests/storage/indexeddb/modern/resources/blob-simple-workers.js	2016-05-20 23:15:15 UTC (rev 201232)
@@ -0,0 +1,270 @@
+if (this.importScripts) {
+    importScripts('../../../../resources/js-test.js');
+    importScripts('../../resources/shared.js');
+}
+
+// Original implementation of test from Mozilla: ./dom/indexedDB/test/test_blob_simple.html
+description("This tests basic operations putting blobs into an object store and then retrieving them.");
+
+indexedDBTest(prepareDatabase);
+
+function done()
+{
+    finishJSTest();
+}
+
+function log(message)
+{
+    debug(message);
+}
+
+var testGenerator;
+
+function continueWithEvent(event)
+{
+    testGenerator.next(event);
+}
+
+function asyncContinue()
+{
+    setTimeout("testGenerator.next();", 0);
+}
+
+function idbRequest(request)
+{
+    request._onerror_ = continueWithEvent;
+    request._onsuccess_ = continueWithEvent;
+}
+
+var db;
+
+function prepareDatabase(event)
+{
+    debug("Initial upgrade needed: Old version - " + event.oldVersion + " New version - " + event.newVersion);
+
+    db = event.target.result;
+    db.createObjectStore("foo", { autoIncrement: true }).createIndex("foo", "index");
+    event.target._onsuccess_ = function() {
+        testGenerator = testSteps();
+        testGenerator.next();
+    };
+}
+
+function is(a, b, message) {
+    result = a == b ? "true" : "false";
+    debug(message + ": " + result);
+}
+
+function* testSteps()
+{
+    debug("Let's create a blob and store it in IndexedDB twice.");
+
+    const BLOB_DATA = ["fun ", "times ", "all ", "around!"];
+    const INDEX_KEY = 5;
+    let blob = new Blob(BLOB_DATA, { type: "text/plain" });
+    let data = { blob: blob, index: INDEX_KEY };
+
+    objectStore = db.transaction("foo", "readwrite").objectStore("foo");
+    
+    idbRequest(objectStore.add(data));
+    event = yield;
+    
+    debug("Added blob to database once");
+
+    let key1 = event.target.result;
+
+    idbRequest(objectStore.add(data));
+    event = yield;
+
+    debug("Added blob to database twice");
+    
+    let key2 = event.target.result;
+
+    debug("Let's retrieve the blob again and verify the contents is the same.");
+
+    objectStore = db.transaction("foo").objectStore("foo");
+    idbRequest(objectStore.get(key1));
+    event = yield;
+
+    debug("Got blob from database");
+    
+    let fileReader = new FileReader();
+    fileReader._onload_ = continueWithEvent;
+    fileReader.readAsText(event.target.result.blob);
+    event = yield;
+
+    is(event.target.result, BLOB_DATA.join(""), "Correct text");
+
+    debug("Let's retrieve it again, create an object URL for the blob, load it via an XMLHttpRequest, and verify the contents are the same.");
+    
+    objectStore = db.transaction("foo").objectStore("foo");
+    idbRequest(objectStore.get(key1));
+    event = yield;
+    
+    debug("Got blob from database");
+    
+    let blobURL = URL.createObjectURL(event.target.result.blob);
+    
+    let xhr = new XMLHttpRequest();
+    xhr.open("GET", blobURL);
+    xhr._onload_ = continueWithEvent;
+    xhr.send();
+    yield;
+    
+    URL.revokeObjectURL(blobURL);    
+    is(xhr.responseText, BLOB_DATA.join(""), "Correct responseText");
+    
+    debug("Retrieve both blob entries from the database and verify contents.");
+
+    var objects = []
+    
+    objectStore = db.transaction("foo").objectStore("foo");
+    idbRequest(objectStore.get(key1));
+    event = yield;
+    
+    objects.push(event.target.result);
+    
+    objectStore = db.transaction("foo").objectStore("foo");
+    idbRequest(objectStore.get(key2));
+    event = yield;
+    
+    objects.push(event.target.result);
+        
+    fileReader = new FileReader();
+    fileReader._onload_ = continueWithEvent;
+    fileReader.readAsText(objects[0].blob);
+    event = yield;
+
+    is(event.target.result, BLOB_DATA.join(""), "Correct text on first value");
+
+    fileReader = new FileReader();
+    fileReader._onload_ = continueWithEvent;
+    fileReader.readAsText(objects[1].blob);
+    event = yield;
+
+    is(event.target.result, BLOB_DATA.join(""), "Correct text on second value");
+
+    let cursorResults = [];
+    
+    objectStore = db.transaction("foo").objectStore("foo");
+    objectStore.openCursor()._onsuccess_ = function(event) {
+      let cursor = event.target.result;
+      if (cursor) {
+        debug("Got item from cursor");
+        cursorResults.push(cursor.value);
+        cursor.continue();
+      }
+      else {
+        debug("Finished cursor");
+        asyncContinue();
+      }
+    };
+    yield;
+
+    is(cursorResults.length, 2, "Got right number of items");
+
+    fileReader = new FileReader();
+    fileReader._onload_ = continueWithEvent;
+    fileReader.readAsText(cursorResults[0].blob);
+    event = yield;
+    
+    is(event.target.result, BLOB_DATA.join(""), "Correct text");
+
+    debug("Retrieve blobs from database via index and verify contents.");
+    
+    index = db.transaction("foo").objectStore("foo").index("foo");
+    idbRequest(index.get(INDEX_KEY)); 
+    event = yield;
+    
+    debug("Got blob from database");
+    
+    fileReader = new FileReader();
+    fileReader._onload_ = continueWithEvent;
+    fileReader.readAsText(event.target.result.blob);
+    event = yield;
+    
+    is(event.target.result, BLOB_DATA.join(""), "Correct text");
+        
+    cursorResults = [];
+    
+    index = db.transaction("foo").objectStore("foo").index("foo");
+    index.openCursor()._onsuccess_ = function(event) {
+      let cursor = event.target.result;
+      if (cursor) {
+        debug("Got item from cursor");
+        cursorResults.push(cursor.value);
+        cursor.continue();
+      }
+      else {
+        debug("Finished cursor");
+        asyncContinue();
+      }
+    };
+    yield;
+    
+    is(cursorResults.length, 2, "Got right number of items");
+    
+    fileReader = new FileReader();
+    fileReader._onload_ = continueWithEvent;
+    fileReader.readAsText(cursorResults[0].blob);
+    event = yield;
+    
+    is(event.target.result, BLOB_DATA.join(""), "Correct text");
+    
+    fileReader = new FileReader();
+    fileReader._onload_ = continueWithEvent;
+    fileReader.readAsText(cursorResults[1].blob);
+    event = yield;
+    
+    is(event.target.result, BLOB_DATA.join(""), "Correct text");
+    
+    debug("Slice the the retrieved blob and verify its contents.");
+    
+    let slice = cursorResults[1].blob.slice(0, BLOB_DATA[0].length);
+    
+    fileReader = new FileReader();
+    fileReader._onload_ = continueWithEvent;
+    fileReader.readAsText(slice);
+    event = yield;
+    
+    is(event.target.result, BLOB_DATA[0], "Correct text");
+    
+    debug("Store a blob back in the database, and keep holding on to the " +
+         "blob, verifying that it still can be read.");
+    
+    objectStore = db.transaction("foo").objectStore("foo");
+    idbRequest(objectStore.get(key1)); 
+    event = yield;
+    
+    let blobFromDB = event.target.result.blob;
+    debug("Got blob from database");
+    
+    let txn = db.transaction("foo", "readwrite");
+    txn.objectStore("foo").put(event.target.result, key1);
+    txn._oncomplete_ = continueWithEvent;
+    event = yield;
+    
+    debug("Stored blob back into database");
+    
+    fileReader = new FileReader();
+    fileReader._onload_ = continueWithEvent;
+    fileReader.readAsText(blobFromDB);
+    event = yield;
+    
+    is(event.target.result, BLOB_DATA.join(""), "Correct text");
+    
+    blobURL = URL.createObjectURL(blobFromDB);
+    
+    xhr = new XMLHttpRequest();
+    xhr.open("GET", blobURL);
+    xhr._onload_ = continueWithEvent;
+    xhr.send();
+    yield;
+    
+    URL.revokeObjectURL(blobURL);
+    
+    is(xhr.responseText, BLOB_DATA.join(""), "Correct responseText");
+    
+    finishJSTest();
+ }
+ 
\ No newline at end of file

Modified: trunk/Source/WebCore/ChangeLog (201231 => 201232)


--- trunk/Source/WebCore/ChangeLog	2016-05-20 22:53:35 UTC (rev 201231)
+++ trunk/Source/WebCore/ChangeLog	2016-05-20 23:15:15 UTC (rev 201232)
@@ -1,3 +1,27 @@
+2016-05-20  Brady Eidson  <beid...@apple.com>
+
+        Modern IDB: Properly handle blobs in Workers.
+        https://bugs.webkit.org/show_bug.cgi?id=157947
+
+        Reviewed by Alex Christensen.
+
+        Test: storage/indexeddb/modern/blob-simple-workers.html
+
+        * Modules/indexeddb/IDBTransaction.cpp:
+        (WebCore::IDBTransaction::putOrAddOnServer): Use writeBlobsToDiskForIndexedDBSynchronously from
+          background threads instead of the asynchronous form.
+        
+        Add ability to set an existing empty IDBValue to be an isolated copy of a different IDBValue:
+        * Modules/indexeddb/IDBValue.cpp:
+        (WebCore::IDBValue::setAsIsolatedCopy):
+        (WebCore::IDBValue::isolatedCopy):
+        * Modules/indexeddb/IDBValue.h:
+        
+        Add a method - only to be called from a non-main thread - that synchronously writes blobs to disk:
+        * bindings/js/SerializedScriptValue.cpp:
+        (WebCore::SerializedScriptValue::writeBlobsToDiskForIndexedDBSynchronously):
+        * bindings/js/SerializedScriptValue.h:
+        
 2016-05-20  Anders Carlsson  <ander...@apple.com>
 
         Allow named images on iOS

Modified: trunk/Source/WebCore/Modules/indexeddb/IDBTransaction.cpp (201231 => 201232)


--- trunk/Source/WebCore/Modules/indexeddb/IDBTransaction.cpp	2016-05-20 22:53:35 UTC (rev 201231)
+++ trunk/Source/WebCore/Modules/indexeddb/IDBTransaction.cpp	2016-05-20 23:15:15 UTC (rev 201232)
@@ -938,7 +938,7 @@
 void IDBTransaction::putOrAddOnServer(IDBClient::TransactionOperation& operation, RefPtr<IDBKey> key, RefPtr<SerializedScriptValue> value, const IndexedDB::ObjectStoreOverwriteMode& overwriteMode)
 {
     LOG(IndexedDB, "IDBTransaction::putOrAddOnServer");
-    ASSERT(currentThread() == m_database->originThreadID());
+    ASSERT(currentThread() == originThreadID());
     ASSERT(!isReadOnly());
     ASSERT(value);
 
@@ -947,9 +947,30 @@
         return;
     }
 
+    // Due to current limitations on our ability to post tasks back to a worker thread,
+    // workers currently write blobs to disk synchronously.
+    // FIXME: https://bugs.webkit.org/show_bug.cgi?id=157958 - Make this asynchronous after refactoring allows it.
+    if (!isMainThread()) {
+        auto idbValue = value->writeBlobsToDiskForIndexedDBSynchronously();
+        if (idbValue.data().data())
+            m_database->connectionProxy().putOrAdd(operation, key.get(), idbValue, overwriteMode);
+        else {
+            // If the IDBValue doesn't have any data, then something went wrong writing the blobs to disk.
+            // In that case, we cannot successfully store this record, so we callback with an error.
+            RefPtr<IDBClient::TransactionOperation> protectedOperation(&operation);
+            auto result = IDBResultData::error(operation.identifier(), { IDBDatabaseException::UnknownError, ASCIILiteral("Error preparing Blob/File data to be stored in object store") });
+            scriptExecutionContext()->postTask([protectedOperation, result](ScriptExecutionContext&) {
+                protectedOperation->completed(result);
+            });
+        }
+        return;
+    }
+
     RefPtr<IDBTransaction> protectedThis(this);
     RefPtr<IDBClient::TransactionOperation> protectedOperation(&operation);
     value->writeBlobsToDiskForIndexedDB([protectedThis, this, protectedOperation, key, value, overwriteMode](const IDBValue& idbValue) {
+        ASSERT(currentThread() == originThreadID());
+        ASSERT(isMainThread());
         if (idbValue.data().data()) {
             m_database->connectionProxy().putOrAdd(*protectedOperation, key.get(), idbValue, overwriteMode);
             return;

Modified: trunk/Source/WebCore/Modules/indexeddb/IDBValue.cpp (201231 => 201232)


--- trunk/Source/WebCore/Modules/indexeddb/IDBValue.cpp	2016-05-20 22:53:35 UTC (rev 201231)
+++ trunk/Source/WebCore/Modules/indexeddb/IDBValue.cpp	2016-05-20 23:15:15 UTC (rev 201232)
@@ -28,6 +28,7 @@
 
 #if ENABLE(INDEXED_DATABASE)
 
+#include "CrossThreadCopier.h"
 #include "SerializedScriptValue.h"
 
 namespace WebCore {
@@ -69,19 +70,19 @@
 {
 }
 
+void IDBValue::setAsIsolatedCopy(const IDBValue& other)
+{
+    ASSERT(m_blobURLs.isEmpty() && m_blobFilePaths.isEmpty());
+
+    m_data = other.m_data;
+    m_blobURLs = CrossThreadCopier<Vector<String>>::copy(other.m_blobURLs);
+    m_blobFilePaths = CrossThreadCopier<Vector<String>>::copy(other.m_blobFilePaths);
+}
+
 IDBValue IDBValue::isolatedCopy() const
 {
     IDBValue result;
-    result.m_data = m_data;
-
-    result.m_blobURLs.reserveInitialCapacity(m_blobURLs.size());
-    for (auto& url : m_blobURLs)
-        result.m_blobURLs.uncheckedAppend(url.isolatedCopy());
-
-    result.m_blobFilePaths.reserveInitialCapacity(m_blobFilePaths.size());
-    for (auto& path : m_blobFilePaths)
-        result.m_blobFilePaths.uncheckedAppend(path.isolatedCopy());
-
+    result.setAsIsolatedCopy(*this);
     return result;
 }
 

Modified: trunk/Source/WebCore/Modules/indexeddb/IDBValue.h (201231 => 201232)


--- trunk/Source/WebCore/Modules/indexeddb/IDBValue.h	2016-05-20 22:53:35 UTC (rev 201231)
+++ trunk/Source/WebCore/Modules/indexeddb/IDBValue.h	2016-05-20 23:15:15 UTC (rev 201232)
@@ -42,6 +42,7 @@
     IDBValue(const ThreadSafeDataBuffer&, Vector<String>&& blobURLs, Vector<String>&& blobFilePaths);
     IDBValue(const ThreadSafeDataBuffer&, const Vector<String>& blobURLs, const Vector<String>& blobFilePaths);
 
+    void setAsIsolatedCopy(const IDBValue&);
     IDBValue isolatedCopy() const;
 
     const ThreadSafeDataBuffer& data() const { return m_data; }

Modified: trunk/Source/WebCore/bindings/js/SerializedScriptValue.cpp (201231 => 201232)


--- trunk/Source/WebCore/bindings/js/SerializedScriptValue.cpp	2016-05-20 22:53:35 UTC (rev 201231)
+++ trunk/Source/WebCore/bindings/js/SerializedScriptValue.cpp	2016-05-20 23:15:15 UTC (rev 201232)
@@ -79,6 +79,7 @@
 #include <runtime/TypedArrays.h>
 #include <wtf/HashTraits.h>
 #include <wtf/MainThread.h>
+#include <wtf/RunLoop.h>
 #include <wtf/Vector.h>
 
 using namespace JSC;
@@ -2816,6 +2817,33 @@
         completionHandler({ *this, m_blobURLs, blobFilePaths });
     });
 }
+
+IDBValue SerializedScriptValue::writeBlobsToDiskForIndexedDBSynchronously()
+{
+    ASSERT(!isMainThread());
+
+    IDBValue value;
+    IDBValue* valuePtr = &value;
+
+    Lock lock;
+    Condition condition;
+    Condition* conditionPtr = &condition;
+    lock.lock();
+
+    RunLoop::main().dispatch([this, conditionPtr, valuePtr] {
+        writeBlobsToDiskForIndexedDB([conditionPtr, valuePtr](const IDBValue& result) {
+            ASSERT(isMainThread());
+            valuePtr->setAsIsolatedCopy(result);
+
+            conditionPtr->notifyAll();
+        });
+    });
+
+    condition.wait(lock);
+
+    return value;
+}
+
 #endif // ENABLE(INDEXED_DATABASE)
 
 } // namespace WebCore

Modified: trunk/Source/WebCore/bindings/js/SerializedScriptValue.h (201231 => 201232)


--- trunk/Source/WebCore/bindings/js/SerializedScriptValue.h	2016-05-20 22:53:35 UTC (rev 201231)
+++ trunk/Source/WebCore/bindings/js/SerializedScriptValue.h	2016-05-20 23:15:15 UTC (rev 201232)
@@ -87,6 +87,7 @@
 #if ENABLE(INDEXED_DATABASE)
     Vector<String> blobURLsIsolatedCopy() const;
     void writeBlobsToDiskForIndexedDB(std::function<void (const IDBValue&)> completionHandler);
+    IDBValue writeBlobsToDiskForIndexedDBSynchronously();
 #endif // ENABLE(INDEXED_DATABASE)
 
     static Ref<SerializedScriptValue> createFromWireBytes(Vector<uint8_t>&& data)
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to