InspectorWorkerAgent.cpp 8.4 KB

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