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/InspectorAgent.h"
41 #include "core/inspector/InspectorApplicationCacheAgent.h"
42 #include "core/inspector/InspectorBaseAgent.h"
43 #include "core/inspector/InspectorCSSAgent.h"
44 #include "core/inspector/InspectorCanvasAgent.h"
45 #include "core/inspector/InspectorClient.h"
46 #include "core/inspector/InspectorDOMAgent.h"
47 #include "core/inspector/InspectorDOMDebuggerAgent.h"
48 #include "core/inspector/InspectorDOMStorageAgent.h"
49 #include "core/inspector/InspectorDatabaseAgent.h"
50 #include "core/inspector/InspectorDebuggerAgent.h"
51 #include "core/inspector/InspectorFileSystemAgent.h"
52 #include "core/inspector/InspectorFrontendClient.h"
53 #include "core/inspector/InspectorHeapProfilerAgent.h"
54 #include "core/inspector/InspectorIndexedDBAgent.h"
55 #include "core/inspector/InspectorInputAgent.h"
56 #include "core/inspector/InspectorInstrumentation.h"
57 #include "core/inspector/InspectorLayerTreeAgent.h"
58 #include "core/inspector/InspectorMemoryAgent.h"
59 #include "core/inspector/InspectorOverlay.h"
60 #include "core/inspector/InspectorPageAgent.h"
61 #include "core/inspector/InspectorProfilerAgent.h"
62 #include "core/inspector/InspectorResourceAgent.h"
63 #include "core/inspector/InspectorState.h"
64 #include "core/inspector/InspectorTimelineAgent.h"
65 #include "core/inspector/InspectorWorkerAgent.h"
66 #include "core/inspector/InstrumentingAgents.h"
67 #include "core/inspector/PageConsoleAgent.h"
68 #include "core/inspector/PageDebuggerAgent.h"
69 #include "core/inspector/PageRuntimeAgent.h"
70 #include "core/page/Page.h"
71 #include "platform/PlatformMouseEvent.h"
75 InspectorController::InspectorController(Page* page, InspectorClient* inspectorClient)
76 : m_instrumentingAgents(InstrumentingAgents::create())
77 , m_injectedScriptManager(InjectedScriptManager::createForPage())
78 , m_state(adoptPtr(new InspectorCompositeState(inspectorClient)))
79 , m_overlay(InspectorOverlay::create(page, inspectorClient))
81 , m_inspectorClient(inspectorClient)
82 , m_isUnderTest(false)
84 m_agents.append(InspectorAgent::create(page, m_injectedScriptManager.get(), m_instrumentingAgents.get(), m_state.get()));
86 OwnPtr<InspectorPageAgent> pageAgentPtr(InspectorPageAgent::create(m_instrumentingAgents.get(), page, m_state.get(), m_injectedScriptManager.get(), inspectorClient, m_overlay.get()));
87 InspectorPageAgent* pageAgent = pageAgentPtr.get();
88 m_agents.append(pageAgentPtr.release());
90 OwnPtr<InspectorDOMAgent> domAgentPtr(InspectorDOMAgent::create(m_instrumentingAgents.get(), pageAgent, m_state.get(), m_injectedScriptManager.get(), m_overlay.get(), inspectorClient));
91 InspectorDOMAgent* domAgent = domAgentPtr.get();
92 m_agents.append(domAgentPtr.release());
94 OwnPtr<InspectorResourceAgent> resourceAgentPtr(InspectorResourceAgent::create(m_instrumentingAgents.get(), pageAgent, inspectorClient, m_state.get()));
95 InspectorResourceAgent* resourceAgent = resourceAgentPtr.get();
96 m_agents.append(resourceAgentPtr.release());
98 m_agents.append(InspectorCSSAgent::create(m_instrumentingAgents.get(), m_state.get(), domAgent, pageAgent, resourceAgent));
100 m_agents.append(InspectorDatabaseAgent::create(m_instrumentingAgents.get(), m_state.get()));
102 m_agents.append(InspectorIndexedDBAgent::create(m_instrumentingAgents.get(), m_state.get(), m_injectedScriptManager.get(), pageAgent));
104 m_agents.append(InspectorFileSystemAgent::create(m_instrumentingAgents.get(), pageAgent, m_state.get()));
106 m_agents.append(InspectorDOMStorageAgent::create(m_instrumentingAgents.get(), pageAgent, m_state.get()));
108 OwnPtr<InspectorMemoryAgent> memoryAgentPtr(InspectorMemoryAgent::create(m_instrumentingAgents.get(), m_state.get()));
109 m_memoryAgent = memoryAgentPtr.get();
110 m_agents.append(memoryAgentPtr.release());
112 OwnPtr<InspectorTimelineAgent> timelineAgentPtr(InspectorTimelineAgent::create(m_instrumentingAgents.get(), pageAgent, m_memoryAgent, domAgent, m_state.get(),
113 InspectorTimelineAgent::PageInspector, inspectorClient));
114 m_timelineAgent = timelineAgentPtr.get();
115 m_agents.append(timelineAgentPtr.release());
117 m_agents.append(InspectorApplicationCacheAgent::create(m_instrumentingAgents.get(), m_state.get(), pageAgent));
119 PageScriptDebugServer* pageScriptDebugServer = &PageScriptDebugServer::shared();
121 m_agents.append(PageRuntimeAgent::create(m_instrumentingAgents.get(), m_state.get(), m_injectedScriptManager.get(), pageScriptDebugServer, page, pageAgent));
123 OwnPtr<InspectorConsoleAgent> consoleAgentPtr(PageConsoleAgent::create(m_instrumentingAgents.get(), m_state.get(), m_injectedScriptManager.get(), domAgent, m_timelineAgent));
124 InspectorConsoleAgent* consoleAgent = consoleAgentPtr.get();
125 m_agents.append(consoleAgentPtr.release());
127 OwnPtr<InspectorDebuggerAgent> debuggerAgentPtr(PageDebuggerAgent::create(m_instrumentingAgents.get(), m_state.get(), pageScriptDebugServer, pageAgent, m_injectedScriptManager.get(), m_overlay.get()));
128 InspectorDebuggerAgent* debuggerAgent = debuggerAgentPtr.get();
129 m_agents.append(debuggerAgentPtr.release());
131 m_agents.append(InspectorDOMDebuggerAgent::create(m_instrumentingAgents.get(), m_state.get(), domAgent, debuggerAgent));
133 m_agents.append(InspectorProfilerAgent::create(m_instrumentingAgents.get(), consoleAgent, m_state.get(), m_injectedScriptManager.get()));
135 m_agents.append(InspectorHeapProfilerAgent::create(m_instrumentingAgents.get(), m_state.get(), m_injectedScriptManager.get()));
138 m_agents.append(InspectorWorkerAgent::create(m_instrumentingAgents.get(), m_state.get()));
140 m_agents.append(InspectorCanvasAgent::create(m_instrumentingAgents.get(), m_state.get(), pageAgent, m_injectedScriptManager.get()));
142 m_agents.append(InspectorInputAgent::create(m_instrumentingAgents.get(), m_state.get(), page, inspectorClient));
144 m_agents.append(InspectorLayerTreeAgent::create(m_instrumentingAgents.get(), m_state.get(), domAgent, page));
146 ASSERT_ARG(inspectorClient, inspectorClient);
147 m_injectedScriptManager->injectedScriptHost()->init(m_instrumentingAgents.get(), pageScriptDebugServer);
150 InspectorController::~InspectorController()
152 m_instrumentingAgents->reset();
153 m_agents.discardAgents();
154 ASSERT(!m_inspectorClient);
157 PassOwnPtr<InspectorController> InspectorController::create(Page* page, InspectorClient* client)
159 return adoptPtr(new InspectorController(page, client));
162 void InspectorController::inspectedPageDestroyed()
164 disconnectFrontend();
165 m_injectedScriptManager->disconnect();
166 m_inspectorClient = 0;
170 void InspectorController::setInspectorFrontendClient(PassOwnPtr<InspectorFrontendClient> inspectorFrontendClient)
172 m_inspectorFrontendClient = inspectorFrontendClient;
175 void InspectorController::didClearWindowObjectInWorld(Frame* frame, DOMWrapperWorld* world)
177 if (world != mainThreadNormalWorld())
180 // If the page is supposed to serve as InspectorFrontend notify inspector frontend
181 // client that it's cleared so that the client can expose inspector bindings.
182 if (m_inspectorFrontendClient && frame == m_page->mainFrame())
183 m_inspectorFrontendClient->windowObjectCleared();
186 void InspectorController::connectFrontend(InspectorFrontendChannel* frontendChannel)
188 ASSERT(frontendChannel);
190 m_inspectorFrontend = adoptPtr(new InspectorFrontend(frontendChannel));
191 // We can reconnect to existing front-end -> unmute state.
194 m_agents.setFrontend(m_inspectorFrontend.get());
196 InspectorInstrumentation::registerInstrumentingAgents(m_instrumentingAgents.get());
197 InspectorInstrumentation::frontendCreated();
199 ASSERT(m_inspectorClient);
200 m_inspectorBackendDispatcher = InspectorBackendDispatcher::create(frontendChannel);
202 m_agents.registerInDispatcher(m_inspectorBackendDispatcher.get());
205 void InspectorController::disconnectFrontend()
207 if (!m_inspectorFrontend)
209 m_inspectorBackendDispatcher->clearFrontend();
210 m_inspectorBackendDispatcher.clear();
212 // Destroying agents would change the state, but we don't want that.
213 // Pre-disconnect state will be used to restore inspector agents.
216 m_agents.clearFrontend();
218 m_inspectorFrontend.clear();
220 // relese overlay page resources
221 m_overlay->freePage();
222 InspectorInstrumentation::frontendDeleted();
223 InspectorInstrumentation::unregisterInstrumentingAgents(m_instrumentingAgents.get());
226 void InspectorController::reconnectFrontend()
228 if (!m_inspectorFrontend)
230 InspectorFrontendChannel* frontendChannel = m_inspectorFrontend->inspector()->getInspectorFrontendChannel();
231 disconnectFrontend();
232 connectFrontend(frontendChannel);
235 void InspectorController::reuseFrontend(InspectorFrontendChannel* frontendChannel, const String& inspectorStateCookie)
237 ASSERT(!m_inspectorFrontend);
238 connectFrontend(frontendChannel);
239 m_state->loadFromCookie(inspectorStateCookie);
243 void InspectorController::setProcessId(long processId)
245 IdentifiersFactory::setProcessId(processId);
248 void InspectorController::setLayerTreeId(int id)
250 m_timelineAgent->setLayerTreeId(id);
253 void InspectorController::webViewResized(const IntSize& size)
255 if (InspectorPageAgent* pageAgent = m_instrumentingAgents->inspectorPageAgent())
256 pageAgent->webViewResized(size);
259 bool InspectorController::isUnderTest()
261 return m_isUnderTest;
264 void InspectorController::evaluateForTestInFrontend(long callId, const String& script)
266 m_isUnderTest = true;
267 if (InspectorAgent* inspectorAgent = m_instrumentingAgents->inspectorAgent())
268 inspectorAgent->evaluateForTestInFrontend(callId, script);
271 void InspectorController::drawHighlight(GraphicsContext& context) const
273 m_overlay->paint(context);
276 void InspectorController::getHighlight(Highlight* highlight) const
278 m_overlay->getHighlight(highlight);
281 void InspectorController::inspect(Node* node)
285 Document* document = node->ownerDocument();
288 Frame* frame = document->frame();
292 if (node->nodeType() != Node::ELEMENT_NODE && node->nodeType() != Node::DOCUMENT_NODE)
293 node = node->parentNode();
295 InjectedScript injectedScript = m_injectedScriptManager->injectedScriptFor(mainWorldScriptState(frame));
296 if (injectedScript.hasNoValue())
298 injectedScript.inspectNode(node);
301 Page* InspectorController::inspectedPage() const
306 void InspectorController::setInjectedScriptForOrigin(const String& origin, const String& source)
308 if (InspectorAgent* inspectorAgent = m_instrumentingAgents->inspectorAgent())
309 inspectorAgent->setInjectedScriptForOrigin(origin, source);
312 void InspectorController::dispatchMessageFromFrontend(const String& message)
314 if (m_inspectorBackendDispatcher)
315 m_inspectorBackendDispatcher->dispatch(message);
318 void InspectorController::hideHighlight()
320 m_overlay->hideHighlight();
323 Node* InspectorController::highlightedNode() const
325 return m_overlay->highlightedNode();
328 bool InspectorController::handleGestureEvent(Frame* frame, const PlatformGestureEvent& event)
330 // Overlay should not consume events.
331 m_overlay->handleGestureEvent(event);
332 if (InspectorDOMAgent* domAgent = m_instrumentingAgents->inspectorDOMAgent())
333 return domAgent->handleGestureEvent(frame, event);
337 bool InspectorController::handleMouseEvent(Frame* frame, const PlatformMouseEvent& event)
339 // Overlay should not consume events.
340 m_overlay->handleMouseEvent(event);
342 if (event.type() == PlatformEvent::MouseMoved) {
343 if (InspectorDOMAgent* domAgent = m_instrumentingAgents->inspectorDOMAgent())
344 domAgent->handleMouseMove(frame, event);
347 if (event.type() == PlatformEvent::MousePressed) {
348 if (InspectorDOMAgent* domAgent = m_instrumentingAgents->inspectorDOMAgent())
349 return domAgent->handleMousePress();
354 bool InspectorController::handleTouchEvent(Frame* frame, const PlatformTouchEvent& event)
356 // Overlay should not consume events.
357 m_overlay->handleTouchEvent(event);
358 if (InspectorDOMAgent* domAgent = m_instrumentingAgents->inspectorDOMAgent())
359 return domAgent->handleTouchEvent(frame, event);
363 void InspectorController::requestPageScaleFactor(float scale, const IntPoint& origin)
365 m_inspectorClient->requestPageScaleFactor(scale, origin);
368 bool InspectorController::deviceEmulationEnabled()
370 if (InspectorPageAgent* pageAgent = m_instrumentingAgents->inspectorPageAgent())
371 return pageAgent->deviceMetricsOverrideEnabled();
375 void InspectorController::resume()
377 if (InspectorDebuggerAgent* debuggerAgent = m_instrumentingAgents->inspectorDebuggerAgent()) {
379 debuggerAgent->resume(&error);
383 void InspectorController::setResourcesDataSizeLimitsFromInternals(int maximumResourcesContentSize, int maximumSingleResourceContentSize)
385 if (InspectorResourceAgent* resourceAgent = m_instrumentingAgents->inspectorResourceAgent())
386 resourceAgent->setResourcesDataSizeLimitsFromInternals(maximumResourcesContentSize, maximumSingleResourceContentSize);
389 void InspectorController::willProcessTask()
391 if (InspectorTimelineAgent* timelineAgent = m_instrumentingAgents->inspectorTimelineAgent())
392 timelineAgent->willProcessTask();
393 if (InspectorProfilerAgent* profilerAgent = m_instrumentingAgents->inspectorProfilerAgent())
394 profilerAgent->willProcessTask();
397 void InspectorController::didProcessTask()
399 if (InspectorTimelineAgent* timelineAgent = m_instrumentingAgents->inspectorTimelineAgent())
400 timelineAgent->didProcessTask();
401 if (InspectorProfilerAgent* profilerAgent = m_instrumentingAgents->inspectorProfilerAgent())
402 profilerAgent->didProcessTask();
403 if (InspectorDOMDebuggerAgent* domDebuggerAgent = m_instrumentingAgents->inspectorDOMDebuggerAgent())
404 domDebuggerAgent->didProcessTask();
407 void InspectorController::didBeginFrame(int frameId)
409 if (InspectorTimelineAgent* timelineAgent = m_instrumentingAgents->inspectorTimelineAgent())
410 timelineAgent->didBeginFrame(frameId);
411 if (InspectorCanvasAgent* canvasAgent = m_instrumentingAgents->inspectorCanvasAgent())
412 canvasAgent->didBeginFrame();
415 void InspectorController::didCancelFrame()
417 if (InspectorTimelineAgent* timelineAgent = m_instrumentingAgents->inspectorTimelineAgent())
418 timelineAgent->didCancelFrame();
421 void InspectorController::willComposite()
423 if (InspectorTimelineAgent* timelineAgent = m_instrumentingAgents->inspectorTimelineAgent())
424 timelineAgent->willComposite();
427 void InspectorController::didComposite()
429 if (InspectorTimelineAgent* timelineAgent = m_instrumentingAgents->inspectorTimelineAgent())
430 timelineAgent->didComposite();
433 } // namespace WebCore