WorkerInspectorProxy.cpp [plain text]
#include "config.h"
#include "WorkerInspectorProxy.h"
#include "InspectorInstrumentation.h"
#include "ScriptExecutionContext.h"
#include "WorkerGlobalScope.h"
#include "WorkerInspectorController.h"
#include "WorkerRunLoop.h"
#include <inspector/InspectorAgentBase.h>
#include <wtf/NeverDestroyed.h>
using namespace Inspector;
namespace WebCore {
HashSet<WorkerInspectorProxy*>& WorkerInspectorProxy::allWorkerInspectorProxies()
{
static NeverDestroyed<HashSet<WorkerInspectorProxy*>> proxies;
return proxies;
}
WorkerInspectorProxy::WorkerInspectorProxy(const String& identifier)
: m_identifier(identifier)
{
}
WorkerInspectorProxy::~WorkerInspectorProxy()
{
ASSERT(!m_workerThread);
ASSERT(!m_pageChannel);
}
WorkerThreadStartMode WorkerInspectorProxy::workerStartMode(ScriptExecutionContext& scriptExecutionContext)
{
bool pauseOnStart = InspectorInstrumentation::shouldWaitForDebuggerOnStart(scriptExecutionContext);
return pauseOnStart ? WorkerThreadStartMode::WaitForInspector : WorkerThreadStartMode::Normal;
}
void WorkerInspectorProxy::workerStarted(ScriptExecutionContext* scriptExecutionContext, WorkerThread* thread, const URL& url)
{
ASSERT(!m_workerThread);
m_scriptExecutionContext = scriptExecutionContext;
m_workerThread = thread;
m_url = url;
allWorkerInspectorProxies().add(this);
InspectorInstrumentation::workerStarted(*m_scriptExecutionContext.get(), this, m_url);
}
void WorkerInspectorProxy::workerTerminated()
{
if (!m_workerThread)
return;
InspectorInstrumentation::workerTerminated(*m_scriptExecutionContext.get(), this);
allWorkerInspectorProxies().remove(this);
m_scriptExecutionContext = nullptr;
m_workerThread = nullptr;
m_pageChannel = nullptr;
}
void WorkerInspectorProxy::resumeWorkerIfPaused()
{
m_workerThread->runLoop().postTaskForMode([] (ScriptExecutionContext& context) {
downcast<WorkerGlobalScope>(context).thread().stopRunningDebuggerTasks();
}, WorkerRunLoop::debuggerMode());
}
void WorkerInspectorProxy::connectToWorkerInspectorController(PageChannel* channel)
{
ASSERT(m_workerThread);
if (!m_workerThread)
return;
m_pageChannel = channel;
m_workerThread->runLoop().postTaskForMode([] (ScriptExecutionContext& context) {
downcast<WorkerGlobalScope>(context).inspectorController().connectFrontend();
}, WorkerRunLoop::debuggerMode());
}
void WorkerInspectorProxy::disconnectFromWorkerInspectorController()
{
ASSERT(m_workerThread);
if (!m_workerThread)
return;
m_pageChannel = nullptr;
m_workerThread->runLoop().postTaskForMode([] (ScriptExecutionContext& context) {
downcast<WorkerGlobalScope>(context).inspectorController().disconnectFrontend(DisconnectReason::InspectorDestroyed);
downcast<WorkerGlobalScope>(context).thread().stopRunningDebuggerTasks();
}, WorkerRunLoop::debuggerMode());
}
void WorkerInspectorProxy::sendMessageToWorkerInspectorController(const String& message)
{
ASSERT(m_workerThread);
if (!m_workerThread)
return;
m_workerThread->runLoop().postTaskForMode([message = message.isolatedCopy()] (ScriptExecutionContext& context) {
downcast<WorkerGlobalScope>(context).inspectorController().dispatchMessageFromFrontend(message);
}, WorkerRunLoop::debuggerMode());
}
void WorkerInspectorProxy::sendMessageFromWorkerToFrontend(const String& message)
{
if (!m_pageChannel)
return;
m_pageChannel->sendMessageFromWorkerToFrontend(this, message);
}
}