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