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