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