2 * Copyright (C) 2011 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
32 #include "core/inspector/InspectorController.h"
34 #include "bindings/core/v8/DOMWrapperWorld.h"
35 #include "core/InspectorBackendDispatcher.h"
36 #include "core/InspectorFrontend.h"
37 #include "core/inspector/IdentifiersFactory.h"
38 #include "core/inspector/InjectedScriptHost.h"
39 #include "core/inspector/InjectedScriptManager.h"
40 #include "core/inspector/InspectorApplicationCacheAgent.h"
41 #include "core/inspector/InspectorCSSAgent.h"
42 #include "core/inspector/InspectorCanvasAgent.h"
43 #include "core/inspector/InspectorClient.h"
44 #include "core/inspector/InspectorDOMAgent.h"
45 #include "core/inspector/InspectorDOMDebuggerAgent.h"
46 #include "core/inspector/InspectorDOMStorageAgent.h"
47 #include "core/inspector/InspectorDebuggerAgent.h"
48 #include "core/inspector/InspectorFrontendClient.h"
49 #include "core/inspector/InspectorHeapProfilerAgent.h"
50 #include "core/inspector/InspectorInputAgent.h"
51 #include "core/inspector/InspectorInspectorAgent.h"
52 #include "core/inspector/InspectorInstrumentation.h"
53 #include "core/inspector/InspectorLayerTreeAgent.h"
54 #include "core/inspector/InspectorMemoryAgent.h"
55 #include "core/inspector/InspectorOverlay.h"
56 #include "core/inspector/InspectorPageAgent.h"
57 #include "core/inspector/InspectorProfilerAgent.h"
58 #include "core/inspector/InspectorResourceAgent.h"
59 #include "core/inspector/InspectorState.h"
60 #include "core/inspector/InspectorTimelineAgent.h"
61 #include "core/inspector/InspectorTracingAgent.h"
62 #include "core/inspector/InspectorWorkerAgent.h"
63 #include "core/inspector/InstrumentingAgents.h"
64 #include "core/inspector/PageConsoleAgent.h"
65 #include "core/inspector/PageDebuggerAgent.h"
66 #include "core/inspector/PageRuntimeAgent.h"
67 #include "core/page/ContextMenuProvider.h"
68 #include "core/page/Page.h"
69 #include "core/rendering/RenderLayer.h"
70 #include "platform/PlatformMouseEvent.h"
74 InspectorController::InspectorController(Page* page, InspectorClient* inspectorClient)
75 : m_instrumentingAgents(InstrumentingAgents::create())
76 , m_injectedScriptManager(InjectedScriptManager::createForPage())
77 , m_state(adoptPtrWillBeNoop(new InspectorCompositeState(inspectorClient)))
78 , m_overlay(InspectorOverlay::create(page, inspectorClient))
80 , m_resourceAgent(nullptr)
81 , m_layerTreeAgent(nullptr)
82 , m_inspectorFrontendClient(nullptr)
84 , m_inspectorClient(inspectorClient)
85 , m_agents(m_instrumentingAgents.get(), m_state.get())
86 , m_isUnderTest(false)
87 , m_deferredAgentsInitialized(false)
89 InjectedScriptManager* injectedScriptManager = m_injectedScriptManager.get();
90 InspectorOverlay* overlay = m_overlay.get();
92 m_agents.append(InspectorInspectorAgent::create(m_page, injectedScriptManager));
94 OwnPtrWillBeRawPtr<InspectorPageAgent> pageAgentPtr(InspectorPageAgent::create(m_page, injectedScriptManager, inspectorClient, overlay));
95 m_pageAgent = pageAgentPtr.get();
96 m_agents.append(pageAgentPtr.release());
98 OwnPtrWillBeRawPtr<InspectorDOMAgent> domAgentPtr(InspectorDOMAgent::create(m_pageAgent, injectedScriptManager, overlay));
99 m_domAgent = domAgentPtr.get();
100 m_agents.append(domAgentPtr.release());
103 OwnPtrWillBeRawPtr<InspectorLayerTreeAgent> layerTreeAgentPtr(InspectorLayerTreeAgent::create(m_page));
104 m_layerTreeAgent = layerTreeAgentPtr.get();
105 m_agents.append(layerTreeAgentPtr.release());
107 OwnPtrWillBeRawPtr<InspectorWorkerAgent> workerAgentPtr = InspectorWorkerAgent::create();
109 OwnPtrWillBeRawPtr<InspectorTracingAgent> tracingAgentPtr = InspectorTracingAgent::create(inspectorClient, workerAgentPtr.get());
110 m_tracingAgent = tracingAgentPtr.get();
111 m_agents.append(tracingAgentPtr.release());
113 m_agents.append(workerAgentPtr.release());
115 OwnPtrWillBeRawPtr<InspectorTimelineAgent> timelineAgentPtr(InspectorTimelineAgent::create(m_pageAgent, m_layerTreeAgent,
116 overlay, InspectorTimelineAgent::PageInspector, inspectorClient));
117 m_timelineAgent = timelineAgentPtr.get();
118 m_agents.append(timelineAgentPtr.release());
120 PageScriptDebugServer* pageScriptDebugServer = &PageScriptDebugServer::shared();
122 m_agents.append(PageRuntimeAgent::create(injectedScriptManager, inspectorClient, pageScriptDebugServer, m_page, m_pageAgent));
124 m_agents.append(PageConsoleAgent::create(injectedScriptManager, m_domAgent, m_timelineAgent, m_page));
126 ASSERT_ARG(inspectorClient, inspectorClient);
127 m_injectedScriptManager->injectedScriptHost()->init(m_instrumentingAgents.get(), pageScriptDebugServer);
130 InspectorController::~InspectorController()
134 void InspectorController::trace(Visitor* visitor)
136 visitor->trace(m_instrumentingAgents);
137 visitor->trace(m_injectedScriptManager);
138 visitor->trace(m_state);
139 visitor->trace(m_domAgent);
140 visitor->trace(m_pageAgent);
141 visitor->trace(m_timelineAgent);
142 visitor->trace(m_cssAgent);
143 visitor->trace(m_resourceAgent);
144 visitor->trace(m_layerTreeAgent);
145 visitor->trace(m_tracingAgent);
146 visitor->trace(m_inspectorBackendDispatcher);
147 visitor->trace(m_page);
148 visitor->trace(m_agents);
151 PassOwnPtrWillBeRawPtr<InspectorController> InspectorController::create(Page* page, InspectorClient* client)
153 return adoptPtrWillBeNoop(new InspectorController(page, client));
156 void InspectorController::setTextAutosizingEnabled(bool enabled)
158 m_pageAgent->setTextAutosizingEnabled(enabled);
161 void InspectorController::setDeviceScaleAdjustment(float deviceScaleAdjustment)
163 m_pageAgent->setDeviceScaleAdjustment(deviceScaleAdjustment);
166 void InspectorController::setPreferCompositingToLCDTextEnabled(bool enabled)
168 m_pageAgent->setPreferCompositingToLCDTextEnabled(enabled);
171 void InspectorController::initializeDeferredAgents()
173 if (m_deferredAgentsInitialized)
175 m_deferredAgentsInitialized = true;
177 InjectedScriptManager* injectedScriptManager = m_injectedScriptManager.get();
178 InspectorOverlay* overlay = m_overlay.get();
180 OwnPtrWillBeRawPtr<InspectorResourceAgent> resourceAgentPtr(InspectorResourceAgent::create(m_pageAgent));
181 m_resourceAgent = resourceAgentPtr.get();
182 m_agents.append(resourceAgentPtr.release());
184 OwnPtrWillBeRawPtr<InspectorCSSAgent> cssAgentPtr(InspectorCSSAgent::create(m_domAgent, m_pageAgent, m_resourceAgent));
185 m_cssAgent = cssAgentPtr.get();
186 m_agents.append(cssAgentPtr.release());
188 m_agents.append(InspectorDOMStorageAgent::create(m_pageAgent));
190 m_agents.append(InspectorMemoryAgent::create());
192 m_agents.append(InspectorApplicationCacheAgent::create(m_pageAgent));
194 PageScriptDebugServer* pageScriptDebugServer = &PageScriptDebugServer::shared();
196 OwnPtrWillBeRawPtr<InspectorDebuggerAgent> debuggerAgentPtr(PageDebuggerAgent::create(pageScriptDebugServer, m_pageAgent, injectedScriptManager, overlay));
197 InspectorDebuggerAgent* debuggerAgent = debuggerAgentPtr.get();
198 m_agents.append(debuggerAgentPtr.release());
200 m_agents.append(InspectorDOMDebuggerAgent::create(m_domAgent, debuggerAgent));
202 m_agents.append(InspectorProfilerAgent::create(injectedScriptManager, overlay));
204 m_agents.append(InspectorHeapProfilerAgent::create(injectedScriptManager));
206 m_agents.append(InspectorCanvasAgent::create(m_pageAgent, injectedScriptManager));
208 m_agents.append(InspectorInputAgent::create(m_page, m_inspectorClient));
211 void InspectorController::willBeDestroyed()
214 ASSERT(m_page->mainFrame());
215 if (m_page->mainFrame()->isLocalFrame())
216 ASSERT(m_page->deprecatedLocalMainFrame()->view());
219 disconnectFrontend();
220 m_injectedScriptManager->disconnect();
221 m_inspectorClient = 0;
223 m_instrumentingAgents->reset();
224 m_agents.discardAgents();
225 if (m_inspectorFrontendClient)
226 m_inspectorFrontendClient->dispose();
229 void InspectorController::registerModuleAgent(PassOwnPtrWillBeRawPtr<InspectorAgent> agent)
231 m_agents.append(agent);
234 void InspectorController::setInspectorFrontendClient(InspectorFrontendClient* inspectorFrontendClient)
236 m_inspectorFrontendClient = inspectorFrontendClient;
239 void InspectorController::didClearDocumentOfWindowObject(LocalFrame* frame)
241 // If the page is supposed to serve as InspectorFrontend notify inspector frontend
242 // client that it's cleared so that the client can expose inspector bindings.
243 if (m_inspectorFrontendClient && frame == m_page->mainFrame())
244 m_inspectorFrontendClient->windowObjectCleared();
247 void InspectorController::connectFrontend(const String& hostId, InspectorFrontendChannel* frontendChannel)
249 ASSERT(frontendChannel);
252 initializeDeferredAgents();
253 m_resourceAgent->setHostId(hostId);
255 m_inspectorFrontend = adoptPtr(new InspectorFrontend(frontendChannel));
256 // We can reconnect to existing front-end -> unmute state.
259 m_agents.setFrontend(m_inspectorFrontend.get());
261 InspectorInstrumentation::registerInstrumentingAgents(m_instrumentingAgents.get());
262 InspectorInstrumentation::frontendCreated();
264 ASSERT(m_inspectorClient);
265 m_inspectorBackendDispatcher = InspectorBackendDispatcher::create(frontendChannel);
267 m_agents.registerInDispatcher(m_inspectorBackendDispatcher.get());
270 void InspectorController::disconnectFrontend()
272 if (!m_inspectorFrontend)
274 m_inspectorBackendDispatcher->clearFrontend();
275 m_inspectorBackendDispatcher.clear();
277 // Destroying agents would change the state, but we don't want that.
278 // Pre-disconnect state will be used to restore inspector agents.
281 m_agents.clearFrontend();
283 m_inspectorFrontend.clear();
285 // relese overlay page resources
286 m_overlay->freePage();
287 InspectorInstrumentation::frontendDeleted();
288 InspectorInstrumentation::unregisterInstrumentingAgents(m_instrumentingAgents.get());
292 void InspectorController::reconnectFrontend()
294 if (!m_inspectorFrontend)
296 InspectorFrontendChannel* frontendChannel = m_inspectorFrontend->channel();
297 String hostId = m_hostId;
298 disconnectFrontend();
299 connectFrontend(hostId, frontendChannel);
302 void InspectorController::reuseFrontend(const String& hostId, InspectorFrontendChannel* frontendChannel, const String& inspectorStateCookie)
304 ASSERT(!m_inspectorFrontend);
305 connectFrontend(hostId, frontendChannel);
306 m_state->loadFromCookie(inspectorStateCookie);
310 void InspectorController::setProcessId(long processId)
312 IdentifiersFactory::setProcessId(processId);
315 void InspectorController::setLayerTreeId(int id)
317 m_timelineAgent->setLayerTreeId(id);
318 m_tracingAgent->setLayerTreeId(id);
321 bool InspectorController::isUnderTest()
323 return m_isUnderTest;
326 void InspectorController::evaluateForTestInFrontend(long callId, const String& script)
328 m_isUnderTest = true;
329 if (InspectorInspectorAgent* inspectorAgent = m_instrumentingAgents->inspectorInspectorAgent())
330 inspectorAgent->evaluateForTestInFrontend(callId, script);
333 void InspectorController::drawHighlight(GraphicsContext& context) const
335 m_overlay->paint(context);
338 void InspectorController::inspect(Node* node)
342 Document* document = node->ownerDocument();
345 LocalFrame* frame = document->frame();
349 if (!node->isElementNode() && !node->isDocumentNode())
350 node = node->parentNode();
352 InjectedScript injectedScript = m_injectedScriptManager->injectedScriptFor(ScriptState::forMainWorld(frame));
353 if (injectedScript.isEmpty())
355 injectedScript.inspectNode(node);
358 void InspectorController::setInjectedScriptForOrigin(const String& origin, const String& source)
360 if (InspectorInspectorAgent* inspectorAgent = m_instrumentingAgents->inspectorInspectorAgent())
361 inspectorAgent->setInjectedScriptForOrigin(origin, source);
364 void InspectorController::showContextMenu(float x, float y, PassRefPtr<ContextMenuProvider> menuProvider)
366 if (!m_inspectorClient)
368 m_inspectorClient->showContextMenu(x, y, menuProvider);
371 void InspectorController::dispatchMessageFromFrontend(const String& message)
373 if (m_inspectorBackendDispatcher)
374 m_inspectorBackendDispatcher->dispatch(message);
377 bool InspectorController::handleGestureEvent(LocalFrame* frame, const PlatformGestureEvent& event)
379 // Overlay should not consume events.
380 m_overlay->handleGestureEvent(event);
381 if (InspectorDOMAgent* domAgent = m_instrumentingAgents->inspectorDOMAgent())
382 return domAgent->handleGestureEvent(frame, event);
386 bool InspectorController::handleMouseEvent(LocalFrame* frame, const PlatformMouseEvent& event)
388 // Overlay should not consume events.
389 m_overlay->handleMouseEvent(event);
391 if (event.type() == PlatformEvent::MouseMoved) {
392 if (InspectorDOMAgent* domAgent = m_instrumentingAgents->inspectorDOMAgent())
393 return domAgent->handleMouseMove(frame, event);
396 if (event.type() == PlatformEvent::MousePressed) {
397 if (InspectorDOMAgent* domAgent = m_instrumentingAgents->inspectorDOMAgent())
398 return domAgent->handleMousePress();
403 bool InspectorController::handleTouchEvent(LocalFrame* frame, const PlatformTouchEvent& event)
405 // Overlay should not consume events.
406 m_overlay->handleTouchEvent(event);
407 if (InspectorDOMAgent* domAgent = m_instrumentingAgents->inspectorDOMAgent())
408 return domAgent->handleTouchEvent(frame, event);
412 bool InspectorController::handleKeyboardEvent(LocalFrame* frame, const PlatformKeyboardEvent& event)
414 // Overlay should not consume events.
415 m_overlay->handleKeyboardEvent(event);
419 void InspectorController::deviceOrPageScaleFactorChanged()
421 m_pageAgent->deviceOrPageScaleFactorChanged();
424 bool InspectorController::deviceEmulationEnabled()
426 return m_pageAgent->deviceMetricsOverrideEnabled();
429 bool InspectorController::screencastEnabled()
431 return m_pageAgent->screencastEnabled();
434 void InspectorController::resume()
436 if (InspectorDebuggerAgent* debuggerAgent = m_instrumentingAgents->inspectorDebuggerAgent()) {
438 debuggerAgent->resume(&error);
442 void InspectorController::setResourcesDataSizeLimitsFromInternals(int maximumResourcesContentSize, int maximumSingleResourceContentSize)
445 m_resourceAgent->setResourcesDataSizeLimitsFromInternals(maximumResourcesContentSize, maximumSingleResourceContentSize);
448 void InspectorController::willProcessTask()
450 if (InspectorTimelineAgent* timelineAgent = m_instrumentingAgents->inspectorTimelineAgent())
451 timelineAgent->willProcessTask();
452 if (InspectorProfilerAgent* profilerAgent = m_instrumentingAgents->inspectorProfilerAgent())
453 profilerAgent->willProcessTask();
456 void InspectorController::didProcessTask()
458 if (InspectorTimelineAgent* timelineAgent = m_instrumentingAgents->inspectorTimelineAgent())
459 timelineAgent->didProcessTask();
460 if (InspectorProfilerAgent* profilerAgent = m_instrumentingAgents->inspectorProfilerAgent())
461 profilerAgent->didProcessTask();
462 if (InspectorDOMDebuggerAgent* domDebuggerAgent = m_instrumentingAgents->inspectorDOMDebuggerAgent())
463 domDebuggerAgent->didProcessTask();
466 void InspectorController::flushPendingFrontendMessages()
468 m_agents.flushPendingFrontendMessages();
471 void InspectorController::didCommitLoadForMainFrame()
473 m_agents.didCommitLoadForMainFrame();
476 void InspectorController::didBeginFrame(int frameId)
478 if (InspectorTimelineAgent* timelineAgent = m_instrumentingAgents->inspectorTimelineAgent())
479 timelineAgent->didBeginFrame(frameId);
480 if (InspectorCanvasAgent* canvasAgent = m_instrumentingAgents->inspectorCanvasAgent())
481 canvasAgent->didBeginFrame();
484 void InspectorController::didCancelFrame()
486 if (InspectorTimelineAgent* timelineAgent = m_instrumentingAgents->inspectorTimelineAgent())
487 timelineAgent->didCancelFrame();
490 void InspectorController::willComposite()
492 if (InspectorTimelineAgent* timelineAgent = m_instrumentingAgents->inspectorTimelineAgent())
493 timelineAgent->willComposite();
496 void InspectorController::didComposite()
498 if (InspectorTimelineAgent* timelineAgent = m_instrumentingAgents->inspectorTimelineAgent())
499 timelineAgent->didComposite();
502 void InspectorController::processGPUEvent(double timestamp, int phase, bool foreign, uint64_t usedGPUMemoryBytes, uint64_t limitGPUMemoryBytes)
504 if (InspectorTimelineAgent* timelineAgent = m_instrumentingAgents->inspectorTimelineAgent())
505 timelineAgent->processGPUEvent(InspectorTimelineAgent::GPUEvent(timestamp, phase, foreign, usedGPUMemoryBytes, limitGPUMemoryBytes));
508 void InspectorController::scriptsEnabled(bool enabled)
510 if (InspectorPageAgent* pageAgent = m_instrumentingAgents->inspectorPageAgent())
511 pageAgent->scriptsEnabled(enabled);
514 void InspectorController::willAddPageOverlay(const GraphicsLayer* layer)
516 if (m_layerTreeAgent)
517 m_layerTreeAgent->willAddPageOverlay(layer);
520 void InspectorController::didRemovePageOverlay(const GraphicsLayer* layer)
522 if (m_layerTreeAgent)
523 m_layerTreeAgent->didRemovePageOverlay(layer);