Web Inspector: convert workers inspection into capability
[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(WORKERS) && ENABLE(INSPECTOR)
34
35 #include "InspectorWorkerAgent.h"
36
37 #include "InspectorFrontend.h"
38 #include "InspectorFrontendChannel.h"
39 #include "InspectorState.h"
40 #include "InspectorValues.h"
41 #include "InstrumentingAgents.h"
42 #include "KURL.h"
43 #include "WorkerContextProxy.h"
44 #include <wtf/PassOwnPtr.h>
45 #include <wtf/RefPtr.h>
46
47 namespace WebCore {
48
49 namespace WorkerAgentState {
50 static const char workerInspectionEnabled[] = "workerInspectionEnabled";
51 static const char autoconnectToWorkers[] = "autoconnectToWorkers";
52 };
53
54 class InspectorWorkerAgent::WorkerFrontendChannel : public WorkerContextProxy::PageInspector {
55     WTF_MAKE_FAST_ALLOCATED;
56 public:
57     explicit WorkerFrontendChannel(InspectorFrontend* frontend, WorkerContextProxy* proxy)
58         : m_frontend(frontend)
59         , m_proxy(proxy)
60         , m_id(s_nextId++)
61         , m_connected(false)
62     {
63     }
64     virtual ~WorkerFrontendChannel()
65     {
66         disconnectFromWorkerContext();
67     }
68
69     int id() const { return m_id; }
70     WorkerContextProxy* proxy() const { return m_proxy; }
71
72     void connectToWorkerContext()
73     {
74         if (m_connected)
75             return;
76         m_connected = true;
77         m_proxy->connectToInspector(this);
78     }
79
80     void disconnectFromWorkerContext()
81     {
82         if (!m_connected)
83             return;
84         m_connected = false;
85         m_proxy->disconnectFromInspector();
86     }
87
88 private:
89     // WorkerContextProxy::PageInspector implementation
90     virtual void dispatchMessageFromWorker(const String& message)
91     {
92         RefPtr<InspectorValue> value = InspectorValue::parseJSON(message);
93         if (!value)
94             return;
95         RefPtr<InspectorObject> messageObject = value->asObject();
96         if (!messageObject)
97             return;
98         m_frontend->worker()->dispatchMessageFromWorker(m_id, messageObject);
99     }
100
101     InspectorFrontend* m_frontend;
102     WorkerContextProxy* m_proxy;
103     int m_id;
104     bool m_connected;
105     static int s_nextId;
106 };
107
108 int InspectorWorkerAgent::WorkerFrontendChannel::s_nextId = 1;
109
110 PassOwnPtr<InspectorWorkerAgent> InspectorWorkerAgent::create(InstrumentingAgents* instrumentingAgents, InspectorCompositeState* inspectorState)
111 {
112     return adoptPtr(new InspectorWorkerAgent(instrumentingAgents, inspectorState));
113 }
114
115 InspectorWorkerAgent::InspectorWorkerAgent(InstrumentingAgents* instrumentingAgents, InspectorCompositeState* inspectorState)
116     : InspectorBaseAgent<InspectorWorkerAgent>("Worker", instrumentingAgents, inspectorState)
117     , m_inspectorFrontend(0)
118 {
119     m_instrumentingAgents->setInspectorWorkerAgent(this);
120 }
121
122 InspectorWorkerAgent::~InspectorWorkerAgent()
123 {
124     m_instrumentingAgents->setInspectorWorkerAgent(0);
125 }
126
127 void InspectorWorkerAgent::setFrontend(InspectorFrontend* frontend)
128 {
129     m_inspectorFrontend = frontend;
130 }
131
132 void InspectorWorkerAgent::restore()
133 {
134     if (m_state->getBoolean(WorkerAgentState::workerInspectionEnabled))
135         createWorkerFrontendChannelsForExistingWorkers();
136 }
137
138 void InspectorWorkerAgent::clearFrontend()
139 {
140     m_state->setBoolean(WorkerAgentState::autoconnectToWorkers, false);
141     disable(0);
142     m_inspectorFrontend = 0;
143 }
144
145 void InspectorWorkerAgent::enable(ErrorString*)
146 {
147     m_state->setBoolean(WorkerAgentState::workerInspectionEnabled, true);
148     if (!m_inspectorFrontend)
149         return;
150     createWorkerFrontendChannelsForExistingWorkers();
151 }
152
153 void InspectorWorkerAgent::disable(ErrorString*)
154 {
155     m_state->setBoolean(WorkerAgentState::workerInspectionEnabled, false);
156     if (!m_inspectorFrontend)
157         return;
158     destroyWorkerFrontendChannels();
159 }
160
161 void InspectorWorkerAgent::canInspectWorkers(ErrorString*, bool* result)
162 {
163 #if ENABLE(WORKERS)
164     *result = true;
165 #else
166     *result = false;
167 #endif
168 }
169
170 void InspectorWorkerAgent::connectToWorker(ErrorString* error, int workerId)
171 {
172     WorkerFrontendChannel* channel = m_idToChannel.get(workerId);
173     if (channel)
174         channel->connectToWorkerContext();
175     else
176         *error = "Worker is gone";
177 }
178
179 void InspectorWorkerAgent::disconnectFromWorker(ErrorString* error, int workerId)
180 {
181     WorkerFrontendChannel* channel = m_idToChannel.get(workerId);
182     if (channel)
183         channel->disconnectFromWorkerContext();
184     else
185         *error = "Worker is gone";
186 }
187
188 void InspectorWorkerAgent::sendMessageToWorker(ErrorString* error, int workerId, const RefPtr<InspectorObject>& message)
189 {
190     WorkerFrontendChannel* channel = m_idToChannel.get(workerId);
191     if (channel)
192         channel->proxy()->sendMessageToInspector(message->toJSONString());
193     else
194         *error = "Worker is gone";
195 }
196
197 void InspectorWorkerAgent::setAutoconnectToWorkers(ErrorString*, bool value)
198 {
199     m_state->setBoolean(WorkerAgentState::autoconnectToWorkers, value);
200 }
201
202 bool InspectorWorkerAgent::shouldPauseDedicatedWorkerOnStart()
203 {
204     return m_state->getBoolean(WorkerAgentState::autoconnectToWorkers);
205 }
206
207 void InspectorWorkerAgent::didStartWorkerContext(WorkerContextProxy* workerContextProxy, const KURL& url)
208 {
209     m_dedicatedWorkers.set(workerContextProxy, url.string());
210     if (m_inspectorFrontend && m_state->getBoolean(WorkerAgentState::workerInspectionEnabled))
211         createWorkerFrontendChannel(workerContextProxy, url.string());
212 }
213
214 void InspectorWorkerAgent::workerContextTerminated(WorkerContextProxy* proxy)
215 {
216     m_dedicatedWorkers.remove(proxy);
217     for (WorkerChannels::iterator it = m_idToChannel.begin(); it != m_idToChannel.end(); ++it) {
218         if (proxy == it->value->proxy()) {
219             m_inspectorFrontend->worker()->workerTerminated(it->key);
220             delete it->value;
221             m_idToChannel.remove(it);
222             return;
223         }
224     }
225 }
226
227 void InspectorWorkerAgent::createWorkerFrontendChannelsForExistingWorkers()
228 {
229     for (DedicatedWorkers::iterator it = m_dedicatedWorkers.begin(); it != m_dedicatedWorkers.end(); ++it)
230         createWorkerFrontendChannel(it->key, it->value);
231 }
232
233 void InspectorWorkerAgent::destroyWorkerFrontendChannels()
234 {
235     for (WorkerChannels::iterator it = m_idToChannel.begin(); it != m_idToChannel.end(); ++it) {
236         it->value->disconnectFromWorkerContext();
237         delete it->value;
238     }
239     m_idToChannel.clear();
240 }
241
242 void InspectorWorkerAgent::createWorkerFrontendChannel(WorkerContextProxy* workerContextProxy, const String& url)
243 {
244     WorkerFrontendChannel* channel = new WorkerFrontendChannel(m_inspectorFrontend, workerContextProxy);
245     m_idToChannel.set(channel->id(), channel);
246
247     ASSERT(m_inspectorFrontend);
248     bool autoconnectToWorkers = m_state->getBoolean(WorkerAgentState::autoconnectToWorkers);
249     if (autoconnectToWorkers)
250         channel->connectToWorkerContext();
251     m_inspectorFrontend->worker()->workerCreated(channel->id(), url, autoconnectToWorkers);
252 }
253
254 } // namespace WebCore
255
256 #endif // ENABLE(WORKERS) && ENABLE(INSPECTOR)