blob: 778221f1edd12a3406a534ab4159692fdb0f50b9 [file] [log] [blame]
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001/*
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
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010034#include "core/inspector/InspectorAgent.h"
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010035#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"
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +010040#include "core/inspector/InstrumentingAgents.h"
Ben Murdoch591b9582013-07-10 11:41:44 +010041#include "core/platform/JSONValues.h"
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010042#include "core/platform/Pasteboard.h"
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010043
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +010044#include "wtf/RefPtr.h"
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +010045#include "wtf/text/StringBuilder.h"
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010046
47using namespace std;
48
49namespace WebCore {
50
51PassRefPtr<InjectedScriptHost> InjectedScriptHost::create()
52{
53 return adoptRef(new InjectedScriptHost());
54}
55
56InjectedScriptHost::InjectedScriptHost()
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +010057 : m_instrumentingAgents(0)
58 , m_scriptDebugServer(0)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010059{
Torne (Richard Coles)93ac45c2013-05-29 14:40:20 +010060 ScriptWrappable::init(this);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010061 m_defaultInspectableObject = adoptPtr(new InspectableObject());
62}
63
64InjectedScriptHost::~InjectedScriptHost()
65{
66}
67
68void InjectedScriptHost::disconnect()
69{
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +010070 m_instrumentingAgents = 0;
71 m_scriptDebugServer = 0;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010072}
73
Ben Murdoch591b9582013-07-10 11:41:44 +010074void InjectedScriptHost::inspectImpl(PassRefPtr<JSONValue> object, PassRefPtr<JSONValue> hints)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010075{
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +010076 if (InspectorAgent* inspectorAgent = m_instrumentingAgents ? m_instrumentingAgents->inspectorAgent() : 0) {
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010077 RefPtr<TypeBuilder::Runtime::RemoteObject> remoteObject = TypeBuilder::Runtime::RemoteObject::runtimeCast(object);
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +010078 inspectorAgent->inspect(remoteObject, hints->asObject());
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010079 }
80}
81
82void InjectedScriptHost::getEventListenersImpl(Node* node, Vector<EventListenerInfo>& listenersArray)
83{
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +010084 InspectorDOMAgent::getEventListeners(node, listenersArray, false);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010085}
86
87void InjectedScriptHost::clearConsoleMessages()
88{
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +010089 if (InspectorConsoleAgent* consoleAgent = m_instrumentingAgents ? m_instrumentingAgents->inspectorConsoleAgent() : 0) {
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010090 ErrorString error;
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +010091 consoleAgent->clearMessages(&error);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010092 }
93}
94
95void InjectedScriptHost::copyText(const String& text)
96{
97 Pasteboard::generalPasteboard()->writePlainText(text, Pasteboard::CannotSmartReplace);
98}
99
100ScriptValue InjectedScriptHost::InspectableObject::get(ScriptState*)
101{
102 return ScriptValue();
103};
104
105void InjectedScriptHost::addInspectedObject(PassOwnPtr<InjectedScriptHost::InspectableObject> object)
106{
107 m_inspectedObjects.prepend(object);
108 while (m_inspectedObjects.size() > 5)
109 m_inspectedObjects.removeLast();
110}
111
112void InjectedScriptHost::clearInspectedObjects()
113{
114 m_inspectedObjects.clear();
115}
116
117InjectedScriptHost::InspectableObject* InjectedScriptHost::inspectedObject(unsigned int num)
118{
119 if (num >= m_inspectedObjects.size())
120 return m_defaultInspectableObject.get();
121 return m_inspectedObjects[num].get();
122}
123
124String InjectedScriptHost::databaseIdImpl(Database* database)
125{
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +0100126 if (InspectorDatabaseAgent* databaseAgent = m_instrumentingAgents ? m_instrumentingAgents->inspectorDatabaseAgent() : 0)
127 return databaseAgent->databaseId(database);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100128 return String();
129}
130
131String InjectedScriptHost::storageIdImpl(Storage* storage)
132{
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +0100133 if (InspectorDOMStorageAgent* domStorageAgent = m_instrumentingAgents ? m_instrumentingAgents->inspectorDOMStorageAgent() : 0)
134 return domStorageAgent->storageId(storage);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100135 return String();
136}
137
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +0100138void InjectedScriptHost::debugFunction(const String& scriptId, int lineNumber, int columnNumber)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100139{
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +0100140 if (InspectorDebuggerAgent* debuggerAgent = m_instrumentingAgents ? m_instrumentingAgents->inspectorDebuggerAgent() : 0)
141 debuggerAgent->setBreakpoint(scriptId, lineNumber, columnNumber, InspectorDebuggerAgent::DebugCommandBreakpointSource);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100142}
143
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +0100144void InjectedScriptHost::undebugFunction(const String& scriptId, int lineNumber, int columnNumber)
Torne (Richard Coles)5267f702013-06-11 10:57:24 +0100145{
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +0100146 if (InspectorDebuggerAgent* debuggerAgent = m_instrumentingAgents ? m_instrumentingAgents->inspectorDebuggerAgent() : 0)
147 debuggerAgent->removeBreakpoint(scriptId, lineNumber, columnNumber, InspectorDebuggerAgent::DebugCommandBreakpointSource);
Torne (Richard Coles)5267f702013-06-11 10:57:24 +0100148}
149
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +0100150void InjectedScriptHost::monitorFunction(const String& scriptId, int lineNumber, int columnNumber, const String& functionName)
Torne (Richard Coles)5267f702013-06-11 10:57:24 +0100151{
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +0100152 StringBuilder builder;
153 builder.appendLiteral("console.log(\"function ");
154 if (functionName.isEmpty())
155 builder.appendLiteral("(anonymous function)");
156 else
157 builder.append(functionName);
158 builder.appendLiteral(" called\" + (arguments.length > 0 ? \" with arguments: \" + Array.prototype.join.call(arguments, \", \") : \"\")) && false");
159 if (InspectorDebuggerAgent* debuggerAgent = m_instrumentingAgents ? m_instrumentingAgents->inspectorDebuggerAgent() : 0)
160 debuggerAgent->setBreakpoint(scriptId, lineNumber, columnNumber, InspectorDebuggerAgent::MonitorCommandBreakpointSource, builder.toString());
161}
162
163void InjectedScriptHost::unmonitorFunction(const String& scriptId, int lineNumber, int columnNumber)
164{
165 if (InspectorDebuggerAgent* debuggerAgent = m_instrumentingAgents ? m_instrumentingAgents->inspectorDebuggerAgent() : 0)
166 debuggerAgent->removeBreakpoint(scriptId, lineNumber, columnNumber, InspectorDebuggerAgent::MonitorCommandBreakpointSource);
Torne (Richard Coles)5267f702013-06-11 10:57:24 +0100167}
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100168
169} // namespace WebCore
170