WorkerInspectorController.cpp [plain text]
#include "config.h"
#include "WorkerInspectorController.h"
#include "CommandLineAPIHost.h"
#include "InspectorClient.h"
#include "InspectorForwarding.h"
#include "InspectorInstrumentation.h"
#include "InspectorTimelineAgent.h"
#include "InstrumentingAgents.h"
#include "JSMainThreadExecState.h"
#include "WebInjectedScriptHost.h"
#include "WebInjectedScriptManager.h"
#include "WorkerConsoleAgent.h"
#include "WorkerDebuggerAgent.h"
#include "WorkerGlobalScope.h"
#include "WorkerReportingProxy.h"
#include "WorkerRuntimeAgent.h"
#include "WorkerThread.h"
#include <inspector/InspectorBackendDispatcher.h>
#include <inspector/InspectorFrontendDispatchers.h>
#include <wtf/Stopwatch.h>
using namespace Inspector;
namespace WebCore {
namespace {
class PageInspectorProxy : public InspectorFrontendChannel {
WTF_MAKE_FAST_ALLOCATED;
public:
explicit PageInspectorProxy(WorkerGlobalScope& workerGlobalScope)
: m_workerGlobalScope(workerGlobalScope) { }
virtual ~PageInspectorProxy() { }
private:
virtual bool sendMessageToFrontend(const String& message) override
{
m_workerGlobalScope.thread().workerReportingProxy().postMessageToPageInspector(message);
return true;
}
WorkerGlobalScope& m_workerGlobalScope;
};
}
WorkerInspectorController::WorkerInspectorController(WorkerGlobalScope& workerGlobalScope)
: m_workerGlobalScope(workerGlobalScope)
, m_instrumentingAgents(InstrumentingAgents::create(*this))
, m_injectedScriptManager(std::make_unique<WebInjectedScriptManager>(*this, WebInjectedScriptHost::create()))
, m_runtimeAgent(nullptr)
, m_executionStopwatch(Stopwatch::create())
{
auto runtimeAgent = std::make_unique<WorkerRuntimeAgent>(m_injectedScriptManager.get(), &workerGlobalScope);
m_runtimeAgent = runtimeAgent.get();
m_instrumentingAgents->setWorkerRuntimeAgent(m_runtimeAgent);
m_agents.append(WTF::move(runtimeAgent));
auto consoleAgent = std::make_unique<WorkerConsoleAgent>(m_injectedScriptManager.get());
m_instrumentingAgents->setWebConsoleAgent(consoleAgent.get());
auto debuggerAgent = std::make_unique<WorkerDebuggerAgent>(m_injectedScriptManager.get(), m_instrumentingAgents.get(), &workerGlobalScope);
m_runtimeAgent->setScriptDebugServer(&debuggerAgent->scriptDebugServer());
m_agents.append(WTF::move(debuggerAgent));
m_agents.append(std::make_unique<InspectorTimelineAgent>(m_instrumentingAgents.get(), nullptr, InspectorTimelineAgent::WorkerInspector, nullptr));
m_agents.append(WTF::move(consoleAgent));
if (CommandLineAPIHost* commandLineAPIHost = m_injectedScriptManager->commandLineAPIHost()) {
commandLineAPIHost->init(nullptr
, nullptr
, nullptr
, nullptr
, nullptr
);
}
}
WorkerInspectorController::~WorkerInspectorController()
{
m_instrumentingAgents->reset();
disconnectFrontend(Inspector::DisconnectReason::InspectedTargetDestroyed);
}
void WorkerInspectorController::connectFrontend()
{
ASSERT(!m_frontendChannel);
m_frontendChannel = std::make_unique<PageInspectorProxy>(m_workerGlobalScope);
m_backendDispatcher = BackendDispatcher::create(m_frontendChannel.get());
m_agents.didCreateFrontendAndBackend(m_frontendChannel.get(), m_backendDispatcher.get());
}
void WorkerInspectorController::disconnectFrontend(Inspector::DisconnectReason reason)
{
if (!m_frontendChannel)
return;
m_agents.willDestroyFrontendAndBackend(reason);
m_backendDispatcher->clearFrontend();
m_backendDispatcher = nullptr;
m_frontendChannel = nullptr;
}
void WorkerInspectorController::dispatchMessageFromFrontend(const String& message)
{
if (m_backendDispatcher)
m_backendDispatcher->dispatch(message);
}
void WorkerInspectorController::resume()
{
ErrorString unused;
m_runtimeAgent->run(unused);
}
InspectorFunctionCallHandler WorkerInspectorController::functionCallHandler() const
{
return WebCore::functionCallHandlerFromAnyThread;
}
InspectorEvaluateHandler WorkerInspectorController::evaluateHandler() const
{
return WebCore::evaluateHandlerFromAnyThread;
}
void WorkerInspectorController::willCallInjectedScriptFunction(JSC::ExecState* scriptState, const String& scriptName, int scriptLine)
{
ScriptExecutionContext* scriptExecutionContext = scriptExecutionContextFromExecState(scriptState);
InspectorInstrumentationCookie cookie = InspectorInstrumentation::willCallFunction(scriptExecutionContext, scriptName, scriptLine);
m_injectedScriptInstrumentationCookies.append(cookie);
}
void WorkerInspectorController::didCallInjectedScriptFunction(JSC::ExecState* scriptState)
{
ASSERT(!m_injectedScriptInstrumentationCookies.isEmpty());
ScriptExecutionContext* scriptExecutionContext = scriptExecutionContextFromExecState(scriptState);
InspectorInstrumentationCookie cookie = m_injectedScriptInstrumentationCookies.takeLast();
InspectorInstrumentation::didCallFunction(cookie, scriptExecutionContext);
}
Ref<Stopwatch> WorkerInspectorController::executionStopwatch()
{
return m_executionStopwatch.copyRef();
}
}