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))
79 , m_inspectorClient(inspectorClient)
80 , m_agents(m_instrumentingAgents.get(), m_state.get())
81 , m_isUnderTest(false)
82 , m_deferredAgentsInitialized(false)
84 InjectedScriptManager* injectedScriptManager = m_injectedScriptManager.get();
85 InspectorOverlay* overlay = m_overlay.get();
87 m_agents.append(InspectorInspectorAgent::create(m_page, injectedScriptManager));
89 OwnPtr<InspectorPageAgent> pageAgentPtr(InspectorPageAgent::create(m_page, injectedScriptManager, inspectorClient, overlay));
90 m_pageAgent = pageAgentPtr.get();
91 m_agents.append(pageAgentPtr.release());
93 OwnPtr<InspectorDOMAgent> domAgentPtr(InspectorDOMAgent::create(m_pageAgent, injectedScriptManager, overlay));
94 m_domAgent = domAgentPtr.get();
95 m_agents.append(domAgentPtr.release());
98 OwnPtr<InspectorLayerTreeAgent> layerTreeAgentPtr(InspectorLayerTreeAgent::create(m_domAgent, m_page));
99 m_layerTreeAgent = layerTreeAgentPtr.get();
100 m_agents.append(layerTreeAgentPtr.release());
102 OwnPtr<InspectorTimelineAgent> timelineAgentPtr(InspectorTimelineAgent::create(m_pageAgent, m_domAgent, m_layerTreeAgent,
103 overlay, InspectorTimelineAgent::PageInspector, inspectorClient));
104 m_timelineAgent = timelineAgentPtr.get();
105 m_agents.append(timelineAgentPtr.release());
107 PageScriptDebugServer* pageScriptDebugServer = &PageScriptDebugServer::shared();
109 m_agents.append(PageRuntimeAgent::create(injectedScriptManager, pageScriptDebugServer, m_page, m_pageAgent));
111 m_agents.append(PageConsoleAgent::create(injectedScriptManager, m_domAgent, m_timelineAgent));
113 m_agents.append(InspectorWorkerAgent::create());
115 ASSERT_ARG(inspectorClient, inspectorClient);
116 m_injectedScriptManager->injectedScriptHost()->init(m_instrumentingAgents.get(), pageScriptDebugServer);
119 InspectorController::~InspectorController()
121 m_instrumentingAgents->reset();
122 m_agents.discardAgents();
125 PassOwnPtr<InspectorController> InspectorController::create(Page* page, InspectorClient* client)
127 return adoptPtr(new InspectorController(page, client));
130 void InspectorController::setTextAutosizingEnabled(bool enabled)
132 m_pageAgent->setTextAutosizingEnabled(enabled);
135 void InspectorController::setDeviceScaleAdjustment(float deviceScaleAdjustment)
137 m_pageAgent->setDeviceScaleAdjustment(deviceScaleAdjustment);
140 void InspectorController::initializeDeferredAgents()
142 if (m_deferredAgentsInitialized)
144 m_deferredAgentsInitialized = true;
146 InjectedScriptManager* injectedScriptManager = m_injectedScriptManager.get();
147 InspectorOverlay* overlay = m_overlay.get();
149 OwnPtr<InspectorResourceAgent> resourceAgentPtr(InspectorResourceAgent::create(m_pageAgent, m_inspectorClient));
150 InspectorResourceAgent* resourceAgent = resourceAgentPtr.get();
151 m_agents.append(resourceAgentPtr.release());
153 m_agents.append(InspectorCSSAgent::create(m_domAgent, m_pageAgent, resourceAgent));
155 m_agents.append(InspectorDOMStorageAgent::create(m_pageAgent));
157 m_agents.append(InspectorMemoryAgent::create());
159 m_agents.append(InspectorApplicationCacheAgent::create(m_pageAgent));
161 PageScriptDebugServer* pageScriptDebugServer = &PageScriptDebugServer::shared();
163 OwnPtr<InspectorDebuggerAgent> debuggerAgentPtr(PageDebuggerAgent::create(pageScriptDebugServer, m_pageAgent, injectedScriptManager, overlay));
164 InspectorDebuggerAgent* debuggerAgent = debuggerAgentPtr.get();
165 m_agents.append(debuggerAgentPtr.release());
167 m_agents.append(InspectorDOMDebuggerAgent::create(m_domAgent, debuggerAgent));
169 m_agents.append(InspectorProfilerAgent::create(injectedScriptManager, overlay));
171 m_agents.append(InspectorHeapProfilerAgent::create(injectedScriptManager));
173 m_agents.append(InspectorCanvasAgent::create(m_pageAgent, injectedScriptManager));
175 m_agents.append(InspectorInputAgent::create(m_page, m_inspectorClient));
178 void InspectorController::inspectedPageDestroyed()
180 disconnectFrontend();
181 m_injectedScriptManager->disconnect();
182 m_inspectorClient = 0;
184 m_instrumentingAgents->reset();
187 void InspectorController::registerModuleAgent(PassOwnPtr<InspectorAgent> agent)
189 m_moduleAgents.append(agent.get());
190 m_agents.append(agent);
193 void InspectorController::setInspectorFrontendClient(PassOwnPtr<InspectorFrontendClient> inspectorFrontendClient)
195 m_inspectorFrontendClient = inspectorFrontendClient;
198 void InspectorController::didClearWindowObjectInMainWorld(LocalFrame* frame)
200 // If the page is supposed to serve as InspectorFrontend notify inspector frontend
201 // client that it's cleared so that the client can expose inspector bindings.
202 if (m_inspectorFrontendClient && frame == m_page->mainFrame())
203 m_inspectorFrontendClient->windowObjectCleared();
206 void InspectorController::connectFrontend(InspectorFrontendChannel* frontendChannel)
208 ASSERT(frontendChannel);
210 initializeDeferredAgents();
212 m_inspectorFrontend = adoptPtr(new InspectorFrontend(frontendChannel));
213 // We can reconnect to existing front-end -> unmute state.
216 m_agents.setFrontend(m_inspectorFrontend.get());
218 InspectorInstrumentation::registerInstrumentingAgents(m_instrumentingAgents.get());
219 InspectorInstrumentation::frontendCreated();
221 ASSERT(m_inspectorClient);
222 m_inspectorBackendDispatcher = InspectorBackendDispatcher::create(frontendChannel);
224 m_agents.registerInDispatcher(m_inspectorBackendDispatcher.get());
227 void InspectorController::disconnectFrontend()
229 if (!m_inspectorFrontend)
231 m_inspectorBackendDispatcher->clearFrontend();
232 m_inspectorBackendDispatcher.clear();
234 // Destroying agents would change the state, but we don't want that.
235 // Pre-disconnect state will be used to restore inspector agents.
238 m_agents.clearFrontend();
240 m_inspectorFrontend.clear();
242 // relese overlay page resources
243 m_overlay->freePage();
244 InspectorInstrumentation::frontendDeleted();
245 InspectorInstrumentation::unregisterInstrumentingAgents(m_instrumentingAgents.get());
248 void InspectorController::reconnectFrontend()
250 if (!m_inspectorFrontend)
252 InspectorFrontendChannel* frontendChannel = m_inspectorFrontend->channel();
253 disconnectFrontend();
254 connectFrontend(frontendChannel);
257 void InspectorController::reuseFrontend(InspectorFrontendChannel* frontendChannel, const String& inspectorStateCookie)
259 ASSERT(!m_inspectorFrontend);
260 connectFrontend(frontendChannel);
261 m_state->loadFromCookie(inspectorStateCookie);
265 void InspectorController::setProcessId(long processId)
267 IdentifiersFactory::setProcessId(processId);
270 void InspectorController::setLayerTreeId(int id)
272 m_timelineAgent->setLayerTreeId(id);
275 void InspectorController::webViewResized(const IntSize& size)
277 if (InspectorPageAgent* pageAgent = m_instrumentingAgents->inspectorPageAgent())
278 pageAgent->webViewResized(size);
281 bool InspectorController::isUnderTest()
283 return m_isUnderTest;
286 void InspectorController::evaluateForTestInFrontend(long callId, const String& script)
288 m_isUnderTest = true;
289 if (InspectorInspectorAgent* inspectorAgent = m_instrumentingAgents->inspectorInspectorAgent())
290 inspectorAgent->evaluateForTestInFrontend(callId, script);
293 void InspectorController::drawHighlight(GraphicsContext& context) const
295 m_overlay->paint(context);
298 void InspectorController::getHighlight(Highlight* highlight) const
300 m_overlay->getHighlight(highlight);
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(mainWorldScriptState(frame));
318 if (injectedScript.hasNoValue())
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 void InspectorController::hideHighlight()
337 m_overlay->hideHighlight();
340 Node* InspectorController::highlightedNode() const
342 return m_overlay->highlightedNode();
345 bool InspectorController::handleGestureEvent(LocalFrame* frame, const PlatformGestureEvent& event)
347 // Overlay should not consume events.
348 m_overlay->handleGestureEvent(event);
349 if (InspectorDOMAgent* domAgent = m_instrumentingAgents->inspectorDOMAgent())
350 return domAgent->handleGestureEvent(frame, event);
354 bool InspectorController::handleMouseEvent(LocalFrame* frame, const PlatformMouseEvent& event)
356 // Overlay should not consume events.
357 m_overlay->handleMouseEvent(event);
359 if (event.type() == PlatformEvent::MouseMoved) {
360 if (InspectorDOMAgent* domAgent = m_instrumentingAgents->inspectorDOMAgent())
361 domAgent->handleMouseMove(frame, event);
364 if (event.type() == PlatformEvent::MousePressed) {
365 if (InspectorDOMAgent* domAgent = m_instrumentingAgents->inspectorDOMAgent())
366 return domAgent->handleMousePress();
371 bool InspectorController::handleTouchEvent(LocalFrame* frame, const PlatformTouchEvent& event)
373 // Overlay should not consume events.
374 m_overlay->handleTouchEvent(event);
375 if (InspectorDOMAgent* domAgent = m_instrumentingAgents->inspectorDOMAgent())
376 return domAgent->handleTouchEvent(frame, event);
380 bool InspectorController::handleKeyboardEvent(LocalFrame* frame, const PlatformKeyboardEvent& event)
382 // Overlay should not consume events.
383 m_overlay->handleKeyboardEvent(event);
387 void InspectorController::requestPageScaleFactor(float scale, const IntPoint& origin)
389 m_inspectorClient->requestPageScaleFactor(scale, origin);
392 bool InspectorController::deviceEmulationEnabled()
394 if (InspectorPageAgent* pageAgent = m_instrumentingAgents->inspectorPageAgent())
395 return pageAgent->deviceMetricsOverrideEnabled();
399 void InspectorController::resume()
401 if (InspectorDebuggerAgent* debuggerAgent = m_instrumentingAgents->inspectorDebuggerAgent()) {
403 debuggerAgent->resume(&error);
407 void InspectorController::setResourcesDataSizeLimitsFromInternals(int maximumResourcesContentSize, int maximumSingleResourceContentSize)
409 if (InspectorResourceAgent* resourceAgent = m_instrumentingAgents->inspectorResourceAgent())
410 resourceAgent->setResourcesDataSizeLimitsFromInternals(maximumResourcesContentSize, maximumSingleResourceContentSize);
413 void InspectorController::willProcessTask()
415 if (InspectorTimelineAgent* timelineAgent = m_instrumentingAgents->inspectorTimelineAgent())
416 timelineAgent->willProcessTask();
417 if (InspectorProfilerAgent* profilerAgent = m_instrumentingAgents->inspectorProfilerAgent())
418 profilerAgent->willProcessTask();
421 void InspectorController::didProcessTask()
423 if (InspectorTimelineAgent* timelineAgent = m_instrumentingAgents->inspectorTimelineAgent())
424 timelineAgent->didProcessTask();
425 if (InspectorProfilerAgent* profilerAgent = m_instrumentingAgents->inspectorProfilerAgent())
426 profilerAgent->didProcessTask();
427 if (InspectorDOMDebuggerAgent* domDebuggerAgent = m_instrumentingAgents->inspectorDOMDebuggerAgent())
428 domDebuggerAgent->didProcessTask();
431 void InspectorController::flushPendingFrontendMessages()
433 m_agents.flushPendingFrontendMessages();
436 void InspectorController::didCommitLoadForMainFrame()
438 Vector<InspectorAgent*> agents = m_moduleAgents;
439 for (size_t i = 0; i < agents.size(); i++)
440 agents[i]->didCommitLoadForMainFrame();
443 void InspectorController::didBeginFrame(int frameId)
445 if (InspectorTimelineAgent* timelineAgent = m_instrumentingAgents->inspectorTimelineAgent())
446 timelineAgent->didBeginFrame(frameId);
447 if (InspectorCanvasAgent* canvasAgent = m_instrumentingAgents->inspectorCanvasAgent())
448 canvasAgent->didBeginFrame();
451 void InspectorController::didCancelFrame()
453 if (InspectorTimelineAgent* timelineAgent = m_instrumentingAgents->inspectorTimelineAgent())
454 timelineAgent->didCancelFrame();
457 void InspectorController::willComposite()
459 if (InspectorTimelineAgent* timelineAgent = m_instrumentingAgents->inspectorTimelineAgent())
460 timelineAgent->willComposite();
463 void InspectorController::didComposite()
465 if (InspectorTimelineAgent* timelineAgent = m_instrumentingAgents->inspectorTimelineAgent())
466 timelineAgent->didComposite();
469 void InspectorController::processGPUEvent(double timestamp, int phase, bool foreign, size_t usedGPUMemoryBytes)
471 if (InspectorTimelineAgent* timelineAgent = m_instrumentingAgents->inspectorTimelineAgent())
472 timelineAgent->processGPUEvent(InspectorTimelineAgent::GPUEvent(timestamp, phase, foreign, usedGPUMemoryBytes));
475 void InspectorController::scriptsEnabled(bool enabled)
477 if (InspectorPageAgent* pageAgent = m_instrumentingAgents->inspectorPageAgent())
478 pageAgent->scriptsEnabled(enabled);
481 void InspectorController::willAddPageOverlay(const GraphicsLayer* layer)
483 if (m_layerTreeAgent)
484 m_layerTreeAgent->willAddPageOverlay(layer);
487 void InspectorController::didRemovePageOverlay(const GraphicsLayer* layer)
489 if (m_layerTreeAgent)
490 m_layerTreeAgent->didRemovePageOverlay(layer);
493 } // namespace WebCore