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 "InjectedScriptHost.h"
33
34#if ENABLE(INSPECTOR)
35
36#include "Element.h"
37#include "Frame.h"
38#include "FrameLoader.h"
39#include "HTMLFrameOwnerElement.h"
40#include "InjectedScript.h"
41#include "InspectorAgent.h"
42#include "InspectorClient.h"
43#include "InspectorConsoleAgent.h"
44#include "InspectorDOMStorageAgent.h"
45#include "InspectorDatabaseAgent.h"
46#include "InspectorFrontend.h"
47#include "InspectorValues.h"
48#include "Pasteboard.h"
49
50#if ENABLE(JAVASCRIPT_DEBUGGER)
51#include "ScriptDebugServer.h"
52#endif
53
54#if ENABLE(DATABASE)
55#include "Database.h"
56#endif
57
58#if ENABLE(DOM_STORAGE)
59#include "Storage.h"
60#endif
61
62#include "markup.h"
63
64#include <wtf/RefPtr.h>
65#include <wtf/StdLibExtras.h>
66
67using namespace std;
68
69namespace WebCore {
70
71PassRefPtr<InjectedScriptHost> InjectedScriptHost::create()
72{
73    return adoptRef(new InjectedScriptHost());
74}
75
76InjectedScriptHost::InjectedScriptHost()
77    : m_inspectorAgent(0)
78    , m_consoleAgent(0)
79#if ENABLE(DATABASE)
80    , m_databaseAgent(0)
81#endif
82#if ENABLE(DOM_STORAGE)
83    , m_domStorageAgent(0)
84#endif
85    , m_frontend(0)
86    , m_lastWorkerId(1 << 31) // Distinguish ids of fake workers from real ones, to minimize the chances they overlap.
87{
88}
89
90InjectedScriptHost::~InjectedScriptHost()
91{
92}
93
94void InjectedScriptHost::disconnect()
95{
96    m_inspectorAgent = 0;
97    m_consoleAgent = 0;
98#if ENABLE(DATABASE)
99    m_databaseAgent = 0;
100#endif
101#if ENABLE(DOM_STORAGE)
102    m_domStorageAgent = 0;
103#endif
104    m_frontend = 0;
105}
106
107void InjectedScriptHost::addInspectedNode(Node* node)
108{
109    m_inspectedNodes.prepend(node);
110    while (m_inspectedNodes.size() > 5)
111        m_inspectedNodes.removeLast();
112}
113
114void InjectedScriptHost::clearInspectedNodes()
115{
116    m_inspectedNodes.clear();
117}
118
119void InjectedScriptHost::inspectImpl(PassRefPtr<InspectorValue> object, PassRefPtr<InspectorValue> hints)
120{
121    if (m_frontend)
122        m_frontend->inspector()->inspect(object->asObject(), hints->asObject());
123}
124
125void InjectedScriptHost::clearConsoleMessages()
126{
127    if (m_consoleAgent) {
128        ErrorString error;
129        m_consoleAgent->clearConsoleMessages(&error);
130    }
131}
132
133void InjectedScriptHost::copyText(const String& text)
134{
135    Pasteboard::generalPasteboard()->writePlainText(text);
136}
137
138Node* InjectedScriptHost::inspectedNode(unsigned int num)
139{
140    if (num < m_inspectedNodes.size())
141        return m_inspectedNodes[num].get();
142    return 0;
143}
144
145#if ENABLE(DATABASE)
146int InjectedScriptHost::databaseIdImpl(Database* database)
147{
148    if (m_databaseAgent)
149        return m_databaseAgent->databaseId(database);
150    return 0;
151}
152#endif
153
154#if ENABLE(DOM_STORAGE)
155int InjectedScriptHost::storageIdImpl(Storage* storage)
156{
157    if (m_domStorageAgent)
158        return m_domStorageAgent->storageId(storage);
159    return 0;
160}
161#endif
162
163#if ENABLE(WORKERS)
164long InjectedScriptHost::nextWorkerId()
165{
166    return ++m_lastWorkerId;
167}
168
169void InjectedScriptHost::didCreateWorker(long id, const String& url, bool isSharedWorker)
170{
171    if (m_inspectorAgent)
172        m_inspectorAgent->didCreateWorker(static_cast<int>(id), url, isSharedWorker);
173}
174
175void InjectedScriptHost::didDestroyWorker(long id)
176{
177    if (m_inspectorAgent)
178        m_inspectorAgent->didDestroyWorker(static_cast<int>(id));
179}
180#endif // ENABLE(WORKERS)
181
182} // namespace WebCore
183
184#endif // ENABLE(INSPECTOR)
185