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
33#if ENABLE(INSPECTOR)
34
35#include "InjectedScriptHost.h"
36
37#include "Element.h"
38#include "Frame.h"
39#include "FrameLoader.h"
40#include "HTMLFrameOwnerElement.h"
41#include "InjectedScript.h"
42#include "InspectorAgent.h"
43#include "InspectorClient.h"
44#include "InspectorConsoleAgent.h"
45#include "InspectorDOMAgent.h"
46#include "InspectorDOMStorageAgent.h"
47#include "InspectorDatabaseAgent.h"
48#include "InspectorDebuggerAgent.h"
49#include "InspectorFrontend.h"
50#include "InspectorValues.h"
51#include "Pasteboard.h"
52#include "Storage.h"
53
54#if ENABLE(SQL_DATABASE)
55#include "Database.h"
56#endif
57
58#include "markup.h"
59
60#include <wtf/RefPtr.h>
61#include <wtf/StdLibExtras.h>
62
63using namespace std;
64
65namespace WebCore {
66
67PassRefPtr<InjectedScriptHost> InjectedScriptHost::create()
68{
69    return adoptRef(new InjectedScriptHost());
70}
71
72InjectedScriptHost::InjectedScriptHost()
73    : m_inspectorAgent(0)
74    , m_consoleAgent(0)
75#if ENABLE(SQL_DATABASE)
76    , m_databaseAgent(0)
77#endif
78    , m_domStorageAgent(0)
79    , m_domAgent(0)
80{
81    m_defaultInspectableObject = adoptPtr(new InspectableObject());
82}
83
84InjectedScriptHost::~InjectedScriptHost()
85{
86}
87
88void InjectedScriptHost::disconnect()
89{
90    m_inspectorAgent = 0;
91    m_consoleAgent = 0;
92#if ENABLE(SQL_DATABASE)
93    m_databaseAgent = 0;
94#endif
95    m_domStorageAgent = 0;
96    m_domAgent = 0;
97}
98
99void InjectedScriptHost::inspectImpl(PassRefPtr<InspectorValue> object, PassRefPtr<InspectorValue> hints)
100{
101    if (m_inspectorAgent) {
102        RefPtr<TypeBuilder::Runtime::RemoteObject> remoteObject = TypeBuilder::Runtime::RemoteObject::runtimeCast(object);
103        m_inspectorAgent->inspect(remoteObject, hints->asObject());
104    }
105}
106
107void InjectedScriptHost::getEventListenersImpl(Node* node, Vector<EventListenerInfo>& listenersArray)
108{
109    if (m_domAgent)
110        m_domAgent->getEventListeners(node, listenersArray, false);
111}
112
113void InjectedScriptHost::clearConsoleMessages()
114{
115    if (m_consoleAgent) {
116        ErrorString error;
117        m_consoleAgent->clearMessages(&error);
118    }
119}
120
121void InjectedScriptHost::copyText(const String& text)
122{
123    Pasteboard::generalPasteboard()->writePlainText(text, Pasteboard::CannotSmartReplace);
124}
125
126ScriptValue InjectedScriptHost::InspectableObject::get(ScriptState*)
127{
128    return ScriptValue();
129};
130
131void InjectedScriptHost::addInspectedObject(PassOwnPtr<InjectedScriptHost::InspectableObject> object)
132{
133    m_inspectedObjects.insert(0, object);
134    while (m_inspectedObjects.size() > 5)
135        m_inspectedObjects.removeLast();
136}
137
138void InjectedScriptHost::clearInspectedObjects()
139{
140    m_inspectedObjects.clear();
141}
142
143InjectedScriptHost::InspectableObject* InjectedScriptHost::inspectedObject(unsigned int num)
144{
145    if (num >= m_inspectedObjects.size())
146        return m_defaultInspectableObject.get();
147    return m_inspectedObjects[num].get();
148}
149
150#if ENABLE(SQL_DATABASE)
151String InjectedScriptHost::databaseIdImpl(Database* database)
152{
153    if (m_databaseAgent)
154        return m_databaseAgent->databaseId(database);
155    return String();
156}
157#endif
158
159String InjectedScriptHost::storageIdImpl(Storage* storage)
160{
161    if (m_domStorageAgent)
162        return m_domStorageAgent->storageId(storage);
163    return String();
164}
165
166#if ENABLE(JAVASCRIPT_DEBUGGER)
167ScriptDebugServer& InjectedScriptHost::scriptDebugServer()
168{
169    return m_debuggerAgent->scriptDebugServer();
170}
171#endif
172
173
174} // namespace WebCore
175
176#endif // ENABLE(INSPECTOR)
177