Add WTF::move()
[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 "InspectorForwarding.h"
40 #include "InspectorInstrumentation.h"
41 #include "InspectorTimelineAgent.h"
42 #include "InspectorWebBackendDispatchers.h"
43 #include "InspectorWebFrontendDispatchers.h"
44 #include "InstrumentingAgents.h"
45 #include "JSMainThreadExecState.h"
46 #include "WebInjectedScriptHost.h"
47 #include "WebInjectedScriptManager.h"
48 #include "WorkerConsoleAgent.h"
49 #include "WorkerDebuggerAgent.h"
50 #include "WorkerGlobalScope.h"
51 #include "WorkerProfilerAgent.h"
52 #include "WorkerReportingProxy.h"
53 #include "WorkerRuntimeAgent.h"
54 #include "WorkerThread.h"
55 #include <inspector/InspectorBackendDispatcher.h>
56
57 using namespace Inspector;
58
59 namespace WebCore {
60
61 namespace {
62
63 class PageInspectorProxy : public InspectorFrontendChannel {
64     WTF_MAKE_FAST_ALLOCATED;
65 public:
66     explicit PageInspectorProxy(WorkerGlobalScope& workerGlobalScope)
67         : m_workerGlobalScope(workerGlobalScope) { }
68     virtual ~PageInspectorProxy() { }
69 private:
70     virtual bool sendMessageToFrontend(const String& message) override
71     {
72         m_workerGlobalScope.thread().workerReportingProxy().postMessageToPageInspector(message);
73         return true;
74     }
75     WorkerGlobalScope& m_workerGlobalScope;
76 };
77
78 }
79
80 WorkerInspectorController::WorkerInspectorController(WorkerGlobalScope& workerGlobalScope)
81     : m_workerGlobalScope(workerGlobalScope)
82     , m_instrumentingAgents(InstrumentingAgents::create(*this))
83     , m_injectedScriptManager(std::make_unique<WebInjectedScriptManager>(*this, WebInjectedScriptHost::create()))
84     , m_runtimeAgent(nullptr)
85 {
86     auto runtimeAgent = std::make_unique<WorkerRuntimeAgent>(m_injectedScriptManager.get(), &workerGlobalScope);
87     m_runtimeAgent = runtimeAgent.get();
88     m_instrumentingAgents->setWorkerRuntimeAgent(m_runtimeAgent);
89     m_agents.append(WTF::move(runtimeAgent));
90
91     auto consoleAgent = std::make_unique<WorkerConsoleAgent>(m_injectedScriptManager.get());
92     m_instrumentingAgents->setWebConsoleAgent(consoleAgent.get());
93
94     auto debuggerAgent = std::make_unique<WorkerDebuggerAgent>(m_injectedScriptManager.get(), m_instrumentingAgents.get(), &workerGlobalScope);
95     m_runtimeAgent->setScriptDebugServer(&debuggerAgent->scriptDebugServer());
96     m_agents.append(WTF::move(debuggerAgent));
97
98     auto profilerAgent = std::make_unique<WorkerProfilerAgent>(m_instrumentingAgents.get(), &workerGlobalScope);
99     profilerAgent->setScriptDebugServer(&debuggerAgent->scriptDebugServer());
100     m_agents.append(WTF::move(profilerAgent));
101
102     m_agents.append(std::make_unique<InspectorTimelineAgent>(m_instrumentingAgents.get(), nullptr, InspectorTimelineAgent::WorkerInspector, nullptr));
103     m_agents.append(WTF::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 void WorkerInspectorController::resume()
149 {
150     ErrorString unused;
151     m_runtimeAgent->run(&unused);
152 }
153
154 InspectorFunctionCallHandler WorkerInspectorController::functionCallHandler() const
155 {
156     return WebCore::functionCallHandlerFromAnyThread;
157 }
158
159 InspectorEvaluateHandler WorkerInspectorController::evaluateHandler() const
160 {
161     return WebCore::evaluateHandlerFromAnyThread;
162 }
163
164 void WorkerInspectorController::willCallInjectedScriptFunction(JSC::ExecState* scriptState, const String& scriptName, int scriptLine)
165 {
166     ScriptExecutionContext* scriptExecutionContext = scriptExecutionContextFromExecState(scriptState);
167     InspectorInstrumentationCookie cookie = InspectorInstrumentation::willCallFunction(scriptExecutionContext, scriptName, scriptLine);
168     m_injectedScriptInstrumentationCookies.append(cookie);
169 }
170
171 void WorkerInspectorController::didCallInjectedScriptFunction(JSC::ExecState* scriptState)
172 {
173     ASSERT(!m_injectedScriptInstrumentationCookies.isEmpty());
174     ScriptExecutionContext* scriptExecutionContext = scriptExecutionContextFromExecState(scriptState);
175     InspectorInstrumentationCookie cookie = m_injectedScriptInstrumentationCookies.takeLast();
176     InspectorInstrumentation::didCallFunction(cookie, scriptExecutionContext);
177 }
178
179 } // namespace WebCore
180
181 #endif // ENABLE(INSPECTOR)