WorkerInspectorController.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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(INSPECTOR) && ENABLE(WORKERS)
  32. #include "WorkerInspectorController.h"
  33. #include "InjectedScriptHost.h"
  34. #include "InjectedScriptManager.h"
  35. #include "InspectorBackendDispatcher.h"
  36. #include "InspectorClient.h"
  37. #include "InspectorConsoleAgent.h"
  38. #include "InspectorFrontend.h"
  39. #include "InspectorFrontendChannel.h"
  40. #include "InspectorHeapProfilerAgent.h"
  41. #include "InspectorProfilerAgent.h"
  42. #include "InspectorState.h"
  43. #include "InspectorStateClient.h"
  44. #include "InspectorTimelineAgent.h"
  45. #include "InstrumentingAgents.h"
  46. #include "WorkerConsoleAgent.h"
  47. #include "WorkerContext.h"
  48. #include "WorkerDebuggerAgent.h"
  49. #include "WorkerReportingProxy.h"
  50. #include "WorkerRuntimeAgent.h"
  51. #include "WorkerThread.h"
  52. #include <wtf/PassOwnPtr.h>
  53. namespace WebCore {
  54. namespace {
  55. class PageInspectorProxy : public InspectorFrontendChannel {
  56. WTF_MAKE_FAST_ALLOCATED;
  57. public:
  58. explicit PageInspectorProxy(WorkerContext* workerContext) : m_workerContext(workerContext) { }
  59. virtual ~PageInspectorProxy() { }
  60. private:
  61. virtual bool sendMessageToFrontend(const String& message)
  62. {
  63. m_workerContext->thread()->workerReportingProxy().postMessageToPageInspector(message);
  64. return true;
  65. }
  66. WorkerContext* m_workerContext;
  67. };
  68. class WorkerStateClient : public InspectorStateClient {
  69. WTF_MAKE_FAST_ALLOCATED;
  70. public:
  71. WorkerStateClient(WorkerContext* context) : m_workerContext(context) { }
  72. virtual ~WorkerStateClient() { }
  73. private:
  74. virtual bool supportsInspectorStateUpdates() const { return true; }
  75. virtual void updateInspectorStateCookie(const String& cookie)
  76. {
  77. m_workerContext->thread()->workerReportingProxy().updateInspectorStateCookie(cookie);
  78. }
  79. WorkerContext* m_workerContext;
  80. };
  81. }
  82. WorkerInspectorController::WorkerInspectorController(WorkerContext* workerContext)
  83. : m_workerContext(workerContext)
  84. , m_stateClient(adoptPtr(new WorkerStateClient(workerContext)))
  85. , m_state(adoptPtr(new InspectorCompositeState(m_stateClient.get())))
  86. , m_instrumentingAgents(InstrumentingAgents::create())
  87. , m_injectedScriptManager(InjectedScriptManager::createForWorker())
  88. , m_runtimeAgent(0)
  89. {
  90. OwnPtr<InspectorRuntimeAgent> runtimeAgent = WorkerRuntimeAgent::create(m_instrumentingAgents.get(), m_state.get(), m_injectedScriptManager.get(), workerContext);
  91. m_runtimeAgent = runtimeAgent.get();
  92. m_agents.append(runtimeAgent.release());
  93. OwnPtr<InspectorConsoleAgent> consoleAgent = WorkerConsoleAgent::create(m_instrumentingAgents.get(), m_state.get(), m_injectedScriptManager.get());
  94. #if ENABLE(JAVASCRIPT_DEBUGGER)
  95. OwnPtr<InspectorDebuggerAgent> debuggerAgent = WorkerDebuggerAgent::create(m_instrumentingAgents.get(), m_state.get(), workerContext, m_injectedScriptManager.get());
  96. InspectorDebuggerAgent* debuggerAgentPtr = debuggerAgent.get();
  97. m_runtimeAgent->setScriptDebugServer(&debuggerAgent->scriptDebugServer());
  98. m_agents.append(debuggerAgent.release());
  99. m_agents.append(InspectorProfilerAgent::create(m_instrumentingAgents.get(), consoleAgent.get(), workerContext, m_state.get(), m_injectedScriptManager.get()));
  100. m_agents.append(InspectorHeapProfilerAgent::create(m_instrumentingAgents.get(), m_state.get(), m_injectedScriptManager.get()));
  101. #endif
  102. m_agents.append(InspectorTimelineAgent::create(m_instrumentingAgents.get(), 0, 0, m_state.get(), InspectorTimelineAgent::WorkerInspector, 0));
  103. m_agents.append(consoleAgent.release());
  104. m_injectedScriptManager->injectedScriptHost()->init(0
  105. , 0
  106. #if ENABLE(SQL_DATABASE)
  107. , 0
  108. #endif
  109. , 0
  110. , 0
  111. #if ENABLE(JAVASCRIPT_DEBUGGER)
  112. , debuggerAgentPtr
  113. #endif
  114. );
  115. }
  116. WorkerInspectorController::~WorkerInspectorController()
  117. {
  118. m_instrumentingAgents->reset();
  119. disconnectFrontend();
  120. }
  121. void WorkerInspectorController::connectFrontend()
  122. {
  123. ASSERT(!m_frontend);
  124. m_state->unmute();
  125. m_frontendChannel = adoptPtr(new PageInspectorProxy(m_workerContext));
  126. m_frontend = adoptPtr(new InspectorFrontend(m_frontendChannel.get()));
  127. m_backendDispatcher = InspectorBackendDispatcher::create(m_frontendChannel.get());
  128. m_agents.registerInDispatcher(m_backendDispatcher.get());
  129. m_agents.setFrontend(m_frontend.get());
  130. }
  131. void WorkerInspectorController::disconnectFrontend()
  132. {
  133. if (!m_frontend)
  134. return;
  135. m_backendDispatcher->clearFrontend();
  136. m_backendDispatcher.clear();
  137. // Destroying agents would change the state, but we don't want that.
  138. // Pre-disconnect state will be used to restore inspector agents.
  139. m_state->mute();
  140. m_agents.clearFrontend();
  141. m_frontend.clear();
  142. m_frontendChannel.clear();
  143. }
  144. void WorkerInspectorController::restoreInspectorStateFromCookie(const String& inspectorCookie)
  145. {
  146. ASSERT(!m_frontend);
  147. connectFrontend();
  148. m_state->loadFromCookie(inspectorCookie);
  149. m_agents.restore();
  150. }
  151. void WorkerInspectorController::dispatchMessageFromFrontend(const String& message)
  152. {
  153. if (m_backendDispatcher)
  154. m_backendDispatcher->dispatch(message);
  155. }
  156. #if ENABLE(JAVASCRIPT_DEBUGGER)
  157. void WorkerInspectorController::resume()
  158. {
  159. ErrorString unused;
  160. m_runtimeAgent->run(&unused);
  161. }
  162. #endif
  163. }
  164. #endif