Run clang-modernize and let it add a bunch of missing overrides in WebCore/inspector
[WebKit-https.git] / Source / WebCore / inspector / InspectorWorkerAgent.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 "InspectorWorkerAgent.h"
36
37 #include "InspectorForwarding.h"
38 #include "InspectorWebFrontendDispatchers.h"
39 #include "InstrumentingAgents.h"
40 #include "URL.h"
41 #include "WorkerGlobalScopeProxy.h"
42 #include <inspector/InspectorValues.h>
43 #include <wtf/PassOwnPtr.h>
44 #include <wtf/RefPtr.h>
45
46 using namespace Inspector;
47
48 namespace WebCore {
49
50 class InspectorWorkerAgent::WorkerFrontendChannel : public WorkerGlobalScopeProxy::PageInspector {
51     WTF_MAKE_FAST_ALLOCATED;
52 public:
53     explicit WorkerFrontendChannel(InspectorWorkerFrontendDispatcher* frontendDispatcher, WorkerGlobalScopeProxy* proxy)
54         : m_frontendDispatcher(frontendDispatcher)
55         , m_proxy(proxy)
56         , m_id(s_nextId++)
57         , m_connected(false)
58     {
59     }
60     virtual ~WorkerFrontendChannel()
61     {
62         disconnectFromWorkerGlobalScope();
63     }
64
65     int id() const { return m_id; }
66     WorkerGlobalScopeProxy* proxy() const { return m_proxy; }
67
68     void connectToWorkerGlobalScope()
69     {
70         if (m_connected)
71             return;
72         m_connected = true;
73         m_proxy->connectToInspector(this);
74     }
75
76     void disconnectFromWorkerGlobalScope()
77     {
78         if (!m_connected)
79             return;
80         m_connected = false;
81         m_proxy->disconnectFromInspector();
82     }
83
84 private:
85     // WorkerGlobalScopeProxy::PageInspector implementation
86     virtual void dispatchMessageFromWorker(const String& message) override
87     {
88         RefPtr<InspectorValue> value = InspectorValue::parseJSON(message);
89         if (!value)
90             return;
91         RefPtr<InspectorObject> messageObject = value->asObject();
92         if (!messageObject)
93             return;
94         m_frontendDispatcher->dispatchMessageFromWorker(m_id, messageObject);
95     }
96
97     InspectorWorkerFrontendDispatcher* m_frontendDispatcher;
98     WorkerGlobalScopeProxy* m_proxy;
99     int m_id;
100     bool m_connected;
101     static int s_nextId;
102 };
103
104 int InspectorWorkerAgent::WorkerFrontendChannel::s_nextId = 1;
105
106 InspectorWorkerAgent::InspectorWorkerAgent(InstrumentingAgents* instrumentingAgents)
107     : InspectorAgentBase(ASCIILiteral("Worker"), instrumentingAgents)
108     , m_enabled(false)
109     , m_shouldPauseDedicatedWorkerOnStart(false)
110 {
111     m_instrumentingAgents->setInspectorWorkerAgent(this);
112 }
113
114 InspectorWorkerAgent::~InspectorWorkerAgent()
115 {
116     m_instrumentingAgents->setInspectorWorkerAgent(0);
117 }
118
119 void InspectorWorkerAgent::didCreateFrontendAndBackend(Inspector::InspectorFrontendChannel* frontendChannel, InspectorBackendDispatcher* backendDispatcher)
120 {
121     m_frontendDispatcher = std::make_unique<InspectorWorkerFrontendDispatcher>(frontendChannel);
122     m_backendDispatcher = InspectorWorkerBackendDispatcher::create(backendDispatcher, this);
123 }
124
125 void InspectorWorkerAgent::willDestroyFrontendAndBackend()
126 {
127     m_shouldPauseDedicatedWorkerOnStart = false;
128     disable(nullptr);
129
130     m_frontendDispatcher = nullptr;
131     m_backendDispatcher.clear();
132 }
133
134 void InspectorWorkerAgent::enable(ErrorString*)
135 {
136     m_enabled = true;
137     if (!m_frontendDispatcher)
138         return;
139
140     createWorkerFrontendChannelsForExistingWorkers();
141 }
142
143 void InspectorWorkerAgent::disable(ErrorString*)
144 {
145     m_enabled = false;
146     if (!m_frontendDispatcher)
147         return;
148
149     destroyWorkerFrontendChannels();
150 }
151
152 void InspectorWorkerAgent::canInspectWorkers(ErrorString*, bool* result)
153 {
154     *result = true;
155 }
156
157 void InspectorWorkerAgent::connectToWorker(ErrorString* error, int workerId)
158 {
159     WorkerFrontendChannel* channel = m_idToChannel.get(workerId);
160     if (channel)
161         channel->connectToWorkerGlobalScope();
162     else
163         *error = "Worker is gone";
164 }
165
166 void InspectorWorkerAgent::disconnectFromWorker(ErrorString* error, int workerId)
167 {
168     WorkerFrontendChannel* channel = m_idToChannel.get(workerId);
169     if (channel)
170         channel->disconnectFromWorkerGlobalScope();
171     else
172         *error = "Worker is gone";
173 }
174
175 void InspectorWorkerAgent::sendMessageToWorker(ErrorString* error, int workerId, const RefPtr<InspectorObject>& message)
176 {
177     WorkerFrontendChannel* channel = m_idToChannel.get(workerId);
178     if (channel)
179         channel->proxy()->sendMessageToInspector(message->toJSONString());
180     else
181         *error = "Worker is gone";
182 }
183
184 void InspectorWorkerAgent::setAutoconnectToWorkers(ErrorString*, bool value)
185 {
186     m_shouldPauseDedicatedWorkerOnStart = value;
187 }
188
189 bool InspectorWorkerAgent::shouldPauseDedicatedWorkerOnStart() const
190 {
191     return m_shouldPauseDedicatedWorkerOnStart;
192 }
193
194 void InspectorWorkerAgent::didStartWorkerGlobalScope(WorkerGlobalScopeProxy* workerGlobalScopeProxy, const URL& url)
195 {
196     m_dedicatedWorkers.set(workerGlobalScopeProxy, url.string());
197     if (m_frontendDispatcher && m_enabled)
198         createWorkerFrontendChannel(workerGlobalScopeProxy, url.string());
199 }
200
201 void InspectorWorkerAgent::workerGlobalScopeTerminated(WorkerGlobalScopeProxy* proxy)
202 {
203     m_dedicatedWorkers.remove(proxy);
204     for (WorkerChannels::iterator it = m_idToChannel.begin(); it != m_idToChannel.end(); ++it) {
205         if (proxy == it->value->proxy()) {
206             m_frontendDispatcher->workerTerminated(it->key);
207             delete it->value;
208             m_idToChannel.remove(it);
209             return;
210         }
211     }
212 }
213
214 void InspectorWorkerAgent::createWorkerFrontendChannelsForExistingWorkers()
215 {
216     for (DedicatedWorkers::iterator it = m_dedicatedWorkers.begin(); it != m_dedicatedWorkers.end(); ++it)
217         createWorkerFrontendChannel(it->key, it->value);
218 }
219
220 void InspectorWorkerAgent::destroyWorkerFrontendChannels()
221 {
222     for (WorkerChannels::iterator it = m_idToChannel.begin(); it != m_idToChannel.end(); ++it) {
223         it->value->disconnectFromWorkerGlobalScope();
224         delete it->value;
225     }
226     m_idToChannel.clear();
227 }
228
229 void InspectorWorkerAgent::createWorkerFrontendChannel(WorkerGlobalScopeProxy* workerGlobalScopeProxy, const String& url)
230 {
231     WorkerFrontendChannel* channel = new WorkerFrontendChannel(m_frontendDispatcher.get(), workerGlobalScopeProxy);
232     m_idToChannel.set(channel->id(), channel);
233
234     ASSERT(m_frontendDispatcher);
235     if (m_shouldPauseDedicatedWorkerOnStart)
236         channel->connectToWorkerGlobalScope();
237     m_frontendDispatcher->workerCreated(channel->id(), url, m_shouldPauseDedicatedWorkerOnStart);
238 }
239
240 } // namespace WebCore
241
242 #endif // ENABLE(INSPECTOR)