1 /*
2  * Copyright (C) 2011 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
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
13  * distribution.
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.
17  *
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.
29  */
30 
31 #include "config.h"
32 #include "InspectorController.h"
33 
34 #if ENABLE(INSPECTOR)
35 
36 #include "Frame.h"
37 #include "GraphicsContext.h"
38 #include "InjectedScriptHost.h"
39 #include "InjectedScriptManager.h"
40 #include "InspectorAgent.h"
41 #include "InspectorBackendDispatcher.h"
42 #include "InspectorDebuggerAgent.h"
43 #include "InspectorClient.h"
44 #include "InspectorDOMAgent.h"
45 #include "InspectorFrontend.h"
46 #include "InspectorFrontendClient.h"
47 #include "InspectorInstrumentation.h"
48 #include "InspectorProfilerAgent.h"
49 #include "InspectorTimelineAgent.h"
50 #include "InspectorWorkerAgent.h"
51 #include "Page.h"
52 #include "ScriptObject.h"
53 #include "Settings.h"
54 #include <wtf/UnusedParam.h>
55 
56 namespace WebCore {
57 
InspectorController(Page * page,InspectorClient * inspectorClient)58 InspectorController::InspectorController(Page* page, InspectorClient* inspectorClient)
59     : m_injectedScriptManager(InjectedScriptManager::createForPage())
60     , m_inspectorAgent(adoptPtr(new InspectorAgent(page, inspectorClient, m_injectedScriptManager.get())))
61     , m_inspectorClient(inspectorClient)
62     , m_openingFrontend(false)
63     , m_startUserInitiatedDebuggingWhenFrontedIsConnected(false)
64 {
65 }
66 
~InspectorController()67 InspectorController::~InspectorController()
68 {
69 }
70 
setInspectorFrontendClient(PassOwnPtr<InspectorFrontendClient> inspectorFrontendClient)71 void InspectorController::setInspectorFrontendClient(PassOwnPtr<InspectorFrontendClient> inspectorFrontendClient)
72 {
73     m_inspectorFrontendClient = inspectorFrontendClient;
74 }
75 
hasInspectorFrontendClient() const76 bool InspectorController::hasInspectorFrontendClient() const
77 {
78     return m_inspectorFrontendClient;
79 }
80 
didClearWindowObjectInWorld(Frame * frame,DOMWrapperWorld * world)81 void InspectorController::didClearWindowObjectInWorld(Frame* frame, DOMWrapperWorld* world)
82 {
83     if (world != mainThreadNormalWorld())
84         return;
85 
86     // If the page is supposed to serve as InspectorFrontend notify inspector frontend
87     // client that it's cleared so that the client can expose inspector bindings.
88     if (m_inspectorFrontendClient && frame == m_inspectorAgent->inspectedPage()->mainFrame())
89         m_inspectorFrontendClient->windowObjectCleared();
90 }
91 
startTimelineProfiler()92 void InspectorController::startTimelineProfiler()
93 {
94     ErrorString error;
95     m_inspectorAgent->timelineAgent()->start(&error);
96 }
97 
stopTimelineProfiler()98 void InspectorController::stopTimelineProfiler()
99 {
100     ErrorString error;
101     m_inspectorAgent->timelineAgent()->stop(&error);
102 }
103 
connectFrontend()104 void InspectorController::connectFrontend()
105 {
106     m_openingFrontend = false;
107     m_inspectorFrontend = adoptPtr(new InspectorFrontend(m_inspectorClient));
108     m_injectedScriptManager->injectedScriptHost()->setFrontend(m_inspectorFrontend.get());
109     m_inspectorAgent->setFrontend(m_inspectorFrontend.get());
110 
111     if (!InspectorInstrumentation::hasFrontends())
112         ScriptController::setCaptureCallStackForUncaughtExceptions(true);
113     InspectorInstrumentation::frontendCreated();
114 
115     ASSERT(m_inspectorClient);
116     m_inspectorBackendDispatcher = adoptPtr(new InspectorBackendDispatcher(
117         m_inspectorClient,
118 #if ENABLE(OFFLINE_WEB_APPLICATIONS)
119         m_inspectorAgent->applicationCacheAgent(),
120 #endif
121         m_inspectorAgent->cssAgent(),
122         m_inspectorAgent->consoleAgent(),
123         m_inspectorAgent->domAgent(),
124 #if ENABLE(JAVASCRIPT_DEBUGGER)
125         m_inspectorAgent->domDebuggerAgent(),
126 #endif
127 #if ENABLE(DOM_STORAGE)
128         m_inspectorAgent->domStorageAgent(),
129 #endif
130 #if ENABLE(DATABASE)
131         m_inspectorAgent->databaseAgent(),
132 #endif
133 #if ENABLE(JAVASCRIPT_DEBUGGER)
134         m_inspectorAgent->debuggerAgent(),
135 #endif
136         m_inspectorAgent->resourceAgent(),
137         m_inspectorAgent->pageAgent(),
138 #if ENABLE(JAVASCRIPT_DEBUGGER)
139         m_inspectorAgent->profilerAgent(),
140 #endif
141         m_inspectorAgent->runtimeAgent(),
142         m_inspectorAgent->timelineAgent()
143 #if ENABLE(WORKERS)
144         , m_inspectorAgent->workerAgent()
145 #endif
146     ));
147 
148     if (m_startUserInitiatedDebuggingWhenFrontedIsConnected) {
149         m_inspectorFrontend->inspector()->startUserInitiatedDebugging();
150         m_startUserInitiatedDebuggingWhenFrontedIsConnected = false;
151     }
152 }
153 
disconnectFrontend()154 void InspectorController::disconnectFrontend()
155 {
156     if (!m_inspectorFrontend)
157         return;
158     m_inspectorBackendDispatcher.clear();
159 
160     m_inspectorAgent->disconnectFrontend();
161     m_injectedScriptManager->injectedScriptHost()->clearFrontend();
162 
163     m_inspectorFrontend.clear();
164 
165     InspectorInstrumentation::frontendDeleted();
166     if (!InspectorInstrumentation::hasFrontends())
167         ScriptController::setCaptureCallStackForUncaughtExceptions(false);
168 }
169 
show()170 void InspectorController::show()
171 {
172     if (!enabled())
173         return;
174 
175     if (m_openingFrontend)
176         return;
177 
178     if (m_inspectorFrontend)
179         m_inspectorFrontend->inspector()->bringToFront();
180     else {
181         m_openingFrontend = true;
182         m_inspectorClient->openInspectorFrontend(this);
183     }
184 }
185 
close()186 void InspectorController::close()
187 {
188     if (!m_inspectorFrontend)
189         return;
190     m_inspectorFrontend->inspector()->disconnectFromBackend();
191     disconnectFrontend();
192 }
193 
restoreInspectorStateFromCookie(const String & inspectorStateCookie)194 void InspectorController::restoreInspectorStateFromCookie(const String& inspectorStateCookie)
195 {
196     ASSERT(!m_inspectorFrontend);
197     connectFrontend();
198     m_inspectorAgent->restoreInspectorStateFromCookie(inspectorStateCookie);
199 }
200 
evaluateForTestInFrontend(long callId,const String & script)201 void InspectorController::evaluateForTestInFrontend(long callId, const String& script)
202 {
203     m_inspectorAgent->evaluateForTestInFrontend(callId, script);
204 }
205 
drawNodeHighlight(GraphicsContext & context) const206 void InspectorController::drawNodeHighlight(GraphicsContext& context) const
207 {
208     m_inspectorAgent->domAgent()->drawNodeHighlight(context);
209 }
210 
showConsole()211 void InspectorController::showConsole()
212 {
213     if (!enabled())
214         return;
215     show();
216     m_inspectorAgent->showConsole();
217 }
218 
inspect(Node * node)219 void InspectorController::inspect(Node* node)
220 {
221     if (!enabled())
222         return;
223 
224     show();
225 
226     m_inspectorAgent->domAgent()->inspect(node);
227 }
228 
enabled() const229 bool InspectorController::enabled() const
230 {
231     return m_inspectorAgent->enabled();
232 }
233 
inspectedPage() const234 Page* InspectorController::inspectedPage() const
235 {
236     return m_inspectorAgent->inspectedPage();
237 }
238 
timelineProfilerEnabled()239 bool InspectorController::timelineProfilerEnabled()
240 {
241     return m_inspectorAgent->timelineAgent()->started();
242 }
243 
setInspectorExtensionAPI(const String & source)244 void InspectorController::setInspectorExtensionAPI(const String& source)
245 {
246     m_inspectorAgent->setInspectorExtensionAPI(source);
247 }
248 
dispatchMessageFromFrontend(const String & message)249 void InspectorController::dispatchMessageFromFrontend(const String& message)
250 {
251     if (m_inspectorBackendDispatcher)
252         m_inspectorBackendDispatcher->dispatch(message);
253 }
254 
hideHighlight()255 void InspectorController::hideHighlight()
256 {
257     ErrorString error;
258     m_inspectorAgent->domAgent()->hideHighlight(&error);
259 }
260 
highlightedNode() const261 Node* InspectorController::highlightedNode() const
262 {
263     return m_inspectorAgent->domAgent()->highlightedNode();
264 }
265 
266 #if ENABLE(JAVASCRIPT_DEBUGGER)
enableProfiler()267 void InspectorController::enableProfiler()
268 {
269     ErrorString error;
270     m_inspectorAgent->profilerAgent()->enable(&error);
271 }
272 
disableProfiler()273 void InspectorController::disableProfiler()
274 {
275     ErrorString error;
276     m_inspectorAgent->profilerAgent()->disable(&error);
277 }
278 
profilerEnabled()279 bool InspectorController::profilerEnabled()
280 {
281     return m_inspectorAgent->profilerAgent()->enabled();
282 }
283 
debuggerEnabled()284 bool InspectorController::debuggerEnabled()
285 {
286     return m_inspectorAgent->debuggerAgent()->enabled();
287 }
288 
showAndEnableDebugger()289 void InspectorController::showAndEnableDebugger()
290 {
291     if (!enabled())
292         return;
293     show();
294 
295     if (m_inspectorFrontend)
296         m_inspectorFrontend->inspector()->startUserInitiatedDebugging();
297     else
298         m_startUserInitiatedDebuggingWhenFrontedIsConnected = true;
299 }
300 
disableDebugger()301 void InspectorController::disableDebugger()
302 {
303     m_inspectorAgent->debuggerAgent()->disable();
304 }
305 
startUserInitiatedProfiling()306 void InspectorController::startUserInitiatedProfiling()
307 {
308     m_inspectorAgent->profilerAgent()->startUserInitiatedProfiling();
309 }
310 
stopUserInitiatedProfiling()311 void InspectorController::stopUserInitiatedProfiling()
312 {
313     if (!enabled())
314         return;
315     show();
316     m_inspectorAgent->profilerAgent()->stopUserInitiatedProfiling();
317     m_inspectorAgent->showProfilesPanel();
318 }
319 
isRecordingUserInitiatedProfile() const320 bool InspectorController::isRecordingUserInitiatedProfile() const
321 {
322     return m_inspectorAgent->profilerAgent()->isRecordingUserInitiatedProfile();
323 }
324 
resume()325 void InspectorController::resume()
326 {
327     if (InspectorDebuggerAgent* debuggerAgent = m_inspectorAgent->debuggerAgent()) {
328         ErrorString error;
329         debuggerAgent->resume(&error);
330     }
331 }
332 
333 #endif
334 
335 } // namespace WebCore
336 
337 #endif // ENABLE(INSPECTOR)
338