Web Inspector: Database: lazily create the agent
authordrousso@apple.com <drousso@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 20 Mar 2019 18:53:47 +0000 (18:53 +0000)
committerdrousso@apple.com <drousso@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 20 Mar 2019 18:53:47 +0000 (18:53 +0000)
https://bugs.webkit.org/show_bug.cgi?id=195587
<rdar://problem/48791735>

Reviewed by Timothy Hatcher.

Source/WebCore:

No change in functionality.

* inspector/agents/InspectorDatabaseAgent.h:
* inspector/agents/InspectorDatabaseAgent.cpp:
(WebCore::InspectorDatabaseAgent::didCommitLoad): Added.
(WebCore::InspectorDatabaseAgent::didOpenDatabase):
(WebCore::InspectorDatabaseAgent::InspectorDatabaseAgent):
(WebCore::InspectorDatabaseAgent::enable):
(WebCore::InspectorDatabaseAgent::disable):
(WebCore::InspectorDatabaseAgent::getDatabaseTableNames):
(WebCore::InspectorDatabaseAgent::executeSQL):
(WebCore::InspectorDatabaseAgent::databaseId):
(WebCore::InspectorDatabaseAgent::findByFileName):
(WebCore::InspectorDatabaseAgent::databaseForId):
(WebCore::InspectorDatabaseAgent::clearResources): Deleted.
(WebCore::InspectorDatabaseAgent::~InspectorDatabaseAgent): Deleted.
* inspector/InspectorDatabaseResource.h:
(WebCore::InspectorDatabaseResource::database const):
(WebCore::InspectorDatabaseResource::setDatabase):
(WebCore::InspectorDatabaseResource::database): Deleted.
* inspector/InspectorDatabaseResource.cpp:
(WebCore::InspectorDatabaseResource::create):
(WebCore::InspectorDatabaseResource::InspectorDatabaseResource):
(WebCore::InspectorDatabaseResource::bind):
* inspector/InspectorInstrumentation.h:
(WebCore::InspectorInstrumentation::didOpenDatabase):
* inspector/InspectorInstrumentation.cpp:
(WebCore::InspectorInstrumentation::didCommitLoadImpl):
(WebCore::InspectorInstrumentation::didOpenDatabaseImpl):
Pass the `Database` as a reference instead of a pointer. Retrieve any information directly
from the `Database`, rather than using the arguments that were used to create it.

* Modules/webdatabase/Database.h:
(WebCore::Database::expectedVersion const): Deleted.
* Modules/webdatabase/Database.cpp:
(WebCore::Database::expectedVersion const): Added.
* Modules/webdatabase/DatabaseManager.cpp:
(WebCore::DatabaseManager::openDatabase):
* Modules/webdatabase/DatabaseTracker.h:
* Modules/webdatabase/DatabaseTracker.cpp:
(WebCore::DatabaseTracker::closeAllDatabases):
(WebCore::DatabaseTracker::openDatabases): Added.
Expose various values used by `InspectorDatabaseAgent` and `InspectorDatabaseResource`.

* inspector/InspectorController.cpp:
(WebCore::InspectorController::InspectorController):
(WebCore::InspectorController::createLazyAgents):
* inspector/WorkerInspectorController.cpp:
(WebCore::WorkerInspectorController::WorkerInspectorController):

* inspector/CommandLineAPIHost.h:
(WebCore::CommandLineAPIHost::init):
* inspector/CommandLineAPIHost.cpp:
(WebCore::CommandLineAPIHost::disconnect):
(WebCore::CommandLineAPIHost::inspect):
(WebCore::CommandLineAPIHost::clearConsoleMessages):
(WebCore::CommandLineAPIHost::databaseId):
(WebCore::CommandLineAPIHost::storageId):
Rather than pass each agent individually, pass the entire `InstrumentingAgents` so that the
current agent can be used instead of whatever was initially created.

LayoutTests:

* inspector/protocol/backend-dispatcher-argument-errors.html:
* inspector/runtime/CommandLineAPI-inspect.html:

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@243219 268f45cc-cd09-0410-ab3c-d52691b4dbfc

20 files changed:
LayoutTests/ChangeLog
LayoutTests/inspector/protocol/backend-dispatcher-argument-errors-expected.txt
LayoutTests/inspector/protocol/backend-dispatcher-argument-errors.html
LayoutTests/inspector/runtime/CommandLineAPI-inspect.html
Source/WebCore/ChangeLog
Source/WebCore/Modules/webdatabase/Database.cpp
Source/WebCore/Modules/webdatabase/Database.h
Source/WebCore/Modules/webdatabase/DatabaseManager.cpp
Source/WebCore/Modules/webdatabase/DatabaseTracker.cpp
Source/WebCore/Modules/webdatabase/DatabaseTracker.h
Source/WebCore/inspector/CommandLineAPIHost.cpp
Source/WebCore/inspector/CommandLineAPIHost.h
Source/WebCore/inspector/InspectorController.cpp
Source/WebCore/inspector/InspectorDatabaseResource.cpp
Source/WebCore/inspector/InspectorDatabaseResource.h
Source/WebCore/inspector/InspectorInstrumentation.cpp
Source/WebCore/inspector/InspectorInstrumentation.h
Source/WebCore/inspector/WorkerInspectorController.cpp
Source/WebCore/inspector/agents/InspectorDatabaseAgent.cpp
Source/WebCore/inspector/agents/InspectorDatabaseAgent.h

index bbc01f6..167694f 100644 (file)
@@ -1,3 +1,14 @@
+2019-03-20  Devin Rousso  <drousso@apple.com>
+
+        Web Inspector: Database: lazily create the agent
+        https://bugs.webkit.org/show_bug.cgi?id=195587
+        <rdar://problem/48791735>
+
+        Reviewed by Timothy Hatcher.
+
+        * inspector/protocol/backend-dispatcher-argument-errors.html:
+        * inspector/runtime/CommandLineAPI-inspect.html:
+
 2019-03-20  Simon Fraser  <simon.fraser@apple.com>
 
         Some elements lag behind async overflow scrolling on palace-games.com
index a48c277..95417dd 100644 (file)
@@ -35,26 +35,18 @@ Actual error message: Some arguments of method 'Runtime.evaluate' can't be proce
 Actual error data: [{"code":-32602,"message":"Parameter 'includeCommandLineAPI' has wrong type. It must be 'Boolean'."},{"code":-32602,"message":"Some arguments of method 'Runtime.evaluate' can't be processed"}]
 
 -- Running test case: TestErrorCodeForSyncServerError
-Sending message: {"id":123,"method":"Database.getDatabaseTableNames","params":{"databaseId":"thisisNotADatabase"}}
+Sending message: {"id":123,"method":"Runtime.getProperties","params":{"objectId":"thisisNotAnId"}}
 PASS: the backend should send a protocol error when receiving an invalid message.
 PASS: the reported error should be "ServerError" (-32000)
 Actual error code: -32000
-Actual error message: Database agent is not enabled
-Actual error data: [{"code":-32000,"message":"Database agent is not enabled"}]
+Actual error message: Could not find InjectedScript for objectId
+Actual error data: [{"code":-32000,"message":"Could not find InjectedScript for objectId"}]
 
 -- Running test case: TestErrorCodeForAsyncServerError
-Sending message: {"id":123,"method":"Database.executeSQL","params":{"databaseId":"thisisNotADatabase","query":"asdf"}}
-PASS: the backend should send a protocol error when receiving an invalid message.
-PASS: the reported error should be "ServerError" (-32000)
-Actual error code: -32000
-Actual error message: Database agent is not enabled
-Actual error data: [{"code":-32000,"message":"Database agent is not enabled"}]
-
--- Running test case: CommandWithBadArgumentValue
-Sending message: {"id":123,"method":"Runtime.getProperties","params":{"objectId":"thisisNotAnId"}}
+Sending message: {"id":123,"method":"Runtime.awaitPromise","params":{"promiseObjectId":"thisisNotAnId"}}
 PASS: the backend should send a protocol error when receiving an invalid message.
 PASS: the reported error should be "ServerError" (-32000)
 Actual error code: -32000
-Actual error message: Could not find InjectedScript for objectId
-Actual error data: [{"code":-32000,"message":"Could not find InjectedScript for objectId"}]
+Actual error message: Could not find InjectedScript for promiseObjectId
+Actual error data: [{"code":-32000,"message":"Could not find InjectedScript for promiseObjectId"}]
 
index ad426eb..b36ea16 100644 (file)
@@ -76,21 +76,14 @@ function test()
     addErrorResponseTestCase({
         name: "TestErrorCodeForSyncServerError",
         description: "The backend should return a server error with the correct error code.",
-        message: {id: 123, method: "Database.getDatabaseTableNames", params: {databaseId: "thisisNotADatabase"}},
+        message: {id: 123, method: "Runtime.getProperties", params: {objectId: "thisisNotAnId"}},
         expectedError: "ServerError"
     });
 
     addErrorResponseTestCase({
         name: "TestErrorCodeForAsyncServerError",
         description: "The backend should return a server error with the correct error code.",
-        message: {id: 123, method: "Database.executeSQL", params: {databaseId: "thisisNotADatabase", query: "asdf"}},
-        expectedError: "ServerError"
-    });
-
-    addErrorResponseTestCase({
-        name: "CommandWithBadArgumentValue",
-        description: "The backend should return an error if something went wrong when invoking the agent's method.",
-        message: {id: 123, method: "Runtime.getProperties", params: {objectId: "thisisNotAnId"}},
+        message: {id: 123, method: "Runtime.awaitPromise", params: {promiseObjectId: "thisisNotAnId"}},
         expectedError: "ServerError"
     });
 
index e637dbb..6c1a1b1 100644 (file)
@@ -25,6 +25,7 @@ function test()
     let suite = ProtocolTest.createAsyncSuite("CommandLineAPI.prototype.inspect");
 
     InspectorProtocol.awaitCommand({method: "Inspector.enable", params: {}})
+    InspectorProtocol.awaitCommand({method: "Database.enable", params: {}})
 
     function addTestCase({name, description, expression}) {
         suite.addTestCase({
index 6cd6d17..c368467 100644 (file)
@@ -1,3 +1,72 @@
+2019-03-20  Devin Rousso  <drousso@apple.com>
+
+        Web Inspector: Database: lazily create the agent
+        https://bugs.webkit.org/show_bug.cgi?id=195587
+        <rdar://problem/48791735>
+
+        Reviewed by Timothy Hatcher.
+
+        No change in functionality.
+
+        * inspector/agents/InspectorDatabaseAgent.h:
+        * inspector/agents/InspectorDatabaseAgent.cpp:
+        (WebCore::InspectorDatabaseAgent::didCommitLoad): Added.
+        (WebCore::InspectorDatabaseAgent::didOpenDatabase):
+        (WebCore::InspectorDatabaseAgent::InspectorDatabaseAgent):
+        (WebCore::InspectorDatabaseAgent::enable):
+        (WebCore::InspectorDatabaseAgent::disable):
+        (WebCore::InspectorDatabaseAgent::getDatabaseTableNames):
+        (WebCore::InspectorDatabaseAgent::executeSQL):
+        (WebCore::InspectorDatabaseAgent::databaseId):
+        (WebCore::InspectorDatabaseAgent::findByFileName):
+        (WebCore::InspectorDatabaseAgent::databaseForId):
+        (WebCore::InspectorDatabaseAgent::clearResources): Deleted.
+        (WebCore::InspectorDatabaseAgent::~InspectorDatabaseAgent): Deleted.
+        * inspector/InspectorDatabaseResource.h:
+        (WebCore::InspectorDatabaseResource::database const):
+        (WebCore::InspectorDatabaseResource::setDatabase):
+        (WebCore::InspectorDatabaseResource::database): Deleted.
+        * inspector/InspectorDatabaseResource.cpp:
+        (WebCore::InspectorDatabaseResource::create):
+        (WebCore::InspectorDatabaseResource::InspectorDatabaseResource):
+        (WebCore::InspectorDatabaseResource::bind):
+        * inspector/InspectorInstrumentation.h:
+        (WebCore::InspectorInstrumentation::didOpenDatabase):
+        * inspector/InspectorInstrumentation.cpp:
+        (WebCore::InspectorInstrumentation::didCommitLoadImpl):
+        (WebCore::InspectorInstrumentation::didOpenDatabaseImpl):
+        Pass the `Database` as a reference instead of a pointer. Retrieve any information directly
+        from the `Database`, rather than using the arguments that were used to create it.
+
+        * Modules/webdatabase/Database.h:
+        (WebCore::Database::expectedVersion const): Deleted.
+        * Modules/webdatabase/Database.cpp:
+        (WebCore::Database::expectedVersion const): Added.
+        * Modules/webdatabase/DatabaseManager.cpp:
+        (WebCore::DatabaseManager::openDatabase):
+        * Modules/webdatabase/DatabaseTracker.h:
+        * Modules/webdatabase/DatabaseTracker.cpp:
+        (WebCore::DatabaseTracker::closeAllDatabases):
+        (WebCore::DatabaseTracker::openDatabases): Added.
+        Expose various values used by `InspectorDatabaseAgent` and `InspectorDatabaseResource`.
+
+        * inspector/InspectorController.cpp:
+        (WebCore::InspectorController::InspectorController):
+        (WebCore::InspectorController::createLazyAgents):
+        * inspector/WorkerInspectorController.cpp:
+        (WebCore::WorkerInspectorController::WorkerInspectorController):
+
+        * inspector/CommandLineAPIHost.h:
+        (WebCore::CommandLineAPIHost::init):
+        * inspector/CommandLineAPIHost.cpp:
+        (WebCore::CommandLineAPIHost::disconnect):
+        (WebCore::CommandLineAPIHost::inspect):
+        (WebCore::CommandLineAPIHost::clearConsoleMessages):
+        (WebCore::CommandLineAPIHost::databaseId):
+        (WebCore::CommandLineAPIHost::storageId):
+        Rather than pass each agent individually, pass the entire `InstrumentingAgents` so that the
+        current agent can be used instead of whatever was initially created.
+
 2019-03-20  Oriol Brufau  <obrufau@igalia.com>
 
         [css-grid] Always consider baseline shim for the minimum contribution
index f3ccf3d..52b0a08 100644 (file)
@@ -618,6 +618,12 @@ String Database::displayName() const
     return m_displayName.isolatedCopy();
 }
 
+String Database::expectedVersion() const
+{
+    // Return a deep copy for ref counting thread safety
+    return m_expectedVersion.isolatedCopy();
+}
+
 unsigned long long Database::estimatedSize() const
 {
     return m_estimatedSize;
index 1f75740..2bdde1d 100644 (file)
@@ -88,6 +88,7 @@ public:
     // Internal engine support
     String stringIdentifier() const;
     String displayName() const;
+    String expectedVersion() const;
     unsigned long long estimatedSize() const;
     String fileName() const;
     DatabaseDetails details() const;
@@ -133,7 +134,6 @@ private:
     bool getVersionFromDatabase(String& version, bool shouldCacheVersion = true);
     bool setVersionInDatabase(const String& version, bool shouldCacheVersion = true);
     void setExpectedVersion(const String&);
-    const String& expectedVersion() const { return m_expectedVersion; }
     String getCachedVersion() const;
     void setCachedVersion(const String&);
     bool getActualVersionForTransaction(String& version);
index 9015d13..339409b 100644 (file)
@@ -211,7 +211,7 @@ ExceptionOr<Ref<Database>> DatabaseManager::openDatabase(ScriptExecutionContext&
 
     auto databaseContext = this->databaseContext(context);
     databaseContext->setHasOpenDatabases();
-    InspectorInstrumentation::didOpenDatabase(&context, database.copyRef(), context.securityOrigin()->host(), name, expectedVersion);
+    InspectorInstrumentation::didOpenDatabase(*database);
 
     if (database->isNew() && creationCallback.get()) {
         LOG(StorageAPI, "Scheduling DatabaseCreationCallbackTask for database %p\n", database.get());
index dcfb77e..4bf3ad6 100644 (file)
@@ -289,19 +289,7 @@ unsigned long long DatabaseTracker::maximumSize(Database& database)
 
 void DatabaseTracker::closeAllDatabases(CurrentQueryBehavior currentQueryBehavior)
 {
-    Vector<Ref<Database>> openDatabases;
-    {
-        LockHolder openDatabaseMapLock(m_openDatabaseMapGuard);
-        if (!m_openDatabaseMap)
-            return;
-        for (auto& nameMap : m_openDatabaseMap->values()) {
-            for (auto& set : nameMap->values()) {
-                for (auto& database : *set)
-                    openDatabases.append(*database);
-            }
-        }
-    }
-    for (auto& database : openDatabases) {
+    for (auto& database : openDatabases()) {
         if (currentQueryBehavior == CurrentQueryBehavior::Interrupt)
             database->interrupt();
         database->close();
@@ -533,6 +521,24 @@ void DatabaseTracker::doneCreatingDatabase(Database& database)
     doneCreatingDatabase(database.securityOrigin(), database.stringIdentifier());
 }
 
+Vector<Ref<Database>> DatabaseTracker::openDatabases()
+{
+    Vector<Ref<Database>> openDatabases;
+    {
+        LockHolder openDatabaseMapLock(m_openDatabaseMapGuard);
+
+        if (m_openDatabaseMap) {
+            for (auto& nameMap : m_openDatabaseMap->values()) {
+                for (auto& set : nameMap->values()) {
+                    for (auto& database : *set)
+                        openDatabases.append(*database);
+                }
+            }
+        }
+    }
+    return openDatabases;
+}
+
 void DatabaseTracker::addOpenDatabase(Database& database)
 {
     LockHolder openDatabaseMapLock(m_openDatabaseMapGuard);
index c11a865..1a3b12d 100644 (file)
@@ -72,6 +72,7 @@ public:
     void setDatabaseDetails(const SecurityOriginData&, const String& name, const String& displayName, unsigned long long estimatedSize);
     WEBCORE_EXPORT String fullPathForDatabase(const SecurityOriginData&, const String& name, bool createIfDoesNotExist);
 
+    Vector<Ref<Database>> openDatabases();
     void addOpenDatabase(Database&);
     void removeOpenDatabase(Database&);
 
index 69e434a..031080e 100644 (file)
@@ -41,8 +41,8 @@
 #include "JSEventListener.h"
 #include "Pasteboard.h"
 #include "Storage.h"
+#include "WebConsoleAgent.h"
 #include <JavaScriptCore/InspectorAgent.h>
-#include <JavaScriptCore/InspectorConsoleAgent.h>
 #include <JavaScriptCore/JSCInlines.h>
 #include <JavaScriptCore/JSLock.h>
 #include <JavaScriptCore/ScriptValue.h>
@@ -69,14 +69,17 @@ CommandLineAPIHost::~CommandLineAPIHost() = default;
 
 void CommandLineAPIHost::disconnect()
 {
-    m_inspectorAgent = nullptr;
-    m_consoleAgent = nullptr;
-    m_databaseAgent = nullptr;
+
+    m_instrumentingAgents = nullptr;
 }
 
 void CommandLineAPIHost::inspect(JSC::ExecState& state, JSC::JSValue valueToInspect, JSC::JSValue hintsValue)
 {
-    if (!m_inspectorAgent)
+    if (!m_instrumentingAgents)
+        return;
+
+    auto* inspectorAgent = m_instrumentingAgents->inspectorAgent();
+    if (!inspectorAgent)
         return;
 
     RefPtr<JSON::Object> hintsObject;
@@ -84,7 +87,7 @@ void CommandLineAPIHost::inspect(JSC::ExecState& state, JSC::JSValue valueToInsp
         return;
 
     auto remoteObject = BindingTraits<Inspector::Protocol::Runtime::RemoteObject>::runtimeCast(Inspector::toInspectorValue(state, valueToInspect));
-    m_inspectorAgent->inspect(WTFMove(remoteObject), WTFMove(hintsObject));
+    inspectorAgent->inspect(WTFMove(remoteObject), WTFMove(hintsObject));
 }
 
 CommandLineAPIHost::EventListenersRecord CommandLineAPIHost::getEventListeners(ExecState& state, EventTarget& target)
@@ -126,10 +129,15 @@ CommandLineAPIHost::EventListenersRecord CommandLineAPIHost::getEventListeners(E
 
 void CommandLineAPIHost::clearConsoleMessages()
 {
-    if (m_consoleAgent) {
-        ErrorString unused;
-        m_consoleAgent->clearMessages(unused);
-    }
+    if (!m_instrumentingAgents)
+        return;
+
+    auto* consoleAgent = m_instrumentingAgents->webConsoleAgent();
+    if (!consoleAgent)
+        return;
+
+    ErrorString unused;
+    consoleAgent->clearMessages(unused);
 }
 
 void CommandLineAPIHost::copyText(const String& text)
@@ -159,8 +167,10 @@ JSC::JSValue CommandLineAPIHost::inspectedObject(JSC::ExecState& state)
 
 String CommandLineAPIHost::databaseId(Database& database)
 {
-    if (m_databaseAgent)
-        return m_databaseAgent->databaseId(database);
+    if (m_instrumentingAgents) {
+        if (auto* databaseAgent = m_instrumentingAgents->inspectorDatabaseAgent())
+            return databaseAgent->databaseId(database);
+    }
     return { };
 }
 
index 2dcc7ac..15f2385 100644 (file)
@@ -29,6 +29,7 @@
 
 #pragma once
 
+#include "InstrumentingAgents.h"
 #include <JavaScriptCore/PerGlobalObjectWrapperWorld.h>
 #include <wtf/RefCounted.h>
 #include <wtf/Vector.h>
@@ -38,16 +39,10 @@ namespace JSC {
 class JSValue;
 }
 
-namespace Inspector {
-class InspectorAgent;
-class InspectorConsoleAgent;
-}
-
 namespace WebCore {
 
 class Database;
 class EventTarget;
-class InspectorDatabaseAgent;
 class JSDOMGlobalObject;
 class Storage;
 
@@ -58,14 +53,9 @@ public:
     static Ref<CommandLineAPIHost> create();
     ~CommandLineAPIHost();
 
-    void init(Inspector::InspectorAgent* inspectorAgent
-        , Inspector::InspectorConsoleAgent* consoleAgent
-        , InspectorDatabaseAgent* databaseAgent
-        )
+    void init(RefPtr<InstrumentingAgents> instrumentingAgents)
     {
-        m_inspectorAgent = inspectorAgent;
-        m_consoleAgent = consoleAgent;
-        m_databaseAgent = databaseAgent;
+        m_instrumentingAgents = instrumentingAgents;
     }
 
     void disconnect();
@@ -102,10 +92,7 @@ public:
 private:
     CommandLineAPIHost();
 
-    Inspector::InspectorAgent* m_inspectorAgent { nullptr };
-    Inspector::InspectorConsoleAgent* m_consoleAgent { nullptr };
-    InspectorDatabaseAgent* m_databaseAgent { nullptr };
-
+    RefPtr<InstrumentingAgents> m_instrumentingAgents;
     std::unique_ptr<InspectableObject> m_inspectedObject; // $0
     Inspector::PerGlobalObjectWrapperWorld m_wrappers;
 };
index bf16237..05f1961 100644 (file)
@@ -123,18 +123,13 @@ InspectorController::InspectorController(Page& page, InspectorClient* inspectorC
     m_domAgent = domAgentPtr.get();
     m_agents.append(WTFMove(domAgentPtr));
 
-    auto databaseAgentPtr = std::make_unique<InspectorDatabaseAgent>(pageContext);
-    InspectorDatabaseAgent* databaseAgent = databaseAgentPtr.get();
-    m_agents.append(WTFMove(databaseAgentPtr));
-
-    auto consoleAgentPtr = std::make_unique<PageConsoleAgent>(pageContext, m_domAgent);
-    WebConsoleAgent* consoleAgent = consoleAgentPtr.get();
-    m_instrumentingAgents->setWebConsoleAgent(consoleAgentPtr.get());
-    m_agents.append(WTFMove(consoleAgentPtr));
+    auto consoleAgent = std::make_unique<PageConsoleAgent>(pageContext, m_domAgent);
+    m_instrumentingAgents->setWebConsoleAgent(consoleAgent.get());
+    m_agents.append(WTFMove(consoleAgent));
 
     ASSERT(m_injectedScriptManager->commandLineAPIHost());
-    if (CommandLineAPIHost* commandLineAPIHost = m_injectedScriptManager->commandLineAPIHost())
-        commandLineAPIHost->init(m_inspectorAgent, consoleAgent, databaseAgent);
+    if (auto* commandLineAPIHost = m_injectedScriptManager->commandLineAPIHost())
+        commandLineAPIHost->init(m_instrumentingAgents.copyRef());
 }
 
 InspectorController::~InspectorController()
@@ -185,6 +180,7 @@ void InspectorController::createLazyAgents()
     m_agents.append(std::make_unique<InspectorLayerTreeAgent>(pageContext));
     m_agents.append(std::make_unique<InspectorWorkerAgent>(pageContext));
     m_agents.append(std::make_unique<InspectorDOMStorageAgent>(pageContext));
+    m_agents.append(std::make_unique<InspectorDatabaseAgent>(pageContext));
 #if ENABLE(INDEXED_DATABASE)
     m_agents.append(std::make_unique<InspectorIndexedDBAgent>(pageContext, m_pageAgent));
 #endif
index c73941f..5557c3c 100644 (file)
@@ -40,12 +40,12 @@ using namespace Inspector;
 
 static int nextUnusedId = 1;
 
-Ref<InspectorDatabaseResource> InspectorDatabaseResource::create(RefPtr<Database>&& database, const String& domain, const String& name, const String& version)
+Ref<InspectorDatabaseResource> InspectorDatabaseResource::create(Database& database, const String& domain, const String& name, const String& version)
 {
-    return adoptRef(*new InspectorDatabaseResource(WTFMove(database), domain, name, version));
+    return adoptRef(*new InspectorDatabaseResource(database, domain, name, version));
 }
 
-InspectorDatabaseResource::InspectorDatabaseResource(RefPtr<Database>&& database, const String& domain, const String& name, const String& version)
+InspectorDatabaseResource::InspectorDatabaseResource(Database& database, const String& domain, const String& name, const String& version)
     : m_database(database)
     , m_id(String::number(nextUnusedId++))
     , m_domain(domain)
@@ -54,7 +54,7 @@ InspectorDatabaseResource::InspectorDatabaseResource(RefPtr<Database>&& database
 {
 }
 
-void InspectorDatabaseResource::bind(Inspector::DatabaseFrontendDispatcher* databaseFrontendDispatcher)
+void InspectorDatabaseResource::bind(Inspector::DatabaseFrontendDispatcher& databaseFrontendDispatcher)
 {
     auto jsonObject = Inspector::Protocol::Database::Database::create()
         .setId(m_id)
@@ -62,7 +62,7 @@ void InspectorDatabaseResource::bind(Inspector::DatabaseFrontendDispatcher* data
         .setName(m_name)
         .setVersion(m_version)
         .release();
-    databaseFrontendDispatcher->addDatabase(WTFMove(jsonObject));
+    databaseFrontendDispatcher.addDatabase(WTFMove(jsonObject));
 }
 
 } // namespace WebCore
index 666deb3..5e58766 100644 (file)
@@ -41,17 +41,19 @@ class Database;
 
 class InspectorDatabaseResource : public RefCounted<InspectorDatabaseResource> {
 public:
-    static Ref<InspectorDatabaseResource> create(RefPtr<Database>&&, const String& domain, const String& name, const String& version);
+    static Ref<InspectorDatabaseResource> create(Database&, const String& domain, const String& name, const String& version);
+
+    void bind(Inspector::DatabaseFrontendDispatcher&);
+
+    Database& database() const { return m_database.get(); }
+    void setDatabase(Database& database) { m_database = database; }
 
-    void bind(Inspector::DatabaseFrontendDispatcher*);
-    Database* database() { return m_database.get(); }
-    void setDatabase(RefPtr<Database>&& database) { m_database = WTFMove(database); }
     String id() const { return m_id; }
 
 private:
-    InspectorDatabaseResource(RefPtr<Database>&&, const String& domain, const String& name, const String& version);
+    InspectorDatabaseResource(Database&, const String& domain, const String& name, const String& version);
 
-    RefPtr<Database> m_database;
+    Ref<Database> m_database;
     String m_id;
     String m_domain;
     String m_name;
index 8c4eb1c..758add0 100644 (file)
@@ -35,7 +35,6 @@
 #include "CachedResource.h"
 #include "DOMWindow.h"
 #include "DOMWrapperWorld.h"
-#include "Database.h"
 #include "DocumentLoader.h"
 #include "Event.h"
 #include "Frame.h"
@@ -715,8 +714,8 @@ void InspectorInstrumentation::didCommitLoadImpl(InstrumentingAgents& instrument
         if (InspectorCSSAgent* cssAgent = instrumentingAgents.inspectorCSSAgent())
             cssAgent->reset();
 
-        if (InspectorDatabaseAgent* databaseAgent = instrumentingAgents.inspectorDatabaseAgent())
-            databaseAgent->clearResources();
+        if (auto* databaseAgent = instrumentingAgents.inspectorDatabaseAgent())
+            databaseAgent->didCommitLoad();
 
         if (InspectorDOMAgent* domAgent = instrumentingAgents.inspectorDOMAgent())
             domAgent->setDocument(frame.document());
@@ -902,12 +901,10 @@ void InspectorInstrumentation::consoleStartRecordingCanvasImpl(InstrumentingAgen
         canvasAgent->consoleStartRecordingCanvas(context, exec, options);
 }
 
-void InspectorInstrumentation::didOpenDatabaseImpl(InstrumentingAgents& instrumentingAgents, RefPtr<Database>&& database, const String& domain, const String& name, const String& version)
+void InspectorInstrumentation::didOpenDatabaseImpl(InstrumentingAgents& instrumentingAgents, Database& database)
 {
-    if (!instrumentingAgents.inspectorEnvironment().developerExtrasEnabled())
-        return;
-    if (InspectorDatabaseAgent* dbAgent = instrumentingAgents.inspectorDatabaseAgent())
-        dbAgent->didOpenDatabase(WTFMove(database), domain, name, version);
+    if (auto* databaseAgent = instrumentingAgents.inspectorDatabaseAgent())
+        databaseAgent->didOpenDatabase(database);
 }
 
 void InspectorInstrumentation::didDispatchDOMStorageEventImpl(InstrumentingAgents& instrumentingAgents, const String& key, const String& oldValue, const String& newValue, StorageType storageType, SecurityOrigin* securityOrigin)
index a8c85a4..fd9ed0d 100644 (file)
@@ -35,6 +35,7 @@
 #include "CallTracerTypes.h"
 #include "CanvasBase.h"
 #include "CanvasRenderingContext.h"
+#include "Database.h"
 #include "DocumentThreadableLoader.h"
 #include "Element.h"
 #include "EventTarget.h"
@@ -68,7 +69,6 @@ class CachedResource;
 class CharacterData;
 class DOMWindow;
 class DOMWrapperWorld;
-class Database;
 class Document;
 class DocumentLoader;
 class EventListener;
@@ -240,7 +240,7 @@ public:
     static InspectorInstrumentationCookie willFireObserverCallback(ScriptExecutionContext&, const String& callbackType);
     static void didFireObserverCallback(const InspectorInstrumentationCookie&);
 
-    static void didOpenDatabase(ScriptExecutionContext*, RefPtr<Database>&&, const String& domain, const String& name, const String& version);
+    static void didOpenDatabase(Database&);
 
     static void didDispatchDOMStorageEvent(Page&, const String& key, const String& oldValue, const String& newValue, StorageType, SecurityOrigin*);
 
@@ -417,7 +417,7 @@ private:
     static InspectorInstrumentationCookie willFireObserverCallbackImpl(InstrumentingAgents&, const String&, ScriptExecutionContext&);
     static void didFireObserverCallbackImpl(const InspectorInstrumentationCookie&);
 
-    static void didOpenDatabaseImpl(InstrumentingAgents&, RefPtr<Database>&&, const String& domain, const String& name, const String& version);
+    static void didOpenDatabaseImpl(InstrumentingAgents&, Database&);
 
     static void didDispatchDOMStorageEventImpl(InstrumentingAgents&, const String& key, const String& oldValue, const String& newValue, StorageType, SecurityOrigin*);
 
@@ -1172,10 +1172,11 @@ inline void InspectorInstrumentation::willDestroyCachedResource(CachedResource&
     willDestroyCachedResourceImpl(cachedResource);
 }
 
-inline void InspectorInstrumentation::didOpenDatabase(ScriptExecutionContext* context, RefPtr<Database>&& database, const String& domain, const String& name, const String& version)
+inline void InspectorInstrumentation::didOpenDatabase(Database& database)
 {
-    if (InstrumentingAgents* instrumentingAgents = instrumentingAgentsForContext(context))
-        didOpenDatabaseImpl(*instrumentingAgents, WTFMove(database), domain, name, version);
+    FAST_RETURN_IF_NO_FRONTENDS(void());
+    if (auto* instrumentingAgents = instrumentingAgentsForContext(database.scriptExecutionContext()))
+        didOpenDatabaseImpl(*instrumentingAgents, database);
 }
 
 inline void InspectorInstrumentation::didDispatchDOMStorageEvent(Page& page, const String& key, const String& oldValue, const String& newValue, StorageType storageType, SecurityOrigin* securityOrigin)
index 6a40b6b..c54b55d 100644 (file)
@@ -75,8 +75,8 @@ WorkerInspectorController::WorkerInspectorController(WorkerGlobalScope& workerGl
     m_agents.append(std::make_unique<WorkerRuntimeAgent>(workerContext));
     m_agents.append(WTFMove(consoleAgent));
 
-    if (CommandLineAPIHost* commandLineAPIHost = m_injectedScriptManager->commandLineAPIHost())
-        commandLineAPIHost->init(nullptr, m_instrumentingAgents->webConsoleAgent(), nullptr);
+    if (auto* commandLineAPIHost = m_injectedScriptManager->commandLineAPIHost())
+        commandLineAPIHost->init(m_instrumentingAgents.copyRef());
 }
 
 WorkerInspectorController::~WorkerInspectorController()
index 5a40e03..5f730b0 100644 (file)
@@ -31,6 +31,7 @@
 #include "InspectorDatabaseAgent.h"
 
 #include "Database.h"
+#include "DatabaseTracker.h"
 #include "InspectorDatabaseResource.h"
 #include "InstrumentingAgents.h"
 #include "SQLError.h"
@@ -197,23 +198,21 @@ private:
 
 } // namespace
 
-void InspectorDatabaseAgent::didOpenDatabase(RefPtr<Database>&& database, const String& domain, const String& name, const String& version)
+void InspectorDatabaseAgent::didCommitLoad()
 {
-    if (auto* resource = findByFileName(database->fileName())) {
-        resource->setDatabase(WTFMove(database));
+    m_resources.clear();
+}
+
+void InspectorDatabaseAgent::didOpenDatabase(Database& database)
+{
+    if (auto resource = findByFileName(database.fileName())) {
+        resource->setDatabase(database);
         return;
     }
 
-    auto resource = InspectorDatabaseResource::create(WTFMove(database), domain, name, version);
+    auto resource = InspectorDatabaseResource::create(database, database.securityOrigin().host, database.stringIdentifier(), database.expectedVersion());
     m_resources.add(resource->id(), resource.ptr());
-    // Resources are only bound while visible.
-    if (m_enabled)
-        resource->bind(m_frontendDispatcher.get());
-}
-
-void InspectorDatabaseAgent::clearResources()
-{
-    m_resources.clear();
+    resource->bind(*m_frontendDispatcher);
 }
 
 InspectorDatabaseAgent::InspectorDatabaseAgent(WebAgentContext& context)
@@ -221,12 +220,6 @@ InspectorDatabaseAgent::InspectorDatabaseAgent(WebAgentContext& context)
     , m_frontendDispatcher(std::make_unique<Inspector::DatabaseFrontendDispatcher>(context.frontendRouter))
     , m_backendDispatcher(Inspector::DatabaseBackendDispatcher::create(context.backendDispatcher, this))
 {
-    m_instrumentingAgents.setInspectorDatabaseAgent(this);
-}
-
-InspectorDatabaseAgent::~InspectorDatabaseAgent()
-{
-    m_instrumentingAgents.setInspectorDatabaseAgent(nullptr);
 }
 
 void InspectorDatabaseAgent::didCreateFrontendAndBackend(Inspector::FrontendRouter*, Inspector::BackendDispatcher*)
@@ -241,25 +234,26 @@ void InspectorDatabaseAgent::willDestroyFrontendAndBackend(Inspector::Disconnect
 
 void InspectorDatabaseAgent::enable(ErrorString&)
 {
-    if (m_enabled)
+    if (m_instrumentingAgents.inspectorDatabaseAgent() == this)
         return;
-    m_enabled = true;
 
-    for (auto& resource : m_resources.values())
-        resource->bind(m_frontendDispatcher.get());
+    m_instrumentingAgents.setInspectorDatabaseAgent(this);
+
+    for (auto& database : DatabaseTracker::singleton().openDatabases())
+        didOpenDatabase(database.get());
 }
 
 void InspectorDatabaseAgent::disable(ErrorString&)
 {
-    if (!m_enabled)
-        return;
-    m_enabled = false;
+    m_instrumentingAgents.setInspectorDatabaseAgent(nullptr);
+
+    m_resources.clear();
 }
 
-void InspectorDatabaseAgent::getDatabaseTableNames(ErrorString& error, const String& databaseId, RefPtr<JSON::ArrayOf<String>>& names)
+void InspectorDatabaseAgent::getDatabaseTableNames(ErrorString& errorString, const String& databaseId, RefPtr<JSON::ArrayOf<String>>& names)
 {
-    if (!m_enabled) {
-        error = "Database agent is not enabled"_s;
+    if (m_instrumentingAgents.inspectorDatabaseAgent() != this) {
+        errorString = "Database agent is not enabled"_s;
         return;
     }
 
@@ -273,8 +267,8 @@ void InspectorDatabaseAgent::getDatabaseTableNames(ErrorString& error, const Str
 
 void InspectorDatabaseAgent::executeSQL(const String& databaseId, const String& query, Ref<ExecuteSQLCallback>&& requestCallback)
 {
-    if (!m_enabled) {
-        requestCallback->sendFailure("Database agent is not enabled");
+    if (m_instrumentingAgents.inspectorDatabaseAgent() != this) {
+        requestCallback->sendFailure("Database agent is not enabled"_s);
         return;
     }
 
@@ -292,7 +286,7 @@ void InspectorDatabaseAgent::executeSQL(const String& databaseId, const String&
 String InspectorDatabaseAgent::databaseId(Database& database)
 {
     for (auto& resource : m_resources) {
-        if (resource.value->database() == &database)
+        if (&resource.value->database() == &database)
             return resource.key;
     }
     return String();
@@ -301,7 +295,7 @@ String InspectorDatabaseAgent::databaseId(Database& database)
 InspectorDatabaseResource* InspectorDatabaseAgent::findByFileName(const String& fileName)
 {
     for (auto& resource : m_resources.values()) {
-        if (resource->database()->fileName() == fileName)
+        if (resource->database().fileName() == fileName)
             return resource.get();
     }
     return nullptr;
@@ -309,10 +303,9 @@ InspectorDatabaseResource* InspectorDatabaseAgent::findByFileName(const String&
 
 Database* InspectorDatabaseAgent::databaseForId(const String& databaseId)
 {
-    auto* resource = m_resources.get(databaseId);
-    if (!resource)
-        return nullptr;
-    return resource->database();
+    if (auto resource = m_resources.get(databaseId))
+        return &resource->database();
+    return nullptr;
 }
 
 } // namespace WebCore
index a0aaade..5c7902f 100644 (file)
@@ -33,6 +33,7 @@
 #include <JavaScriptCore/InspectorBackendDispatchers.h>
 #include <JavaScriptCore/InspectorFrontendDispatchers.h>
 #include <wtf/HashMap.h>
+#include <wtf/Optional.h>
 #include <wtf/text/WTFString.h>
 
 namespace WebCore {
@@ -47,12 +48,11 @@ class InspectorDatabaseAgent final : public InspectorAgentBase, public Inspector
     WTF_MAKE_FAST_ALLOCATED;
 public:
     explicit InspectorDatabaseAgent(WebAgentContext&);
-    virtual ~InspectorDatabaseAgent();
+    virtual ~InspectorDatabaseAgent() = default;
 
     void didCreateFrontendAndBackend(Inspector::FrontendRouter*, Inspector::BackendDispatcher*) override;
     void willDestroyFrontendAndBackend(Inspector::DisconnectReason) override;
 
-    void clearResources();
 
     // Called from the front-end.
     void enable(ErrorString&) override;
@@ -63,7 +63,10 @@ public:
     // Called from the injected script.
     String databaseId(Database&);
 
-    void didOpenDatabase(RefPtr<Database>&&, const String& domain, const String& name, const String& version);
+    // InspectorInstrumentation
+    void didCommitLoad();
+    void didOpenDatabase(Database&);
+
 private:
     Database* databaseForId(const String& databaseId);
     InspectorDatabaseResource* findByFileName(const String& fileName);
@@ -71,9 +74,7 @@ private:
     std::unique_ptr<Inspector::DatabaseFrontendDispatcher> m_frontendDispatcher;
     RefPtr<Inspector::DatabaseBackendDispatcher> m_backendDispatcher;
 
-    typedef HashMap<String, RefPtr<InspectorDatabaseResource>> DatabaseResourcesMap;
-    DatabaseResourcesMap m_resources;
-    bool m_enabled { false };
+    HashMap<String, RefPtr<InspectorDatabaseResource>> m_resources;
 };
 
 } // namespace WebCore