d467ef886cd5a1aa31ee766ee6fc7f288a86e94f
[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::connectToWorker(ErrorString* error, int workerId)
162 {
163     WorkerFrontendChannel* channel = m_idToChannel.get(workerId);
164     if (channel)
165         channel->connectToWorkerContext();
166     else
167         *error = "Worker is gone";
168 }
169
170 void InspectorWorkerAgent::disconnectFromWorker(ErrorString* error, int workerId)
171 {
172     WorkerFrontendChannel* channel = m_idToChannel.get(workerId);
173     if (channel)
174         channel->disconnectFromWorkerContext();
175     else
176         *error = "Worker is gone";
177 }
178
179 void InspectorWorkerAgent::sendMessageToWorker(ErrorString* error, int workerId, const RefPtr<InspectorObject>& message)
180 {
181     WorkerFrontendChannel* channel = m_idToChannel.get(workerId);
182     if (channel)
183         channel->proxy()->sendMessageToInspector(message->toJSONString());
184     else
185         *error = "Worker is gone";
186 }
187
188 void InspectorWorkerAgent::setAutoconnectToWorkers(ErrorString*, bool value)
189 {
190     m_state->setBoolean(WorkerAgentState::autoconnectToWorkers, value);
191 }
192
193 bool InspectorWorkerAgent::shouldPauseDedicatedWorkerOnStart()
194 {
195     return m_state->getBoolean(WorkerAgentState::autoconnectToWorkers);
196 }
197
198 void InspectorWorkerAgent::didStartWorkerContext(WorkerContextProxy* workerContextProxy, const KURL& url)
199 {
200     m_dedicatedWorkers.set(workerContextProxy, url.string());
201     if (m_inspectorFrontend && m_state->getBoolean(WorkerAgentState::workerInspectionEnabled))
202         createWorkerFrontendChannel(workerContextProxy, url.string());
203 }
204
205 void InspectorWorkerAgent::workerContextTerminated(WorkerContextProxy* proxy)
206 {
207     m_dedicatedWorkers.remove(proxy);
208     for (WorkerChannels::iterator it = m_idToChannel.begin(); it != m_idToChannel.end(); ++it) {
209         if (proxy == it->value->proxy()) {
210             m_inspectorFrontend->worker()->workerTerminated(it->key);
211             delete it->value;
212             m_idToChannel.remove(it);
213             return;
214         }
215     }
216 }
217
218 void InspectorWorkerAgent::createWorkerFrontendChannelsForExistingWorkers()
219 {
220     for (DedicatedWorkers::iterator it = m_dedicatedWorkers.begin(); it != m_dedicatedWorkers.end(); ++it)
221         createWorkerFrontendChannel(it->key, it->value);
222 }
223
224 void InspectorWorkerAgent::destroyWorkerFrontendChannels()
225 {
226     for (WorkerChannels::iterator it = m_idToChannel.begin(); it != m_idToChannel.end(); ++it) {
227         it->value->disconnectFromWorkerContext();
228         delete it->value;
229     }
230     m_idToChannel.clear();
231 }
232
233 void InspectorWorkerAgent::createWorkerFrontendChannel(WorkerContextProxy* workerContextProxy, const String& url)
234 {
235     WorkerFrontendChannel* channel = new WorkerFrontendChannel(m_inspectorFrontend, workerContextProxy);
236     m_idToChannel.set(channel->id(), channel);
237
238     ASSERT(m_inspectorFrontend);
239     bool autoconnectToWorkers = m_state->getBoolean(WorkerAgentState::autoconnectToWorkers);
240     if (autoconnectToWorkers)
241         channel->connectToWorkerContext();
242     m_inspectorFrontend->worker()->workerCreated(channel->id(), url, autoconnectToWorkers);
243 }
244
245 } // namespace WebCore
246
247 #endif // ENABLE(WORKERS) && ENABLE(INSPECTOR)