1/*
2 * Copyright (C) 2016 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "WorkerInspectorProxy.h"
28
29#include "InspectorInstrumentation.h"
30#include "ScriptExecutionContext.h"
31#include "WorkerGlobalScope.h"
32#include "WorkerInspectorController.h"
33#include "WorkerRunLoop.h"
34#include <JavaScriptCore/InspectorAgentBase.h>
35#include <wtf/NeverDestroyed.h>
36
37
38namespace WebCore {
39using namespace Inspector;
40
41HashSet<WorkerInspectorProxy*>& WorkerInspectorProxy::allWorkerInspectorProxies()
42{
43 static NeverDestroyed<HashSet<WorkerInspectorProxy*>> proxies;
44 return proxies;
45}
46
47WorkerInspectorProxy::WorkerInspectorProxy(const String& identifier)
48 : m_identifier(identifier)
49{
50}
51
52WorkerInspectorProxy::~WorkerInspectorProxy()
53{
54 ASSERT(!m_workerThread);
55 ASSERT(!m_pageChannel);
56}
57
58WorkerThreadStartMode WorkerInspectorProxy::workerStartMode(ScriptExecutionContext& scriptExecutionContext)
59{
60 bool pauseOnStart = InspectorInstrumentation::shouldWaitForDebuggerOnStart(scriptExecutionContext);
61 return pauseOnStart ? WorkerThreadStartMode::WaitForInspector : WorkerThreadStartMode::Normal;
62}
63
64void WorkerInspectorProxy::workerStarted(ScriptExecutionContext* scriptExecutionContext, WorkerThread* thread, const URL& url)
65{
66 ASSERT(!m_workerThread);
67
68 m_scriptExecutionContext = scriptExecutionContext;
69 m_workerThread = thread;
70 m_url = url;
71
72 allWorkerInspectorProxies().add(this);
73
74 InspectorInstrumentation::workerStarted(*m_scriptExecutionContext.get(), this, m_url);
75}
76
77void WorkerInspectorProxy::workerTerminated()
78{
79 if (!m_workerThread)
80 return;
81
82 InspectorInstrumentation::workerTerminated(*m_scriptExecutionContext.get(), this);
83
84 allWorkerInspectorProxies().remove(this);
85
86 m_scriptExecutionContext = nullptr;
87 m_workerThread = nullptr;
88 m_pageChannel = nullptr;
89}
90
91void WorkerInspectorProxy::resumeWorkerIfPaused()
92{
93 m_workerThread->runLoop().postDebuggerTask([] (ScriptExecutionContext& context) {
94 downcast<WorkerGlobalScope>(context).thread().stopRunningDebuggerTasks();
95 });
96}
97
98void WorkerInspectorProxy::connectToWorkerInspectorController(PageChannel* channel)
99{
100 ASSERT(m_workerThread);
101 if (!m_workerThread)
102 return;
103
104 m_pageChannel = channel;
105
106 m_workerThread->runLoop().postDebuggerTask([] (ScriptExecutionContext& context) {
107 downcast<WorkerGlobalScope>(context).inspectorController().connectFrontend();
108 });
109}
110
111void WorkerInspectorProxy::disconnectFromWorkerInspectorController()
112{
113 ASSERT(m_workerThread);
114 if (!m_workerThread)
115 return;
116
117 m_pageChannel = nullptr;
118
119 m_workerThread->runLoop().postDebuggerTask([] (ScriptExecutionContext& context) {
120 downcast<WorkerGlobalScope>(context).inspectorController().disconnectFrontend(DisconnectReason::InspectorDestroyed);
121
122 // In case the worker is paused running debugger tasks, ensure we break out of
123 // the pause since this will be the last debugger task we send to the worker.
124 downcast<WorkerGlobalScope>(context).thread().stopRunningDebuggerTasks();
125 });
126}
127
128void WorkerInspectorProxy::sendMessageToWorkerInspectorController(const String& message)
129{
130 ASSERT(m_workerThread);
131 if (!m_workerThread)
132 return;
133
134 m_workerThread->runLoop().postDebuggerTask([message = message.isolatedCopy()] (ScriptExecutionContext& context) {
135 downcast<WorkerGlobalScope>(context).inspectorController().dispatchMessageFromFrontend(message);
136 });
137}
138
139void WorkerInspectorProxy::sendMessageFromWorkerToFrontend(const String& message)
140{
141 if (!m_pageChannel)
142 return;
143
144 m_pageChannel->sendMessageFromWorkerToFrontend(this, message);
145}
146
147} // namespace WebCore
148