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/InspectorDatabaseAgent.h"
48 #include "core/inspector/InspectorDebuggerAgent.h"
49 #include "core/inspector/InspectorFileSystemAgent.h"
50 #include "core/inspector/InspectorFrontendClient.h"
51 #include "core/inspector/InspectorHeapProfilerAgent.h"
52 #include "core/inspector/InspectorIndexedDBAgent.h"
53 #include "core/inspector/InspectorInputAgent.h"
54 #include "core/inspector/InspectorInspectorAgent.h"
55 #include "core/inspector/InspectorInstrumentation.h"
56 #include "core/inspector/InspectorLayerTreeAgent.h"
57 #include "core/inspector/InspectorMemoryAgent.h"
58 #include "core/inspector/InspectorOverlay.h"
59 #include "core/inspector/InspectorPageAgent.h"
60 #include "core/inspector/InspectorProfilerAgent.h"
61 #include "core/inspector/InspectorResourceAgent.h"
62 #include "core/inspector/InspectorState.h"
63 #include "core/inspector/InspectorTimelineAgent.h"
64 #include "core/inspector/InspectorWorkerAgent.h"
65 #include "core/inspector/InstrumentingAgents.h"
66 #include "core/inspector/PageConsoleAgent.h"
67 #include "core/inspector/PageDebuggerAgent.h"
68 #include "core/inspector/PageRuntimeAgent.h"
69 #include "core/page/Page.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(adoptPtr(new InspectorCompositeState(inspectorClient)))
78 , 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());
98 m_agents.append(InspectorDatabaseAgent::create());
100 OwnPtr<InspectorTimelineAgent> timelineAgentPtr(InspectorTimelineAgent::create(m_pageAgent, m_domAgent, overlay,
101 InspectorTimelineAgent::PageInspector, inspectorClient));
102 m_timelineAgent = timelineAgentPtr.get();
103 m_agents.append(timelineAgentPtr.release());
105 PageScriptDebugServer* pageScriptDebugServer = &PageScriptDebugServer::shared();
107 m_agents.append(PageRuntimeAgent::create(injectedScriptManager, pageScriptDebugServer, m_page, m_pageAgent));
109 m_agents.append(PageConsoleAgent::create(injectedScriptManager, m_domAgent, m_timelineAgent));
111 m_agents.append(InspectorWorkerAgent::create());
113 ASSERT_ARG(inspectorClient, inspectorClient);
114 m_injectedScriptManager->injectedScriptHost()->init(m_instrumentingAgents.get(), pageScriptDebugServer);
117 InspectorController::~InspectorController()
119 m_instrumentingAgents->reset();
120 m_agents.discardAgents();
121 ASSERT(!m_inspectorClient);
124 PassOwnPtr<InspectorController> InspectorController::create(Page* page, InspectorClient* client)
126 return adoptPtr(new InspectorController(page, client));
129 void InspectorController::initializeDeferredAgents()
131 if (m_deferredAgentsInitialized)
133 m_deferredAgentsInitialized = true;
135 InjectedScriptManager* injectedScriptManager = m_injectedScriptManager.get();
136 InspectorOverlay* overlay = m_overlay.get();
138 OwnPtr<InspectorResourceAgent> resourceAgentPtr(InspectorResourceAgent::create(m_pageAgent, m_inspectorClient));
139 InspectorResourceAgent* resourceAgent = resourceAgentPtr.get();
140 m_agents.append(resourceAgentPtr.release());
142 m_agents.append(InspectorCSSAgent::create(m_domAgent, m_pageAgent, resourceAgent));
144 m_agents.append(InspectorIndexedDBAgent::create(injectedScriptManager, m_pageAgent));
146 m_agents.append(InspectorFileSystemAgent::create(m_pageAgent));
148 m_agents.append(InspectorDOMStorageAgent::create(m_pageAgent));
150 m_agents.append(InspectorMemoryAgent::create());
152 m_agents.append(InspectorApplicationCacheAgent::create(m_pageAgent));
154 PageScriptDebugServer* pageScriptDebugServer = &PageScriptDebugServer::shared();
156 OwnPtr<InspectorDebuggerAgent> debuggerAgentPtr(PageDebuggerAgent::create(pageScriptDebugServer, m_pageAgent, injectedScriptManager, overlay));
157 InspectorDebuggerAgent* debuggerAgent = debuggerAgentPtr.get();
158 m_agents.append(debuggerAgentPtr.release());
160 m_agents.append(InspectorDOMDebuggerAgent::create(m_domAgent, debuggerAgent));
162 m_agents.append(InspectorProfilerAgent::create(injectedScriptManager, overlay));
164 m_agents.append(InspectorHeapProfilerAgent::create(injectedScriptManager));
166 m_agents.append(InspectorCanvasAgent::create(m_pageAgent, injectedScriptManager));
168 m_agents.append(InspectorInputAgent::create(m_page, m_inspectorClient));
170 m_agents.append(InspectorLayerTreeAgent::create(m_domAgent, m_page));
173 void InspectorController::inspectedPageDestroyed()
175 disconnectFrontend();
176 m_injectedScriptManager->disconnect();
177 m_inspectorClient = 0;
181 void InspectorController::registerModuleAgent(PassOwnPtr<InspectorAgent> agent)
183 m_agents.append(agent);
186 void InspectorController::setInspectorFrontendClient(PassOwnPtr<InspectorFrontendClient> inspectorFrontendClient)
188 m_inspectorFrontendClient = inspectorFrontendClient;
191 void InspectorController::didClearWindowObjectInMainWorld(Frame* frame)
193 // If the page is supposed to serve as InspectorFrontend notify inspector frontend
194 // client that it's cleared so that the client can expose inspector bindings.
195 if (m_inspectorFrontendClient && frame == m_page->mainFrame())
196 m_inspectorFrontendClient->windowObjectCleared();
199 void InspectorController::connectFrontend(InspectorFrontendChannel* frontendChannel)
201 ASSERT(frontendChannel);
203 initializeDeferredAgents();
205 m_inspectorFrontend = adoptPtr(new InspectorFrontend(frontendChannel));
206 // We can reconnect to existing front-end -> unmute state.
209 m_agents.setFrontend(m_inspectorFrontend.get());
211 InspectorInstrumentation::registerInstrumentingAgents(m_instrumentingAgents.get());
212 InspectorInstrumentation::frontendCreated();
214 ASSERT(m_inspectorClient);
215 m_inspectorBackendDispatcher = InspectorBackendDispatcher::create(frontendChannel);
217 m_agents.registerInDispatcher(m_inspectorBackendDispatcher.get());
220 void InspectorController::disconnectFrontend()
222 if (!m_inspectorFrontend)
224 m_inspectorBackendDispatcher->clearFrontend();
225 m_inspectorBackendDispatcher.clear();
227 // Destroying agents would change the state, but we don't want that.
228 // Pre-disconnect state will be used to restore inspector agents.
231 m_agents.clearFrontend();
233 m_inspectorFrontend.clear();
235 // relese overlay page resources
236 m_overlay->freePage();
237 InspectorInstrumentation::frontendDeleted();
238 InspectorInstrumentation::unregisterInstrumentingAgents(m_instrumentingAgents.get());
241 void InspectorController::reconnectFrontend()
243 if (!m_inspectorFrontend)
245 InspectorFrontendChannel* frontendChannel = m_inspectorFrontend->inspector()->getInspectorFrontendChannel();
246 disconnectFrontend();
247 connectFrontend(frontendChannel);
250 void InspectorController::reuseFrontend(InspectorFrontendChannel* frontendChannel, const String& inspectorStateCookie)
252 ASSERT(!m_inspectorFrontend);
253 connectFrontend(frontendChannel);
254 m_state->loadFromCookie(inspectorStateCookie);
258 void InspectorController::setProcessId(long processId)
260 IdentifiersFactory::setProcessId(processId);
263 void InspectorController::setLayerTreeId(int id)
265 m_timelineAgent->setLayerTreeId(id);
268 void InspectorController::webViewResized(const IntSize& size)
270 if (InspectorPageAgent* pageAgent = m_instrumentingAgents->inspectorPageAgent())
271 pageAgent->webViewResized(size);
274 bool InspectorController::isUnderTest()
276 return m_isUnderTest;
279 void InspectorController::evaluateForTestInFrontend(long callId, const String& script)
281 m_isUnderTest = true;
282 if (InspectorInspectorAgent* inspectorAgent = m_instrumentingAgents->inspectorInspectorAgent())
283 inspectorAgent->evaluateForTestInFrontend(callId, script);
286 void InspectorController::drawHighlight(GraphicsContext& context) const
288 m_overlay->paint(context);
291 void InspectorController::getHighlight(Highlight* highlight) const
293 m_overlay->getHighlight(highlight);
296 void InspectorController::inspect(Node* node)
300 Document* document = node->ownerDocument();
303 Frame* frame = document->frame();
307 if (node->nodeType() != Node::ELEMENT_NODE && node->nodeType() != Node::DOCUMENT_NODE)
308 node = node->parentNode();
310 InjectedScript injectedScript = m_injectedScriptManager->injectedScriptFor(mainWorldScriptState(frame));
311 if (injectedScript.hasNoValue())
313 injectedScript.inspectNode(node);
316 void InspectorController::setInjectedScriptForOrigin(const String& origin, const String& source)
318 if (InspectorInspectorAgent* inspectorAgent = m_instrumentingAgents->inspectorInspectorAgent())
319 inspectorAgent->setInjectedScriptForOrigin(origin, source);
322 void InspectorController::dispatchMessageFromFrontend(const String& message)
324 if (m_inspectorBackendDispatcher)
325 m_inspectorBackendDispatcher->dispatch(message);
328 void InspectorController::hideHighlight()
330 m_overlay->hideHighlight();
333 Node* InspectorController::highlightedNode() const
335 return m_overlay->highlightedNode();
338 bool InspectorController::handleGestureEvent(Frame* frame, const PlatformGestureEvent& event)
340 // Overlay should not consume events.
341 m_overlay->handleGestureEvent(event);
342 if (InspectorDOMAgent* domAgent = m_instrumentingAgents->inspectorDOMAgent())
343 return domAgent->handleGestureEvent(frame, event);
347 bool InspectorController::handleMouseEvent(Frame* frame, const PlatformMouseEvent& event)
349 // Overlay should not consume events.
350 m_overlay->handleMouseEvent(event);
352 if (event.type() == PlatformEvent::MouseMoved) {
353 if (InspectorDOMAgent* domAgent = m_instrumentingAgents->inspectorDOMAgent())
354 domAgent->handleMouseMove(frame, event);
357 if (event.type() == PlatformEvent::MousePressed) {
358 if (InspectorDOMAgent* domAgent = m_instrumentingAgents->inspectorDOMAgent())
359 return domAgent->handleMousePress();
364 bool InspectorController::handleTouchEvent(Frame* frame, const PlatformTouchEvent& event)
366 // Overlay should not consume events.
367 m_overlay->handleTouchEvent(event);
368 if (InspectorDOMAgent* domAgent = m_instrumentingAgents->inspectorDOMAgent())
369 return domAgent->handleTouchEvent(frame, event);
373 bool InspectorController::handleKeyboardEvent(Frame* frame, const PlatformKeyboardEvent& event)
375 // Overlay should not consume events.
376 m_overlay->handleKeyboardEvent(event);
380 void InspectorController::requestPageScaleFactor(float scale, const IntPoint& origin)
382 m_inspectorClient->requestPageScaleFactor(scale, origin);
385 bool InspectorController::deviceEmulationEnabled()
387 if (InspectorPageAgent* pageAgent = m_instrumentingAgents->inspectorPageAgent())
388 return pageAgent->deviceMetricsOverrideEnabled();
392 void InspectorController::resume()
394 if (InspectorDebuggerAgent* debuggerAgent = m_instrumentingAgents->inspectorDebuggerAgent()) {
396 debuggerAgent->resume(&error);
400 void InspectorController::setResourcesDataSizeLimitsFromInternals(int maximumResourcesContentSize, int maximumSingleResourceContentSize)
402 if (InspectorResourceAgent* resourceAgent = m_instrumentingAgents->inspectorResourceAgent())
403 resourceAgent->setResourcesDataSizeLimitsFromInternals(maximumResourcesContentSize, maximumSingleResourceContentSize);
406 void InspectorController::willProcessTask()
408 if (InspectorTimelineAgent* timelineAgent = m_instrumentingAgents->inspectorTimelineAgent())
409 timelineAgent->willProcessTask();
410 if (InspectorProfilerAgent* profilerAgent = m_instrumentingAgents->inspectorProfilerAgent())
411 profilerAgent->willProcessTask();
414 void InspectorController::didProcessTask()
416 if (InspectorTimelineAgent* timelineAgent = m_instrumentingAgents->inspectorTimelineAgent())
417 timelineAgent->didProcessTask();
418 if (InspectorProfilerAgent* profilerAgent = m_instrumentingAgents->inspectorProfilerAgent())
419 profilerAgent->didProcessTask();
420 if (InspectorDOMDebuggerAgent* domDebuggerAgent = m_instrumentingAgents->inspectorDOMDebuggerAgent())
421 domDebuggerAgent->didProcessTask();
424 void InspectorController::didBeginFrame(int frameId)
426 if (InspectorTimelineAgent* timelineAgent = m_instrumentingAgents->inspectorTimelineAgent())
427 timelineAgent->didBeginFrame(frameId);
428 if (InspectorCanvasAgent* canvasAgent = m_instrumentingAgents->inspectorCanvasAgent())
429 canvasAgent->didBeginFrame();
432 void InspectorController::didCancelFrame()
434 if (InspectorTimelineAgent* timelineAgent = m_instrumentingAgents->inspectorTimelineAgent())
435 timelineAgent->didCancelFrame();
438 void InspectorController::willComposite()
440 if (InspectorTimelineAgent* timelineAgent = m_instrumentingAgents->inspectorTimelineAgent())
441 timelineAgent->willComposite();
444 void InspectorController::didComposite()
446 if (InspectorTimelineAgent* timelineAgent = m_instrumentingAgents->inspectorTimelineAgent())
447 timelineAgent->didComposite();
450 void InspectorController::processGPUEvent(double timestamp, int phase, bool foreign, size_t usedGPUMemoryBytes)
452 if (InspectorTimelineAgent* timelineAgent = m_instrumentingAgents->inspectorTimelineAgent())
453 timelineAgent->processGPUEvent(InspectorTimelineAgent::GPUEvent(timestamp, phase, foreign, usedGPUMemoryBytes));
456 } // namespace WebCore