Home | History | Annotate | Download | only in inspector
      1 /*
      2  * Copyright (C) 2007, 2008 Apple Inc. All rights reserved.
      3  * Copyright (C) 2008 Matt Lilek <webkit (at) mattlilek.com>
      4  * Copyright (C) 2010 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/InjectedScriptHost.h"
     33 
     34 #include "core/inspector/InspectorAgent.h"
     35 #include "core/inspector/InspectorConsoleAgent.h"
     36 #include "core/inspector/InspectorDOMAgent.h"
     37 #include "core/inspector/InspectorDOMStorageAgent.h"
     38 #include "core/inspector/InspectorDatabaseAgent.h"
     39 #include "core/inspector/InspectorDebuggerAgent.h"
     40 #include "core/inspector/InstrumentingAgents.h"
     41 #include "core/platform/JSONValues.h"
     42 
     43 #include "wtf/RefPtr.h"
     44 #include "wtf/text/StringBuilder.h"
     45 
     46 using namespace std;
     47 
     48 namespace WebCore {
     49 
     50 PassRefPtr<InjectedScriptHost> InjectedScriptHost::create()
     51 {
     52     return adoptRef(new InjectedScriptHost());
     53 }
     54 
     55 InjectedScriptHost::InjectedScriptHost()
     56     : m_instrumentingAgents(0)
     57     , m_scriptDebugServer(0)
     58 {
     59     ScriptWrappable::init(this);
     60     m_defaultInspectableObject = adoptPtr(new InspectableObject());
     61 }
     62 
     63 InjectedScriptHost::~InjectedScriptHost()
     64 {
     65 }
     66 
     67 void InjectedScriptHost::disconnect()
     68 {
     69     m_instrumentingAgents = 0;
     70     m_scriptDebugServer = 0;
     71 }
     72 
     73 void InjectedScriptHost::inspectImpl(PassRefPtr<JSONValue> object, PassRefPtr<JSONValue> hints)
     74 {
     75     if (InspectorAgent* inspectorAgent = m_instrumentingAgents ? m_instrumentingAgents->inspectorAgent() : 0) {
     76         RefPtr<TypeBuilder::Runtime::RemoteObject> remoteObject = TypeBuilder::Runtime::RemoteObject::runtimeCast(object);
     77         inspectorAgent->inspect(remoteObject, hints->asObject());
     78     }
     79 }
     80 
     81 void InjectedScriptHost::getEventListenersImpl(Node* node, Vector<EventListenerInfo>& listenersArray)
     82 {
     83     InspectorDOMAgent::getEventListeners(node, listenersArray, false);
     84 }
     85 
     86 void InjectedScriptHost::clearConsoleMessages()
     87 {
     88     if (InspectorConsoleAgent* consoleAgent = m_instrumentingAgents ? m_instrumentingAgents->inspectorConsoleAgent() : 0) {
     89         ErrorString error;
     90         consoleAgent->clearMessages(&error);
     91     }
     92 }
     93 
     94 ScriptValue InjectedScriptHost::InspectableObject::get(ScriptState*)
     95 {
     96     return ScriptValue();
     97 };
     98 
     99 void InjectedScriptHost::addInspectedObject(PassOwnPtr<InjectedScriptHost::InspectableObject> object)
    100 {
    101     m_inspectedObjects.prepend(object);
    102     while (m_inspectedObjects.size() > 5)
    103         m_inspectedObjects.removeLast();
    104 }
    105 
    106 void InjectedScriptHost::clearInspectedObjects()
    107 {
    108     m_inspectedObjects.clear();
    109 }
    110 
    111 InjectedScriptHost::InspectableObject* InjectedScriptHost::inspectedObject(unsigned int num)
    112 {
    113     if (num >= m_inspectedObjects.size())
    114         return m_defaultInspectableObject.get();
    115     return m_inspectedObjects[num].get();
    116 }
    117 
    118 String InjectedScriptHost::databaseIdImpl(Database* database)
    119 {
    120     if (InspectorDatabaseAgent* databaseAgent = m_instrumentingAgents ? m_instrumentingAgents->inspectorDatabaseAgent() : 0)
    121         return databaseAgent->databaseId(database);
    122     return String();
    123 }
    124 
    125 String InjectedScriptHost::storageIdImpl(Storage* storage)
    126 {
    127     if (InspectorDOMStorageAgent* domStorageAgent = m_instrumentingAgents ? m_instrumentingAgents->inspectorDOMStorageAgent() : 0)
    128         return domStorageAgent->storageId(storage);
    129     return String();
    130 }
    131 
    132 void InjectedScriptHost::debugFunction(const String& scriptId, int lineNumber, int columnNumber)
    133 {
    134     if (InspectorDebuggerAgent* debuggerAgent = m_instrumentingAgents ? m_instrumentingAgents->inspectorDebuggerAgent() : 0)
    135         debuggerAgent->setBreakpoint(scriptId, lineNumber, columnNumber, InspectorDebuggerAgent::DebugCommandBreakpointSource);
    136 }
    137 
    138 void InjectedScriptHost::undebugFunction(const String& scriptId, int lineNumber, int columnNumber)
    139 {
    140     if (InspectorDebuggerAgent* debuggerAgent = m_instrumentingAgents ? m_instrumentingAgents->inspectorDebuggerAgent() : 0)
    141         debuggerAgent->removeBreakpoint(scriptId, lineNumber, columnNumber, InspectorDebuggerAgent::DebugCommandBreakpointSource);
    142 }
    143 
    144 void InjectedScriptHost::monitorFunction(const String& scriptId, int lineNumber, int columnNumber, const String& functionName)
    145 {
    146     StringBuilder builder;
    147     builder.appendLiteral("console.log(\"function ");
    148     if (functionName.isEmpty())
    149         builder.appendLiteral("(anonymous function)");
    150     else
    151         builder.append(functionName);
    152     builder.appendLiteral(" called\" + (arguments.length > 0 ? \" with arguments: \" + Array.prototype.join.call(arguments, \", \") : \"\")) && false");
    153     if (InspectorDebuggerAgent* debuggerAgent = m_instrumentingAgents ? m_instrumentingAgents->inspectorDebuggerAgent() : 0)
    154         debuggerAgent->setBreakpoint(scriptId, lineNumber, columnNumber, InspectorDebuggerAgent::MonitorCommandBreakpointSource, builder.toString());
    155 }
    156 
    157 void InjectedScriptHost::unmonitorFunction(const String& scriptId, int lineNumber, int columnNumber)
    158 {
    159     if (InspectorDebuggerAgent* debuggerAgent = m_instrumentingAgents ? m_instrumentingAgents->inspectorDebuggerAgent() : 0)
    160         debuggerAgent->removeBreakpoint(scriptId, lineNumber, columnNumber, InspectorDebuggerAgent::MonitorCommandBreakpointSource);
    161 }
    162 
    163 } // namespace WebCore
    164 
    165