Unreviewed, rolling out r224863.
[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 <inspector/InspectorAgentBase.h>
43 #include <inspector/InspectorBackendDispatcher.h>
44 #include <inspector/InspectorFrontendChannel.h>
45 #include <inspector/InspectorFrontendDispatchers.h>
46 #include <inspector/InspectorFrontendRouter.h>
47 #include <inspector/agents/InspectorAgent.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     InspectorInstrumentation::frontendCreated();
114
115     m_executionStopwatch->reset();
116     m_executionStopwatch->start();
117
118     m_forwardingChannel = std::make_unique<WorkerToPageFrontendChannel>(m_workerGlobalScope);
119     m_frontendRouter->connectFrontend(m_forwardingChannel.get());
120     m_agents.didCreateFrontendAndBackend(&m_frontendRouter.get(), &m_backendDispatcher.get());
121 }
122
123 void WorkerInspectorController::disconnectFrontend(Inspector::DisconnectReason reason)
124 {
125     if (!m_frontendRouter->hasFrontends())
126         return;
127
128     ASSERT(m_forwardingChannel);
129
130     InspectorInstrumentation::frontendDeleted();
131
132     m_agents.willDestroyFrontendAndBackend(reason);
133     m_frontendRouter->disconnectFrontend(m_forwardingChannel.get());
134     m_forwardingChannel = nullptr;
135 }
136
137 void WorkerInspectorController::dispatchMessageFromFrontend(const String& message)
138 {
139     m_backendDispatcher->dispatch(message);
140 }
141
142 WorkerAgentContext WorkerInspectorController::workerAgentContext()
143 {
144     AgentContext baseContext = {
145         *this,
146         *m_injectedScriptManager,
147         m_frontendRouter.get(),
148         m_backendDispatcher.get(),
149     };
150
151     WebAgentContext webContext = {
152         baseContext,
153         m_instrumentingAgents.get(),
154     };
155
156     WorkerAgentContext workerContext = {
157         webContext,
158         m_workerGlobalScope,
159     };
160
161     return workerContext;
162 }
163
164 void WorkerInspectorController::createLazyAgents()
165 {
166     if (m_didCreateLazyAgents)
167         return;
168
169     m_didCreateLazyAgents = true;
170
171 #if ENABLE(SERVICE_WORKER)
172     auto workerContext = workerAgentContext();
173     if (is<ServiceWorkerGlobalScope>(m_workerGlobalScope)) {
174         m_agents.append(std::make_unique<ServiceWorkerAgent>(workerContext));
175         m_agents.append(std::make_unique<WorkerNetworkAgent>(workerContext));
176     }
177 #endif
178 }
179
180 InspectorFunctionCallHandler WorkerInspectorController::functionCallHandler() const
181 {
182     return WebCore::functionCallHandlerFromAnyThread;
183 }
184
185 InspectorEvaluateHandler WorkerInspectorController::evaluateHandler() const
186 {
187     return WebCore::evaluateHandlerFromAnyThread;
188 }
189
190 VM& WorkerInspectorController::vm()
191 {
192     return m_workerGlobalScope.vm();
193 }
194
195 } // namespace WebCore