WorkerInspectorController.cpp [plain text]
#include "config.h"
#include "WorkerInspectorController.h"
#include "CommandLineAPIHost.h"
#include "InstrumentingAgents.h"
#include "JSExecState.h"
#include "WebHeapAgent.h"
#include "WebInjectedScriptHost.h"
#include "WebInjectedScriptManager.h"
#include "WorkerAuditAgent.h"
#include "WorkerConsoleAgent.h"
#include "WorkerDOMDebuggerAgent.h"
#include "WorkerDebuggerAgent.h"
#include "WorkerGlobalScope.h"
#include "WorkerNetworkAgent.h"
#include "WorkerRuntimeAgent.h"
#include "WorkerThread.h"
#include "WorkerToPageFrontendChannel.h"
#include <JavaScriptCore/InspectorAgentBase.h>
#include <JavaScriptCore/InspectorBackendDispatcher.h>
#include <JavaScriptCore/InspectorFrontendChannel.h>
#include <JavaScriptCore/InspectorFrontendDispatchers.h>
#include <JavaScriptCore/InspectorFrontendRouter.h>
#if ENABLE(SERVICE_WORKER)
#include "ServiceWorkerAgent.h"
#include "ServiceWorkerGlobalScope.h"
#endif
namespace WebCore {
using namespace JSC;
using namespace Inspector;
WorkerInspectorController::WorkerInspectorController(WorkerGlobalScope& workerGlobalScope)
: m_instrumentingAgents(InstrumentingAgents::create(*this))
, m_injectedScriptManager(makeUnique<WebInjectedScriptManager>(*this, WebInjectedScriptHost::create()))
, m_frontendRouter(FrontendRouter::create())
, m_backendDispatcher(BackendDispatcher::create(m_frontendRouter.copyRef()))
, m_executionStopwatch(Stopwatch::create())
, m_scriptDebugServer(workerGlobalScope)
, m_workerGlobalScope(workerGlobalScope)
{
ASSERT(workerGlobalScope.isContextThread());
auto workerContext = workerAgentContext();
auto consoleAgent = makeUnique<WorkerConsoleAgent>(workerContext);
m_instrumentingAgents->setWebConsoleAgent(consoleAgent.get());
m_agents.append(WTFMove(consoleAgent));
}
WorkerInspectorController::~WorkerInspectorController()
{
ASSERT(!m_frontendRouter->hasFrontends());
ASSERT(!m_forwardingChannel);
m_instrumentingAgents->reset();
}
void WorkerInspectorController::workerTerminating()
{
m_injectedScriptManager->disconnect();
disconnectFrontend(Inspector::DisconnectReason::InspectedTargetDestroyed);
m_agents.discardValues();
}
void WorkerInspectorController::connectFrontend()
{
ASSERT(!m_frontendRouter->hasFrontends());
ASSERT(!m_forwardingChannel);
createLazyAgents();
callOnMainThread([] {
InspectorInstrumentation::frontendCreated();
});
m_executionStopwatch->reset();
m_executionStopwatch->start();
m_forwardingChannel = makeUnique<WorkerToPageFrontendChannel>(m_workerGlobalScope);
m_frontendRouter->connectFrontend(*m_forwardingChannel.get());
m_agents.didCreateFrontendAndBackend(&m_frontendRouter.get(), &m_backendDispatcher.get());
}
void WorkerInspectorController::disconnectFrontend(Inspector::DisconnectReason reason)
{
if (!m_frontendRouter->hasFrontends())
return;
ASSERT(m_forwardingChannel);
callOnMainThread([] {
InspectorInstrumentation::frontendDeleted();
});
m_agents.willDestroyFrontendAndBackend(reason);
m_frontendRouter->disconnectFrontend(*m_forwardingChannel.get());
m_forwardingChannel = nullptr;
}
void WorkerInspectorController::dispatchMessageFromFrontend(const String& message)
{
m_backendDispatcher->dispatch(message);
}
WorkerAgentContext WorkerInspectorController::workerAgentContext()
{
AgentContext baseContext = {
*this,
*m_injectedScriptManager,
m_frontendRouter.get(),
m_backendDispatcher.get(),
};
WebAgentContext webContext = {
baseContext,
m_instrumentingAgents.get(),
};
WorkerAgentContext workerContext = {
webContext,
m_workerGlobalScope,
};
return workerContext;
}
void WorkerInspectorController::createLazyAgents()
{
if (m_didCreateLazyAgents)
return;
m_didCreateLazyAgents = true;
m_injectedScriptManager->connect();
auto workerContext = workerAgentContext();
m_agents.append(makeUnique<WorkerRuntimeAgent>(workerContext));
#if ENABLE(SERVICE_WORKER)
if (is<ServiceWorkerGlobalScope>(m_workerGlobalScope)) {
m_agents.append(makeUnique<ServiceWorkerAgent>(workerContext));
m_agents.append(makeUnique<WorkerNetworkAgent>(workerContext));
}
#endif
m_agents.append(makeUnique<WebHeapAgent>(workerContext));
auto debuggerAgent = makeUnique<WorkerDebuggerAgent>(workerContext);
auto debuggerAgentPtr = debuggerAgent.get();
m_agents.append(WTFMove(debuggerAgent));
m_agents.append(makeUnique<WorkerDOMDebuggerAgent>(workerContext, debuggerAgentPtr));
m_agents.append(makeUnique<WorkerAuditAgent>(workerContext));
if (auto& commandLineAPIHost = m_injectedScriptManager->commandLineAPIHost())
commandLineAPIHost->init(m_instrumentingAgents.copyRef());
}
InspectorFunctionCallHandler WorkerInspectorController::functionCallHandler() const
{
return WebCore::functionCallHandlerFromAnyThread;
}
InspectorEvaluateHandler WorkerInspectorController::evaluateHandler() const
{
return WebCore::evaluateHandlerFromAnyThread;
}
VM& WorkerInspectorController::vm()
{
return m_workerGlobalScope.vm();
}
}