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