Upstream version 7.36.149.0
[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/InspectorDebuggerAgent.h"
48 #include "core/inspector/InspectorFrontendClient.h"
49 #include "core/inspector/InspectorHeapProfilerAgent.h"
50 #include "core/inspector/InspectorInputAgent.h"
51 #include "core/inspector/InspectorInspectorAgent.h"
52 #include "core/inspector/InspectorInstrumentation.h"
53 #include "core/inspector/InspectorLayerTreeAgent.h"
54 #include "core/inspector/InspectorMemoryAgent.h"
55 #include "core/inspector/InspectorOverlay.h"
56 #include "core/inspector/InspectorPageAgent.h"
57 #include "core/inspector/InspectorProfilerAgent.h"
58 #include "core/inspector/InspectorResourceAgent.h"
59 #include "core/inspector/InspectorState.h"
60 #include "core/inspector/InspectorTimelineAgent.h"
61 #include "core/inspector/InspectorTracingAgent.h"
62 #include "core/inspector/InspectorWorkerAgent.h"
63 #include "core/inspector/InstrumentingAgents.h"
64 #include "core/inspector/PageConsoleAgent.h"
65 #include "core/inspector/PageDebuggerAgent.h"
66 #include "core/inspector/PageRuntimeAgent.h"
67 #include "core/page/Page.h"
68 #include "core/rendering/RenderLayer.h"
69 #include "platform/PlatformMouseEvent.h"
70
71 namespace WebCore {
72
73 InspectorController::InspectorController(Page* page, InspectorClient* inspectorClient)
74     : m_instrumentingAgents(InstrumentingAgents::create())
75     , m_injectedScriptManager(InjectedScriptManager::createForPage())
76     , m_state(adoptPtr(new InspectorCompositeState(inspectorClient)))
77     , m_overlay(InspectorOverlay::create(page, inspectorClient))
78     , m_layerTreeAgent(0)
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
99     OwnPtr<InspectorLayerTreeAgent> layerTreeAgentPtr(InspectorLayerTreeAgent::create(m_page));
100     m_layerTreeAgent = layerTreeAgentPtr.get();
101     m_agents.append(layerTreeAgentPtr.release());
102
103     OwnPtr<InspectorTracingAgent> tracingAgentPtr = InspectorTracingAgent::create(inspectorClient);
104     m_tracingAgent = tracingAgentPtr.get();
105     m_agents.append(tracingAgentPtr.release());
106
107     OwnPtr<InspectorTimelineAgent> timelineAgentPtr(InspectorTimelineAgent::create(m_pageAgent, m_layerTreeAgent,
108         overlay, InspectorTimelineAgent::PageInspector, inspectorClient));
109     m_timelineAgent = timelineAgentPtr.get();
110     m_agents.append(timelineAgentPtr.release());
111
112     PageScriptDebugServer* pageScriptDebugServer = &PageScriptDebugServer::shared();
113
114     m_agents.append(PageRuntimeAgent::create(injectedScriptManager, pageScriptDebugServer, m_page, m_pageAgent));
115
116     m_agents.append(PageConsoleAgent::create(injectedScriptManager, m_domAgent, m_timelineAgent));
117
118     m_agents.append(InspectorWorkerAgent::create());
119
120     ASSERT_ARG(inspectorClient, inspectorClient);
121     m_injectedScriptManager->injectedScriptHost()->init(m_instrumentingAgents.get(), pageScriptDebugServer);
122 }
123
124 InspectorController::~InspectorController()
125 {
126 }
127
128 PassOwnPtr<InspectorController> InspectorController::create(Page* page, InspectorClient* client)
129 {
130     return adoptPtr(new InspectorController(page, client));
131 }
132
133 void InspectorController::setTextAutosizingEnabled(bool enabled)
134 {
135     m_pageAgent->setTextAutosizingEnabled(enabled);
136 }
137
138 void InspectorController::setDeviceScaleAdjustment(float deviceScaleAdjustment)
139 {
140     m_pageAgent->setDeviceScaleAdjustment(deviceScaleAdjustment);
141 }
142
143 void InspectorController::initializeDeferredAgents()
144 {
145     if (m_deferredAgentsInitialized)
146         return;
147     m_deferredAgentsInitialized = true;
148
149     InjectedScriptManager* injectedScriptManager = m_injectedScriptManager.get();
150     InspectorOverlay* overlay = m_overlay.get();
151
152     OwnPtr<InspectorResourceAgent> resourceAgentPtr(InspectorResourceAgent::create(m_pageAgent));
153     InspectorResourceAgent* resourceAgent = resourceAgentPtr.get();
154     m_agents.append(resourceAgentPtr.release());
155
156     m_agents.append(InspectorCSSAgent::create(m_domAgent, m_pageAgent, resourceAgent));
157
158     m_agents.append(InspectorDOMStorageAgent::create(m_pageAgent));
159
160     m_agents.append(InspectorMemoryAgent::create());
161
162     m_agents.append(InspectorApplicationCacheAgent::create(m_pageAgent));
163
164     PageScriptDebugServer* pageScriptDebugServer = &PageScriptDebugServer::shared();
165
166     OwnPtr<InspectorDebuggerAgent> debuggerAgentPtr(PageDebuggerAgent::create(pageScriptDebugServer, m_pageAgent, injectedScriptManager, overlay));
167     InspectorDebuggerAgent* debuggerAgent = debuggerAgentPtr.get();
168     m_agents.append(debuggerAgentPtr.release());
169
170     m_agents.append(InspectorDOMDebuggerAgent::create(m_domAgent, debuggerAgent));
171
172     m_agents.append(InspectorProfilerAgent::create(injectedScriptManager, overlay));
173
174     m_agents.append(InspectorHeapProfilerAgent::create(injectedScriptManager));
175
176     m_agents.append(InspectorCanvasAgent::create(m_pageAgent, injectedScriptManager));
177
178     m_agents.append(InspectorInputAgent::create(m_page, m_inspectorClient));
179 }
180
181 void InspectorController::willBeDestroyed()
182 {
183     disconnectFrontend();
184     m_injectedScriptManager->disconnect();
185     m_inspectorClient = 0;
186     m_page = 0;
187     m_instrumentingAgents->reset();
188     m_agents.discardAgents();
189 }
190
191 void InspectorController::registerModuleAgent(PassOwnPtr<InspectorAgent> agent)
192 {
193     m_moduleAgents.append(agent.get());
194     m_agents.append(agent);
195 }
196
197 void InspectorController::setInspectorFrontendClient(PassOwnPtr<InspectorFrontendClient> inspectorFrontendClient)
198 {
199     m_inspectorFrontendClient = inspectorFrontendClient;
200 }
201
202 void InspectorController::didClearDocumentOfWindowObject(LocalFrame* frame)
203 {
204     // If the page is supposed to serve as InspectorFrontend notify inspector frontend
205     // client that it's cleared so that the client can expose inspector bindings.
206     if (m_inspectorFrontendClient && frame == m_page->mainFrame())
207         m_inspectorFrontendClient->windowObjectCleared();
208 }
209
210 void InspectorController::connectFrontend(InspectorFrontendChannel* frontendChannel)
211 {
212     ASSERT(frontendChannel);
213
214     initializeDeferredAgents();
215
216     m_inspectorFrontend = adoptPtr(new InspectorFrontend(frontendChannel));
217     // We can reconnect to existing front-end -> unmute state.
218     m_state->unmute();
219
220     m_agents.setFrontend(m_inspectorFrontend.get());
221
222     InspectorInstrumentation::registerInstrumentingAgents(m_instrumentingAgents.get());
223     InspectorInstrumentation::frontendCreated();
224
225     ASSERT(m_inspectorClient);
226     m_inspectorBackendDispatcher = InspectorBackendDispatcher::create(frontendChannel);
227
228     m_agents.registerInDispatcher(m_inspectorBackendDispatcher.get());
229 }
230
231 void InspectorController::disconnectFrontend()
232 {
233     if (!m_inspectorFrontend)
234         return;
235     m_inspectorBackendDispatcher->clearFrontend();
236     m_inspectorBackendDispatcher.clear();
237
238     // Destroying agents would change the state, but we don't want that.
239     // Pre-disconnect state will be used to restore inspector agents.
240     m_state->mute();
241
242     m_agents.clearFrontend();
243
244     m_inspectorFrontend.clear();
245
246     // relese overlay page resources
247     m_overlay->freePage();
248     InspectorInstrumentation::frontendDeleted();
249     InspectorInstrumentation::unregisterInstrumentingAgents(m_instrumentingAgents.get());
250 }
251
252 void InspectorController::reconnectFrontend()
253 {
254     if (!m_inspectorFrontend)
255         return;
256     InspectorFrontendChannel* frontendChannel = m_inspectorFrontend->channel();
257     disconnectFrontend();
258     connectFrontend(frontendChannel);
259 }
260
261 void InspectorController::reuseFrontend(InspectorFrontendChannel* frontendChannel, const String& inspectorStateCookie)
262 {
263     ASSERT(!m_inspectorFrontend);
264     connectFrontend(frontendChannel);
265     m_state->loadFromCookie(inspectorStateCookie);
266     m_agents.restore();
267 }
268
269 void InspectorController::setProcessId(long processId)
270 {
271     IdentifiersFactory::setProcessId(processId);
272 }
273
274 void InspectorController::setLayerTreeId(int id)
275 {
276     m_timelineAgent->setLayerTreeId(id);
277     m_tracingAgent->setLayerTreeId(id);
278 }
279
280 void InspectorController::webViewResized(const IntSize& size)
281 {
282     if (InspectorPageAgent* pageAgent = m_instrumentingAgents->inspectorPageAgent())
283         pageAgent->webViewResized(size);
284 }
285
286 bool InspectorController::isUnderTest()
287 {
288     return m_isUnderTest;
289 }
290
291 void InspectorController::evaluateForTestInFrontend(long callId, const String& script)
292 {
293     m_isUnderTest = true;
294     if (InspectorInspectorAgent* inspectorAgent = m_instrumentingAgents->inspectorInspectorAgent())
295         inspectorAgent->evaluateForTestInFrontend(callId, script);
296 }
297
298 void InspectorController::drawHighlight(GraphicsContext& context) const
299 {
300     m_overlay->paint(context);
301 }
302
303 void InspectorController::inspect(Node* node)
304 {
305     if (!node)
306         return;
307     Document* document = node->ownerDocument();
308     if (!document)
309         return;
310     LocalFrame* frame = document->frame();
311     if (!frame)
312         return;
313
314     if (node->nodeType() != Node::ELEMENT_NODE && node->nodeType() != Node::DOCUMENT_NODE)
315         node = node->parentNode();
316
317     InjectedScript injectedScript = m_injectedScriptManager->injectedScriptFor(ScriptState::forMainWorld(frame));
318     if (injectedScript.isEmpty())
319         return;
320     injectedScript.inspectNode(node);
321 }
322
323 void InspectorController::setInjectedScriptForOrigin(const String& origin, const String& source)
324 {
325     if (InspectorInspectorAgent* inspectorAgent = m_instrumentingAgents->inspectorInspectorAgent())
326         inspectorAgent->setInjectedScriptForOrigin(origin, source);
327 }
328
329 void InspectorController::dispatchMessageFromFrontend(const String& message)
330 {
331     if (m_inspectorBackendDispatcher)
332         m_inspectorBackendDispatcher->dispatch(message);
333 }
334
335 bool InspectorController::handleGestureEvent(LocalFrame* frame, const PlatformGestureEvent& event)
336 {
337     // Overlay should not consume events.
338     m_overlay->handleGestureEvent(event);
339     if (InspectorDOMAgent* domAgent = m_instrumentingAgents->inspectorDOMAgent())
340         return domAgent->handleGestureEvent(frame, event);
341     return false;
342 }
343
344 bool InspectorController::handleMouseEvent(LocalFrame* frame, const PlatformMouseEvent& event)
345 {
346     // Overlay should not consume events.
347     m_overlay->handleMouseEvent(event);
348
349     if (event.type() == PlatformEvent::MouseMoved) {
350         if (InspectorDOMAgent* domAgent = m_instrumentingAgents->inspectorDOMAgent())
351             domAgent->handleMouseMove(frame, event);
352         return false;
353     }
354     if (event.type() == PlatformEvent::MousePressed) {
355         if (InspectorDOMAgent* domAgent = m_instrumentingAgents->inspectorDOMAgent())
356             return domAgent->handleMousePress();
357     }
358     return false;
359 }
360
361 bool InspectorController::handleTouchEvent(LocalFrame* frame, const PlatformTouchEvent& event)
362 {
363     // Overlay should not consume events.
364     m_overlay->handleTouchEvent(event);
365     if (InspectorDOMAgent* domAgent = m_instrumentingAgents->inspectorDOMAgent())
366         return domAgent->handleTouchEvent(frame, event);
367     return false;
368 }
369
370 bool InspectorController::handleKeyboardEvent(LocalFrame* frame, const PlatformKeyboardEvent& event)
371 {
372     // Overlay should not consume events.
373     m_overlay->handleKeyboardEvent(event);
374     return false;
375 }
376
377 void InspectorController::requestPageScaleFactor(float scale, const IntPoint& origin)
378 {
379     m_inspectorClient->requestPageScaleFactor(scale, origin);
380 }
381
382 bool InspectorController::deviceEmulationEnabled()
383 {
384     if (InspectorPageAgent* pageAgent = m_instrumentingAgents->inspectorPageAgent())
385         return pageAgent->deviceMetricsOverrideEnabled();
386     return false;
387 }
388
389 void InspectorController::resume()
390 {
391     if (InspectorDebuggerAgent* debuggerAgent = m_instrumentingAgents->inspectorDebuggerAgent()) {
392         ErrorString error;
393         debuggerAgent->resume(&error);
394     }
395 }
396
397 void InspectorController::setResourcesDataSizeLimitsFromInternals(int maximumResourcesContentSize, int maximumSingleResourceContentSize)
398 {
399     if (InspectorResourceAgent* resourceAgent = m_instrumentingAgents->inspectorResourceAgent())
400         resourceAgent->setResourcesDataSizeLimitsFromInternals(maximumResourcesContentSize, maximumSingleResourceContentSize);
401 }
402
403 void InspectorController::willProcessTask()
404 {
405     if (InspectorTimelineAgent* timelineAgent = m_instrumentingAgents->inspectorTimelineAgent())
406         timelineAgent->willProcessTask();
407     if (InspectorProfilerAgent* profilerAgent = m_instrumentingAgents->inspectorProfilerAgent())
408         profilerAgent->willProcessTask();
409 }
410
411 void InspectorController::didProcessTask()
412 {
413     if (InspectorTimelineAgent* timelineAgent = m_instrumentingAgents->inspectorTimelineAgent())
414         timelineAgent->didProcessTask();
415     if (InspectorProfilerAgent* profilerAgent = m_instrumentingAgents->inspectorProfilerAgent())
416         profilerAgent->didProcessTask();
417     if (InspectorDOMDebuggerAgent* domDebuggerAgent = m_instrumentingAgents->inspectorDOMDebuggerAgent())
418         domDebuggerAgent->didProcessTask();
419 }
420
421 void InspectorController::flushPendingFrontendMessages()
422 {
423     m_agents.flushPendingFrontendMessages();
424 }
425
426 void InspectorController::didCommitLoadForMainFrame()
427 {
428     Vector<InspectorAgent*> agents = m_moduleAgents;
429     for (size_t i = 0; i < agents.size(); i++)
430         agents[i]->didCommitLoadForMainFrame();
431 }
432
433 void InspectorController::didBeginFrame(int frameId)
434 {
435     if (InspectorTimelineAgent* timelineAgent = m_instrumentingAgents->inspectorTimelineAgent())
436         timelineAgent->didBeginFrame(frameId);
437     if (InspectorCanvasAgent* canvasAgent = m_instrumentingAgents->inspectorCanvasAgent())
438         canvasAgent->didBeginFrame();
439 }
440
441 void InspectorController::didCancelFrame()
442 {
443     if (InspectorTimelineAgent* timelineAgent = m_instrumentingAgents->inspectorTimelineAgent())
444         timelineAgent->didCancelFrame();
445 }
446
447 void InspectorController::willComposite()
448 {
449     if (InspectorTimelineAgent* timelineAgent = m_instrumentingAgents->inspectorTimelineAgent())
450         timelineAgent->willComposite();
451 }
452
453 void InspectorController::didComposite()
454 {
455     if (InspectorTimelineAgent* timelineAgent = m_instrumentingAgents->inspectorTimelineAgent())
456         timelineAgent->didComposite();
457 }
458
459 void InspectorController::processGPUEvent(double timestamp, int phase, bool foreign, uint64_t usedGPUMemoryBytes, uint64_t limitGPUMemoryBytes)
460 {
461     if (InspectorTimelineAgent* timelineAgent = m_instrumentingAgents->inspectorTimelineAgent())
462         timelineAgent->processGPUEvent(InspectorTimelineAgent::GPUEvent(timestamp, phase, foreign, usedGPUMemoryBytes, limitGPUMemoryBytes));
463 }
464
465 void InspectorController::scriptsEnabled(bool  enabled)
466 {
467     if (InspectorPageAgent* pageAgent = m_instrumentingAgents->inspectorPageAgent())
468         pageAgent->scriptsEnabled(enabled);
469 }
470
471 void InspectorController::willAddPageOverlay(const GraphicsLayer* layer)
472 {
473     if (m_layerTreeAgent)
474         m_layerTreeAgent->willAddPageOverlay(layer);
475 }
476
477 void InspectorController::didRemovePageOverlay(const GraphicsLayer* layer)
478 {
479     if (m_layerTreeAgent)
480         m_layerTreeAgent->didRemovePageOverlay(layer);
481 }
482
483 } // namespace WebCore