Unreviewed, rolling out r234489.
[WebKit-https.git] / Source / WebCore / inspector / WorkerInspectorController.cpp
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 "WorkerInspectorController.h"
28
29 #include "CommandLineAPIHost.h"
30 #include "InstrumentingAgents.h"
31 #include "JSMainThreadExecState.h"
32 #include "WebHeapAgent.h"
33 #include "WebInjectedScriptHost.h"
34 #include "WebInjectedScriptManager.h"
35 #include "WorkerConsoleAgent.h"
36 #include "WorkerDebuggerAgent.h"
37 #include "WorkerGlobalScope.h"
38 #include "WorkerNetworkAgent.h"
39 #include "WorkerRuntimeAgent.h"
40 #include "WorkerThread.h"
41 #include "WorkerToPageFrontendChannel.h"
42 #include <JavaScriptCore/InspectorAgent.h>
43 #include <JavaScriptCore/InspectorAgentBase.h>
44 #include <JavaScriptCore/InspectorBackendDispatcher.h>
45 #include <JavaScriptCore/InspectorFrontendChannel.h>
46 #include <JavaScriptCore/InspectorFrontendDispatchers.h>
47 #include <JavaScriptCore/InspectorFrontendRouter.h>
48
49 #if ENABLE(SERVICE_WORKER)
50 #include "ServiceWorkerAgent.h"
51 #include "ServiceWorkerGlobalScope.h"
52 #endif
53
54 namespace WebCore {
55
56 using namespace JSC;
57 using namespace Inspector;
58
59 WorkerInspectorController::WorkerInspectorController(WorkerGlobalScope& workerGlobalScope)
60     : m_instrumentingAgents(InstrumentingAgents::create(*this))
61     , m_injectedScriptManager(std::make_unique<WebInjectedScriptManager>(*this, WebInjectedScriptHost::create()))
62     , m_frontendRouter(FrontendRouter::create())
63     , m_backendDispatcher(BackendDispatcher::create(m_frontendRouter.copyRef()))
64     , m_executionStopwatch(Stopwatch::create())
65     , m_scriptDebugServer(workerGlobalScope)
66     , m_workerGlobalScope(workerGlobalScope)
67 {
68     ASSERT(workerGlobalScope.isContextThread());
69
70     auto workerContext = workerAgentContext();
71
72     auto inspectorAgent = std::make_unique<InspectorAgent>(workerContext);
73     auto heapAgent = std::make_unique<WebHeapAgent>(workerContext);
74     auto consoleAgent = std::make_unique<WorkerConsoleAgent>(workerContext, heapAgent.get());
75
76     m_instrumentingAgents->setInspectorAgent(inspectorAgent.get());
77     m_instrumentingAgents->setWebConsoleAgent(consoleAgent.get());
78
79     m_agents.append(WTFMove(inspectorAgent));
80     m_agents.append(std::make_unique<WorkerRuntimeAgent>(workerContext));
81     m_agents.append(std::make_unique<WorkerDebuggerAgent>(workerContext));
82     m_agents.append(WTFMove(consoleAgent));
83     m_agents.append(WTFMove(heapAgent));
84
85     if (CommandLineAPIHost* commandLineAPIHost = m_injectedScriptManager->commandLineAPIHost())
86         commandLineAPIHost->init(m_instrumentingAgents->inspectorAgent(), m_instrumentingAgents->webConsoleAgent(), nullptr, nullptr, nullptr);
87 }
88
89 WorkerInspectorController::~WorkerInspectorController()
90 {
91     ASSERT(!m_frontendRouter->hasFrontends());
92     ASSERT(!m_forwardingChannel);
93
94     m_instrumentingAgents->reset();
95 }
96
97 void WorkerInspectorController::workerTerminating()
98 {
99     m_injectedScriptManager->disconnect();
100
101     disconnectFrontend(Inspector::DisconnectReason::InspectedTargetDestroyed);
102
103     m_agents.discardValues();
104 }
105
106 void WorkerInspectorController::connectFrontend()
107 {
108     ASSERT(!m_frontendRouter->hasFrontends());
109     ASSERT(!m_forwardingChannel);
110
111     createLazyAgents();
112
113     callOnMainThread([] {
114         InspectorInstrumentation::frontendCreated();
115     });
116
117     m_executionStopwatch->reset();
118     m_executionStopwatch->start();
119
120     m_forwardingChannel = std::make_unique<WorkerToPageFrontendChannel>(m_workerGlobalScope);
121     m_frontendRouter->connectFrontend(m_forwardingChannel.get());
122     m_agents.didCreateFrontendAndBackend(&m_frontendRouter.get(), &m_backendDispatcher.get());
123 }
124
125 void WorkerInspectorController::disconnectFrontend(Inspector::DisconnectReason reason)
126 {
127     if (!m_frontendRouter->hasFrontends())
128         return;
129
130     ASSERT(m_forwardingChannel);
131
132     callOnMainThread([] {
133         InspectorInstrumentation::frontendDeleted();
134     });
135
136     m_agents.willDestroyFrontendAndBackend(reason);
137     m_frontendRouter->disconnectFrontend(m_forwardingChannel.get());
138     m_forwardingChannel = nullptr;
139 }
140
141 void WorkerInspectorController::dispatchMessageFromFrontend(const String& message)
142 {
143     m_backendDispatcher->dispatch(message);
144 }
145
146 WorkerAgentContext WorkerInspectorController::workerAgentContext()
147 {
148     AgentContext baseContext = {
149         *this,
150         *m_injectedScriptManager,
151         m_frontendRouter.get(),
152         m_backendDispatcher.get(),
153     };
154
155     WebAgentContext webContext = {
156         baseContext,
157         m_instrumentingAgents.get(),
158     };
159
160     WorkerAgentContext workerContext = {
161         webContext,
162         m_workerGlobalScope,
163     };
164
165     return workerContext;
166 }
167
168 void WorkerInspectorController::createLazyAgents()
169 {
170     if (m_didCreateLazyAgents)
171         return;
172
173     m_didCreateLazyAgents = true;
174
175 #if ENABLE(SERVICE_WORKER)
176     auto workerContext = workerAgentContext();
177     if (is<ServiceWorkerGlobalScope>(m_workerGlobalScope)) {
178         m_agents.append(std::make_unique<ServiceWorkerAgent>(workerContext));
179         m_agents.append(std::make_unique<WorkerNetworkAgent>(workerContext));
180     }
181 #endif
182 }
183
184 InspectorFunctionCallHandler WorkerInspectorController::functionCallHandler() const
185 {
186     return WebCore::functionCallHandlerFromAnyThread;
187 }
188
189 InspectorEvaluateHandler WorkerInspectorController::evaluateHandler() const
190 {
191     return WebCore::evaluateHandlerFromAnyThread;
192 }
193
194 VM& WorkerInspectorController::vm()
195 {
196     return m_workerGlobalScope.vm();
197 }
198
199 } // namespace WebCore