Home | History | Annotate | Download | only in inspector
      1 /*
      2  * Copyright (C) 2007, 2008, 2009, 2010 Apple Inc. All rights reserved.
      3  * Copyright (C) 2008 Matt Lilek <webkit (at) mattlilek.com>
      4  * Copyright (C) 2011 Google Inc. All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  *
     10  * 1.  Redistributions of source code must retain the above copyright
     11  *     notice, this list of conditions and the following disclaimer.
     12  * 2.  Redistributions in binary form must reproduce the above copyright
     13  *     notice, this list of conditions and the following disclaimer in the
     14  *     documentation and/or other materials provided with the distribution.
     15  * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
     16  *     its contributors may be used to endorse or promote products derived
     17  *     from this software without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
     20  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     21  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     22  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
     23  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 #include "config.h"
     32 #include "core/inspector/InspectorAgent.h"
     33 
     34 #include "InspectorFrontend.h"
     35 #include "bindings/v8/DOMWrapperWorld.h"
     36 #include "bindings/v8/ScriptController.h"
     37 #include "core/dom/Document.h"
     38 #include "core/inspector/InjectedScriptHost.h"
     39 #include "core/inspector/InjectedScriptManager.h"
     40 #include "core/inspector/InspectorController.h"
     41 #include "core/inspector/InspectorState.h"
     42 #include "core/inspector/InstrumentingAgents.h"
     43 #include "core/loader/DocumentLoader.h"
     44 #include "core/page/Frame.h"
     45 #include "core/page/Page.h"
     46 #include "core/platform/JSONValues.h"
     47 #include "weborigin/SecurityOrigin.h"
     48 #include "wtf/text/StringBuilder.h"
     49 
     50 namespace WebCore {
     51 
     52 namespace InspectorAgentState {
     53 static const char inspectorAgentEnabled[] = "inspectorAgentEnabled";
     54 }
     55 
     56 InspectorAgent::InspectorAgent(Page* page, InjectedScriptManager* injectedScriptManager, InstrumentingAgents* instrumentingAgents, InspectorCompositeState* state)
     57     : InspectorBaseAgent<InspectorAgent>("Inspector", instrumentingAgents, state)
     58     , m_inspectedPage(page)
     59     , m_frontend(0)
     60     , m_injectedScriptManager(injectedScriptManager)
     61 {
     62     ASSERT_ARG(page, page);
     63     m_instrumentingAgents->setInspectorAgent(this);
     64 }
     65 
     66 InspectorAgent::~InspectorAgent()
     67 {
     68     m_instrumentingAgents->setInspectorAgent(0);
     69 }
     70 
     71 void InspectorAgent::didClearWindowObjectInWorld(Frame* frame, DOMWrapperWorld* world)
     72 {
     73     if (world != mainThreadNormalWorld())
     74         return;
     75 
     76     if (m_injectedScriptForOrigin.isEmpty())
     77         return;
     78 
     79     String origin = frame->document()->securityOrigin()->toRawString();
     80     String script = m_injectedScriptForOrigin.get(origin);
     81     if (script.isEmpty())
     82         return;
     83     int injectedScriptId = m_injectedScriptManager->injectedScriptIdFor(mainWorldScriptState(frame));
     84     StringBuilder scriptSource;
     85     scriptSource.append(script);
     86     scriptSource.append("(");
     87     scriptSource.appendNumber(injectedScriptId);
     88     scriptSource.append(")");
     89     frame->script()->executeScript(scriptSource.toString());
     90 }
     91 
     92 void InspectorAgent::setFrontend(InspectorFrontend* inspectorFrontend)
     93 {
     94     m_frontend = inspectorFrontend;
     95 }
     96 
     97 void InspectorAgent::clearFrontend()
     98 {
     99     m_pendingEvaluateTestCommands.clear();
    100     m_frontend = 0;
    101     m_injectedScriptManager->discardInjectedScripts();
    102     ErrorString error;
    103     disable(&error);
    104 }
    105 
    106 void InspectorAgent::didCommitLoad(Frame* frame, DocumentLoader* loader)
    107 {
    108     if (loader->frame() != frame->page()->mainFrame())
    109         return;
    110 
    111     m_injectedScriptManager->discardInjectedScripts();
    112 }
    113 
    114 void InspectorAgent::enable(ErrorString*)
    115 {
    116     m_state->setBoolean(InspectorAgentState::inspectorAgentEnabled, true);
    117 
    118     if (m_pendingInspectData.first)
    119         inspect(m_pendingInspectData.first, m_pendingInspectData.second);
    120 
    121     for (Vector<pair<long, String> >::iterator it = m_pendingEvaluateTestCommands.begin(); m_frontend && it != m_pendingEvaluateTestCommands.end(); ++it)
    122         m_frontend->inspector()->evaluateForTestInFrontend(static_cast<int>((*it).first), (*it).second);
    123     m_pendingEvaluateTestCommands.clear();
    124 }
    125 
    126 void InspectorAgent::disable(ErrorString*)
    127 {
    128     m_state->setBoolean(InspectorAgentState::inspectorAgentEnabled, false);
    129 }
    130 
    131 void InspectorAgent::reset(ErrorString*)
    132 {
    133     m_inspectedPage->inspectorController()->reconnectFrontend();
    134 }
    135 
    136 void InspectorAgent::domContentLoadedEventFired(Frame* frame)
    137 {
    138     if (frame->page()->mainFrame() != frame)
    139         return;
    140 
    141     m_injectedScriptManager->injectedScriptHost()->clearInspectedObjects();
    142 }
    143 
    144 bool InspectorAgent::isMainResourceLoader(DocumentLoader* loader, const KURL& requestUrl)
    145 {
    146     return loader->frame() == m_inspectedPage->mainFrame() && requestUrl == loader->requestURL();
    147 }
    148 
    149 void InspectorAgent::evaluateForTestInFrontend(long callId, const String& script)
    150 {
    151     if (m_state->getBoolean(InspectorAgentState::inspectorAgentEnabled))
    152         m_frontend->inspector()->evaluateForTestInFrontend(static_cast<int>(callId), script);
    153     else
    154         m_pendingEvaluateTestCommands.append(pair<long, String>(callId, script));
    155 }
    156 
    157 void InspectorAgent::setInjectedScriptForOrigin(const String& origin, const String& source)
    158 {
    159     m_injectedScriptForOrigin.set(origin, source);
    160 }
    161 
    162 void InspectorAgent::inspect(PassRefPtr<TypeBuilder::Runtime::RemoteObject> objectToInspect, PassRefPtr<JSONObject> hints)
    163 {
    164     if (m_state->getBoolean(InspectorAgentState::inspectorAgentEnabled) && m_frontend) {
    165         m_frontend->inspector()->inspect(objectToInspect, hints);
    166         m_pendingInspectData.first = 0;
    167         m_pendingInspectData.second = 0;
    168         return;
    169     }
    170     m_pendingInspectData.first = objectToInspect;
    171     m_pendingInspectData.second = hints;
    172 }
    173 
    174 KURL InspectorAgent::inspectedURL() const
    175 {
    176     return m_inspectedPage->mainFrame()->document()->url();
    177 }
    178 
    179 KURL InspectorAgent::inspectedURLWithoutFragment() const
    180 {
    181     KURL url = inspectedURL();
    182     url.removeFragmentIdentifier();
    183     return url;
    184 }
    185 
    186 } // namespace WebCore
    187 
    188