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/InspectorWorkerAgent.h"
62 #include "core/inspector/InstrumentingAgents.h"
63 #include "core/inspector/PageConsoleAgent.h"
64 #include "core/inspector/PageDebuggerAgent.h"
65 #include "core/inspector/PageRuntimeAgent.h"
66 #include "core/page/Page.h"
67 #include "core/rendering/RenderLayer.h"
68 #include "platform/PlatformMouseEvent.h"
72 InspectorController::InspectorController(Page* page, InspectorClient* inspectorClient)
73 : m_instrumentingAgents(InstrumentingAgents::create())
74 , m_injectedScriptManager(InjectedScriptManager::createForPage())
75 , m_state(adoptPtr(new InspectorCompositeState(inspectorClient)))
76 , m_overlay(InspectorOverlay::create(page, inspectorClient))
78 , m_inspectorClient(inspectorClient)
79 , m_agents(m_instrumentingAgents.get(), m_state.get())
80 , m_isUnderTest(false)
81 , m_deferredAgentsInitialized(false)
83 InjectedScriptManager* injectedScriptManager = m_injectedScriptManager.get();
84 InspectorOverlay* overlay = m_overlay.get();
86 m_agents.append(InspectorInspectorAgent::create(m_page, injectedScriptManager));
88 OwnPtr<InspectorPageAgent> pageAgentPtr(InspectorPageAgent::create(m_page, injectedScriptManager, inspectorClient, overlay));
89 m_pageAgent = pageAgentPtr.get();
90 m_agents.append(pageAgentPtr.release());
92 OwnPtr<InspectorDOMAgent> domAgentPtr(InspectorDOMAgent::create(m_pageAgent, injectedScriptManager, overlay));
93 m_domAgent = domAgentPtr.get();
94 m_agents.append(domAgentPtr.release());
96 OwnPtr<InspectorTimelineAgent> timelineAgentPtr(InspectorTimelineAgent::create(m_pageAgent, m_domAgent, overlay,
97 InspectorTimelineAgent::PageInspector, inspectorClient));
98 m_timelineAgent = timelineAgentPtr.get();
99 m_agents.append(timelineAgentPtr.release());
101 PageScriptDebugServer* pageScriptDebugServer = &PageScriptDebugServer::shared();
103 m_agents.append(PageRuntimeAgent::create(injectedScriptManager, pageScriptDebugServer, m_page, m_pageAgent));
105 m_agents.append(PageConsoleAgent::create(injectedScriptManager, m_domAgent, m_timelineAgent));
107 m_agents.append(InspectorWorkerAgent::create());
109 ASSERT_ARG(inspectorClient, inspectorClient);
110 m_injectedScriptManager->injectedScriptHost()->init(m_instrumentingAgents.get(), pageScriptDebugServer);
113 InspectorController::~InspectorController()
115 m_instrumentingAgents->reset();
116 m_agents.discardAgents();
117 ASSERT(!m_inspectorClient);
120 PassOwnPtr<InspectorController> InspectorController::create(Page* page, InspectorClient* client)
122 return adoptPtr(new InspectorController(page, client));
125 void InspectorController::setTextAutosizingEnabled(bool enabled)
127 m_pageAgent->setTextAutosizingEnabled(enabled);
130 void InspectorController::setDeviceScaleAdjustment(float deviceScaleAdjustment)
132 m_pageAgent->setDeviceScaleAdjustment(deviceScaleAdjustment);
135 void InspectorController::initializeDeferredAgents()
137 if (m_deferredAgentsInitialized)
139 m_deferredAgentsInitialized = true;
141 InjectedScriptManager* injectedScriptManager = m_injectedScriptManager.get();
142 InspectorOverlay* overlay = m_overlay.get();
144 OwnPtr<InspectorResourceAgent> resourceAgentPtr(InspectorResourceAgent::create(m_pageAgent, m_inspectorClient));
145 InspectorResourceAgent* resourceAgent = resourceAgentPtr.get();
146 m_agents.append(resourceAgentPtr.release());
148 m_agents.append(InspectorCSSAgent::create(m_domAgent, m_pageAgent, resourceAgent));
150 m_agents.append(InspectorDOMStorageAgent::create(m_pageAgent));
152 m_agents.append(InspectorMemoryAgent::create());
154 m_agents.append(InspectorApplicationCacheAgent::create(m_pageAgent));
156 PageScriptDebugServer* pageScriptDebugServer = &PageScriptDebugServer::shared();
158 OwnPtr<InspectorDebuggerAgent> debuggerAgentPtr(PageDebuggerAgent::create(pageScriptDebugServer, m_pageAgent, injectedScriptManager, overlay));
159 InspectorDebuggerAgent* debuggerAgent = debuggerAgentPtr.get();
160 m_agents.append(debuggerAgentPtr.release());
162 m_agents.append(InspectorDOMDebuggerAgent::create(m_domAgent, debuggerAgent));
164 m_agents.append(InspectorProfilerAgent::create(injectedScriptManager, overlay));
166 m_agents.append(InspectorHeapProfilerAgent::create(injectedScriptManager));
168 m_agents.append(InspectorCanvasAgent::create(m_pageAgent, injectedScriptManager));
170 m_agents.append(InspectorInputAgent::create(m_page, m_inspectorClient));
172 m_agents.append(InspectorLayerTreeAgent::create(m_domAgent, m_page));
175 void InspectorController::inspectedPageDestroyed()
177 disconnectFrontend();
178 m_injectedScriptManager->disconnect();
179 m_inspectorClient = 0;
183 void InspectorController::registerModuleAgent(PassOwnPtr<InspectorAgent> agent)
185 m_moduleAgents.append(agent.get());
186 m_agents.append(agent);
189 void InspectorController::setInspectorFrontendClient(PassOwnPtr<InspectorFrontendClient> inspectorFrontendClient)
191 m_inspectorFrontendClient = inspectorFrontendClient;
194 void InspectorController::didClearWindowObjectInMainWorld(Frame* frame)
196 // If the page is supposed to serve as InspectorFrontend notify inspector frontend
197 // client that it's cleared so that the client can expose inspector bindings.
198 if (m_inspectorFrontendClient && frame == m_page->mainFrame())
199 m_inspectorFrontendClient->windowObjectCleared();
202 void InspectorController::connectFrontend(InspectorFrontendChannel* frontendChannel)
204 ASSERT(frontendChannel);
206 initializeDeferredAgents();
208 m_inspectorFrontend = adoptPtr(new InspectorFrontend(frontendChannel));
209 // We can reconnect to existing front-end -> unmute state.
212 m_agents.setFrontend(m_inspectorFrontend.get());
214 InspectorInstrumentation::registerInstrumentingAgents(m_instrumentingAgents.get());
215 InspectorInstrumentation::frontendCreated();
217 ASSERT(m_inspectorClient);
218 m_inspectorBackendDispatcher = InspectorBackendDispatcher::create(frontendChannel);
220 m_agents.registerInDispatcher(m_inspectorBackendDispatcher.get());
223 void InspectorController::disconnectFrontend()
225 if (!m_inspectorFrontend)
227 m_inspectorBackendDispatcher->clearFrontend();
228 m_inspectorBackendDispatcher.clear();
230 // Destroying agents would change the state, but we don't want that.
231 // Pre-disconnect state will be used to restore inspector agents.
234 m_agents.clearFrontend();
236 m_inspectorFrontend.clear();
238 // relese overlay page resources
239 m_overlay->freePage();
240 InspectorInstrumentation::frontendDeleted();
241 InspectorInstrumentation::unregisterInstrumentingAgents(m_instrumentingAgents.get());
244 void InspectorController::reconnectFrontend()
246 if (!m_inspectorFrontend)
248 InspectorFrontendChannel* frontendChannel = m_inspectorFrontend->inspector()->getInspectorFrontendChannel();
249 disconnectFrontend();
250 connectFrontend(frontendChannel);
253 void InspectorController::reuseFrontend(InspectorFrontendChannel* frontendChannel, const String& inspectorStateCookie)
255 ASSERT(!m_inspectorFrontend);
256 connectFrontend(frontendChannel);
257 m_state->loadFromCookie(inspectorStateCookie);
261 void InspectorController::setProcessId(long processId)
263 IdentifiersFactory::setProcessId(processId);
266 void InspectorController::setLayerTreeId(int id)
268 m_timelineAgent->setLayerTreeId(id);
271 void InspectorController::webViewResized(const IntSize& size)
273 if (InspectorPageAgent* pageAgent = m_instrumentingAgents->inspectorPageAgent())
274 pageAgent->webViewResized(size);
277 bool InspectorController::isUnderTest()
279 return m_isUnderTest;
282 void InspectorController::evaluateForTestInFrontend(long callId, const String& script)
284 m_isUnderTest = true;
285 if (InspectorInspectorAgent* inspectorAgent = m_instrumentingAgents->inspectorInspectorAgent())
286 inspectorAgent->evaluateForTestInFrontend(callId, script);
289 void InspectorController::drawHighlight(GraphicsContext& context) const
291 // https://code.google.com/p/chromium/issues/detail?id=343757
292 DisableCompositingQueryAsserts disabler;
293 m_overlay->paint(context);
296 void InspectorController::getHighlight(Highlight* highlight) const
298 m_overlay->getHighlight(highlight);
301 void InspectorController::inspect(Node* node)
305 Document* document = node->ownerDocument();
308 Frame* frame = document->frame();
312 if (node->nodeType() != Node::ELEMENT_NODE && node->nodeType() != Node::DOCUMENT_NODE)
313 node = node->parentNode();
315 InjectedScript injectedScript = m_injectedScriptManager->injectedScriptFor(mainWorldScriptState(frame));
316 if (injectedScript.hasNoValue())
318 injectedScript.inspectNode(node);
321 void InspectorController::setInjectedScriptForOrigin(const String& origin, const String& source)
323 if (InspectorInspectorAgent* inspectorAgent = m_instrumentingAgents->inspectorInspectorAgent())
324 inspectorAgent->setInjectedScriptForOrigin(origin, source);
327 void InspectorController::dispatchMessageFromFrontend(const String& message)
329 if (m_inspectorBackendDispatcher)
330 m_inspectorBackendDispatcher->dispatch(message);
333 void InspectorController::hideHighlight()
335 m_overlay->hideHighlight();
338 Node* InspectorController::highlightedNode() const
340 return m_overlay->highlightedNode();
343 bool InspectorController::handleGestureEvent(Frame* frame, const PlatformGestureEvent& event)
345 // Overlay should not consume events.
346 m_overlay->handleGestureEvent(event);
347 if (InspectorDOMAgent* domAgent = m_instrumentingAgents->inspectorDOMAgent())
348 return domAgent->handleGestureEvent(frame, event);
352 bool InspectorController::handleMouseEvent(Frame* frame, const PlatformMouseEvent& event)
354 // Overlay should not consume events.
355 m_overlay->handleMouseEvent(event);
357 if (event.type() == PlatformEvent::MouseMoved) {
358 if (InspectorDOMAgent* domAgent = m_instrumentingAgents->inspectorDOMAgent())
359 domAgent->handleMouseMove(frame, event);
362 if (event.type() == PlatformEvent::MousePressed) {
363 if (InspectorDOMAgent* domAgent = m_instrumentingAgents->inspectorDOMAgent())
364 return domAgent->handleMousePress();
369 bool InspectorController::handleTouchEvent(Frame* frame, const PlatformTouchEvent& event)
371 // Overlay should not consume events.
372 m_overlay->handleTouchEvent(event);
373 if (InspectorDOMAgent* domAgent = m_instrumentingAgents->inspectorDOMAgent())
374 return domAgent->handleTouchEvent(frame, event);
378 bool InspectorController::handleKeyboardEvent(Frame* frame, const PlatformKeyboardEvent& event)
380 // Overlay should not consume events.
381 m_overlay->handleKeyboardEvent(event);
385 void InspectorController::requestPageScaleFactor(float scale, const IntPoint& origin)
387 m_inspectorClient->requestPageScaleFactor(scale, origin);
390 bool InspectorController::deviceEmulationEnabled()
392 if (InspectorPageAgent* pageAgent = m_instrumentingAgents->inspectorPageAgent())
393 return pageAgent->deviceMetricsOverrideEnabled();
397 void InspectorController::resume()
399 if (InspectorDebuggerAgent* debuggerAgent = m_instrumentingAgents->inspectorDebuggerAgent()) {
401 debuggerAgent->resume(&error);
405 void InspectorController::setResourcesDataSizeLimitsFromInternals(int maximumResourcesContentSize, int maximumSingleResourceContentSize)
407 if (InspectorResourceAgent* resourceAgent = m_instrumentingAgents->inspectorResourceAgent())
408 resourceAgent->setResourcesDataSizeLimitsFromInternals(maximumResourcesContentSize, maximumSingleResourceContentSize);
411 void InspectorController::willProcessTask()
413 if (InspectorTimelineAgent* timelineAgent = m_instrumentingAgents->inspectorTimelineAgent())
414 timelineAgent->willProcessTask();
415 if (InspectorProfilerAgent* profilerAgent = m_instrumentingAgents->inspectorProfilerAgent())
416 profilerAgent->willProcessTask();
419 void InspectorController::didProcessTask()
421 if (InspectorTimelineAgent* timelineAgent = m_instrumentingAgents->inspectorTimelineAgent())
422 timelineAgent->didProcessTask();
423 if (InspectorProfilerAgent* profilerAgent = m_instrumentingAgents->inspectorProfilerAgent())
424 profilerAgent->didProcessTask();
425 if (InspectorDOMDebuggerAgent* domDebuggerAgent = m_instrumentingAgents->inspectorDOMDebuggerAgent())
426 domDebuggerAgent->didProcessTask();
429 void InspectorController::didCommitLoadForMainFrame()
431 Vector<InspectorAgent*> agents = m_moduleAgents;
432 for (size_t i = 0; i < agents.size(); i++)
433 agents[i]->didCommitLoadForMainFrame();
436 void InspectorController::didBeginFrame(int frameId)
438 if (InspectorTimelineAgent* timelineAgent = m_instrumentingAgents->inspectorTimelineAgent())
439 timelineAgent->didBeginFrame(frameId);
440 if (InspectorCanvasAgent* canvasAgent = m_instrumentingAgents->inspectorCanvasAgent())
441 canvasAgent->didBeginFrame();
444 void InspectorController::didCancelFrame()
446 if (InspectorTimelineAgent* timelineAgent = m_instrumentingAgents->inspectorTimelineAgent())
447 timelineAgent->didCancelFrame();
450 void InspectorController::willComposite()
452 if (InspectorTimelineAgent* timelineAgent = m_instrumentingAgents->inspectorTimelineAgent())
453 timelineAgent->willComposite();
456 void InspectorController::didComposite()
458 if (InspectorTimelineAgent* timelineAgent = m_instrumentingAgents->inspectorTimelineAgent())
459 timelineAgent->didComposite();
462 void InspectorController::processGPUEvent(double timestamp, int phase, bool foreign, size_t usedGPUMemoryBytes)
464 if (InspectorTimelineAgent* timelineAgent = m_instrumentingAgents->inspectorTimelineAgent())
465 timelineAgent->processGPUEvent(InspectorTimelineAgent::GPUEvent(timestamp, phase, foreign, usedGPUMemoryBytes));
468 void InspectorController::scriptsEnabled(bool enabled)
470 if (InspectorPageAgent* pageAgent = m_instrumentingAgents->inspectorPageAgent())
471 pageAgent->scriptsEnabled(enabled);
474 } // namespace WebCore