InspectorController.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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)
  32. #include "InspectorController.h"
  33. #include "DOMWrapperWorld.h"
  34. #include "Frame.h"
  35. #include "GraphicsContext.h"
  36. #include "IdentifiersFactory.h"
  37. #include "InjectedScriptHost.h"
  38. #include "InjectedScriptManager.h"
  39. #include "InspectorAgent.h"
  40. #include "InspectorApplicationCacheAgent.h"
  41. #include "InspectorBackendDispatcher.h"
  42. #include "InspectorBaseAgent.h"
  43. #include "InspectorCSSAgent.h"
  44. #include "InspectorCanvasAgent.h"
  45. #include "InspectorClient.h"
  46. #include "InspectorDOMAgent.h"
  47. #include "InspectorDOMDebuggerAgent.h"
  48. #include "InspectorDOMStorageAgent.h"
  49. #include "InspectorDatabaseAgent.h"
  50. #include "InspectorDebuggerAgent.h"
  51. #include "InspectorFileSystemAgent.h"
  52. #include "InspectorFrontend.h"
  53. #include "InspectorFrontendClient.h"
  54. #include "InspectorHeapProfilerAgent.h"
  55. #include "InspectorIndexedDBAgent.h"
  56. #include "InspectorInputAgent.h"
  57. #include "InspectorInstrumentation.h"
  58. #include "InspectorLayerTreeAgent.h"
  59. #include "InspectorMemoryAgent.h"
  60. #include "InspectorOverlay.h"
  61. #include "InspectorPageAgent.h"
  62. #include "InspectorProfilerAgent.h"
  63. #include "InspectorResourceAgent.h"
  64. #include "InspectorState.h"
  65. #include "InspectorTimelineAgent.h"
  66. #include "InspectorWorkerAgent.h"
  67. #include "InstrumentingAgents.h"
  68. #include "PageConsoleAgent.h"
  69. #include "PageDebuggerAgent.h"
  70. #include "PageRuntimeAgent.h"
  71. #include "Page.h"
  72. #include "ScriptObject.h"
  73. #include "Settings.h"
  74. #if PLATFORM(MANX) && OS(ORBIS)
  75. #include "InspectorManxAgent.h"
  76. #endif
  77. namespace WebCore {
  78. InspectorController::InspectorController(Page* page, InspectorClient* inspectorClient)
  79. : m_instrumentingAgents(InstrumentingAgents::create())
  80. , m_injectedScriptManager(InjectedScriptManager::createForPage())
  81. , m_state(adoptPtr(new InspectorCompositeState(inspectorClient)))
  82. , m_overlay(InspectorOverlay::create(page, inspectorClient))
  83. , m_page(page)
  84. , m_inspectorClient(inspectorClient)
  85. , m_isUnderTest(false)
  86. {
  87. OwnPtr<InspectorAgent> inspectorAgentPtr(InspectorAgent::create(page, m_injectedScriptManager.get(), m_instrumentingAgents.get(), m_state.get()));
  88. m_inspectorAgent = inspectorAgentPtr.get();
  89. m_agents.append(inspectorAgentPtr.release());
  90. OwnPtr<InspectorPageAgent> pageAgentPtr(InspectorPageAgent::create(m_instrumentingAgents.get(), page, m_inspectorAgent, m_state.get(), m_injectedScriptManager.get(), inspectorClient, m_overlay.get()));
  91. InspectorPageAgent* pageAgent = pageAgentPtr.get();
  92. m_pageAgent = pageAgentPtr.get();
  93. m_agents.append(pageAgentPtr.release());
  94. OwnPtr<InspectorDOMAgent> domAgentPtr(InspectorDOMAgent::create(m_instrumentingAgents.get(), pageAgent, m_state.get(), m_injectedScriptManager.get(), m_overlay.get(), inspectorClient));
  95. m_domAgent = domAgentPtr.get();
  96. m_agents.append(domAgentPtr.release());
  97. m_agents.append(InspectorCSSAgent::create(m_instrumentingAgents.get(), m_state.get(), m_domAgent));
  98. #if ENABLE(SQL_DATABASE)
  99. OwnPtr<InspectorDatabaseAgent> databaseAgentPtr(InspectorDatabaseAgent::create(m_instrumentingAgents.get(), m_state.get()));
  100. InspectorDatabaseAgent* databaseAgent = databaseAgentPtr.get();
  101. m_agents.append(databaseAgentPtr.release());
  102. #endif
  103. #if ENABLE(INDEXED_DATABASE)
  104. m_agents.append(InspectorIndexedDBAgent::create(m_instrumentingAgents.get(), m_state.get(), m_injectedScriptManager.get(), pageAgent));
  105. #endif
  106. #if ENABLE(FILE_SYSTEM)
  107. m_agents.append(InspectorFileSystemAgent::create(m_instrumentingAgents.get(), pageAgent, m_state.get()));
  108. #endif
  109. OwnPtr<InspectorDOMStorageAgent> domStorageAgentPtr(InspectorDOMStorageAgent::create(m_instrumentingAgents.get(), m_pageAgent, m_state.get()));
  110. InspectorDOMStorageAgent* domStorageAgent = domStorageAgentPtr.get();
  111. m_agents.append(domStorageAgentPtr.release());
  112. OwnPtr<InspectorMemoryAgent> memoryAgentPtr(InspectorMemoryAgent::create(m_instrumentingAgents.get(), m_state.get()));
  113. m_memoryAgent = memoryAgentPtr.get();
  114. m_agents.append(memoryAgentPtr.release());
  115. m_agents.append(InspectorTimelineAgent::create(m_instrumentingAgents.get(), pageAgent, m_memoryAgent, m_state.get(), InspectorTimelineAgent::PageInspector,
  116. inspectorClient));
  117. m_agents.append(InspectorApplicationCacheAgent::create(m_instrumentingAgents.get(), m_state.get(), pageAgent));
  118. OwnPtr<InspectorResourceAgent> resourceAgentPtr(InspectorResourceAgent::create(m_instrumentingAgents.get(), pageAgent, inspectorClient, m_state.get()));
  119. m_resourceAgent = resourceAgentPtr.get();
  120. m_agents.append(resourceAgentPtr.release());
  121. OwnPtr<InspectorRuntimeAgent> runtimeAgentPtr(PageRuntimeAgent::create(m_instrumentingAgents.get(), m_state.get(), m_injectedScriptManager.get(), page, pageAgent));
  122. InspectorRuntimeAgent* runtimeAgent = runtimeAgentPtr.get();
  123. m_agents.append(runtimeAgentPtr.release());
  124. OwnPtr<InspectorConsoleAgent> consoleAgentPtr(PageConsoleAgent::create(m_instrumentingAgents.get(), m_inspectorAgent, m_state.get(), m_injectedScriptManager.get(), m_domAgent));
  125. InspectorConsoleAgent* consoleAgent = consoleAgentPtr.get();
  126. m_agents.append(consoleAgentPtr.release());
  127. #if ENABLE(JAVASCRIPT_DEBUGGER)
  128. OwnPtr<InspectorDebuggerAgent> debuggerAgentPtr(PageDebuggerAgent::create(m_instrumentingAgents.get(), m_state.get(), pageAgent, m_injectedScriptManager.get(), m_overlay.get()));
  129. m_debuggerAgent = debuggerAgentPtr.get();
  130. m_agents.append(debuggerAgentPtr.release());
  131. OwnPtr<InspectorDOMDebuggerAgent> domDebuggerAgentPtr(InspectorDOMDebuggerAgent::create(m_instrumentingAgents.get(), m_state.get(), m_domAgent, m_debuggerAgent, m_inspectorAgent));
  132. m_domDebuggerAgent = domDebuggerAgentPtr.get();
  133. m_agents.append(domDebuggerAgentPtr.release());
  134. OwnPtr<InspectorProfilerAgent> profilerAgentPtr(InspectorProfilerAgent::create(m_instrumentingAgents.get(), consoleAgent, page, m_state.get(), m_injectedScriptManager.get()));
  135. m_profilerAgent = profilerAgentPtr.get();
  136. m_agents.append(profilerAgentPtr.release());
  137. m_agents.append(InspectorHeapProfilerAgent::create(m_instrumentingAgents.get(), m_state.get(), m_injectedScriptManager.get()));
  138. #endif
  139. #if ENABLE(WORKERS)
  140. m_agents.append(InspectorWorkerAgent::create(m_instrumentingAgents.get(), m_state.get()));
  141. #endif
  142. m_agents.append(InspectorCanvasAgent::create(m_instrumentingAgents.get(), m_state.get(), pageAgent, m_injectedScriptManager.get()));
  143. m_agents.append(InspectorInputAgent::create(m_instrumentingAgents.get(), m_state.get(), page));
  144. #if USE(ACCELERATED_COMPOSITING)
  145. m_agents.append(InspectorLayerTreeAgent::create(m_instrumentingAgents.get(), m_state.get()));
  146. #endif
  147. #if PLATFORM(MANX) && OS(ORBIS)
  148. m_agents.append(InspectorManxAgent::create(m_instrumentingAgents.get(), m_state.get(), pageAgent));
  149. #endif
  150. ASSERT_ARG(inspectorClient, inspectorClient);
  151. m_injectedScriptManager->injectedScriptHost()->init(m_inspectorAgent
  152. , consoleAgent
  153. #if ENABLE(SQL_DATABASE)
  154. , databaseAgent
  155. #endif
  156. , domStorageAgent
  157. , m_domAgent
  158. , m_debuggerAgent
  159. );
  160. #if ENABLE(JAVASCRIPT_DEBUGGER)
  161. runtimeAgent->setScriptDebugServer(&m_debuggerAgent->scriptDebugServer());
  162. #endif
  163. }
  164. InspectorController::~InspectorController()
  165. {
  166. m_instrumentingAgents->reset();
  167. m_agents.discardAgents();
  168. ASSERT(!m_inspectorClient);
  169. }
  170. PassOwnPtr<InspectorController> InspectorController::create(Page* page, InspectorClient* client)
  171. {
  172. return adoptPtr(new InspectorController(page, client));
  173. }
  174. void InspectorController::inspectedPageDestroyed()
  175. {
  176. disconnectFrontend();
  177. m_injectedScriptManager->disconnect();
  178. m_inspectorClient->inspectorDestroyed();
  179. m_inspectorClient = 0;
  180. m_page = 0;
  181. }
  182. void InspectorController::setInspectorFrontendClient(PassOwnPtr<InspectorFrontendClient> inspectorFrontendClient)
  183. {
  184. m_inspectorFrontendClient = inspectorFrontendClient;
  185. }
  186. bool InspectorController::hasInspectorFrontendClient() const
  187. {
  188. return m_inspectorFrontendClient;
  189. }
  190. void InspectorController::didClearWindowObjectInWorld(Frame* frame, DOMWrapperWorld* world)
  191. {
  192. if (world != mainThreadNormalWorld())
  193. return;
  194. // If the page is supposed to serve as InspectorFrontend notify inspector frontend
  195. // client that it's cleared so that the client can expose inspector bindings.
  196. if (m_inspectorFrontendClient && frame == m_page->mainFrame())
  197. m_inspectorFrontendClient->windowObjectCleared();
  198. }
  199. void InspectorController::connectFrontend(InspectorFrontendChannel* frontendChannel)
  200. {
  201. ASSERT(frontendChannel);
  202. m_inspectorFrontend = adoptPtr(new InspectorFrontend(frontendChannel));
  203. // We can reconnect to existing front-end -> unmute state.
  204. m_state->unmute();
  205. m_agents.setFrontend(m_inspectorFrontend.get());
  206. InspectorInstrumentation::registerInstrumentingAgents(m_instrumentingAgents.get());
  207. InspectorInstrumentation::frontendCreated();
  208. ASSERT(m_inspectorClient);
  209. m_inspectorBackendDispatcher = InspectorBackendDispatcher::create(frontendChannel);
  210. m_agents.registerInDispatcher(m_inspectorBackendDispatcher.get());
  211. }
  212. void InspectorController::disconnectFrontend()
  213. {
  214. if (!m_inspectorFrontend)
  215. return;
  216. m_inspectorBackendDispatcher->clearFrontend();
  217. m_inspectorBackendDispatcher.clear();
  218. // Destroying agents would change the state, but we don't want that.
  219. // Pre-disconnect state will be used to restore inspector agents.
  220. m_state->mute();
  221. m_agents.clearFrontend();
  222. m_inspectorFrontend.clear();
  223. // relese overlay page resources
  224. m_overlay->freePage();
  225. InspectorInstrumentation::frontendDeleted();
  226. InspectorInstrumentation::unregisterInstrumentingAgents(m_instrumentingAgents.get());
  227. }
  228. void InspectorController::show()
  229. {
  230. if (!enabled())
  231. return;
  232. if (m_inspectorFrontend)
  233. m_inspectorClient->bringFrontendToFront();
  234. else {
  235. InspectorFrontendChannel* frontendChannel = m_inspectorClient->openInspectorFrontend(this);
  236. if (frontendChannel)
  237. connectFrontend(frontendChannel);
  238. }
  239. }
  240. void InspectorController::close()
  241. {
  242. if (!m_inspectorFrontend)
  243. return;
  244. disconnectFrontend();
  245. m_inspectorClient->closeInspectorFrontend();
  246. }
  247. void InspectorController::reconnectFrontend(InspectorFrontendChannel* frontendChannel, const String& inspectorStateCookie)
  248. {
  249. ASSERT(!m_inspectorFrontend);
  250. connectFrontend(frontendChannel);
  251. m_state->loadFromCookie(inspectorStateCookie);
  252. m_agents.restore();
  253. }
  254. void InspectorController::setProcessId(long processId)
  255. {
  256. IdentifiersFactory::setProcessId(processId);
  257. }
  258. void InspectorController::webViewResized(const IntSize& size)
  259. {
  260. m_pageAgent->webViewResized(size);
  261. }
  262. bool InspectorController::isUnderTest()
  263. {
  264. return m_isUnderTest;
  265. }
  266. void InspectorController::evaluateForTestInFrontend(long callId, const String& script)
  267. {
  268. m_isUnderTest = true;
  269. m_inspectorAgent->evaluateForTestInFrontend(callId, script);
  270. }
  271. void InspectorController::drawHighlight(GraphicsContext& context) const
  272. {
  273. m_overlay->paint(context);
  274. }
  275. void InspectorController::getHighlight(Highlight* highlight) const
  276. {
  277. m_overlay->getHighlight(highlight);
  278. }
  279. void InspectorController::inspect(Node* node)
  280. {
  281. if (!enabled())
  282. return;
  283. show();
  284. m_domAgent->inspect(node);
  285. }
  286. bool InspectorController::enabled() const
  287. {
  288. return m_inspectorAgent->developerExtrasEnabled();
  289. }
  290. Page* InspectorController::inspectedPage() const
  291. {
  292. return m_page;
  293. }
  294. void InspectorController::setInjectedScriptForOrigin(const String& origin, const String& source)
  295. {
  296. m_inspectorAgent->setInjectedScriptForOrigin(origin, source);
  297. }
  298. void InspectorController::dispatchMessageFromFrontend(const String& message)
  299. {
  300. if (m_inspectorBackendDispatcher)
  301. m_inspectorBackendDispatcher->dispatch(message);
  302. }
  303. void InspectorController::hideHighlight()
  304. {
  305. ErrorString error;
  306. m_domAgent->hideHighlight(&error);
  307. }
  308. Node* InspectorController::highlightedNode() const
  309. {
  310. return m_overlay->highlightedNode();
  311. }
  312. #if ENABLE(JAVASCRIPT_DEBUGGER)
  313. bool InspectorController::profilerEnabled()
  314. {
  315. return m_profilerAgent->enabled();
  316. }
  317. void InspectorController::setProfilerEnabled(bool enable)
  318. {
  319. ErrorString error;
  320. if (enable)
  321. m_profilerAgent->enable(&error);
  322. else
  323. m_profilerAgent->disable(&error);
  324. }
  325. void InspectorController::resume()
  326. {
  327. if (m_debuggerAgent) {
  328. ErrorString error;
  329. m_debuggerAgent->resume(&error);
  330. }
  331. }
  332. #endif
  333. void InspectorController::setResourcesDataSizeLimitsFromInternals(int maximumResourcesContentSize, int maximumSingleResourceContentSize)
  334. {
  335. m_resourceAgent->setResourcesDataSizeLimitsFromInternals(maximumResourcesContentSize, maximumSingleResourceContentSize);
  336. }
  337. void InspectorController::willProcessTask()
  338. {
  339. if (InspectorTimelineAgent* timelineAgent = m_instrumentingAgents->inspectorTimelineAgent())
  340. timelineAgent->willProcessTask();
  341. #if ENABLE(JAVASCRIPT_DEBUGGER)
  342. m_profilerAgent->willProcessTask();
  343. #endif
  344. }
  345. void InspectorController::didProcessTask()
  346. {
  347. if (InspectorTimelineAgent* timelineAgent = m_instrumentingAgents->inspectorTimelineAgent())
  348. timelineAgent->didProcessTask();
  349. #if ENABLE(JAVASCRIPT_DEBUGGER)
  350. m_profilerAgent->didProcessTask();
  351. m_domDebuggerAgent->didProcessTask();
  352. #endif
  353. }
  354. void InspectorController::didBeginFrame()
  355. {
  356. if (InspectorTimelineAgent* timelineAgent = m_instrumentingAgents->inspectorTimelineAgent())
  357. timelineAgent->didBeginFrame();
  358. if (InspectorCanvasAgent* canvasAgent = m_instrumentingAgents->inspectorCanvasAgent())
  359. canvasAgent->didBeginFrame();
  360. }
  361. void InspectorController::didCancelFrame()
  362. {
  363. if (InspectorTimelineAgent* timelineAgent = m_instrumentingAgents->inspectorTimelineAgent())
  364. timelineAgent->didCancelFrame();
  365. }
  366. void InspectorController::willComposite()
  367. {
  368. if (InspectorTimelineAgent* timelineAgent = m_instrumentingAgents->inspectorTimelineAgent())
  369. timelineAgent->willComposite();
  370. }
  371. void InspectorController::didComposite()
  372. {
  373. if (InspectorTimelineAgent* timelineAgent = m_instrumentingAgents->inspectorTimelineAgent())
  374. timelineAgent->didComposite();
  375. }
  376. } // namespace WebCore
  377. #endif // ENABLE(INSPECTOR)