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.
35 #include "InspectorController.h"
37 #include "DOMNodeHighlighter.h"
39 #include "GraphicsContext.h"
40 #include "IdentifiersFactory.h"
41 #include "InjectedScriptHost.h"
42 #include "InjectedScriptManager.h"
43 #include "InspectorAgent.h"
44 #include "InspectorApplicationCacheAgent.h"
45 #include "InspectorBackendDispatcher.h"
46 #include "InspectorBaseAgent.h"
47 #include "InspectorCSSAgent.h"
48 #include "InspectorClient.h"
49 #include "InspectorDOMAgent.h"
50 #include "InspectorDOMDebuggerAgent.h"
51 #include "InspectorDOMStorageAgent.h"
52 #include "InspectorDatabaseAgent.h"
53 #include "InspectorDebuggerAgent.h"
54 #include "InspectorFileSystemAgent.h"
55 #include "InspectorFrontend.h"
56 #include "InspectorFrontendClient.h"
57 #include "InspectorIndexedDBAgent.h"
58 #include "InspectorInstrumentation.h"
59 #include "InspectorMemoryAgent.h"
60 #include "InspectorPageAgent.h"
61 #include "InspectorProfilerAgent.h"
62 #include "InspectorResourceAgent.h"
63 #include "InspectorState.h"
64 #include "InspectorTimelineAgent.h"
65 #include "InspectorWebGLAgent.h"
66 #include "InspectorWorkerAgent.h"
67 #include "InstrumentingAgents.h"
68 #include "PageConsoleAgent.h"
69 #include "PageDebuggerAgent.h"
70 #include "PageRuntimeAgent.h"
72 #include "ScriptObject.h"
74 #include <wtf/UnusedParam.h>
78 InspectorController::InspectorController(Page* page, InspectorClient* inspectorClient)
79 : m_instrumentingAgents(adoptPtr(new InstrumentingAgents()))
80 , m_injectedScriptManager(InjectedScriptManager::createForPage())
81 , m_state(adoptPtr(new InspectorState(inspectorClient)))
82 , m_overlay(InspectorOverlay::create(page, inspectorClient))
84 , m_inspectorClient(inspectorClient)
86 OwnPtr<InspectorAgent> inspectorAgentPtr(InspectorAgent::create(page, m_injectedScriptManager.get(), m_instrumentingAgents.get(), m_state.get()));
87 m_inspectorAgent = inspectorAgentPtr.get();
88 m_agents.append(inspectorAgentPtr.release());
90 OwnPtr<InspectorPageAgent> pageAgentPtr(InspectorPageAgent::create(m_instrumentingAgents.get(), page, m_inspectorAgent, m_state.get(), m_injectedScriptManager.get(), inspectorClient, m_overlay.get()));
91 InspectorPageAgent* pageAgent = pageAgentPtr.get();
92 m_pageAgent = pageAgentPtr.get();
93 m_agents.append(pageAgentPtr.release());
95 OwnPtr<InspectorDOMAgent> domAgentPtr(InspectorDOMAgent::create(m_instrumentingAgents.get(), pageAgent, m_state.get(), m_injectedScriptManager.get(), m_overlay.get()));
96 m_domAgent = domAgentPtr.get();
97 m_agents.append(domAgentPtr.release());
99 m_agents.append(InspectorCSSAgent::create(m_instrumentingAgents.get(), m_state.get(), m_domAgent));
101 #if ENABLE(SQL_DATABASE)
102 OwnPtr<InspectorDatabaseAgent> databaseAgentPtr(InspectorDatabaseAgent::create(m_instrumentingAgents.get(), m_state.get()));
103 InspectorDatabaseAgent* databaseAgent = databaseAgentPtr.get();
104 m_agents.append(databaseAgentPtr.release());
107 #if ENABLE(INDEXED_DATABASE)
108 m_agents.append(InspectorIndexedDBAgent::create(m_instrumentingAgents.get(), m_state.get(), m_injectedScriptManager.get(), pageAgent));
111 #if ENABLE(FILE_SYSTEM)
112 m_agents.append(InspectorFileSystemAgent::create(m_instrumentingAgents.get(), pageAgent, m_state.get()));
114 OwnPtr<InspectorDOMStorageAgent> domStorageAgentPtr(InspectorDOMStorageAgent::create(m_instrumentingAgents.get(), m_state.get()));
115 InspectorDOMStorageAgent* domStorageAgent = domStorageAgentPtr.get();
116 m_agents.append(domStorageAgentPtr.release());
117 m_agents.append(InspectorMemoryAgent::create(m_instrumentingAgents.get(), m_state.get(), m_page, m_domAgent));
118 m_agents.append(InspectorTimelineAgent::create(m_instrumentingAgents.get(), pageAgent, m_state.get(), InspectorTimelineAgent::PageInspector,
120 m_agents.append(InspectorApplicationCacheAgent::create(m_instrumentingAgents.get(), m_state.get(), pageAgent));
122 OwnPtr<InspectorResourceAgent> resourceAgentPtr(InspectorResourceAgent::create(m_instrumentingAgents.get(), pageAgent, inspectorClient, m_state.get()));
123 m_resourceAgent = resourceAgentPtr.get();
124 m_agents.append(resourceAgentPtr.release());
126 OwnPtr<InspectorRuntimeAgent> runtimeAgentPtr(PageRuntimeAgent::create(m_instrumentingAgents.get(), m_state.get(), m_injectedScriptManager.get(), page, pageAgent, m_inspectorAgent));
127 InspectorRuntimeAgent* runtimeAgent = runtimeAgentPtr.get();
128 m_agents.append(runtimeAgentPtr.release());
130 OwnPtr<InspectorConsoleAgent> consoleAgentPtr(PageConsoleAgent::create(m_instrumentingAgents.get(), m_inspectorAgent, m_state.get(), m_injectedScriptManager.get(), m_domAgent));
131 InspectorConsoleAgent* consoleAgent = consoleAgentPtr.get();
132 m_agents.append(consoleAgentPtr.release());
134 #if ENABLE(JAVASCRIPT_DEBUGGER)
135 OwnPtr<InspectorDebuggerAgent> debuggerAgentPtr(PageDebuggerAgent::create(m_instrumentingAgents.get(), m_state.get(), page, m_injectedScriptManager.get(), m_overlay.get()));
136 m_debuggerAgent = debuggerAgentPtr.get();
137 m_agents.append(debuggerAgentPtr.release());
139 m_agents.append(InspectorDOMDebuggerAgent::create(m_instrumentingAgents.get(), m_state.get(), m_domAgent, m_debuggerAgent, m_inspectorAgent));
141 OwnPtr<InspectorProfilerAgent> profilerAgentPtr(InspectorProfilerAgent::create(m_instrumentingAgents.get(), consoleAgent, page, m_state.get(), m_injectedScriptManager.get()));
142 m_profilerAgent = profilerAgentPtr.get();
143 m_agents.append(profilerAgentPtr.release());
147 m_agents.append(InspectorWorkerAgent::create(m_instrumentingAgents.get(), m_state.get()));
151 m_agents.append(InspectorWebGLAgent::create(m_instrumentingAgents.get(), m_state.get(), m_injectedScriptManager.get()));
154 ASSERT_ARG(inspectorClient, inspectorClient);
155 m_injectedScriptManager->injectedScriptHost()->init(m_inspectorAgent
157 #if ENABLE(SQL_DATABASE)
165 #if ENABLE(JAVASCRIPT_DEBUGGER)
166 runtimeAgent->setScriptDebugServer(&m_debuggerAgent->scriptDebugServer());
170 InspectorController::~InspectorController()
172 for (Agents::iterator it = m_agents.begin(); it != m_agents.end(); ++it)
173 (*it)->discardAgent();
175 ASSERT(!m_inspectorClient);
178 PassOwnPtr<InspectorController> InspectorController::create(Page* page, InspectorClient* client)
180 return adoptPtr(new InspectorController(page, client));
183 void InspectorController::inspectedPageDestroyed()
185 disconnectFrontend();
186 m_injectedScriptManager->disconnect();
187 m_inspectorClient->inspectorDestroyed();
188 m_inspectorClient = 0;
192 void InspectorController::setInspectorFrontendClient(PassOwnPtr<InspectorFrontendClient> inspectorFrontendClient)
194 m_inspectorFrontendClient = inspectorFrontendClient;
197 bool InspectorController::hasInspectorFrontendClient() const
199 return m_inspectorFrontendClient;
202 void InspectorController::didClearWindowObjectInWorld(Frame* frame, DOMWrapperWorld* world)
204 if (world != mainThreadNormalWorld())
207 // If the page is supposed to serve as InspectorFrontend notify inspector frontend
208 // client that it's cleared so that the client can expose inspector bindings.
209 if (m_inspectorFrontendClient && frame == m_page->mainFrame())
210 m_inspectorFrontendClient->windowObjectCleared();
213 void InspectorController::connectFrontend(InspectorFrontendChannel* frontendChannel)
215 ASSERT(frontendChannel);
217 m_inspectorFrontend = adoptPtr(new InspectorFrontend(frontendChannel));
218 // We can reconnect to existing front-end -> unmute state.
221 InspectorFrontend* frontend = m_inspectorFrontend.get();
222 for (Agents::iterator it = m_agents.begin(); it != m_agents.end(); ++it)
223 (*it)->setFrontend(frontend);
225 if (!InspectorInstrumentation::hasFrontends())
226 ScriptController::setCaptureCallStackForUncaughtExceptions(true);
227 InspectorInstrumentation::frontendCreated();
229 ASSERT(m_inspectorClient);
230 m_inspectorBackendDispatcher = InspectorBackendDispatcher::create(frontendChannel);
232 InspectorBackendDispatcher* dispatcher = m_inspectorBackendDispatcher.get();
233 for (Agents::iterator it = m_agents.begin(); it != m_agents.end(); ++it)
234 (*it)->registerInDispatcher(dispatcher);
237 void InspectorController::disconnectFrontend()
239 if (!m_inspectorFrontend)
241 m_inspectorBackendDispatcher->clearFrontend();
242 m_inspectorBackendDispatcher.clear();
244 // Destroying agents would change the state, but we don't want that.
245 // Pre-disconnect state will be used to restore inspector agents.
248 for (Agents::iterator it = m_agents.begin(); it != m_agents.end(); ++it)
249 (*it)->clearFrontend();
251 m_inspectorFrontend.clear();
253 InspectorInstrumentation::frontendDeleted();
254 if (!InspectorInstrumentation::hasFrontends())
255 ScriptController::setCaptureCallStackForUncaughtExceptions(false);
258 void InspectorController::show()
263 if (m_inspectorFrontend)
264 m_inspectorClient->bringFrontendToFront();
266 InspectorFrontendChannel* frontendChannel = m_inspectorClient->openInspectorFrontend(this);
268 connectFrontend(frontendChannel);
272 void InspectorController::close()
274 if (!m_inspectorFrontend)
276 disconnectFrontend();
277 m_inspectorClient->closeInspectorFrontend();
280 void InspectorController::reconnectFrontend(InspectorFrontendChannel* frontendChannel, const String& inspectorStateCookie)
282 ASSERT(!m_inspectorFrontend);
283 connectFrontend(frontendChannel);
284 m_state->loadFromCookie(inspectorStateCookie);
286 for (Agents::iterator it = m_agents.begin(); it != m_agents.end(); ++it)
290 void InspectorController::setProcessId(long processId)
292 IdentifiersFactory::setProcessId(processId);
295 void InspectorController::evaluateForTestInFrontend(long callId, const String& script)
297 m_inspectorAgent->evaluateForTestInFrontend(callId, script);
300 void InspectorController::drawHighlight(GraphicsContext& context) const
302 m_overlay->paint(context);
305 void InspectorController::getHighlight(Highlight* highlight) const
307 m_overlay->getHighlight(highlight);
310 void InspectorController::inspect(Node* node)
317 m_domAgent->inspect(node);
320 bool InspectorController::enabled() const
322 return m_inspectorAgent->developerExtrasEnabled();
325 Page* InspectorController::inspectedPage() const
330 void InspectorController::setInjectedScriptForOrigin(const String& origin, const String& source)
332 m_inspectorAgent->setInjectedScriptForOrigin(origin, source);
335 void InspectorController::dispatchMessageFromFrontend(const String& message)
337 if (m_inspectorBackendDispatcher)
338 m_inspectorBackendDispatcher->dispatch(message);
341 void InspectorController::hideHighlight()
344 m_domAgent->hideHighlight(&error);
347 Node* InspectorController::highlightedNode() const
349 return m_overlay->highlightedNode();
352 #if ENABLE(JAVASCRIPT_DEBUGGER)
353 bool InspectorController::profilerEnabled()
355 return m_profilerAgent->enabled();
358 void InspectorController::setProfilerEnabled(bool enable)
362 m_profilerAgent->enable(&error);
364 m_profilerAgent->disable(&error);
367 void InspectorController::resume()
369 if (m_debuggerAgent) {
371 m_debuggerAgent->resume(&error);
376 void InspectorController::setResourcesDataSizeLimitsFromInternals(int maximumResourcesContentSize, int maximumSingleResourceContentSize)
378 m_resourceAgent->setResourcesDataSizeLimitsFromInternals(maximumResourcesContentSize, maximumSingleResourceContentSize);
381 } // namespace WebCore
383 #endif // ENABLE(INSPECTOR)