<!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>[247219] trunk/Source/WebCore</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/247219">247219</a></dd>
<dt>Author</dt> <dd>cdumez@apple.com</dd>
<dt>Date</dt> <dd>2019-07-08 11:38:32 -0700 (Mon, 08 Jul 2019)</dd>
</dl>

<h3>Log Message</h3>
<pre>Fix thread safety issue in Database::scheduleTransactionCallback()
https://bugs.webkit.org/show_bug.cgi?id=199557

Reviewed by Alex Christensen.

I am working on adding threading assertions to WeakPtr and found a potentially
unsafe call to makeWeakPtr() on a Document from Database::scheduleTransactionCallback()
via Document::postTask(), on a background database thread. Document is a main thread
object and we should therefore not be interacting with it from a background thread.

For clarity, this patch also switches the webdatabase code to use Document instead
of ScriptExecution as type since it is only exposed to Window contexts, not workers.

* Modules/webdatabase/Database.cpp:
(WebCore::Database::Database):
(WebCore::Database::~Database):
(WebCore::Database::runTransaction):
(WebCore::Database::scheduleTransactionCallback):
(WebCore::Database::logErrorMessage):
(WebCore::Database::securityOrigin):
(WebCore::Database::didExceedQuota):
* Modules/webdatabase/Database.h:
(WebCore::Database::document):
* Modules/webdatabase/DatabaseContext.cpp:
(WebCore::DatabaseContext::DatabaseContext):
* Modules/webdatabase/DatabaseContext.h:
* Modules/webdatabase/DatabaseManager.cpp:
(WebCore::DatabaseManager::databaseContext):
(WebCore::logOpenDatabaseError):
(WebCore::DatabaseManager::openDatabaseBackend):
(WebCore::DatabaseManager::tryToOpenDatabaseBackend):
(WebCore::DatabaseManager::openDatabase):
(WebCore::DatabaseManager::hasOpenDatabases):
(WebCore::DatabaseManager::stopDatabases):
(WebCore::DatabaseManager::logErrorMessage):
* Modules/webdatabase/DatabaseManager.h:
* Modules/webdatabase/SQLStatement.cpp:
(WebCore::SQLStatement::SQLStatement):
* Modules/webdatabase/SQLTransaction.cpp:
(WebCore::SQLTransaction::SQLTransaction):
* inspector/InspectorInstrumentation.h:
(WebCore::InspectorInstrumentation::didOpenDatabase):
* inspector/agents/InspectorDatabaseAgent.cpp:
(WebCore::InspectorDatabaseAgent::executeSQL):</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceWebCoreChangeLog">trunk/Source/WebCore/ChangeLog</a></li>
<li><a href="#trunkSourceWebCoreModuleswebdatabaseDatabasecpp">trunk/Source/WebCore/Modules/webdatabase/Database.cpp</a></li>
<li><a href="#trunkSourceWebCoreModuleswebdatabaseDatabaseh">trunk/Source/WebCore/Modules/webdatabase/Database.h</a></li>
<li><a href="#trunkSourceWebCoreModuleswebdatabaseDatabaseContextcpp">trunk/Source/WebCore/Modules/webdatabase/DatabaseContext.cpp</a></li>
<li><a href="#trunkSourceWebCoreModuleswebdatabaseDatabaseContexth">trunk/Source/WebCore/Modules/webdatabase/DatabaseContext.h</a></li>
<li><a href="#trunkSourceWebCoreModuleswebdatabaseDatabaseManagercpp">trunk/Source/WebCore/Modules/webdatabase/DatabaseManager.cpp</a></li>
<li><a href="#trunkSourceWebCoreModuleswebdatabaseDatabaseManagerh">trunk/Source/WebCore/Modules/webdatabase/DatabaseManager.h</a></li>
<li><a href="#trunkSourceWebCoreModuleswebdatabaseSQLStatementcpp">trunk/Source/WebCore/Modules/webdatabase/SQLStatement.cpp</a></li>
<li><a href="#trunkSourceWebCoreModuleswebdatabaseSQLTransactioncpp">trunk/Source/WebCore/Modules/webdatabase/SQLTransaction.cpp</a></li>
<li><a href="#trunkSourceWebCoreinspectorInspectorInstrumentationh">trunk/Source/WebCore/inspector/InspectorInstrumentation.h</a></li>
<li><a href="#trunkSourceWebCoreinspectoragentsInspectorDatabaseAgentcpp">trunk/Source/WebCore/inspector/agents/InspectorDatabaseAgent.cpp</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceWebCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/ChangeLog (247218 => 247219)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/ChangeLog   2019-07-08 18:26:47 UTC (rev 247218)
+++ trunk/Source/WebCore/ChangeLog      2019-07-08 18:38:32 UTC (rev 247219)
</span><span class="lines">@@ -1,5 +1,52 @@
</span><span class="cx"> 2019-07-08  Chris Dumez  <cdumez@apple.com>
</span><span class="cx"> 
</span><ins>+        Fix thread safety issue in Database::scheduleTransactionCallback()
+        https://bugs.webkit.org/show_bug.cgi?id=199557
+
+        Reviewed by Alex Christensen.
+
+        I am working on adding threading assertions to WeakPtr and found a potentially
+        unsafe call to makeWeakPtr() on a Document from Database::scheduleTransactionCallback()
+        via Document::postTask(), on a background database thread. Document is a main thread
+        object and we should therefore not be interacting with it from a background thread.
+
+        For clarity, this patch also switches the webdatabase code to use Document instead
+        of ScriptExecution as type since it is only exposed to Window contexts, not workers.
+
+        * Modules/webdatabase/Database.cpp:
+        (WebCore::Database::Database):
+        (WebCore::Database::~Database):
+        (WebCore::Database::runTransaction):
+        (WebCore::Database::scheduleTransactionCallback):
+        (WebCore::Database::logErrorMessage):
+        (WebCore::Database::securityOrigin):
+        (WebCore::Database::didExceedQuota):
+        * Modules/webdatabase/Database.h:
+        (WebCore::Database::document):
+        * Modules/webdatabase/DatabaseContext.cpp:
+        (WebCore::DatabaseContext::DatabaseContext):
+        * Modules/webdatabase/DatabaseContext.h:
+        * Modules/webdatabase/DatabaseManager.cpp:
+        (WebCore::DatabaseManager::databaseContext):
+        (WebCore::logOpenDatabaseError):
+        (WebCore::DatabaseManager::openDatabaseBackend):
+        (WebCore::DatabaseManager::tryToOpenDatabaseBackend):
+        (WebCore::DatabaseManager::openDatabase):
+        (WebCore::DatabaseManager::hasOpenDatabases):
+        (WebCore::DatabaseManager::stopDatabases):
+        (WebCore::DatabaseManager::logErrorMessage):
+        * Modules/webdatabase/DatabaseManager.h:
+        * Modules/webdatabase/SQLStatement.cpp:
+        (WebCore::SQLStatement::SQLStatement):
+        * Modules/webdatabase/SQLTransaction.cpp:
+        (WebCore::SQLTransaction::SQLTransaction):
+        * inspector/InspectorInstrumentation.h:
+        (WebCore::InspectorInstrumentation::didOpenDatabase):
+        * inspector/agents/InspectorDatabaseAgent.cpp:
+        (WebCore::InspectorDatabaseAgent::executeSQL):
+
+2019-07-08  Chris Dumez  <cdumez@apple.com>
+
</ins><span class="cx">         Add threading assertion to WTF::CompletionHandler
</span><span class="cx">         https://bugs.webkit.org/show_bug.cgi?id=199516
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkSourceWebCoreModuleswebdatabaseDatabasecpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/Modules/webdatabase/Database.cpp (247218 => 247219)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/Modules/webdatabase/Database.cpp    2019-07-08 18:26:47 UTC (rev 247218)
+++ trunk/Source/WebCore/Modules/webdatabase/Database.cpp       2019-07-08 18:38:32 UTC (rev 247219)
</span><span class="lines">@@ -193,15 +193,15 @@
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> Database::Database(DatabaseContext& context, const String& name, const String& expectedVersion, const String& displayName, unsigned long long estimatedSize)
</span><del>-    : m_scriptExecutionContext(*context.scriptExecutionContext())
-    , m_contextThreadSecurityOrigin(m_scriptExecutionContext->securityOrigin()->isolatedCopy())
-    , m_databaseThreadSecurityOrigin(m_scriptExecutionContext->securityOrigin()->isolatedCopy())
</del><ins>+    : m_document(*context.document())
+    , m_contextThreadSecurityOrigin(m_document->securityOrigin().isolatedCopy())
+    , m_databaseThreadSecurityOrigin(m_document->securityOrigin().isolatedCopy())
</ins><span class="cx">     , m_databaseContext(context)
</span><span class="cx">     , m_name((name.isNull() ? emptyString() : name).isolatedCopy())
</span><span class="cx">     , m_expectedVersion(expectedVersion.isolatedCopy())
</span><span class="cx">     , m_displayName(displayName.isolatedCopy())
</span><span class="cx">     , m_estimatedSize(estimatedSize)
</span><del>-    , m_filename(DatabaseManager::singleton().fullPathForDatabase(*m_scriptExecutionContext->securityOrigin(), m_name))
</del><ins>+    , m_filename(DatabaseManager::singleton().fullPathForDatabase(m_document->securityOrigin(), m_name))
</ins><span class="cx">     , m_databaseAuthorizer(DatabaseAuthorizer::create(unqualifiedInfoTableName))
</span><span class="cx"> {
</span><span class="cx">     {
</span><span class="lines">@@ -226,14 +226,9 @@
</span><span class="cx"> 
</span><span class="cx"> Database::~Database()
</span><span class="cx"> {
</span><del>-    // The reference to the ScriptExecutionContext needs to be cleared on the JavaScript thread.  If we're on that thread already, we can just let the RefPtr's destruction do the dereffing.
-    if (!m_scriptExecutionContext->isContextThread()) {
-        auto passedContext = WTFMove(m_scriptExecutionContext);
-        auto& contextRef = passedContext.get();
-        contextRef.postTask({ScriptExecutionContext::Task::CleanupTask, [passedContext = WTFMove(passedContext), databaseContext = WTFMove(m_databaseContext)] (ScriptExecutionContext& context) {
-            ASSERT_UNUSED(context, &context == passedContext.ptr());
-        }});
-    }
</del><ins>+    // The reference to the Document needs to be cleared on the JavaScript thread. If we're on that thread already, we can just let the RefPtr's destruction do the dereffing.
+    if (!isMainThread())
+        callOnMainThread([document = WTFMove(m_document), databaseContext = WTFMove(m_databaseContext)] { });
</ins><span class="cx"> 
</span><span class="cx">     // SQLite is "multi-thread safe", but each database handle can only be used
</span><span class="cx">     // on a single thread at a time.
</span><span class="lines">@@ -692,9 +687,8 @@
</span><span class="cx">     LockHolder locker(m_transactionInProgressMutex);
</span><span class="cx">     if (!m_isTransactionQueueEnabled) {
</span><span class="cx">         if (errorCallback) {
</span><del>-            RefPtr<SQLTransactionErrorCallback> errorCallbackProtector = WTFMove(errorCallback);
-            m_scriptExecutionContext->postTask([errorCallbackProtector](ScriptExecutionContext&) {
-                errorCallbackProtector->handleEvent(SQLError::create(SQLError::UNKNOWN_ERR, "database has been closed"));
</del><ins>+            callOnMainThread([errorCallback = makeRef(*errorCallback)]() {
+                errorCallback->handleEvent(SQLError::create(SQLError::UNKNOWN_ERR, "database has been closed"));
</ins><span class="cx">             });
</span><span class="cx">         }
</span><span class="cx">         return;
</span><span class="lines">@@ -707,9 +701,8 @@
</span><span class="cx"> 
</span><span class="cx"> void Database::scheduleTransactionCallback(SQLTransaction* transaction)
</span><span class="cx"> {
</span><del>-    RefPtr<SQLTransaction> transactionProtector(transaction);
-    m_scriptExecutionContext->postTask([transactionProtector] (ScriptExecutionContext&) {
-        transactionProtector->performPendingCallback();
</del><ins>+    callOnMainThread([transaction = makeRefPtr(transaction)] {
+        transaction->performPendingCallback();
</ins><span class="cx">     });
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="lines">@@ -757,7 +750,7 @@
</span><span class="cx"> 
</span><span class="cx"> void Database::logErrorMessage(const String& message)
</span><span class="cx"> {
</span><del>-    m_scriptExecutionContext->addConsoleMessage(MessageSource::Storage, MessageLevel::Error, message);
</del><ins>+    m_document->addConsoleMessage(MessageSource::Storage, MessageLevel::Error, message);
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> Vector<String> Database::tableNames()
</span><span class="lines">@@ -779,7 +772,7 @@
</span><span class="cx"> 
</span><span class="cx"> SecurityOriginData Database::securityOrigin()
</span><span class="cx"> {
</span><del>-    if (m_scriptExecutionContext->isContextThread())
</del><ins>+    if (isMainThread())
</ins><span class="cx">         return m_contextThreadSecurityOrigin->data();
</span><span class="cx">     if (databaseThread().getThread() == &Thread::current())
</span><span class="cx">         return m_databaseThreadSecurityOrigin->data();
</span><span class="lines">@@ -798,7 +791,7 @@
</span><span class="cx"> 
</span><span class="cx"> bool Database::didExceedQuota()
</span><span class="cx"> {
</span><del>-    ASSERT(databaseContext().scriptExecutionContext()->isContextThread());
</del><ins>+    ASSERT(isMainThread());
</ins><span class="cx">     auto& tracker = DatabaseTracker::singleton();
</span><span class="cx">     auto oldQuota = tracker.quota(securityOrigin());
</span><span class="cx">     if (estimatedSize() <= oldQuota) {
</span></span></pre></div>
<a id="trunkSourceWebCoreModuleswebdatabaseDatabaseh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/Modules/webdatabase/Database.h (247218 => 247219)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/Modules/webdatabase/Database.h      2019-07-08 18:26:47 UTC (rev 247218)
+++ trunk/Source/WebCore/Modules/webdatabase/Database.h 2019-07-08 18:38:32 UTC (rev 247219)
</span><span class="lines">@@ -39,7 +39,7 @@
</span><span class="cx"> class DatabaseContext;
</span><span class="cx"> class DatabaseDetails;
</span><span class="cx"> class DatabaseThread;
</span><del>-class ScriptExecutionContext;
</del><ins>+class Document;
</ins><span class="cx"> class SecurityOrigin;
</span><span class="cx"> class SQLTransaction;
</span><span class="cx"> class SQLTransactionBackend;
</span><span class="lines">@@ -105,7 +105,7 @@
</span><span class="cx"> 
</span><span class="cx">     DatabaseContext& databaseContext() { return m_databaseContext; }
</span><span class="cx">     DatabaseThread& databaseThread();
</span><del>-    ScriptExecutionContext& scriptExecutionContext() { return m_scriptExecutionContext; }
</del><ins>+    Document& document() { return m_document; }
</ins><span class="cx">     void logErrorMessage(const String& message);
</span><span class="cx"> 
</span><span class="cx">     Vector<String> tableNames();
</span><span class="lines">@@ -147,7 +147,7 @@
</span><span class="cx">     String databaseDebugName() const;
</span><span class="cx"> #endif
</span><span class="cx"> 
</span><del>-    Ref<ScriptExecutionContext> m_scriptExecutionContext;
</del><ins>+    Ref<Document> m_document;
</ins><span class="cx">     Ref<SecurityOrigin> m_contextThreadSecurityOrigin;
</span><span class="cx">     Ref<SecurityOrigin> m_databaseThreadSecurityOrigin;
</span><span class="cx">     Ref<DatabaseContext> m_databaseContext;
</span></span></pre></div>
<a id="trunkSourceWebCoreModuleswebdatabaseDatabaseContextcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/Modules/webdatabase/DatabaseContext.cpp (247218 => 247219)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/Modules/webdatabase/DatabaseContext.cpp     2019-07-08 18:26:47 UTC (rev 247218)
+++ trunk/Source/WebCore/Modules/webdatabase/DatabaseContext.cpp        2019-07-08 18:38:32 UTC (rev 247219)
</span><span class="lines">@@ -94,14 +94,14 @@
</span><span class="cx"> // DatabaseContext will outlive both regardless of which of the 2 destructs first.
</span><span class="cx"> 
</span><span class="cx"> 
</span><del>-DatabaseContext::DatabaseContext(ScriptExecutionContext& context)
-    : ActiveDOMObject(&context)
</del><ins>+DatabaseContext::DatabaseContext(Document& document)
+    : ActiveDOMObject(document)
</ins><span class="cx"> {
</span><span class="cx">     // ActiveDOMObject expects this to be called to set internal flags.
</span><span class="cx">     suspendIfNeeded();
</span><span class="cx"> 
</span><del>-    ASSERT(!context.databaseContext());
-    context.setDatabaseContext(this);
</del><ins>+    ASSERT(!document.databaseContext());
+    document.setDatabaseContext(this);
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> DatabaseContext::~DatabaseContext()
</span></span></pre></div>
<a id="trunkSourceWebCoreModuleswebdatabaseDatabaseContexth"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/Modules/webdatabase/DatabaseContext.h (247218 => 247219)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/Modules/webdatabase/DatabaseContext.h       2019-07-08 18:26:47 UTC (rev 247218)
+++ trunk/Source/WebCore/Modules/webdatabase/DatabaseContext.h  2019-07-08 18:38:32 UTC (rev 247219)
</span><span class="lines">@@ -60,13 +60,13 @@
</span><span class="cx">     bool allowDatabaseAccess() const;
</span><span class="cx">     void databaseExceededQuota(const String& name, DatabaseDetails);
</span><span class="cx"> 
</span><del>-    using ActiveDOMObject::scriptExecutionContext;
</del><ins>+    Document* document() const { return downcast<Document>(ActiveDOMObject::scriptExecutionContext()); }
</ins><span class="cx">     const SecurityOriginData& securityOrigin() const;
</span><span class="cx"> 
</span><span class="cx">     bool isContextThread() const;
</span><span class="cx"> 
</span><span class="cx"> private:
</span><del>-    explicit DatabaseContext(ScriptExecutionContext&);
</del><ins>+    explicit DatabaseContext(Document&);
</ins><span class="cx"> 
</span><span class="cx">     void stopDatabases() { stopDatabases(nullptr); }
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkSourceWebCoreModuleswebdatabaseDatabaseManagercpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/Modules/webdatabase/DatabaseManager.cpp (247218 => 247219)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/Modules/webdatabase/DatabaseManager.cpp     2019-07-08 18:26:47 UTC (rev 247218)
+++ trunk/Source/WebCore/Modules/webdatabase/DatabaseManager.cpp        2019-07-08 18:38:32 UTC (rev 247219)
</span><span class="lines">@@ -31,11 +31,11 @@
</span><span class="cx"> #include "DatabaseContext.h"
</span><span class="cx"> #include "DatabaseTask.h"
</span><span class="cx"> #include "DatabaseTracker.h"
</span><ins>+#include "Document.h"
</ins><span class="cx"> #include "InspectorInstrumentation.h"
</span><span class="cx"> #include "Logging.h"
</span><span class="cx"> #include "PlatformStrategies.h"
</span><span class="cx"> #include "ScriptController.h"
</span><del>-#include "ScriptExecutionContext.h"
</del><span class="cx"> #include "SecurityOrigin.h"
</span><span class="cx"> #include "SecurityOriginData.h"
</span><span class="cx"> #include <wtf/NeverDestroyed.h>
</span><span class="lines">@@ -97,31 +97,31 @@
</span><span class="cx">     m_databaseIsAvailable = available;
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-Ref<DatabaseContext> DatabaseManager::databaseContext(ScriptExecutionContext& context)
</del><ins>+Ref<DatabaseContext> DatabaseManager::databaseContext(Document& document)
</ins><span class="cx"> {
</span><del>-    if (auto databaseContext = context.databaseContext())
</del><ins>+    if (auto databaseContext = document.databaseContext())
</ins><span class="cx">         return *databaseContext;
</span><del>-    return adoptRef(*new DatabaseContext(context));
</del><ins>+    return adoptRef(*new DatabaseContext(document));
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> #if LOG_DISABLED
</span><span class="cx"> 
</span><del>-static inline void logOpenDatabaseError(ScriptExecutionContext&, const String&)
</del><ins>+static inline void logOpenDatabaseError(Document&, const String&)
</ins><span class="cx"> {
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> #else
</span><span class="cx"> 
</span><del>-static void logOpenDatabaseError(ScriptExecutionContext& context, const String& name)
</del><ins>+static void logOpenDatabaseError(Document& document, const String& name)
</ins><span class="cx"> {
</span><del>-    LOG(StorageAPI, "Database %s for origin %s not allowed to be established", name.utf8().data(), context.securityOrigin()->toString().utf8().data());
</del><ins>+    LOG(StorageAPI, "Database %s for origin %s not allowed to be established", name.utf8().data(), document.securityOrigin().toString().utf8().data());
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> #endif
</span><span class="cx"> 
</span><del>-ExceptionOr<Ref<Database>> DatabaseManager::openDatabaseBackend(ScriptExecutionContext& context, const String& name, const String& expectedVersion, const String& displayName, unsigned estimatedSize, bool setVersionInNewDatabase)
</del><ins>+ExceptionOr<Ref<Database>> DatabaseManager::openDatabaseBackend(Document& document, const String& name, const String& expectedVersion, const String& displayName, unsigned estimatedSize, bool setVersionInNewDatabase)
</ins><span class="cx"> {
</span><del>-    auto backend = tryToOpenDatabaseBackend(context, name, expectedVersion, displayName, estimatedSize, setVersionInNewDatabase, FirstTryToOpenDatabase);
</del><ins>+    auto backend = tryToOpenDatabaseBackend(document, name, expectedVersion, displayName, estimatedSize, setVersionInNewDatabase, FirstTryToOpenDatabase);
</ins><span class="cx"> 
</span><span class="cx">     if (backend.hasException()) {
</span><span class="cx">         if (backend.exception().code() == QuotaExceededError) {
</span><span class="lines">@@ -129,39 +129,31 @@
</span><span class="cx">             // The client may want to increase the quota, and we'll give it
</span><span class="cx">             // one more try after if that is the case.
</span><span class="cx">             {
</span><del>-                // FIXME: What guarantees context.securityOrigin() is non-null?
-                ProposedDatabase proposedDatabase { *this, *context.securityOrigin(), name, displayName, estimatedSize };
-                this->databaseContext(context)->databaseExceededQuota(name, proposedDatabase.details());
</del><ins>+                ProposedDatabase proposedDatabase { *this, document.securityOrigin(), name, displayName, estimatedSize };
+                this->databaseContext(document)->databaseExceededQuota(name, proposedDatabase.details());
</ins><span class="cx">             }
</span><del>-            backend = tryToOpenDatabaseBackend(context, name, expectedVersion, displayName, estimatedSize, setVersionInNewDatabase, RetryOpenDatabase);
</del><ins>+            backend = tryToOpenDatabaseBackend(document, name, expectedVersion, displayName, estimatedSize, setVersionInNewDatabase, RetryOpenDatabase);
</ins><span class="cx">         }
</span><span class="cx">     }
</span><span class="cx"> 
</span><span class="cx">     if (backend.hasException()) {
</span><span class="cx">         if (backend.exception().code() == InvalidStateError)
</span><del>-            logErrorMessage(context, backend.exception().message());
</del><ins>+            logErrorMessage(document, backend.exception().message());
</ins><span class="cx">         else
</span><del>-            logOpenDatabaseError(context, name);
</del><ins>+            logOpenDatabaseError(document, name);
</ins><span class="cx">     }
</span><span class="cx"> 
</span><span class="cx">     return backend;
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-ExceptionOr<Ref<Database>> DatabaseManager::tryToOpenDatabaseBackend(ScriptExecutionContext& scriptContext, const String& name, const String& expectedVersion, const String& displayName, unsigned estimatedSize, bool setVersionInNewDatabase,
</del><ins>+ExceptionOr<Ref<Database>> DatabaseManager::tryToOpenDatabaseBackend(Document& document, const String& name, const String& expectedVersion, const String& displayName, unsigned estimatedSize, bool setVersionInNewDatabase,
</ins><span class="cx">     OpenAttempt attempt)
</span><span class="cx"> {
</span><del>-    if (is<Document>(&scriptContext)) {
-        auto* page = downcast<Document>(scriptContext).page();
-        if (!page || page->usesEphemeralSession())
-            return Exception { SecurityError };
-    }
-
-    if (scriptContext.isWorkerGlobalScope()) {
-        ASSERT_NOT_REACHED();
</del><ins>+    auto* page = document.page();
+    if (!page || page->usesEphemeralSession())
</ins><span class="cx">         return Exception { SecurityError };
</span><del>-    }
</del><span class="cx"> 
</span><del>-    auto backendContext = this->databaseContext(scriptContext);
</del><ins>+    auto backendContext = this->databaseContext(document);
</ins><span class="cx"> 
</span><span class="cx">     ExceptionOr<void> preflightResult;
</span><span class="cx">     switch (attempt) {
</span><span class="lines">@@ -198,18 +190,18 @@
</span><span class="cx">     m_proposedDatabases.remove(&database);
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-ExceptionOr<Ref<Database>> DatabaseManager::openDatabase(ScriptExecutionContext& context, const String& name, const String& expectedVersion, const String& displayName, unsigned estimatedSize, RefPtr<DatabaseCallback>&& creationCallback)
</del><ins>+ExceptionOr<Ref<Database>> DatabaseManager::openDatabase(Document& document, const String& name, const String& expectedVersion, const String& displayName, unsigned estimatedSize, RefPtr<DatabaseCallback>&& creationCallback)
</ins><span class="cx"> {
</span><span class="cx">     ScriptController::initializeThreading();
</span><span class="cx"> 
</span><span class="cx">     bool setVersionInNewDatabase = !creationCallback;
</span><del>-    auto openResult = openDatabaseBackend(context, name, expectedVersion, displayName, estimatedSize, setVersionInNewDatabase);
</del><ins>+    auto openResult = openDatabaseBackend(document, name, expectedVersion, displayName, estimatedSize, setVersionInNewDatabase);
</ins><span class="cx">     if (openResult.hasException())
</span><span class="cx">         return openResult.releaseException();
</span><span class="cx"> 
</span><span class="cx">     RefPtr<Database> database = openResult.releaseReturnValue();
</span><span class="cx"> 
</span><del>-    auto databaseContext = this->databaseContext(context);
</del><ins>+    auto databaseContext = this->databaseContext(document);
</ins><span class="cx">     databaseContext->setHasOpenDatabases();
</span><span class="cx">     InspectorInstrumentation::didOpenDatabase(*database);
</span><span class="cx"> 
</span><span class="lines">@@ -216,7 +208,7 @@
</span><span class="cx">     if (database->isNew() && creationCallback.get()) {
</span><span class="cx">         LOG(StorageAPI, "Scheduling DatabaseCreationCallbackTask for database %p\n", database.get());
</span><span class="cx">         database->setHasPendingCreationEvent(true);
</span><del>-        database->m_scriptExecutionContext->postTask([creationCallback, database] (ScriptExecutionContext&) {
</del><ins>+        database->m_document->postTask([creationCallback, database] (ScriptExecutionContext&) {
</ins><span class="cx">             creationCallback->handleEvent(*database);
</span><span class="cx">             database->setHasPendingCreationEvent(false);
</span><span class="cx">         });
</span><span class="lines">@@ -225,15 +217,15 @@
</span><span class="cx">     return database.releaseNonNull();
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-bool DatabaseManager::hasOpenDatabases(ScriptExecutionContext& context)
</del><ins>+bool DatabaseManager::hasOpenDatabases(Document& document)
</ins><span class="cx"> {
</span><del>-    auto databaseContext = context.databaseContext();
</del><ins>+    auto databaseContext = document.databaseContext();
</ins><span class="cx">     return databaseContext && databaseContext->hasOpenDatabases();
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-void DatabaseManager::stopDatabases(ScriptExecutionContext& context, DatabaseTaskSynchronizer* synchronizer)
</del><ins>+void DatabaseManager::stopDatabases(Document& document, DatabaseTaskSynchronizer* synchronizer)
</ins><span class="cx"> {
</span><del>-    auto databaseContext = context.databaseContext();
</del><ins>+    auto databaseContext = document.databaseContext();
</ins><span class="cx">     if (!databaseContext || !databaseContext->stopDatabases(synchronizer)) {
</span><span class="cx">         if (synchronizer)
</span><span class="cx">             synchronizer->taskCompleted();
</span><span class="lines">@@ -267,9 +259,9 @@
</span><span class="cx">     return DatabaseTracker::singleton().detailsForNameAndOrigin(name, origin.data());
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-void DatabaseManager::logErrorMessage(ScriptExecutionContext& context, const String& message)
</del><ins>+void DatabaseManager::logErrorMessage(Document& document, const String& message)
</ins><span class="cx"> {
</span><del>-    context.addConsoleMessage(MessageSource::Storage, MessageLevel::Error, message);
</del><ins>+    document.addConsoleMessage(MessageSource::Storage, MessageLevel::Error, message);
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> #if !PLATFORM(COCOA)
</span></span></pre></div>
<a id="trunkSourceWebCoreModuleswebdatabaseDatabaseManagerh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/Modules/webdatabase/DatabaseManager.h (247218 => 247219)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/Modules/webdatabase/DatabaseManager.h       2019-07-08 18:26:47 UTC (rev 247218)
+++ trunk/Source/WebCore/Modules/webdatabase/DatabaseManager.h  2019-07-08 18:38:32 UTC (rev 247219)
</span><span class="lines">@@ -38,9 +38,9 @@
</span><span class="cx"> class DatabaseContext;
</span><span class="cx"> class DatabaseManagerClient;
</span><span class="cx"> class DatabaseTaskSynchronizer;
</span><ins>+class Document;
</ins><span class="cx"> class Exception;
</span><span class="cx"> class SecurityOrigin;
</span><del>-class ScriptExecutionContext;
</del><span class="cx"> struct SecurityOriginData;
</span><span class="cx"> 
</span><span class="cx"> class DatabaseManager {
</span><span class="lines">@@ -55,14 +55,14 @@
</span><span class="cx">     bool isAvailable();
</span><span class="cx">     WEBCORE_EXPORT void setIsAvailable(bool);
</span><span class="cx"> 
</span><del>-    // This gets a DatabaseContext for the specified ScriptExecutionContext.
</del><ins>+    // This gets a DatabaseContext for the specified Document.
</ins><span class="cx">     // If one doesn't already exist, it will create a new one.
</span><del>-    Ref<DatabaseContext> databaseContext(ScriptExecutionContext&);
</del><ins>+    Ref<DatabaseContext> databaseContext(Document&);
</ins><span class="cx"> 
</span><del>-    ExceptionOr<Ref<Database>> openDatabase(ScriptExecutionContext&, const String& name, const String& expectedVersion, const String& displayName, unsigned estimatedSize, RefPtr<DatabaseCallback>&&);
</del><ins>+    ExceptionOr<Ref<Database>> openDatabase(Document&, const String& name, const String& expectedVersion, const String& displayName, unsigned estimatedSize, RefPtr<DatabaseCallback>&&);
</ins><span class="cx"> 
</span><del>-    WEBCORE_EXPORT bool hasOpenDatabases(ScriptExecutionContext&);
-    void stopDatabases(ScriptExecutionContext&, DatabaseTaskSynchronizer*);
</del><ins>+    WEBCORE_EXPORT bool hasOpenDatabases(Document&);
+    void stopDatabases(Document&, DatabaseTaskSynchronizer*);
</ins><span class="cx"> 
</span><span class="cx">     WEBCORE_EXPORT String fullPathForDatabase(SecurityOrigin&, const String& name, bool createIfDoesNotExist = true);
</span><span class="cx"> 
</span><span class="lines">@@ -75,14 +75,14 @@
</span><span class="cx">     void platformInitialize(const String& databasePath);
</span><span class="cx"> 
</span><span class="cx">     enum OpenAttempt { FirstTryToOpenDatabase, RetryOpenDatabase };
</span><del>-    ExceptionOr<Ref<Database>> openDatabaseBackend(ScriptExecutionContext&, const String& name, const String& expectedVersion, const String& displayName, unsigned estimatedSize, bool setVersionInNewDatabase);
-    ExceptionOr<Ref<Database>> tryToOpenDatabaseBackend(ScriptExecutionContext&, const String& name, const String& expectedVersion, const String& displayName, unsigned estimatedSize, bool setVersionInNewDatabase, OpenAttempt);
</del><ins>+    ExceptionOr<Ref<Database>> openDatabaseBackend(Document&, const String& name, const String& expectedVersion, const String& displayName, unsigned estimatedSize, bool setVersionInNewDatabase);
+    ExceptionOr<Ref<Database>> tryToOpenDatabaseBackend(Document&, const String& name, const String& expectedVersion, const String& displayName, unsigned estimatedSize, bool setVersionInNewDatabase, OpenAttempt);
</ins><span class="cx"> 
</span><span class="cx">     class ProposedDatabase;
</span><span class="cx">     void addProposedDatabase(ProposedDatabase&);
</span><span class="cx">     void removeProposedDatabase(ProposedDatabase&);
</span><span class="cx"> 
</span><del>-    static void logErrorMessage(ScriptExecutionContext&, const String& message);
</del><ins>+    static void logErrorMessage(Document&, const String& message);
</ins><span class="cx"> 
</span><span class="cx">     DatabaseManagerClient* m_client { nullptr };
</span><span class="cx">     bool m_databaseIsAvailable { true };
</span></span></pre></div>
<a id="trunkSourceWebCoreModuleswebdatabaseSQLStatementcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/Modules/webdatabase/SQLStatement.cpp (247218 => 247219)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/Modules/webdatabase/SQLStatement.cpp        2019-07-08 18:26:47 UTC (rev 247218)
+++ trunk/Source/WebCore/Modules/webdatabase/SQLStatement.cpp   2019-07-08 18:38:32 UTC (rev 247219)
</span><span class="lines">@@ -29,6 +29,7 @@
</span><span class="cx"> #include "SQLStatement.h"
</span><span class="cx"> 
</span><span class="cx"> #include "Database.h"
</span><ins>+#include "Document.h"
</ins><span class="cx"> #include "Logging.h"
</span><span class="cx"> #include "SQLError.h"
</span><span class="cx"> #include "SQLResultSet.h"
</span><span class="lines">@@ -77,8 +78,8 @@
</span><span class="cx"> SQLStatement::SQLStatement(Database& database, const String& statement, Vector<SQLValue>&& arguments, RefPtr<SQLStatementCallback>&& callback, RefPtr<SQLStatementErrorCallback>&& errorCallback, int permissions)
</span><span class="cx">     : m_statement(statement.isolatedCopy())
</span><span class="cx">     , m_arguments(WTFMove(arguments))
</span><del>-    , m_statementCallbackWrapper(WTFMove(callback), &database.scriptExecutionContext())
-    , m_statementErrorCallbackWrapper(WTFMove(errorCallback), &database.scriptExecutionContext())
</del><ins>+    , m_statementCallbackWrapper(WTFMove(callback), &database.document())
+    , m_statementErrorCallbackWrapper(WTFMove(errorCallback), &database.document())
</ins><span class="cx">     , m_permissions(permissions)
</span><span class="cx"> {
</span><span class="cx"> }
</span></span></pre></div>
<a id="trunkSourceWebCoreModuleswebdatabaseSQLTransactioncpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/Modules/webdatabase/SQLTransaction.cpp (247218 => 247219)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/Modules/webdatabase/SQLTransaction.cpp      2019-07-08 18:26:47 UTC (rev 247218)
+++ trunk/Source/WebCore/Modules/webdatabase/SQLTransaction.cpp 2019-07-08 18:38:32 UTC (rev 247219)
</span><span class="lines">@@ -34,6 +34,7 @@
</span><span class="cx"> #include "DatabaseContext.h"
</span><span class="cx"> #include "DatabaseThread.h"
</span><span class="cx"> #include "DatabaseTracker.h"
</span><ins>+#include "Document.h"
</ins><span class="cx"> #include "Logging.h"
</span><span class="cx"> #include "OriginLock.h"
</span><span class="cx"> #include "SQLError.h"
</span><span class="lines">@@ -59,9 +60,9 @@
</span><span class="cx"> 
</span><span class="cx"> SQLTransaction::SQLTransaction(Ref<Database>&& database, RefPtr<SQLTransactionCallback>&& callback, RefPtr<VoidCallback>&& successCallback, RefPtr<SQLTransactionErrorCallback>&& errorCallback, RefPtr<SQLTransactionWrapper>&& wrapper, bool readOnly)
</span><span class="cx">     : m_database(WTFMove(database))
</span><del>-    , m_callbackWrapper(WTFMove(callback), &m_database->scriptExecutionContext())
-    , m_successCallbackWrapper(WTFMove(successCallback), &m_database->scriptExecutionContext())
-    , m_errorCallbackWrapper(WTFMove(errorCallback), &m_database->scriptExecutionContext())
</del><ins>+    , m_callbackWrapper(WTFMove(callback), &m_database->document())
+    , m_successCallbackWrapper(WTFMove(successCallback), &m_database->document())
+    , m_errorCallbackWrapper(WTFMove(errorCallback), &m_database->document())
</ins><span class="cx">     , m_wrapper(WTFMove(wrapper))
</span><span class="cx">     , m_nextStep(&SQLTransaction::acquireLock)
</span><span class="cx">     , m_readOnly(readOnly)
</span></span></pre></div>
<a id="trunkSourceWebCoreinspectorInspectorInstrumentationh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/inspector/InspectorInstrumentation.h (247218 => 247219)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/inspector/InspectorInstrumentation.h        2019-07-08 18:26:47 UTC (rev 247218)
+++ trunk/Source/WebCore/inspector/InspectorInstrumentation.h   2019-07-08 18:38:32 UTC (rev 247219)
</span><span class="lines">@@ -1201,7 +1201,7 @@
</span><span class="cx"> inline void InspectorInstrumentation::didOpenDatabase(Database& database)
</span><span class="cx"> {
</span><span class="cx">     FAST_RETURN_IF_NO_FRONTENDS(void());
</span><del>-    if (auto* instrumentingAgents = instrumentingAgentsForContext(database.scriptExecutionContext()))
</del><ins>+    if (auto* instrumentingAgents = instrumentingAgentsForContext(database.document()))
</ins><span class="cx">         didOpenDatabaseImpl(*instrumentingAgents, database);
</span><span class="cx"> }
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkSourceWebCoreinspectoragentsInspectorDatabaseAgentcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/inspector/agents/InspectorDatabaseAgent.cpp (247218 => 247219)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/inspector/agents/InspectorDatabaseAgent.cpp 2019-07-08 18:26:47 UTC (rev 247218)
+++ trunk/Source/WebCore/inspector/agents/InspectorDatabaseAgent.cpp    2019-07-08 18:38:32 UTC (rev 247219)
</span><span class="lines">@@ -278,9 +278,9 @@
</span><span class="cx">         return;
</span><span class="cx">     }
</span><span class="cx"> 
</span><del>-    database->transaction(TransactionCallback::create(&database->scriptExecutionContext(), query, requestCallback.copyRef()),
-        TransactionErrorCallback::create(&database->scriptExecutionContext(), requestCallback.copyRef()),
-        TransactionSuccessCallback::create(&database->scriptExecutionContext()));
</del><ins>+    database->transaction(TransactionCallback::create(&database->document(), query, requestCallback.copyRef()),
+        TransactionErrorCallback::create(&database->document(), requestCallback.copyRef()),
+        TransactionSuccessCallback::create(&database->document()));
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> String InspectorDatabaseAgent::databaseId(Database& database)
</span></span></pre>
</div>
</div>

</body>
</html>