<!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>[279237] trunk</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/279237">279237</a></dd>
<dt>Author</dt> <dd>sihui_liu@apple.com</dd>
<dt>Date</dt> <dd>2021-06-24 10:49:34 -0700 (Thu, 24 Jun 2021)</dd>
</dl>

<h3>Log Message</h3>
<pre>IndexedDB prints error log about blob files when there is no error
https://bugs.webkit.org/show_bug.cgi?id=227092

Reviewed by Chris Dumez.

Source/WebCore:

We should try opening database if file exists, and try getting blob files only if database is opened.

API test: IndexedDB.StoreBlobThenDeleteDatabase

* Modules/indexeddb/server/SQLiteIDBBackingStore.cpp:
(WebCore::IDBServer::SQLiteIDBBackingStore::deleteBackingStore):

Tools:

* TestWebKitAPI/Tests/WebKitCocoa/StoreBlobThenDelete.mm:
(TEST):
* TestWebKitAPI/Tests/WebKitCocoa/StoreBlobToBeDeleted.html:</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceWebCoreChangeLog">trunk/Source/WebCore/ChangeLog</a></li>
<li><a href="#trunkSourceWebCoreModulesindexeddbserverSQLiteIDBBackingStorecpp">trunk/Source/WebCore/Modules/indexeddb/server/SQLiteIDBBackingStore.cpp</a></li>
<li><a href="#trunkToolsChangeLog">trunk/Tools/ChangeLog</a></li>
<li><a href="#trunkToolsTestWebKitAPITestsWebKitCocoaStoreBlobThenDeletemm">trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/StoreBlobThenDelete.mm</a></li>
<li><a href="#trunkToolsTestWebKitAPITestsWebKitCocoaStoreBlobToBeDeletedhtml">trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/StoreBlobToBeDeleted.html</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceWebCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/ChangeLog (279236 => 279237)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/ChangeLog   2021-06-24 17:38:58 UTC (rev 279236)
+++ trunk/Source/WebCore/ChangeLog      2021-06-24 17:49:34 UTC (rev 279237)
</span><span class="lines">@@ -1,3 +1,17 @@
</span><ins>+2021-06-24  Sihui Liu  <sihui_liu@apple.com>
+
+        IndexedDB prints error log about blob files when there is no error
+        https://bugs.webkit.org/show_bug.cgi?id=227092
+
+        Reviewed by Chris Dumez.
+
+        We should try opening database if file exists, and try getting blob files only if database is opened.
+
+        API test: IndexedDB.StoreBlobThenDeleteDatabase
+
+        * Modules/indexeddb/server/SQLiteIDBBackingStore.cpp:
+        (WebCore::IDBServer::SQLiteIDBBackingStore::deleteBackingStore):
+
</ins><span class="cx"> 2021-06-24  Kimmo Kinnunen  <kkinnunen@apple.com>
</span><span class="cx"> 
</span><span class="cx">         Using a video as a source for a WebGL texture is slow and hangs on iOS 15
</span></span></pre></div>
<a id="trunkSourceWebCoreModulesindexeddbserverSQLiteIDBBackingStorecpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/Modules/indexeddb/server/SQLiteIDBBackingStore.cpp (279236 => 279237)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/Modules/indexeddb/server/SQLiteIDBBackingStore.cpp  2021-06-24 17:38:58 UTC (rev 279236)
+++ trunk/Source/WebCore/Modules/indexeddb/server/SQLiteIDBBackingStore.cpp     2021-06-24 17:49:34 UTC (rev 279237)
</span><span class="lines">@@ -2756,17 +2756,23 @@
</span><span class="cx"> 
</span><span class="cx"> void SQLiteIDBBackingStore::deleteBackingStore()
</span><span class="cx"> {
</span><del>-    String dbFilename = fullDatabasePath();
</del><ins>+    String databasePath = fullDatabasePath();
</ins><span class="cx"> 
</span><del>-    LOG(IndexedDB, "SQLiteIDBBackingStore::deleteBackingStore deleting file '%s' on disk", dbFilename.utf8().data());
</del><ins>+    LOG(IndexedDB, "SQLiteIDBBackingStore::deleteBackingStore deleting file '%s' on disk", databasePath.utf8().data());
</ins><span class="cx"> 
</span><del>-    Vector<String> blobFiles;
-    {
-        bool errored = true;
</del><ins>+    if (FileSystem::fileExists(databasePath) && !m_sqliteDB) {
+        m_sqliteDB = makeUnique<SQLiteDatabase>();
+        if (!m_sqliteDB->open(databasePath))
+            closeSQLiteDB();
+    }
</ins><span class="cx"> 
</span><del>-        if (m_sqliteDB) {
</del><ins>+    if (m_sqliteDB) {
+        Vector<String> blobFiles;
+        {
</ins><span class="cx">             auto sql = m_sqliteDB->prepareStatement("SELECT fileName FROM BlobFiles;"_s);
</span><del>-            if (sql) {
</del><ins>+            if (!sql)
+                LOG_ERROR("Error preparing statement to get blob filenames (%i) - %s", m_sqliteDB->lastError(), m_sqliteDB->lastErrorMsg());
+            else {
</ins><span class="cx">                 int result = sql->step();
</span><span class="cx">                 while (result == SQLITE_ROW) {
</span><span class="cx">                     blobFiles.append(sql->columnText(0));
</span><span class="lines">@@ -2773,25 +2779,21 @@
</span><span class="cx">                     result = sql->step();
</span><span class="cx">                 }
</span><span class="cx"> 
</span><del>-                if (result == SQLITE_DONE)
-                    errored = false;
</del><ins>+                if (result != SQLITE_DONE)
+                    LOG_ERROR("Error getting blob filenames (%i) - %s", m_sqliteDB->lastError(), m_sqliteDB->lastErrorMsg());
</ins><span class="cx">             }
</span><span class="cx">         }
</span><span class="cx"> 
</span><del>-        if (errored)
-            LOG_ERROR("Error getting all blob filenames to be deleted");
-    }
</del><ins>+        for (auto& file : blobFiles) {
+            String blobPath = FileSystem::pathByAppendingComponent(m_databaseDirectory, file);
+            if (!FileSystem::deleteFile(blobPath))
+                LOG_ERROR("Error deleting blob file '%s'", blobPath.utf8().data());
+        }
</ins><span class="cx"> 
</span><del>-    for (auto& file : blobFiles) {
-        String fullPath = FileSystem::pathByAppendingComponent(m_databaseDirectory, file);
-        if (!FileSystem::deleteFile(fullPath))
-            LOG_ERROR("Error deleting blob file %s", fullPath.utf8().data());
</del><ins>+        closeSQLiteDB();
</ins><span class="cx">     }
</span><span class="cx"> 
</span><del>-    if (m_sqliteDB)
-        closeSQLiteDB();
-
-    SQLiteFileSystem::deleteDatabaseFile(dbFilename);
</del><ins>+    SQLiteFileSystem::deleteDatabaseFile(databasePath);
</ins><span class="cx">     SQLiteFileSystem::deleteEmptyDatabaseDirectory(m_databaseDirectory);
</span><span class="cx">     SQLiteFileSystem::deleteEmptyDatabaseDirectory(m_identifier.databaseDirectoryRelativeToRoot(m_databaseRootDirectory));
</span><span class="cx"> }
</span></span></pre></div>
<a id="trunkToolsChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Tools/ChangeLog (279236 => 279237)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Tools/ChangeLog    2021-06-24 17:38:58 UTC (rev 279236)
+++ trunk/Tools/ChangeLog       2021-06-24 17:49:34 UTC (rev 279237)
</span><span class="lines">@@ -1,3 +1,14 @@
</span><ins>+2021-06-24  Sihui Liu  <sihui_liu@apple.com>
+
+        IndexedDB prints error log about blob files when there is no error
+        https://bugs.webkit.org/show_bug.cgi?id=227092
+
+        Reviewed by Chris Dumez.
+
+        * TestWebKitAPI/Tests/WebKitCocoa/StoreBlobThenDelete.mm:
+        (TEST):
+        * TestWebKitAPI/Tests/WebKitCocoa/StoreBlobToBeDeleted.html:
+
</ins><span class="cx"> 2021-06-24  Philippe Normand  <pnormand@igalia.com>
</span><span class="cx"> 
</span><span class="cx">         [WPE] Uninstalled Cog now requires a COG_MODULEDIR environment variable
</span></span></pre></div>
<a id="trunkToolsTestWebKitAPITestsWebKitCocoaStoreBlobThenDeletemm"></a>
<div class="modfile"><h4>Modified: trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/StoreBlobThenDelete.mm (279236 => 279237)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/StoreBlobThenDelete.mm       2021-06-24 17:38:58 UTC (rev 279236)
+++ trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/StoreBlobThenDelete.mm  2021-06-24 17:49:34 UTC (rev 279237)
</span><span class="lines">@@ -52,7 +52,7 @@
</span><span class="cx"> 
</span><span class="cx"> @end
</span><span class="cx"> 
</span><del>-TEST(IndexedDB, StoreBlobThenDelete)
</del><ins>+TEST(IndexedDB, StoreBlobThenRemoveData)
</ins><span class="cx"> {
</span><span class="cx">     RetainPtr<StoreBlobMessageHandler> handler = adoptNS([[StoreBlobMessageHandler alloc] init]);
</span><span class="cx">     RetainPtr<WKWebViewConfiguration> configuration = adoptNS([[WKWebViewConfiguration alloc] init]);
</span><span class="lines">@@ -126,3 +126,37 @@
</span><span class="cx">     }];
</span><span class="cx">     TestWebKitAPI::Util::run(&readyToContinue);
</span><span class="cx"> }
</span><ins>+
+TEST(IndexedDB, StoreBlobThenDeleteDatabase)
+{
+    auto handler = adoptNS([[StoreBlobMessageHandler alloc] init]);
+    auto configuration = adoptNS([[WKWebViewConfiguration alloc] init]);
+    [[configuration userContentController] addScriptMessageHandler:handler.get() name:@"testHandler"];
+    auto webView = adoptNS([[WKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600) configuration:configuration.get()]);
+    NSURLRequest *request = [NSURLRequest requestWithURL:[[NSBundle mainBundle] URLForResource:@"StoreBlobToBeDeleted" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"]];
+    [webView loadRequest:request];
+    TestWebKitAPI::Util::run(&readyToContinue);
+    EXPECT_WK_STREQ(@"Success", (NSString *)[lastScriptMessage body]);
+
+    NSString *hash = WebCore::SQLiteFileSystem::computeHashForFileName("StoreBlobToBeDeleted");
+    NSString *originDirectory = @"~/Library/WebKit/com.apple.WebKit.TestWebKitAPI/WebsiteData/IndexedDB/v1/file__0/";
+    NSString *databaseDirectory = [[originDirectory stringByAppendingString:hash] stringByExpandingTildeInPath];
+    NSString *blobFilePath = [databaseDirectory stringByAppendingPathComponent:@"1.blob"];
+    NSString *databaseFilePath = [databaseDirectory stringByAppendingPathComponent:@"IndexedDB.sqlite3"];
+
+    EXPECT_TRUE([[NSFileManager defaultManager] fileExistsAtPath:blobFilePath]);
+    EXPECT_TRUE([[NSFileManager defaultManager] fileExistsAtPath:databaseFilePath]);
+
+    // Add a .blob file that is not created by IndexedDB API.
+    NSString *anotherBlobFilePath = [databaseDirectory stringByAppendingPathComponent:@"7182.blob"];
+    [[NSFileManager defaultManager] copyItemAtPath:blobFilePath toPath:anotherBlobFilePath error:nil];
+
+    readyToContinue = false;
+    [webView evaluateJavaScript:@"deleteDatabase(() => { sendMessage('Delete success'); })" completionHandler:nil];
+    TestWebKitAPI::Util::run(&readyToContinue);
+    EXPECT_WK_STREQ(@"Delete success", (NSString *)[lastScriptMessage body]);
+
+    EXPECT_FALSE([[NSFileManager defaultManager] fileExistsAtPath:blobFilePath]);
+    EXPECT_FALSE([[NSFileManager defaultManager] fileExistsAtPath:databaseFilePath]);
+    EXPECT_TRUE([[NSFileManager defaultManager] fileExistsAtPath:anotherBlobFilePath]);
+}
</ins></span></pre></div>
<a id="trunkToolsTestWebKitAPITestsWebKitCocoaStoreBlobToBeDeletedhtml"></a>
<div class="modfile"><h4>Modified: trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/StoreBlobToBeDeleted.html (279236 => 279237)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/StoreBlobToBeDeleted.html    2021-06-24 17:38:58 UTC (rev 279236)
+++ trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/StoreBlobToBeDeleted.html       2021-06-24 17:49:34 UTC (rev 279237)
</span><span class="lines">@@ -1,22 +1,25 @@
</span><span class="cx"> <script>
</span><span class="cx"> 
</span><span class="cx"> var dbName = "StoreBlobToBeDeleted";
</span><del>-var request = window.indexedDB.deleteDatabase(dbName);
-request.onsuccess = function(e)
</del><ins>+var db;
+
+function sendMessage(text)
</ins><span class="cx"> {
</span><del>-    continueTest();
</del><ins>+    window.webkit.messageHandlers.testHandler.postMessage(text);
</ins><span class="cx"> }
</span><del>-request.onerror = function(e)
</del><ins>+
+function deleteDatabase(successHandler)
</ins><span class="cx"> {
</span><del>-    window.webkit.messageHandlers.testHandler.postMessage('Error deleting database');
</del><ins>+    var request = window.indexedDB.deleteDatabase(dbName);
+    request.onsuccess = successHandler;
+    request.onerror = function() {
+        sendMessage('Error deleting database');
+    }
</ins><span class="cx"> }
</span><span class="cx"> 
</span><del>-var db;
-
-function continueTest(event)
</del><ins>+function storeBlobInDatabase()
</ins><span class="cx"> {
</span><span class="cx">     var request = window.indexedDB.open(dbName);
</span><del>-
</del><span class="cx">     request.onupgradeneeded = function(e) {
</span><span class="cx">         db = e.target.result;
</span><span class="cx">         var objectStore = db.createObjectStore("TestStore");
</span><span class="lines">@@ -26,16 +29,20 @@
</span><span class="cx"> 
</span><span class="cx">         var addRequest = objectStore.add(blob, "BlobKey");
</span><span class="cx">         addRequest.onerror = function() {
</span><del>-            window.webkit.messageHandlers.testHandler.postMessage('Error storing blob in database');
</del><ins>+            sendMessage('Error storing blob in database');
</ins><span class="cx">         }
</span><span class="cx">     }
</span><span class="cx"> 
</span><span class="cx">     request.onsuccess = function() {
</span><span class="cx">         db.close();
</span><del>-        window.webkit.messageHandlers.testHandler.postMessage('Success');
</del><ins>+        sendMessage('Success');
</ins><span class="cx">     }
</span><ins>+
</ins><span class="cx">     request.onerror = function() {
</span><del>-        window.webkit.messageHandlers.testHandler.postMessage('Error storing blob in database');
</del><ins>+        sendMessage('Error storing blob in database');
</ins><span class="cx">     }
</span><span class="cx"> }
</span><ins>+    
+deleteDatabase(storeBlobInDatabase);
+
</ins><span class="cx"> </script>
</span></span></pre>
</div>
</div>

</body>
</html>