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