| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2011 Google Inc. All rights reserved. | 2 * Copyright (C) 2011 Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
| 6 * met: | 6 * met: |
| 7 * | 7 * |
| 8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
| (...skipping 19 matching lines...) Expand all Loading... |
| 30 | 30 |
| 31 #include "config.h" | 31 #include "config.h" |
| 32 | 32 |
| 33 #include "core/inspector/InspectorWorkerAgent.h" | 33 #include "core/inspector/InspectorWorkerAgent.h" |
| 34 | 34 |
| 35 #include "InspectorFrontend.h" | 35 #include "InspectorFrontend.h" |
| 36 #include "core/inspector/InspectorState.h" | 36 #include "core/inspector/InspectorState.h" |
| 37 #include "core/inspector/InstrumentingAgents.h" | 37 #include "core/inspector/InstrumentingAgents.h" |
| 38 #include "core/inspector/JSONParser.h" | 38 #include "core/inspector/JSONParser.h" |
| 39 #include "core/platform/JSONValues.h" | 39 #include "core/platform/JSONValues.h" |
| 40 #include "core/workers/WorkerContextProxy.h" | 40 #include "core/workers/WorkerGlobalScopeProxy.h" |
| 41 #include "weborigin/KURL.h" | 41 #include "weborigin/KURL.h" |
| 42 #include "wtf/PassOwnPtr.h" | 42 #include "wtf/PassOwnPtr.h" |
| 43 #include "wtf/RefPtr.h" | 43 #include "wtf/RefPtr.h" |
| 44 | 44 |
| 45 namespace WebCore { | 45 namespace WebCore { |
| 46 | 46 |
| 47 namespace WorkerAgentState { | 47 namespace WorkerAgentState { |
| 48 static const char workerInspectionEnabled[] = "workerInspectionEnabled"; | 48 static const char workerInspectionEnabled[] = "workerInspectionEnabled"; |
| 49 static const char autoconnectToWorkers[] = "autoconnectToWorkers"; | 49 static const char autoconnectToWorkers[] = "autoconnectToWorkers"; |
| 50 }; | 50 }; |
| 51 | 51 |
| 52 class InspectorWorkerAgent::WorkerFrontendChannel : public WorkerContextProxy::P
ageInspector { | 52 class InspectorWorkerAgent::WorkerFrontendChannel : public WorkerGlobalScopeProx
y::PageInspector { |
| 53 WTF_MAKE_FAST_ALLOCATED; | 53 WTF_MAKE_FAST_ALLOCATED; |
| 54 public: | 54 public: |
| 55 explicit WorkerFrontendChannel(InspectorFrontend* frontend, WorkerContextPro
xy* proxy) | 55 explicit WorkerFrontendChannel(InspectorFrontend* frontend, WorkerGlobalScop
eProxy* proxy) |
| 56 : m_frontend(frontend) | 56 : m_frontend(frontend) |
| 57 , m_proxy(proxy) | 57 , m_proxy(proxy) |
| 58 , m_id(s_nextId++) | 58 , m_id(s_nextId++) |
| 59 , m_connected(false) | 59 , m_connected(false) |
| 60 { | 60 { |
| 61 } | 61 } |
| 62 virtual ~WorkerFrontendChannel() | 62 virtual ~WorkerFrontendChannel() |
| 63 { | 63 { |
| 64 disconnectFromWorkerContext(); | 64 disconnectFromWorkerGlobalScope(); |
| 65 } | 65 } |
| 66 | 66 |
| 67 int id() const { return m_id; } | 67 int id() const { return m_id; } |
| 68 WorkerContextProxy* proxy() const { return m_proxy; } | 68 WorkerGlobalScopeProxy* proxy() const { return m_proxy; } |
| 69 | 69 |
| 70 void connectToWorkerContext() | 70 void connectToWorkerGlobalScope() |
| 71 { | 71 { |
| 72 if (m_connected) | 72 if (m_connected) |
| 73 return; | 73 return; |
| 74 m_connected = true; | 74 m_connected = true; |
| 75 m_proxy->connectToInspector(this); | 75 m_proxy->connectToInspector(this); |
| 76 } | 76 } |
| 77 | 77 |
| 78 void disconnectFromWorkerContext() | 78 void disconnectFromWorkerGlobalScope() |
| 79 { | 79 { |
| 80 if (!m_connected) | 80 if (!m_connected) |
| 81 return; | 81 return; |
| 82 m_connected = false; | 82 m_connected = false; |
| 83 m_proxy->disconnectFromInspector(); | 83 m_proxy->disconnectFromInspector(); |
| 84 } | 84 } |
| 85 | 85 |
| 86 private: | 86 private: |
| 87 // WorkerContextProxy::PageInspector implementation | 87 // WorkerGlobalScopeProxy::PageInspector implementation |
| 88 virtual void dispatchMessageFromWorker(const String& message) | 88 virtual void dispatchMessageFromWorker(const String& message) |
| 89 { | 89 { |
| 90 RefPtr<JSONValue> value = parseJSON(message); | 90 RefPtr<JSONValue> value = parseJSON(message); |
| 91 if (!value) | 91 if (!value) |
| 92 return; | 92 return; |
| 93 RefPtr<JSONObject> messageObject = value->asObject(); | 93 RefPtr<JSONObject> messageObject = value->asObject(); |
| 94 if (!messageObject) | 94 if (!messageObject) |
| 95 return; | 95 return; |
| 96 m_frontend->worker()->dispatchMessageFromWorker(m_id, messageObject); | 96 m_frontend->worker()->dispatchMessageFromWorker(m_id, messageObject); |
| 97 } | 97 } |
| 98 | 98 |
| 99 InspectorFrontend* m_frontend; | 99 InspectorFrontend* m_frontend; |
| 100 WorkerContextProxy* m_proxy; | 100 WorkerGlobalScopeProxy* m_proxy; |
| 101 int m_id; | 101 int m_id; |
| 102 bool m_connected; | 102 bool m_connected; |
| 103 static int s_nextId; | 103 static int s_nextId; |
| 104 }; | 104 }; |
| 105 | 105 |
| 106 int InspectorWorkerAgent::WorkerFrontendChannel::s_nextId = 1; | 106 int InspectorWorkerAgent::WorkerFrontendChannel::s_nextId = 1; |
| 107 | 107 |
| 108 PassOwnPtr<InspectorWorkerAgent> InspectorWorkerAgent::create(InstrumentingAgent
s* instrumentingAgents, InspectorCompositeState* inspectorState) | 108 PassOwnPtr<InspectorWorkerAgent> InspectorWorkerAgent::create(InstrumentingAgent
s* instrumentingAgents, InspectorCompositeState* inspectorState) |
| 109 { | 109 { |
| 110 return adoptPtr(new InspectorWorkerAgent(instrumentingAgents, inspectorState
)); | 110 return adoptPtr(new InspectorWorkerAgent(instrumentingAgents, inspectorState
)); |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 158 | 158 |
| 159 void InspectorWorkerAgent::canInspectWorkers(ErrorString*, bool* result) | 159 void InspectorWorkerAgent::canInspectWorkers(ErrorString*, bool* result) |
| 160 { | 160 { |
| 161 *result = true; | 161 *result = true; |
| 162 } | 162 } |
| 163 | 163 |
| 164 void InspectorWorkerAgent::connectToWorker(ErrorString* error, int workerId) | 164 void InspectorWorkerAgent::connectToWorker(ErrorString* error, int workerId) |
| 165 { | 165 { |
| 166 WorkerFrontendChannel* channel = m_idToChannel.get(workerId); | 166 WorkerFrontendChannel* channel = m_idToChannel.get(workerId); |
| 167 if (channel) | 167 if (channel) |
| 168 channel->connectToWorkerContext(); | 168 channel->connectToWorkerGlobalScope(); |
| 169 else | 169 else |
| 170 *error = "Worker is gone"; | 170 *error = "Worker is gone"; |
| 171 } | 171 } |
| 172 | 172 |
| 173 void InspectorWorkerAgent::disconnectFromWorker(ErrorString* error, int workerId
) | 173 void InspectorWorkerAgent::disconnectFromWorker(ErrorString* error, int workerId
) |
| 174 { | 174 { |
| 175 WorkerFrontendChannel* channel = m_idToChannel.get(workerId); | 175 WorkerFrontendChannel* channel = m_idToChannel.get(workerId); |
| 176 if (channel) | 176 if (channel) |
| 177 channel->disconnectFromWorkerContext(); | 177 channel->disconnectFromWorkerGlobalScope(); |
| 178 else | 178 else |
| 179 *error = "Worker is gone"; | 179 *error = "Worker is gone"; |
| 180 } | 180 } |
| 181 | 181 |
| 182 void InspectorWorkerAgent::sendMessageToWorker(ErrorString* error, int workerId,
const RefPtr<JSONObject>& message) | 182 void InspectorWorkerAgent::sendMessageToWorker(ErrorString* error, int workerId,
const RefPtr<JSONObject>& message) |
| 183 { | 183 { |
| 184 WorkerFrontendChannel* channel = m_idToChannel.get(workerId); | 184 WorkerFrontendChannel* channel = m_idToChannel.get(workerId); |
| 185 if (channel) | 185 if (channel) |
| 186 channel->proxy()->sendMessageToInspector(message->toJSONString()); | 186 channel->proxy()->sendMessageToInspector(message->toJSONString()); |
| 187 else | 187 else |
| 188 *error = "Worker is gone"; | 188 *error = "Worker is gone"; |
| 189 } | 189 } |
| 190 | 190 |
| 191 void InspectorWorkerAgent::setAutoconnectToWorkers(ErrorString*, bool value) | 191 void InspectorWorkerAgent::setAutoconnectToWorkers(ErrorString*, bool value) |
| 192 { | 192 { |
| 193 m_state->setBoolean(WorkerAgentState::autoconnectToWorkers, value); | 193 m_state->setBoolean(WorkerAgentState::autoconnectToWorkers, value); |
| 194 } | 194 } |
| 195 | 195 |
| 196 bool InspectorWorkerAgent::shouldPauseDedicatedWorkerOnStart() | 196 bool InspectorWorkerAgent::shouldPauseDedicatedWorkerOnStart() |
| 197 { | 197 { |
| 198 return m_state->getBoolean(WorkerAgentState::autoconnectToWorkers); | 198 return m_state->getBoolean(WorkerAgentState::autoconnectToWorkers); |
| 199 } | 199 } |
| 200 | 200 |
| 201 void InspectorWorkerAgent::didStartWorkerContext(WorkerContextProxy* workerConte
xtProxy, const KURL& url) | 201 void InspectorWorkerAgent::didStartWorkerGlobalScope(WorkerGlobalScopeProxy* wor
kerGlobalScopeProxy, const KURL& url) |
| 202 { | 202 { |
| 203 m_dedicatedWorkers.set(workerContextProxy, url.string()); | 203 m_dedicatedWorkers.set(workerGlobalScopeProxy, url.string()); |
| 204 if (m_inspectorFrontend && m_state->getBoolean(WorkerAgentState::workerInspe
ctionEnabled)) | 204 if (m_inspectorFrontend && m_state->getBoolean(WorkerAgentState::workerInspe
ctionEnabled)) |
| 205 createWorkerFrontendChannel(workerContextProxy, url.string()); | 205 createWorkerFrontendChannel(workerGlobalScopeProxy, url.string()); |
| 206 } | 206 } |
| 207 | 207 |
| 208 void InspectorWorkerAgent::workerContextTerminated(WorkerContextProxy* proxy) | 208 void InspectorWorkerAgent::workerGlobalScopeTerminated(WorkerGlobalScopeProxy* p
roxy) |
| 209 { | 209 { |
| 210 m_dedicatedWorkers.remove(proxy); | 210 m_dedicatedWorkers.remove(proxy); |
| 211 for (WorkerChannels::iterator it = m_idToChannel.begin(); it != m_idToChanne
l.end(); ++it) { | 211 for (WorkerChannels::iterator it = m_idToChannel.begin(); it != m_idToChanne
l.end(); ++it) { |
| 212 if (proxy == it->value->proxy()) { | 212 if (proxy == it->value->proxy()) { |
| 213 m_inspectorFrontend->worker()->workerTerminated(it->key); | 213 m_inspectorFrontend->worker()->workerTerminated(it->key); |
| 214 delete it->value; | 214 delete it->value; |
| 215 m_idToChannel.remove(it); | 215 m_idToChannel.remove(it); |
| 216 return; | 216 return; |
| 217 } | 217 } |
| 218 } | 218 } |
| 219 } | 219 } |
| 220 | 220 |
| 221 void InspectorWorkerAgent::createWorkerFrontendChannelsForExistingWorkers() | 221 void InspectorWorkerAgent::createWorkerFrontendChannelsForExistingWorkers() |
| 222 { | 222 { |
| 223 for (DedicatedWorkers::iterator it = m_dedicatedWorkers.begin(); it != m_ded
icatedWorkers.end(); ++it) | 223 for (DedicatedWorkers::iterator it = m_dedicatedWorkers.begin(); it != m_ded
icatedWorkers.end(); ++it) |
| 224 createWorkerFrontendChannel(it->key, it->value); | 224 createWorkerFrontendChannel(it->key, it->value); |
| 225 } | 225 } |
| 226 | 226 |
| 227 void InspectorWorkerAgent::destroyWorkerFrontendChannels() | 227 void InspectorWorkerAgent::destroyWorkerFrontendChannels() |
| 228 { | 228 { |
| 229 for (WorkerChannels::iterator it = m_idToChannel.begin(); it != m_idToChanne
l.end(); ++it) { | 229 for (WorkerChannels::iterator it = m_idToChannel.begin(); it != m_idToChanne
l.end(); ++it) { |
| 230 it->value->disconnectFromWorkerContext(); | 230 it->value->disconnectFromWorkerGlobalScope(); |
| 231 delete it->value; | 231 delete it->value; |
| 232 } | 232 } |
| 233 m_idToChannel.clear(); | 233 m_idToChannel.clear(); |
| 234 } | 234 } |
| 235 | 235 |
| 236 void InspectorWorkerAgent::createWorkerFrontendChannel(WorkerContextProxy* worke
rContextProxy, const String& url) | 236 void InspectorWorkerAgent::createWorkerFrontendChannel(WorkerGlobalScopeProxy* w
orkerGlobalScopeProxy, const String& url) |
| 237 { | 237 { |
| 238 WorkerFrontendChannel* channel = new WorkerFrontendChannel(m_inspectorFronte
nd, workerContextProxy); | 238 WorkerFrontendChannel* channel = new WorkerFrontendChannel(m_inspectorFronte
nd, workerGlobalScopeProxy); |
| 239 m_idToChannel.set(channel->id(), channel); | 239 m_idToChannel.set(channel->id(), channel); |
| 240 | 240 |
| 241 ASSERT(m_inspectorFrontend); | 241 ASSERT(m_inspectorFrontend); |
| 242 bool autoconnectToWorkers = m_state->getBoolean(WorkerAgentState::autoconnec
tToWorkers); | 242 bool autoconnectToWorkers = m_state->getBoolean(WorkerAgentState::autoconnec
tToWorkers); |
| 243 if (autoconnectToWorkers) | 243 if (autoconnectToWorkers) |
| 244 channel->connectToWorkerContext(); | 244 channel->connectToWorkerGlobalScope(); |
| 245 m_inspectorFrontend->worker()->workerCreated(channel->id(), url, autoconnect
ToWorkers); | 245 m_inspectorFrontend->worker()->workerCreated(channel->id(), url, autoconnect
ToWorkers); |
| 246 } | 246 } |
| 247 | 247 |
| 248 } // namespace WebCore | 248 } // namespace WebCore |
| OLD | NEW |