Web Inspector: FrontendChannel should know its own connection type
[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 #include "WorkerInspectorController.h"
33
34 #include "CommandLineAPIHost.h"
35 #include "InspectorClient.h"
36 #include "InspectorForwarding.h"
37 #include "InspectorInstrumentation.h"
38 #include "InspectorTimelineAgent.h"
39 #include "InstrumentingAgents.h"
40 #include "JSMainThreadExecState.h"
41 #include "WebInjectedScriptHost.h"
42 #include "WebInjectedScriptManager.h"
43 #include "WorkerConsoleAgent.h"
44 #include "WorkerDebuggerAgent.h"
45 #include "WorkerGlobalScope.h"
46 #include "WorkerReportingProxy.h"
47 #include "WorkerRuntimeAgent.h"
48 #include "WorkerThread.h"
49 #include <inspector/InspectorBackendDispatcher.h>
50 #include <inspector/InspectorFrontendDispatchers.h>
51 #include <wtf/Stopwatch.h>
52
53 using namespace Inspector;
54
55 namespace WebCore {
56
57 namespace {
58
59 class PageInspectorProxy : public InspectorFrontendChannel {
60     WTF_MAKE_FAST_ALLOCATED;
61 public:
62     explicit PageInspectorProxy(WorkerGlobalScope& workerGlobalScope)
63         : m_workerGlobalScope(workerGlobalScope) { }
64     virtual ~PageInspectorProxy() { }
65
66     virtual ConnectionType connectionType() const override { return ConnectionType::Local; }
67 private:
68     virtual bool sendMessageToFrontend(const String& message) override
69     {
70         m_workerGlobalScope.thread().workerReportingProxy().postMessageToPageInspector(message);
71         return true;
72     }
73     WorkerGlobalScope& m_workerGlobalScope;
74 };
75
76 }
77
78 WorkerInspectorController::WorkerInspectorController(WorkerGlobalScope& workerGlobalScope)
79     : m_workerGlobalScope(workerGlobalScope)
80     , m_instrumentingAgents(InstrumentingAgents::create(*this))
81     , m_injectedScriptManager(std::make_unique<WebInjectedScriptManager>(*this, WebInjectedScriptHost::create()))
82     , m_runtimeAgent(nullptr)
83     , m_executionStopwatch(Stopwatch::create())
84 {
85     auto runtimeAgent = std::make_unique<WorkerRuntimeAgent>(m_injectedScriptManager.get(), &workerGlobalScope);
86     m_runtimeAgent = runtimeAgent.get();
87     m_instrumentingAgents->setWorkerRuntimeAgent(m_runtimeAgent);
88     m_agents.append(WTF::move(runtimeAgent));
89
90     auto consoleAgent = std::make_unique<WorkerConsoleAgent>(m_injectedScriptManager.get());
91     m_instrumentingAgents->setWebConsoleAgent(consoleAgent.get());
92
93     auto debuggerAgent = std::make_unique<WorkerDebuggerAgent>(m_injectedScriptManager.get(), m_instrumentingAgents.get(), &workerGlobalScope);
94     m_runtimeAgent->setScriptDebugServer(&debuggerAgent->scriptDebugServer());
95     m_agents.append(WTF::move(debuggerAgent));
96
97     m_agents.append(std::make_unique<InspectorTimelineAgent>(m_instrumentingAgents.get(), nullptr, InspectorTimelineAgent::WorkerInspector, nullptr));
98     m_agents.append(WTF::move(consoleAgent));
99
100     if (CommandLineAPIHost* commandLineAPIHost = m_injectedScriptManager->commandLineAPIHost()) {
101         commandLineAPIHost->init(nullptr
102             , nullptr
103             , nullptr
104             , nullptr
105             , nullptr
106         );
107     }
108 }
109  
110 WorkerInspectorController::~WorkerInspectorController()
111 {
112     m_instrumentingAgents->reset();
113     disconnectFrontend(Inspector::DisconnectReason::InspectedTargetDestroyed);
114 }
115
116 void WorkerInspectorController::connectFrontend()
117 {
118     ASSERT(!m_frontendChannel);
119     m_frontendChannel = std::make_unique<PageInspectorProxy>(m_workerGlobalScope);
120     m_backendDispatcher = BackendDispatcher::create(m_frontendChannel.get());
121     m_agents.didCreateFrontendAndBackend(m_frontendChannel.get(), m_backendDispatcher.get());
122 }
123
124 void WorkerInspectorController::disconnectFrontend(Inspector::DisconnectReason reason)
125 {
126     if (!m_frontendChannel)
127         return;
128
129     m_agents.willDestroyFrontendAndBackend(reason);
130     m_backendDispatcher->clearFrontend();
131     m_backendDispatcher = nullptr;
132     m_frontendChannel = nullptr;
133 }
134
135 void WorkerInspectorController::dispatchMessageFromFrontend(const String& message)
136 {
137     if (m_backendDispatcher)
138         m_backendDispatcher->dispatch(message);
139 }
140
141 void WorkerInspectorController::resume()
142 {
143     ErrorString unused;
144     m_runtimeAgent->run(unused);
145 }
146
147 InspectorFunctionCallHandler WorkerInspectorController::functionCallHandler() const
148 {
149     return WebCore::functionCallHandlerFromAnyThread;
150 }
151
152 InspectorEvaluateHandler WorkerInspectorController::evaluateHandler() const
153 {
154     return WebCore::evaluateHandlerFromAnyThread;
155 }
156
157 void WorkerInspectorController::willCallInjectedScriptFunction(JSC::ExecState* scriptState, const String& scriptName, int scriptLine)
158 {
159     ScriptExecutionContext* scriptExecutionContext = scriptExecutionContextFromExecState(scriptState);
160     InspectorInstrumentationCookie cookie = InspectorInstrumentation::willCallFunction(scriptExecutionContext, scriptName, scriptLine);
161     m_injectedScriptInstrumentationCookies.append(cookie);
162 }
163
164 void WorkerInspectorController::didCallInjectedScriptFunction(JSC::ExecState* scriptState)
165 {
166     ASSERT(!m_injectedScriptInstrumentationCookies.isEmpty());
167     ScriptExecutionContext* scriptExecutionContext = scriptExecutionContextFromExecState(scriptState);
168     InspectorInstrumentationCookie cookie = m_injectedScriptInstrumentationCookies.takeLast();
169     InspectorInstrumentation::didCallFunction(cookie, scriptExecutionContext);
170 }
171
172 Ref<Stopwatch> WorkerInspectorController::executionStopwatch()
173 {
174     return m_executionStopwatch.copyRef();
175 }
176
177 } // namespace WebCore