2010-12-29 Pavel Feldman <pfeldman@chromium.org>
[WebKit-https.git] / WebCore / inspector / InjectedScriptHost.cpp
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
37 #include "Element.h"
38 #include "Frame.h"
39 #include "FrameLoader.h"
40 #include "HTMLFrameOwnerElement.h"
41 #include "InjectedScript.h"
42 #include "InspectorClient.h"
43 #include "InspectorController.h"
44 #include "InspectorDOMAgent.h"
45 #include "InspectorFrontend.h"
46 #include "Pasteboard.h"
47
48 #if ENABLE(JAVASCRIPT_DEBUGGER)
49 #include "ScriptDebugServer.h"
50 #endif
51
52 #if ENABLE(DATABASE)
53 #include "Database.h"
54 #endif
55
56 #if ENABLE(DOM_STORAGE)
57 #include "Storage.h"
58 #endif
59
60 #include "markup.h"
61
62 #include <wtf/RefPtr.h>
63 #include <wtf/StdLibExtras.h>
64
65 using namespace std;
66
67 namespace WebCore {
68
69 InjectedScriptHost::InjectedScriptHost(InspectorController* inspectorController)
70     : m_inspectorController(inspectorController)
71     , m_nextInjectedScriptId(1)
72     , m_lastWorkerId(1 << 31) // Distinguish ids of fake workers from real ones, to minimize the chances they overlap.
73 {
74 }
75
76 InjectedScriptHost::~InjectedScriptHost()
77 {
78 }
79
80 void InjectedScriptHost::clearConsoleMessages()
81 {
82     if (m_inspectorController)
83         m_inspectorController->clearConsoleMessages();
84 }
85
86 void InjectedScriptHost::copyText(const String& text)
87 {
88     Pasteboard::generalPasteboard()->writePlainText(text);
89 }
90
91 Node* InjectedScriptHost::nodeForId(long nodeId)
92 {
93     if (InspectorDOMAgent* domAgent = inspectorDOMAgent())
94         return domAgent->nodeForId(nodeId);
95     return 0;
96 }
97
98 long InjectedScriptHost::pushNodePathToFrontend(Node* node, bool withChildren, bool selectInUI)
99 {
100     InspectorDOMAgent* domAgent = inspectorDOMAgent();
101     if (!domAgent || !frontend())
102         return 0;
103     long id = domAgent->pushNodePathToFrontend(node);
104     if (withChildren)
105         domAgent->pushChildNodesToFrontend(id);
106     if (selectInUI)
107         frontend()->updateFocusedNode(id);
108     return id;
109 }
110
111 long InjectedScriptHost::inspectedNode(unsigned long num)
112 {
113     InspectorDOMAgent* domAgent = inspectorDOMAgent();
114     if (!domAgent)
115         return 0;
116
117     return domAgent->inspectedNode(num);
118 }
119
120 #if ENABLE(DATABASE)
121 Database* InjectedScriptHost::databaseForId(long databaseId)
122 {
123     if (m_inspectorController)
124         return m_inspectorController->databaseForId(databaseId);
125     return 0;
126 }
127
128 void InjectedScriptHost::selectDatabase(Database* database)
129 {
130     if (m_inspectorController)
131         m_inspectorController->selectDatabase(database);
132 }
133 #endif
134
135 #if ENABLE(DOM_STORAGE)
136 void InjectedScriptHost::selectDOMStorage(Storage* storage)
137 {
138     if (m_inspectorController)
139         m_inspectorController->selectDOMStorage(storage);
140 }
141 #endif
142
143 InjectedScript InjectedScriptHost::injectedScriptForId(long id)
144 {
145     return m_idToInjectedScript.get(id);
146 }
147
148 void InjectedScriptHost::discardInjectedScripts()
149 {
150     IdToInjectedScriptMap::iterator end = m_idToInjectedScript.end();
151     for (IdToInjectedScriptMap::iterator it = m_idToInjectedScript.begin(); it != end; ++it)
152         discardInjectedScript(it->second.scriptState());
153     m_idToInjectedScript.clear();
154 }
155
156 void InjectedScriptHost::releaseWrapperObjectGroup(long injectedScriptId, const String& objectGroup)
157 {
158     if (injectedScriptId) {
159          InjectedScript injectedScript = m_idToInjectedScript.get(injectedScriptId);
160          if (!injectedScript.hasNoValue())
161              injectedScript.releaseWrapperObjectGroup(objectGroup);
162     } else {
163          // Iterate over all injected scripts if injectedScriptId is not specified.
164          for (IdToInjectedScriptMap::iterator it = m_idToInjectedScript.begin(); it != m_idToInjectedScript.end(); ++it)
165               it->second.releaseWrapperObjectGroup(objectGroup);
166     }
167 }
168
169 InspectorDOMAgent* InjectedScriptHost::inspectorDOMAgent()
170 {
171     if (!m_inspectorController)
172         return 0;
173     return m_inspectorController->domAgent();
174 }
175
176 InspectorFrontend* InjectedScriptHost::frontend()
177 {
178     if (!m_inspectorController)
179         return 0;
180     return m_inspectorController->m_frontend.get();
181 }
182
183 pair<long, ScriptObject> InjectedScriptHost::injectScript(const String& source, ScriptState* scriptState)
184 {
185     long id = m_nextInjectedScriptId++;
186     return std::make_pair(id, createInjectedScript(source, scriptState, id));
187 }
188
189 #if ENABLE(WORKERS)
190 long InjectedScriptHost::nextWorkerId()
191 {
192     return ++m_lastWorkerId;
193 }
194
195 void InjectedScriptHost::didCreateWorker(long id, const String& url, bool isSharedWorker)
196 {
197     if (m_inspectorController)
198         m_inspectorController->didCreateWorker(id, url, isSharedWorker);
199 }
200
201 void InjectedScriptHost::didDestroyWorker(long id)
202 {
203     if (m_inspectorController)
204         m_inspectorController->didDestroyWorker(id);
205 }
206 #endif // ENABLE(WORKERS)
207
208 } // namespace WebCore
209
210 #endif // ENABLE(INSPECTOR)