701dcaa07b51a04bab90b9ce720d22bacfaa30aa
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / inspector / InspectorController.cpp
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 "core/inspector/InspectorController.h"
33
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"
71
72 namespace WebCore {
73
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))
79     , m_page(page)
80     , m_inspectorClient(inspectorClient)
81     , m_agents(m_instrumentingAgents.get(), m_state.get())
82     , m_isUnderTest(false)
83     , m_deferredAgentsInitialized(false)
84 {
85     InjectedScriptManager* injectedScriptManager = m_injectedScriptManager.get();
86     InspectorOverlay* overlay = m_overlay.get();
87
88     m_agents.append(InspectorInspectorAgent::create(m_page, injectedScriptManager));
89
90     OwnPtr<InspectorPageAgent> pageAgentPtr(InspectorPageAgent::create(m_page, injectedScriptManager, inspectorClient, overlay));
91     m_pageAgent = pageAgentPtr.get();
92     m_agents.append(pageAgentPtr.release());
93
94     OwnPtr<InspectorDOMAgent> domAgentPtr(InspectorDOMAgent::create(m_pageAgent, injectedScriptManager, overlay));
95     m_domAgent = domAgentPtr.get();
96     m_agents.append(domAgentPtr.release());
97
98     m_agents.append(InspectorDatabaseAgent::create());
99
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());
104
105     PageScriptDebugServer* pageScriptDebugServer = &PageScriptDebugServer::shared();
106
107     m_agents.append(PageRuntimeAgent::create(injectedScriptManager, pageScriptDebugServer, m_page, m_pageAgent));
108
109     m_agents.append(PageConsoleAgent::create(injectedScriptManager, m_domAgent, m_timelineAgent));
110
111     m_agents.append(InspectorWorkerAgent::create());
112
113     ASSERT_ARG(inspectorClient, inspectorClient);
114     m_injectedScriptManager->injectedScriptHost()->init(m_instrumentingAgents.get(), pageScriptDebugServer);
115 }
116
117 InspectorController::~InspectorController()
118 {
119     m_instrumentingAgents->reset();
120     m_agents.discardAgents();
121     ASSERT(!m_inspectorClient);
122 }
123
124 PassOwnPtr<InspectorController> InspectorController::create(Page* page, InspectorClient* client)
125 {
126     return adoptPtr(new InspectorController(page, client));
127 }
128
129 void InspectorController::initializeDeferredAgents()
130 {
131     if (m_deferredAgentsInitialized)
132         return;
133     m_deferredAgentsInitialized = true;
134
135     InjectedScriptManager* injectedScriptManager = m_injectedScriptManager.get();
136     InspectorOverlay* overlay = m_overlay.get();
137
138     OwnPtr<InspectorResourceAgent> resourceAgentPtr(InspectorResourceAgent::create(m_pageAgent, m_inspectorClient));
139     InspectorResourceAgent* resourceAgent = resourceAgentPtr.get();
140     m_agents.append(resourceAgentPtr.release());
141
142     m_agents.append(InspectorCSSAgent::create(m_domAgent, m_pageAgent, resourceAgent));
143
144     m_agents.append(InspectorIndexedDBAgent::create(injectedScriptManager, m_pageAgent));
145
146     m_agents.append(InspectorFileSystemAgent::create(m_pageAgent));
147
148     m_agents.append(InspectorDOMStorageAgent::create(m_pageAgent));
149
150     m_agents.append(InspectorMemoryAgent::create());
151
152     m_agents.append(InspectorApplicationCacheAgent::create(m_pageAgent));
153
154     PageScriptDebugServer* pageScriptDebugServer = &PageScriptDebugServer::shared();
155
156     OwnPtr<InspectorDebuggerAgent> debuggerAgentPtr(PageDebuggerAgent::create(pageScriptDebugServer, m_pageAgent, injectedScriptManager, overlay));
157     InspectorDebuggerAgent* debuggerAgent = debuggerAgentPtr.get();
158     m_agents.append(debuggerAgentPtr.release());
159
160     m_agents.append(InspectorDOMDebuggerAgent::create(m_domAgent, debuggerAgent));
161
162     m_agents.append(InspectorProfilerAgent::create(injectedScriptManager, overlay));
163
164     m_agents.append(InspectorHeapProfilerAgent::create(injectedScriptManager));
165
166     m_agents.append(InspectorCanvasAgent::create(m_pageAgent, injectedScriptManager));
167
168     m_agents.append(InspectorInputAgent::create(m_page, m_inspectorClient));
169
170     m_agents.append(InspectorLayerTreeAgent::create(m_domAgent, m_page));
171 }
172
173 void InspectorController::inspectedPageDestroyed()
174 {
175     disconnectFrontend();
176     m_injectedScriptManager->disconnect();
177     m_inspectorClient = 0;
178     m_page = 0;
179 }
180
181 void InspectorController::registerModuleAgent(PassOwnPtr<InspectorAgent> agent)
182 {
183     m_agents.append(agent);
184 }
185
186 void InspectorController::setInspectorFrontendClient(PassOwnPtr<InspectorFrontendClient> inspectorFrontendClient)
187 {
188     m_inspectorFrontendClient = inspectorFrontendClient;
189 }
190
191 void InspectorController::didClearWindowObjectInMainWorld(Frame* frame)
192 {
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();
197 }
198
199 void InspectorController::connectFrontend(InspectorFrontendChannel* frontendChannel)
200 {
201     ASSERT(frontendChannel);
202
203     initializeDeferredAgents();
204
205     m_inspectorFrontend = adoptPtr(new InspectorFrontend(frontendChannel));
206     // We can reconnect to existing front-end -> unmute state.
207     m_state->unmute();
208
209     m_agents.setFrontend(m_inspectorFrontend.get());
210
211     InspectorInstrumentation::registerInstrumentingAgents(m_instrumentingAgents.get());
212     InspectorInstrumentation::frontendCreated();
213
214     ASSERT(m_inspectorClient);
215     m_inspectorBackendDispatcher = InspectorBackendDispatcher::create(frontendChannel);
216
217     m_agents.registerInDispatcher(m_inspectorBackendDispatcher.get());
218 }
219
220 void InspectorController::disconnectFrontend()
221 {
222     if (!m_inspectorFrontend)
223         return;
224     m_inspectorBackendDispatcher->clearFrontend();
225     m_inspectorBackendDispatcher.clear();
226
227     // Destroying agents would change the state, but we don't want that.
228     // Pre-disconnect state will be used to restore inspector agents.
229     m_state->mute();
230
231     m_agents.clearFrontend();
232
233     m_inspectorFrontend.clear();
234
235     // relese overlay page resources
236     m_overlay->freePage();
237     InspectorInstrumentation::frontendDeleted();
238     InspectorInstrumentation::unregisterInstrumentingAgents(m_instrumentingAgents.get());
239 }
240
241 void InspectorController::reconnectFrontend()
242 {
243     if (!m_inspectorFrontend)
244         return;
245     InspectorFrontendChannel* frontendChannel = m_inspectorFrontend->inspector()->getInspectorFrontendChannel();
246     disconnectFrontend();
247     connectFrontend(frontendChannel);
248 }
249
250 void InspectorController::reuseFrontend(InspectorFrontendChannel* frontendChannel, const String& inspectorStateCookie)
251 {
252     ASSERT(!m_inspectorFrontend);
253     connectFrontend(frontendChannel);
254     m_state->loadFromCookie(inspectorStateCookie);
255     m_agents.restore();
256 }
257
258 void InspectorController::setProcessId(long processId)
259 {
260     IdentifiersFactory::setProcessId(processId);
261 }
262
263 void InspectorController::setLayerTreeId(int id)
264 {
265     m_timelineAgent->setLayerTreeId(id);
266 }
267
268 void InspectorController::webViewResized(const IntSize& size)
269 {
270     if (InspectorPageAgent* pageAgent = m_instrumentingAgents->inspectorPageAgent())
271         pageAgent->webViewResized(size);
272 }
273
274 bool InspectorController::isUnderTest()
275 {
276     return m_isUnderTest;
277 }
278
279 void InspectorController::evaluateForTestInFrontend(long callId, const String& script)
280 {
281     m_isUnderTest = true;
282     if (InspectorInspectorAgent* inspectorAgent = m_instrumentingAgents->inspectorInspectorAgent())
283         inspectorAgent->evaluateForTestInFrontend(callId, script);
284 }
285
286 void InspectorController::drawHighlight(GraphicsContext& context) const
287 {
288     m_overlay->paint(context);
289 }
290
291 void InspectorController::getHighlight(Highlight* highlight) const
292 {
293     m_overlay->getHighlight(highlight);
294 }
295
296 void InspectorController::inspect(Node* node)
297 {
298     if (!node)
299         return;
300     Document* document = node->ownerDocument();
301     if (!document)
302         return;
303     Frame* frame = document->frame();
304     if (!frame)
305         return;
306
307     if (node->nodeType() != Node::ELEMENT_NODE && node->nodeType() != Node::DOCUMENT_NODE)
308         node = node->parentNode();
309
310     InjectedScript injectedScript = m_injectedScriptManager->injectedScriptFor(mainWorldScriptState(frame));
311     if (injectedScript.hasNoValue())
312         return;
313     injectedScript.inspectNode(node);
314 }
315
316 void InspectorController::setInjectedScriptForOrigin(const String& origin, const String& source)
317 {
318     if (InspectorInspectorAgent* inspectorAgent = m_instrumentingAgents->inspectorInspectorAgent())
319         inspectorAgent->setInjectedScriptForOrigin(origin, source);
320 }
321
322 void InspectorController::dispatchMessageFromFrontend(const String& message)
323 {
324     if (m_inspectorBackendDispatcher)
325         m_inspectorBackendDispatcher->dispatch(message);
326 }
327
328 void InspectorController::hideHighlight()
329 {
330     m_overlay->hideHighlight();
331 }
332
333 Node* InspectorController::highlightedNode() const
334 {
335     return m_overlay->highlightedNode();
336 }
337
338 bool InspectorController::handleGestureEvent(Frame* frame, const PlatformGestureEvent& event)
339 {
340     // Overlay should not consume events.
341     m_overlay->handleGestureEvent(event);
342     if (InspectorDOMAgent* domAgent = m_instrumentingAgents->inspectorDOMAgent())
343         return domAgent->handleGestureEvent(frame, event);
344     return false;
345 }
346
347 bool InspectorController::handleMouseEvent(Frame* frame, const PlatformMouseEvent& event)
348 {
349     // Overlay should not consume events.
350     m_overlay->handleMouseEvent(event);
351
352     if (event.type() == PlatformEvent::MouseMoved) {
353         if (InspectorDOMAgent* domAgent = m_instrumentingAgents->inspectorDOMAgent())
354             domAgent->handleMouseMove(frame, event);
355         return false;
356     }
357     if (event.type() == PlatformEvent::MousePressed) {
358         if (InspectorDOMAgent* domAgent = m_instrumentingAgents->inspectorDOMAgent())
359             return domAgent->handleMousePress();
360     }
361     return false;
362 }
363
364 bool InspectorController::handleTouchEvent(Frame* frame, const PlatformTouchEvent& event)
365 {
366     // Overlay should not consume events.
367     m_overlay->handleTouchEvent(event);
368     if (InspectorDOMAgent* domAgent = m_instrumentingAgents->inspectorDOMAgent())
369         return domAgent->handleTouchEvent(frame, event);
370     return false;
371 }
372
373 bool InspectorController::handleKeyboardEvent(Frame* frame, const PlatformKeyboardEvent& event)
374 {
375     // Overlay should not consume events.
376     m_overlay->handleKeyboardEvent(event);
377     return false;
378 }
379
380 void InspectorController::requestPageScaleFactor(float scale, const IntPoint& origin)
381 {
382     m_inspectorClient->requestPageScaleFactor(scale, origin);
383 }
384
385 bool InspectorController::deviceEmulationEnabled()
386 {
387     if (InspectorPageAgent* pageAgent = m_instrumentingAgents->inspectorPageAgent())
388         return pageAgent->deviceMetricsOverrideEnabled();
389     return false;
390 }
391
392 void InspectorController::resume()
393 {
394     if (InspectorDebuggerAgent* debuggerAgent = m_instrumentingAgents->inspectorDebuggerAgent()) {
395         ErrorString error;
396         debuggerAgent->resume(&error);
397     }
398 }
399
400 void InspectorController::setResourcesDataSizeLimitsFromInternals(int maximumResourcesContentSize, int maximumSingleResourceContentSize)
401 {
402     if (InspectorResourceAgent* resourceAgent = m_instrumentingAgents->inspectorResourceAgent())
403         resourceAgent->setResourcesDataSizeLimitsFromInternals(maximumResourcesContentSize, maximumSingleResourceContentSize);
404 }
405
406 void InspectorController::willProcessTask()
407 {
408     if (InspectorTimelineAgent* timelineAgent = m_instrumentingAgents->inspectorTimelineAgent())
409         timelineAgent->willProcessTask();
410     if (InspectorProfilerAgent* profilerAgent = m_instrumentingAgents->inspectorProfilerAgent())
411         profilerAgent->willProcessTask();
412 }
413
414 void InspectorController::didProcessTask()
415 {
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();
422 }
423
424 void InspectorController::didBeginFrame(int frameId)
425 {
426     if (InspectorTimelineAgent* timelineAgent = m_instrumentingAgents->inspectorTimelineAgent())
427         timelineAgent->didBeginFrame(frameId);
428     if (InspectorCanvasAgent* canvasAgent = m_instrumentingAgents->inspectorCanvasAgent())
429         canvasAgent->didBeginFrame();
430 }
431
432 void InspectorController::didCancelFrame()
433 {
434     if (InspectorTimelineAgent* timelineAgent = m_instrumentingAgents->inspectorTimelineAgent())
435         timelineAgent->didCancelFrame();
436 }
437
438 void InspectorController::willComposite()
439 {
440     if (InspectorTimelineAgent* timelineAgent = m_instrumentingAgents->inspectorTimelineAgent())
441         timelineAgent->willComposite();
442 }
443
444 void InspectorController::didComposite()
445 {
446     if (InspectorTimelineAgent* timelineAgent = m_instrumentingAgents->inspectorTimelineAgent())
447         timelineAgent->didComposite();
448 }
449
450 void InspectorController::processGPUEvent(double timestamp, int phase, bool foreign, size_t usedGPUMemoryBytes)
451 {
452     if (InspectorTimelineAgent* timelineAgent = m_instrumentingAgents->inspectorTimelineAgent())
453         timelineAgent->processGPUEvent(InspectorTimelineAgent::GPUEvent(timestamp, phase, foreign, usedGPUMemoryBytes));
454 }
455
456 } // namespace WebCore