80891b6016049e2a970b84a1bb02881d388e9466
[WebKit-https.git] / Source / WebCore / inspector / WorkerInspectorController.cpp
1 /*
2  * Copyright (C) 2011 Google 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 are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #include "config.h"
32
33 #if ENABLE(INSPECTOR)
34
35 #include "WorkerInspectorController.h"
36
37 #include "CommandLineAPIHost.h"
38 #include "InspectorClient.h"
39 #include "InspectorConsoleAgent.h"
40 #include "InspectorForwarding.h"
41 #include "InspectorHeapProfilerAgent.h"
42 #include "InspectorInstrumentation.h"
43 #include "InspectorProfilerAgent.h"
44 #include "InspectorTimelineAgent.h"
45 #include "InspectorWebBackendDispatchers.h"
46 #include "InspectorWebFrontendDispatchers.h"
47 #include "InstrumentingAgents.h"
48 #include "JSMainThreadExecState.h"
49 #include "PageInjectedScriptHost.h"
50 #include "PageInjectedScriptManager.h"
51 #include "WorkerConsoleAgent.h"
52 #include "WorkerDebuggerAgent.h"
53 #include "WorkerGlobalScope.h"
54 #include "WorkerReportingProxy.h"
55 #include "WorkerRuntimeAgent.h"
56 #include "WorkerThread.h"
57 #include <inspector/InspectorBackendDispatcher.h>
58
59 using namespace Inspector;
60
61 namespace WebCore {
62
63 namespace {
64
65 class PageInspectorProxy : public InspectorFrontendChannel {
66     WTF_MAKE_FAST_ALLOCATED;
67 public:
68     explicit PageInspectorProxy(WorkerGlobalScope& workerGlobalScope)
69         : m_workerGlobalScope(workerGlobalScope) { }
70     virtual ~PageInspectorProxy() { }
71 private:
72     virtual bool sendMessageToFrontend(const String& message) override
73     {
74         m_workerGlobalScope.thread()->workerReportingProxy().postMessageToPageInspector(message);
75         return true;
76     }
77     WorkerGlobalScope& m_workerGlobalScope;
78 };
79
80 }
81
82 WorkerInspectorController::WorkerInspectorController(WorkerGlobalScope& workerGlobalScope)
83     : m_workerGlobalScope(workerGlobalScope)
84     , m_instrumentingAgents(InstrumentingAgents::create(*this))
85     , m_injectedScriptManager(std::make_unique<PageInjectedScriptManager>(*this, PageInjectedScriptHost::create()))
86     , m_runtimeAgent(nullptr)
87 {
88     auto runtimeAgent = std::make_unique<WorkerRuntimeAgent>(m_injectedScriptManager.get(), &workerGlobalScope);
89     m_runtimeAgent = runtimeAgent.get();
90     m_instrumentingAgents->setWorkerRuntimeAgent(m_runtimeAgent);
91     m_agents.append(std::move(runtimeAgent));
92
93     auto consoleAgent = std::make_unique<WorkerConsoleAgent>(m_instrumentingAgents.get(), m_injectedScriptManager.get());
94 #if ENABLE(JAVASCRIPT_DEBUGGER)
95     auto debuggerAgent = std::make_unique<WorkerDebuggerAgent>(m_instrumentingAgents.get(), &workerGlobalScope, m_injectedScriptManager.get());
96     m_runtimeAgent->setScriptDebugServer(&debuggerAgent->scriptDebugServer());
97     m_agents.append(std::move(debuggerAgent));
98
99     m_agents.append(InspectorProfilerAgent::create(m_instrumentingAgents.get(), consoleAgent.get(), &workerGlobalScope, m_injectedScriptManager.get()));
100     m_agents.append(std::make_unique<InspectorHeapProfilerAgent>(m_instrumentingAgents.get(), m_injectedScriptManager.get()));
101 #endif
102     m_agents.append(std::make_unique<InspectorTimelineAgent>(m_instrumentingAgents.get(), nullptr, nullptr, InspectorTimelineAgent::WorkerInspector, nullptr));
103     m_agents.append(std::move(consoleAgent));
104
105     if (CommandLineAPIHost* commandLineAPIHost = m_injectedScriptManager->commandLineAPIHost()) {
106         commandLineAPIHost->init(nullptr
107             , nullptr
108             , nullptr
109             , nullptr
110 #if ENABLE(SQL_DATABASE)
111             , nullptr
112 #endif
113         );
114     }
115 }
116  
117 WorkerInspectorController::~WorkerInspectorController()
118 {
119     m_instrumentingAgents->reset();
120     disconnectFrontend(InspectorDisconnectReason::InspectedTargetDestroyed);
121 }
122
123 void WorkerInspectorController::connectFrontend()
124 {
125     ASSERT(!m_frontendChannel);
126     m_frontendChannel = std::make_unique<PageInspectorProxy>(m_workerGlobalScope);
127     m_backendDispatcher = InspectorBackendDispatcher::create(m_frontendChannel.get());
128     m_agents.didCreateFrontendAndBackend(m_frontendChannel.get(), m_backendDispatcher.get());
129 }
130
131 void WorkerInspectorController::disconnectFrontend(InspectorDisconnectReason reason)
132 {
133     if (!m_frontendChannel)
134         return;
135
136     m_agents.willDestroyFrontendAndBackend(reason);
137     m_backendDispatcher->clearFrontend();
138     m_backendDispatcher.clear();
139     m_frontendChannel = nullptr;
140 }
141
142 void WorkerInspectorController::dispatchMessageFromFrontend(const String& message)
143 {
144     if (m_backendDispatcher)
145         m_backendDispatcher->dispatch(message);
146 }
147
148 #if ENABLE(JAVASCRIPT_DEBUGGER)
149 void WorkerInspectorController::resume()
150 {
151     ErrorString unused;
152     m_runtimeAgent->run(&unused);
153 }
154 #endif
155
156 InspectorFunctionCallHandler WorkerInspectorController::functionCallHandler() const
157 {
158     return WebCore::functionCallHandlerFromAnyThread;
159 }
160
161 InspectorEvaluateHandler WorkerInspectorController::evaluateHandler() const
162 {
163     return WebCore::evaluateHandlerFromAnyThread;
164 }
165
166 void WorkerInspectorController::willCallInjectedScriptFunction(JSC::ExecState* scriptState, const String& scriptName, int scriptLine)
167 {
168     ScriptExecutionContext* scriptExecutionContext = scriptExecutionContextFromExecState(scriptState);
169     InspectorInstrumentationCookie cookie = InspectorInstrumentation::willCallFunction(scriptExecutionContext, scriptName, scriptLine);
170     m_injectedScriptInstrumentationCookies.append(cookie);
171 }
172
173 void WorkerInspectorController::didCallInjectedScriptFunction()
174 {
175     ASSERT(!m_injectedScriptInstrumentationCookies.isEmpty());
176     InspectorInstrumentationCookie cookie = m_injectedScriptInstrumentationCookies.takeLast();
177     InspectorInstrumentation::didCallFunction(cookie);
178 }
179
180 } // namespace WebCore
181
182 #endif // ENABLE(INSPECTOR)