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/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 "core/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     m_agents.append(InspectorCSSAgent::create(m_instrumentingAgents.get(), m_state.get(), domAgent, pageAgent));
     95 
     96     m_agents.append(InspectorDatabaseAgent::create(m_instrumentingAgents.get(), m_state.get()));
     97 
     98     m_agents.append(InspectorIndexedDBAgent::create(m_instrumentingAgents.get(), m_state.get(), m_injectedScriptManager.get(), pageAgent));
     99 
    100     m_agents.append(InspectorFileSystemAgent::create(m_instrumentingAgents.get(), pageAgent, m_state.get()));
    101 
    102     m_agents.append(InspectorDOMStorageAgent::create(m_instrumentingAgents.get(), pageAgent, m_state.get()));
    103 
    104     OwnPtr<InspectorMemoryAgent> memoryAgentPtr(InspectorMemoryAgent::create(m_instrumentingAgents.get(), m_state.get()));
    105     m_memoryAgent = memoryAgentPtr.get();
    106     m_agents.append(memoryAgentPtr.release());
    107 
    108     OwnPtr<InspectorTimelineAgent> timelineAgentPtr(InspectorTimelineAgent::create(m_instrumentingAgents.get(), pageAgent, m_memoryAgent, domAgent, m_state.get(),
    109         InspectorTimelineAgent::PageInspector, inspectorClient));
    110     m_timelineAgent = timelineAgentPtr.get();
    111     m_agents.append(timelineAgentPtr.release());
    112 
    113     m_agents.append(InspectorApplicationCacheAgent::create(m_instrumentingAgents.get(), m_state.get(), pageAgent));
    114     m_agents.append(InspectorResourceAgent::create(m_instrumentingAgents.get(), pageAgent, inspectorClient, m_state.get(), m_overlay.get()));
    115 
    116     PageScriptDebugServer* pageScriptDebugServer = &PageScriptDebugServer::shared();
    117 
    118     m_agents.append(PageRuntimeAgent::create(m_instrumentingAgents.get(), m_state.get(), m_injectedScriptManager.get(), pageScriptDebugServer, page, pageAgent));
    119 
    120     OwnPtr<InspectorConsoleAgent> consoleAgentPtr(PageConsoleAgent::create(m_instrumentingAgents.get(), m_state.get(), m_injectedScriptManager.get(), domAgent));
    121     InspectorConsoleAgent* consoleAgent = consoleAgentPtr.get();
    122     m_agents.append(consoleAgentPtr.release());
    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(), consoleAgent, m_state.get(), m_injectedScriptManager.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 Page* InspectorController::inspectedPage() const
    299 {
    300     return m_page;
    301 }
    302 
    303 void InspectorController::setInjectedScriptForOrigin(const String& origin, const String& source)
    304 {
    305     if (InspectorAgent* inspectorAgent = m_instrumentingAgents->inspectorAgent())
    306         inspectorAgent->setInjectedScriptForOrigin(origin, source);
    307 }
    308 
    309 void InspectorController::dispatchMessageFromFrontend(const String& message)
    310 {
    311     if (m_inspectorBackendDispatcher)
    312         m_inspectorBackendDispatcher->dispatch(message);
    313 }
    314 
    315 void InspectorController::hideHighlight()
    316 {
    317     m_overlay->hideHighlight();
    318 }
    319 
    320 Node* InspectorController::highlightedNode() const
    321 {
    322     return m_overlay->highlightedNode();
    323 }
    324 
    325 bool InspectorController::handleGestureEvent(Frame* frame, const PlatformGestureEvent& event)
    326 {
    327     // Overlay should not consume events.
    328     m_overlay->handleGestureEvent(event);
    329     if (InspectorDOMAgent* domAgent = m_instrumentingAgents->inspectorDOMAgent())
    330         return domAgent->handleGestureEvent(frame, event);
    331     return false;
    332 }
    333 
    334 bool InspectorController::handleMouseEvent(Frame* frame, const PlatformMouseEvent& event)
    335 {
    336     // Overlay should not consume events.
    337     m_overlay->handleMouseEvent(event);
    338 
    339     if (event.type() == PlatformEvent::MouseMoved) {
    340         if (InspectorDOMAgent* domAgent = m_instrumentingAgents->inspectorDOMAgent())
    341             domAgent->handleMouseMove(frame, event);
    342         return false;
    343     }
    344     if (event.type() == PlatformEvent::MousePressed) {
    345         if (InspectorDOMAgent* domAgent = m_instrumentingAgents->inspectorDOMAgent())
    346             return domAgent->handleMousePress();
    347     }
    348     return false;
    349 }
    350 
    351 bool InspectorController::handleTouchEvent(Frame* frame, const PlatformTouchEvent& event)
    352 {
    353     // Overlay should not consume events.
    354     m_overlay->handleTouchEvent(event);
    355     if (InspectorDOMAgent* domAgent = m_instrumentingAgents->inspectorDOMAgent())
    356         return domAgent->handleTouchEvent(frame, event);
    357     return false;
    358 }
    359 
    360 void InspectorController::resume()
    361 {
    362     if (InspectorDebuggerAgent* debuggerAgent = m_instrumentingAgents->inspectorDebuggerAgent()) {
    363         ErrorString error;
    364         debuggerAgent->resume(&error);
    365     }
    366 }
    367 
    368 void InspectorController::setResourcesDataSizeLimitsFromInternals(int maximumResourcesContentSize, int maximumSingleResourceContentSize)
    369 {
    370     if (InspectorResourceAgent* resourceAgent = m_instrumentingAgents->inspectorResourceAgent())
    371         resourceAgent->setResourcesDataSizeLimitsFromInternals(maximumResourcesContentSize, maximumSingleResourceContentSize);
    372 }
    373 
    374 void InspectorController::willProcessTask()
    375 {
    376     if (InspectorTimelineAgent* timelineAgent = m_instrumentingAgents->inspectorTimelineAgent())
    377         timelineAgent->willProcessTask();
    378     if (InspectorProfilerAgent* profilerAgent = m_instrumentingAgents->inspectorProfilerAgent())
    379         profilerAgent->willProcessTask();
    380 }
    381 
    382 void InspectorController::didProcessTask()
    383 {
    384     if (InspectorTimelineAgent* timelineAgent = m_instrumentingAgents->inspectorTimelineAgent())
    385         timelineAgent->didProcessTask();
    386     if (InspectorProfilerAgent* profilerAgent = m_instrumentingAgents->inspectorProfilerAgent())
    387         profilerAgent->didProcessTask();
    388     if (InspectorDOMDebuggerAgent* domDebuggerAgent = m_instrumentingAgents->inspectorDOMDebuggerAgent())
    389         domDebuggerAgent->didProcessTask();
    390 }
    391 
    392 void InspectorController::didBeginFrame()
    393 {
    394     if (InspectorTimelineAgent* timelineAgent = m_instrumentingAgents->inspectorTimelineAgent())
    395         timelineAgent->didBeginFrame();
    396     if (InspectorCanvasAgent* canvasAgent = m_instrumentingAgents->inspectorCanvasAgent())
    397         canvasAgent->didBeginFrame();
    398 }
    399 
    400 void InspectorController::didCancelFrame()
    401 {
    402     if (InspectorTimelineAgent* timelineAgent = m_instrumentingAgents->inspectorTimelineAgent())
    403         timelineAgent->didCancelFrame();
    404 }
    405 
    406 void InspectorController::willComposite()
    407 {
    408     if (InspectorTimelineAgent* timelineAgent = m_instrumentingAgents->inspectorTimelineAgent())
    409         timelineAgent->willComposite();
    410 }
    411 
    412 void InspectorController::didComposite()
    413 {
    414     if (InspectorTimelineAgent* timelineAgent = m_instrumentingAgents->inspectorTimelineAgent())
    415         timelineAgent->didComposite();
    416 }
    417 
    418 } // namespace WebCore
    419