b063e6fa455f65eeae819cbd8a92fe59aa02fec6
[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 "JSExecState.h"
32 #include "WebHeapAgent.h"
33 #include "WebInjectedScriptHost.h"
34 #include "WebInjectedScriptManager.h"
35 #include "WorkerAuditAgent.h"
36 #include "WorkerConsoleAgent.h"
37 #include "WorkerDebuggerAgent.h"
38 #include "WorkerGlobalScope.h"
39 #include "WorkerNetworkAgent.h"
40 #include "WorkerRuntimeAgent.h"
41 #include "WorkerThread.h"
42 #include "WorkerToPageFrontendChannel.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 heapAgent = std::make_unique<WebHeapAgent>(workerContext);
73     auto consoleAgent = std::make_unique<WorkerConsoleAgent>(workerContext, heapAgent.get());
74
75     m_instrumentingAgents->setWebConsoleAgent(consoleAgent.get());
76
77     m_agents.append(std::make_unique<WorkerRuntimeAgent>(workerContext));
78     m_agents.append(std::make_unique<WorkerDebuggerAgent>(workerContext));
79     m_agents.append(WTFMove(consoleAgent));
80     m_agents.append(WTFMove(heapAgent));
81
82     if (CommandLineAPIHost* commandLineAPIHost = m_injectedScriptManager->commandLineAPIHost())
83         commandLineAPIHost->init(nullptr, m_instrumentingAgents->webConsoleAgent(), nullptr, nullptr);
84 }
85
86 WorkerInspectorController::~WorkerInspectorController()
87 {
88     ASSERT(!m_frontendRouter->hasFrontends());
89     ASSERT(!m_forwardingChannel);
90
91     m_instrumentingAgents->reset();
92 }
93
94 void WorkerInspectorController::workerTerminating()
95 {
96     m_injectedScriptManager->disconnect();
97
98     disconnectFrontend(Inspector::DisconnectReason::InspectedTargetDestroyed);
99
100     m_agents.discardValues();
101 }
102
103 void WorkerInspectorController::connectFrontend()
104 {
105     ASSERT(!m_frontendRouter->hasFrontends());
106     ASSERT(!m_forwardingChannel);
107
108     createLazyAgents();
109
110     callOnMainThread([] {
111         InspectorInstrumentation::frontendCreated();
112     });
113
114     m_executionStopwatch->reset();
115     m_executionStopwatch->start();
116
117     m_forwardingChannel = std::make_unique<WorkerToPageFrontendChannel>(m_workerGlobalScope);
118     m_frontendRouter->connectFrontend(*m_forwardingChannel.get());
119     m_agents.didCreateFrontendAndBackend(&m_frontendRouter.get(), &m_backendDispatcher.get());
120 }
121
122 void WorkerInspectorController::disconnectFrontend(Inspector::DisconnectReason reason)
123 {
124     if (!m_frontendRouter->hasFrontends())
125         return;
126
127     ASSERT(m_forwardingChannel);
128
129     callOnMainThread([] {
130         InspectorInstrumentation::frontendDeleted();
131     });
132
133     m_agents.willDestroyFrontendAndBackend(reason);
134     m_frontendRouter->disconnectFrontend(*m_forwardingChannel.get());
135     m_forwardingChannel = nullptr;
136 }
137
138 void WorkerInspectorController::dispatchMessageFromFrontend(const String& message)
139 {
140     m_backendDispatcher->dispatch(message);
141 }
142
143 WorkerAgentContext WorkerInspectorController::workerAgentContext()
144 {
145     AgentContext baseContext = {
146         *this,
147         *m_injectedScriptManager,
148         m_frontendRouter.get(),
149         m_backendDispatcher.get(),
150     };
151
152     WebAgentContext webContext = {
153         baseContext,
154         m_instrumentingAgents.get(),
155     };
156
157     WorkerAgentContext workerContext = {
158         webContext,
159         m_workerGlobalScope,
160     };
161
162     return workerContext;
163 }
164
165 void WorkerInspectorController::createLazyAgents()
166 {
167     if (m_didCreateLazyAgents)
168         return;
169
170     m_didCreateLazyAgents = true;
171
172     auto workerContext = workerAgentContext();
173
174 #if ENABLE(SERVICE_WORKER)
175     if (is<ServiceWorkerGlobalScope>(m_workerGlobalScope)) {
176         m_agents.append(std::make_unique<ServiceWorkerAgent>(workerContext));
177         m_agents.append(std::make_unique<WorkerNetworkAgent>(workerContext));
178     }
179 #endif
180
181     m_agents.append(std::make_unique<WorkerAuditAgent>(workerContext));
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