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 "InspectorBackendDispatcher.h"
35 #include "InspectorFrontend.h"
36 #include "bindings/v8/DOMWrapperWorld.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/Page.h"
68 #include "core/rendering/RenderLayer.h"
69 #include "platform/PlatformMouseEvent.h"
73 InspectorController::InspectorController(Page* page, InspectorClient* inspectorClient)
74 : m_instrumentingAgents(InstrumentingAgents::create())
75 , m_injectedScriptManager(InjectedScriptManager::createForPage())
76 , m_state(adoptPtr(new InspectorCompositeState(inspectorClient)))
77 , m_overlay(InspectorOverlay::create(page, inspectorClient))
80 , m_inspectorClient(inspectorClient)
81 , m_agents(m_instrumentingAgents.get(), m_state.get())
82 , m_isUnderTest(false)
83 , m_deferredAgentsInitialized(false)
85 InjectedScriptManager* injectedScriptManager = m_injectedScriptManager.get();
86 InspectorOverlay* overlay = m_overlay.get();
88 m_agents.append(InspectorInspectorAgent::create(m_page, injectedScriptManager));
90 OwnPtr<InspectorPageAgent> pageAgentPtr(InspectorPageAgent::create(m_page, injectedScriptManager, inspectorClient, overlay));
91 m_pageAgent = pageAgentPtr.get();
92 m_agents.append(pageAgentPtr.release());
94 OwnPtr<InspectorDOMAgent> domAgentPtr(InspectorDOMAgent::create(m_pageAgent, injectedScriptManager, overlay));
95 m_domAgent = domAgentPtr.get();
96 m_agents.append(domAgentPtr.release());
99 OwnPtr<InspectorLayerTreeAgent> layerTreeAgentPtr(InspectorLayerTreeAgent::create(m_page));
100 m_layerTreeAgent = layerTreeAgentPtr.get();
101 m_agents.append(layerTreeAgentPtr.release());
103 OwnPtr<InspectorTracingAgent> tracingAgentPtr = InspectorTracingAgent::create(inspectorClient);
104 m_tracingAgent = tracingAgentPtr.get();
105 m_agents.append(tracingAgentPtr.release());
107 OwnPtr<InspectorTimelineAgent> timelineAgentPtr(InspectorTimelineAgent::create(m_pageAgent, m_layerTreeAgent,
108 overlay, InspectorTimelineAgent::PageInspector, inspectorClient));
109 m_timelineAgent = timelineAgentPtr.get();
110 m_agents.append(timelineAgentPtr.release());
112 PageScriptDebugServer* pageScriptDebugServer = &PageScriptDebugServer::shared();
114 m_agents.append(PageRuntimeAgent::create(injectedScriptManager, pageScriptDebugServer, m_page, m_pageAgent));
116 m_agents.append(PageConsoleAgent::create(injectedScriptManager, m_domAgent, m_timelineAgent));
118 m_agents.append(InspectorWorkerAgent::create());
120 ASSERT_ARG(inspectorClient, inspectorClient);
121 m_injectedScriptManager->injectedScriptHost()->init(m_instrumentingAgents.get(), pageScriptDebugServer);
124 InspectorController::~InspectorController()
128 PassOwnPtr<InspectorController> InspectorController::create(Page* page, InspectorClient* client)
130 return adoptPtr(new InspectorController(page, client));
133 void InspectorController::setTextAutosizingEnabled(bool enabled)
135 m_pageAgent->setTextAutosizingEnabled(enabled);
138 void InspectorController::setDeviceScaleAdjustment(float deviceScaleAdjustment)
140 m_pageAgent->setDeviceScaleAdjustment(deviceScaleAdjustment);
143 void InspectorController::initializeDeferredAgents()
145 if (m_deferredAgentsInitialized)
147 m_deferredAgentsInitialized = true;
149 InjectedScriptManager* injectedScriptManager = m_injectedScriptManager.get();
150 InspectorOverlay* overlay = m_overlay.get();
152 OwnPtr<InspectorResourceAgent> resourceAgentPtr(InspectorResourceAgent::create(m_pageAgent));
153 InspectorResourceAgent* resourceAgent = resourceAgentPtr.get();
154 m_agents.append(resourceAgentPtr.release());
156 m_agents.append(InspectorCSSAgent::create(m_domAgent, m_pageAgent, resourceAgent));
158 m_agents.append(InspectorDOMStorageAgent::create(m_pageAgent));
160 m_agents.append(InspectorMemoryAgent::create());
162 m_agents.append(InspectorApplicationCacheAgent::create(m_pageAgent));
164 PageScriptDebugServer* pageScriptDebugServer = &PageScriptDebugServer::shared();
166 OwnPtr<InspectorDebuggerAgent> debuggerAgentPtr(PageDebuggerAgent::create(pageScriptDebugServer, m_pageAgent, injectedScriptManager, overlay));
167 InspectorDebuggerAgent* debuggerAgent = debuggerAgentPtr.get();
168 m_agents.append(debuggerAgentPtr.release());
170 m_agents.append(InspectorDOMDebuggerAgent::create(m_domAgent, debuggerAgent));
172 m_agents.append(InspectorProfilerAgent::create(injectedScriptManager, overlay));
174 m_agents.append(InspectorHeapProfilerAgent::create(injectedScriptManager));
176 m_agents.append(InspectorCanvasAgent::create(m_pageAgent, injectedScriptManager));
178 m_agents.append(InspectorInputAgent::create(m_page, m_inspectorClient));
181 void InspectorController::willBeDestroyed()
183 disconnectFrontend();
184 m_injectedScriptManager->disconnect();
185 m_inspectorClient = 0;
187 m_instrumentingAgents->reset();
188 m_agents.discardAgents();
191 void InspectorController::registerModuleAgent(PassOwnPtr<InspectorAgent> agent)
193 m_moduleAgents.append(agent.get());
194 m_agents.append(agent);
197 void InspectorController::setInspectorFrontendClient(PassOwnPtr<InspectorFrontendClient> inspectorFrontendClient)
199 m_inspectorFrontendClient = inspectorFrontendClient;
202 void InspectorController::didClearDocumentOfWindowObject(LocalFrame* frame)
204 // If the page is supposed to serve as InspectorFrontend notify inspector frontend
205 // client that it's cleared so that the client can expose inspector bindings.
206 if (m_inspectorFrontendClient && frame == m_page->mainFrame())
207 m_inspectorFrontendClient->windowObjectCleared();
210 void InspectorController::connectFrontend(InspectorFrontendChannel* frontendChannel)
212 ASSERT(frontendChannel);
214 initializeDeferredAgents();
216 m_inspectorFrontend = adoptPtr(new InspectorFrontend(frontendChannel));
217 // We can reconnect to existing front-end -> unmute state.
220 m_agents.setFrontend(m_inspectorFrontend.get());
222 InspectorInstrumentation::registerInstrumentingAgents(m_instrumentingAgents.get());
223 InspectorInstrumentation::frontendCreated();
225 ASSERT(m_inspectorClient);
226 m_inspectorBackendDispatcher = InspectorBackendDispatcher::create(frontendChannel);
228 m_agents.registerInDispatcher(m_inspectorBackendDispatcher.get());
231 void InspectorController::disconnectFrontend()
233 if (!m_inspectorFrontend)
235 m_inspectorBackendDispatcher->clearFrontend();
236 m_inspectorBackendDispatcher.clear();
238 // Destroying agents would change the state, but we don't want that.
239 // Pre-disconnect state will be used to restore inspector agents.
242 m_agents.clearFrontend();
244 m_inspectorFrontend.clear();
246 // relese overlay page resources
247 m_overlay->freePage();
248 InspectorInstrumentation::frontendDeleted();
249 InspectorInstrumentation::unregisterInstrumentingAgents(m_instrumentingAgents.get());
252 void InspectorController::reconnectFrontend()
254 if (!m_inspectorFrontend)
256 InspectorFrontendChannel* frontendChannel = m_inspectorFrontend->channel();
257 disconnectFrontend();
258 connectFrontend(frontendChannel);
261 void InspectorController::reuseFrontend(InspectorFrontendChannel* frontendChannel, const String& inspectorStateCookie)
263 ASSERT(!m_inspectorFrontend);
264 connectFrontend(frontendChannel);
265 m_state->loadFromCookie(inspectorStateCookie);
269 void InspectorController::setProcessId(long processId)
271 IdentifiersFactory::setProcessId(processId);
274 void InspectorController::setLayerTreeId(int id)
276 m_timelineAgent->setLayerTreeId(id);
277 m_tracingAgent->setLayerTreeId(id);
280 void InspectorController::webViewResized(const IntSize& size)
282 if (InspectorPageAgent* pageAgent = m_instrumentingAgents->inspectorPageAgent())
283 pageAgent->webViewResized(size);
286 bool InspectorController::isUnderTest()
288 return m_isUnderTest;
291 void InspectorController::evaluateForTestInFrontend(long callId, const String& script)
293 m_isUnderTest = true;
294 if (InspectorInspectorAgent* inspectorAgent = m_instrumentingAgents->inspectorInspectorAgent())
295 inspectorAgent->evaluateForTestInFrontend(callId, script);
298 void InspectorController::drawHighlight(GraphicsContext& context) const
300 m_overlay->paint(context);
303 void InspectorController::inspect(Node* node)
307 Document* document = node->ownerDocument();
310 LocalFrame* frame = document->frame();
314 if (node->nodeType() != Node::ELEMENT_NODE && node->nodeType() != Node::DOCUMENT_NODE)
315 node = node->parentNode();
317 InjectedScript injectedScript = m_injectedScriptManager->injectedScriptFor(ScriptState::forMainWorld(frame));
318 if (injectedScript.isEmpty())
320 injectedScript.inspectNode(node);
323 void InspectorController::setInjectedScriptForOrigin(const String& origin, const String& source)
325 if (InspectorInspectorAgent* inspectorAgent = m_instrumentingAgents->inspectorInspectorAgent())
326 inspectorAgent->setInjectedScriptForOrigin(origin, source);
329 void InspectorController::dispatchMessageFromFrontend(const String& message)
331 if (m_inspectorBackendDispatcher)
332 m_inspectorBackendDispatcher->dispatch(message);
335 bool InspectorController::handleGestureEvent(LocalFrame* frame, const PlatformGestureEvent& event)
337 // Overlay should not consume events.
338 m_overlay->handleGestureEvent(event);
339 if (InspectorDOMAgent* domAgent = m_instrumentingAgents->inspectorDOMAgent())
340 return domAgent->handleGestureEvent(frame, event);
344 bool InspectorController::handleMouseEvent(LocalFrame* frame, const PlatformMouseEvent& event)
346 // Overlay should not consume events.
347 m_overlay->handleMouseEvent(event);
349 if (event.type() == PlatformEvent::MouseMoved) {
350 if (InspectorDOMAgent* domAgent = m_instrumentingAgents->inspectorDOMAgent())
351 domAgent->handleMouseMove(frame, event);
354 if (event.type() == PlatformEvent::MousePressed) {
355 if (InspectorDOMAgent* domAgent = m_instrumentingAgents->inspectorDOMAgent())
356 return domAgent->handleMousePress();
361 bool InspectorController::handleTouchEvent(LocalFrame* frame, const PlatformTouchEvent& event)
363 // Overlay should not consume events.
364 m_overlay->handleTouchEvent(event);
365 if (InspectorDOMAgent* domAgent = m_instrumentingAgents->inspectorDOMAgent())
366 return domAgent->handleTouchEvent(frame, event);
370 bool InspectorController::handleKeyboardEvent(LocalFrame* frame, const PlatformKeyboardEvent& event)
372 // Overlay should not consume events.
373 m_overlay->handleKeyboardEvent(event);
377 void InspectorController::requestPageScaleFactor(float scale, const IntPoint& origin)
379 m_inspectorClient->requestPageScaleFactor(scale, origin);
382 bool InspectorController::deviceEmulationEnabled()
384 if (InspectorPageAgent* pageAgent = m_instrumentingAgents->inspectorPageAgent())
385 return pageAgent->deviceMetricsOverrideEnabled();
389 void InspectorController::resume()
391 if (InspectorDebuggerAgent* debuggerAgent = m_instrumentingAgents->inspectorDebuggerAgent()) {
393 debuggerAgent->resume(&error);
397 void InspectorController::setResourcesDataSizeLimitsFromInternals(int maximumResourcesContentSize, int maximumSingleResourceContentSize)
399 if (InspectorResourceAgent* resourceAgent = m_instrumentingAgents->inspectorResourceAgent())
400 resourceAgent->setResourcesDataSizeLimitsFromInternals(maximumResourcesContentSize, maximumSingleResourceContentSize);
403 void InspectorController::willProcessTask()
405 if (InspectorTimelineAgent* timelineAgent = m_instrumentingAgents->inspectorTimelineAgent())
406 timelineAgent->willProcessTask();
407 if (InspectorProfilerAgent* profilerAgent = m_instrumentingAgents->inspectorProfilerAgent())
408 profilerAgent->willProcessTask();
411 void InspectorController::didProcessTask()
413 if (InspectorTimelineAgent* timelineAgent = m_instrumentingAgents->inspectorTimelineAgent())
414 timelineAgent->didProcessTask();
415 if (InspectorProfilerAgent* profilerAgent = m_instrumentingAgents->inspectorProfilerAgent())
416 profilerAgent->didProcessTask();
417 if (InspectorDOMDebuggerAgent* domDebuggerAgent = m_instrumentingAgents->inspectorDOMDebuggerAgent())
418 domDebuggerAgent->didProcessTask();
421 void InspectorController::flushPendingFrontendMessages()
423 m_agents.flushPendingFrontendMessages();
426 void InspectorController::didCommitLoadForMainFrame()
428 Vector<InspectorAgent*> agents = m_moduleAgents;
429 for (size_t i = 0; i < agents.size(); i++)
430 agents[i]->didCommitLoadForMainFrame();
433 void InspectorController::didBeginFrame(int frameId)
435 if (InspectorTimelineAgent* timelineAgent = m_instrumentingAgents->inspectorTimelineAgent())
436 timelineAgent->didBeginFrame(frameId);
437 if (InspectorCanvasAgent* canvasAgent = m_instrumentingAgents->inspectorCanvasAgent())
438 canvasAgent->didBeginFrame();
441 void InspectorController::didCancelFrame()
443 if (InspectorTimelineAgent* timelineAgent = m_instrumentingAgents->inspectorTimelineAgent())
444 timelineAgent->didCancelFrame();
447 void InspectorController::willComposite()
449 if (InspectorTimelineAgent* timelineAgent = m_instrumentingAgents->inspectorTimelineAgent())
450 timelineAgent->willComposite();
453 void InspectorController::didComposite()
455 if (InspectorTimelineAgent* timelineAgent = m_instrumentingAgents->inspectorTimelineAgent())
456 timelineAgent->didComposite();
459 void InspectorController::processGPUEvent(double timestamp, int phase, bool foreign, uint64_t usedGPUMemoryBytes, uint64_t limitGPUMemoryBytes)
461 if (InspectorTimelineAgent* timelineAgent = m_instrumentingAgents->inspectorTimelineAgent())
462 timelineAgent->processGPUEvent(InspectorTimelineAgent::GPUEvent(timestamp, phase, foreign, usedGPUMemoryBytes, limitGPUMemoryBytes));
465 void InspectorController::scriptsEnabled(bool enabled)
467 if (InspectorPageAgent* pageAgent = m_instrumentingAgents->inspectorPageAgent())
468 pageAgent->scriptsEnabled(enabled);
471 void InspectorController::willAddPageOverlay(const GraphicsLayer* layer)
473 if (m_layerTreeAgent)
474 m_layerTreeAgent->willAddPageOverlay(layer);
477 void InspectorController::didRemovePageOverlay(const GraphicsLayer* layer)
479 if (m_layerTreeAgent)
480 m_layerTreeAgent->didRemovePageOverlay(layer);
483 } // namespace WebCore