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