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