OLD | NEW |
1 // Copyright 2008, Google Inc. | 1 /* |
2 // All rights reserved. | 2 * Copyright (C) 2008, 2009 Google Inc. All rights reserved. |
3 // | 3 * |
4 // Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 // modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
6 // met: | 6 * met: |
7 // | 7 * |
8 // * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
9 // notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 // * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
11 // copyright notice, this list of conditions and the following disclaimer | 11 * copyright notice, this list of conditions and the following disclaimer |
12 // in the documentation and/or other materials provided with the | 12 * in the documentation and/or other materials provided with the |
13 // distribution. | 13 * distribution. |
14 // * Neither the name of Google Inc. nor the names of its | 14 * * Neither the name of Google Inc. nor the names of its |
15 // contributors may be used to endorse or promote products derived from | 15 * contributors may be used to endorse or promote products derived from |
16 // this software without specific prior written permission. | 16 * this software without specific prior written permission. |
17 // | 17 * |
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 | 29 */ |
30 // An interface to abstract implementation differences | |
31 // for various Javascript engines. | |
32 | 30 |
33 #ifndef ScriptController_h | 31 #ifndef ScriptController_h |
34 #define ScriptController_h | 32 #define ScriptController_h |
35 | 33 |
36 #include "HashMap.h" | |
37 #include "MessagePort.h" | |
38 #include "ScriptInstance.h" | 34 #include "ScriptInstance.h" |
39 #include "ScriptValue.h" | 35 #include "ScriptValue.h" |
40 #include "SecurityOrigin.h" | |
41 | 36 |
42 #include "bindings/npruntime.h" | 37 #include "V8Proxy.h" |
| 38 |
| 39 #include <v8.h> |
43 | 40 |
44 #include <wtf/HashMap.h> | 41 #include <wtf/HashMap.h> |
45 #include <wtf/Vector.h> | 42 #include <wtf/Vector.h> |
46 | 43 |
47 #include "v8.h" | 44 namespace WebCore { |
48 #include "v8_proxy.h" | 45 class Event; |
| 46 class Frame; |
| 47 class HTMLPlugInElement; |
| 48 class ScriptSourceCode; |
| 49 class ScriptState; |
| 50 class String; |
| 51 class Widget; |
49 | 52 |
50 // JavaScript implementations which expose NPObject will need to implement | 53 class ScriptController { |
51 // these methods. | 54 public: |
52 typedef void (*NPN_ReleaseVariantValueProcPtr) (NPVariant *variant); | 55 ScriptController(Frame*); |
| 56 ~ScriptController(); |
53 | 57 |
54 typedef NPIdentifier(*NPN_GetStringIdentifierProcPtr) (const NPUTF8 *name); | 58 // FIXME: V8Proxy should either be folded into ScriptController |
55 typedef void (*NPN_GetStringIdentifiersProcPtr) (const NPUTF8 **names, | 59 // or this accessor should be made JSProxy* |
56 int32_t nameCount, | 60 V8Proxy* proxy() { return m_proxy.get(); } |
57 NPIdentifier *identifiers); | |
58 typedef NPIdentifier(*NPN_GetIntIdentifierProcPtr) (int32_t intid); | |
59 typedef int32_t (*NPN_IntFromIdentifierProcPtr) (NPIdentifier identifier); | |
60 typedef bool (*NPN_IdentifierIsStringProcPtr) (NPIdentifier identifier); | |
61 typedef NPUTF8 * (*NPN_UTF8FromIdentifierProcPtr) (NPIdentifier identifier); | |
62 | 61 |
63 typedef NPObject* (*NPN_CreateObjectProcPtr) (NPP, | 62 // Evaluate a script file in the environment of this proxy. |
64 NPClass *aClass); | 63 // If succeeded, 'succ' is set to true and result is returned |
65 typedef NPObject* (*NPN_RetainObjectProcPtr) (NPObject *obj); | 64 // as a string. |
66 typedef void (*NPN_ReleaseObjectProcPtr) (NPObject *obj); | 65 ScriptValue evaluate(const ScriptSourceCode&); |
67 typedef bool (*NPN_InvokeProcPtr) (NPP npp, | |
68 NPObject *obj, | |
69 NPIdentifier methodName, | |
70 const NPVariant *args, | |
71 unsigned argCount, | |
72 NPVariant *result); | |
73 typedef bool (*NPN_InvokeDefaultProcPtr) (NPP npp, | |
74 NPObject *obj, | |
75 const NPVariant *args, | |
76 unsigned argCount, | |
77 NPVariant *result); | |
78 typedef bool (*NPN_EvaluateProcPtr) (NPP npp, | |
79 NPObject *obj, | |
80 NPString *script, | |
81 NPVariant *result); | |
82 typedef bool (*NPN_GetPropertyProcPtr) (NPP npp, | |
83 NPObject *obj, | |
84 NPIdentifier propertyName, | |
85 NPVariant *result); | |
86 typedef bool (*NPN_SetPropertyProcPtr) (NPP npp, | |
87 NPObject *obj, | |
88 NPIdentifier propertyName, | |
89 const NPVariant *value); | |
90 typedef bool (*NPN_HasPropertyProcPtr) (NPP, | |
91 NPObject *npobj, | |
92 NPIdentifier propertyName); | |
93 typedef bool (*NPN_HasMethodProcPtr) (NPP npp, | |
94 NPObject *npobj, | |
95 NPIdentifier methodName); | |
96 typedef bool (*NPN_RemovePropertyProcPtr) (NPP npp, | |
97 NPObject *obj, | |
98 NPIdentifier propertyName); | |
99 typedef void (*NPN_SetExceptionProcPtr) (NPObject *obj, | |
100 const NPUTF8 *message); | |
101 | 66 |
102 typedef struct _NPRuntimeFunctions { | 67 // Executes JavaScript in a new context associated with the web frame. T
he |
103 NPN_GetStringIdentifierProcPtr getStringIdentifier; | 68 // script gets its own global scope and its own prototypes for intrinsic |
104 NPN_GetStringIdentifiersProcPtr getStringIdentifiers; | 69 // JavaScript objects (String, Array, and so-on). It shares the wrappers
for |
105 NPN_GetIntIdentifierProcPtr getIntIdentifier; | 70 // all DOM nodes and DOM constructors. |
106 NPN_IdentifierIsStringProcPtr identifierIsString; | 71 void evaluateInNewContext(const Vector<ScriptSourceCode>&); |
107 NPN_UTF8FromIdentifierProcPtr utf8FromIdentifier; | |
108 NPN_IntFromIdentifierProcPtr intFromIdentifier; | |
109 NPN_CreateObjectProcPtr createObject; | |
110 NPN_RetainObjectProcPtr retainObject; | |
111 NPN_ReleaseObjectProcPtr releaseObject; | |
112 NPN_InvokeProcPtr invoke; | |
113 NPN_InvokeDefaultProcPtr invokeDefault; | |
114 NPN_EvaluateProcPtr evaluate; | |
115 NPN_GetPropertyProcPtr getProperty; | |
116 NPN_SetPropertyProcPtr setProperty; | |
117 NPN_RemovePropertyProcPtr removeProperty; | |
118 NPN_HasPropertyProcPtr hasProperty; | |
119 NPN_HasMethodProcPtr hasMethod; | |
120 NPN_ReleaseVariantValueProcPtr releaseVariantValue; | |
121 NPN_SetExceptionProcPtr setException; | |
122 } NPRuntimeFunctions; | |
123 | 72 |
124 namespace WebCore { | 73 // JSC has a WindowShell object, but for V8, the ScriptController |
125 class Document; | 74 // is the WindowShell. |
126 class EventListener; | 75 bool haveWindowShell() const { return true; } |
127 class Event; | |
128 class Frame; | |
129 class HTMLPlugInElement; | |
130 class Node; | |
131 class ScriptSourceCode; | |
132 class ScriptState; | |
133 class String; | |
134 class Widget; | |
135 | 76 |
136 typedef v8::Local<v8::Object> JSInstance; | 77 // Masquerade 'this' as the windowShell. |
137 typedef v8::Local<v8::Object> JSInstanceHandle; | 78 // This is a bit of a hack, but provides reasonable compatibility |
138 typedef v8::Persistent<v8::Object> JSPersistentInstance; | 79 // with what JSC does as well. |
139 typedef v8::Local<v8::Value> JSException; | 80 ScriptController* windowShell() { return this; } |
140 typedef v8::Persistent<v8::Value> JSResult; | |
141 | 81 |
142 class ScriptController { | 82 ScriptState* state() const { return m_scriptState.get(); } |
143 public: | |
144 ScriptController(Frame*); | |
145 ~ScriptController(); | |
146 | 83 |
147 // TODO(eseidel): V8Proxy should either be folded into ScriptController | 84 void collectGarbage(); |
148 // or this accessor should be made JSProxy* | |
149 V8Proxy* proxy() { return m_proxy.get(); } | |
150 | 85 |
151 // Evaluate a script file in the environment of this proxy. | 86 // Creates a property of the global object of a frame. |
152 // If succeeded, 'succ' is set to true and result is returned | 87 void bindToWindowObject(Frame*, const String& key, NPObject*); |
153 // as a string. | |
154 ScriptValue evaluate(const ScriptSourceCode&); | |
155 | 88 |
156 // Executes JavaScript in a new context associated with the web frame. The | 89 PassScriptInstance createScriptInstanceForWidget(Widget*); |
157 // script gets its own global scope and its own prototypes for intrinsic | |
158 // JavaScript objects (String, Array, and so-on). It shares the wrappers for | |
159 // all DOM nodes and DOM constructors. | |
160 void evaluateInNewContext(const Vector<ScriptSourceCode>& sources); | |
161 | 90 |
162 // JSC has a WindowShell object, but for V8, the ScriptController | 91 void disconnectFrame(); |
163 // is the WindowShell. | |
164 bool haveWindowShell() const { return true; } | |
165 | 92 |
166 // Masquerade 'this' as the windowShell. | 93 // Check if the javascript engine has been initialized. |
167 // This is a bit of a hack, but provides reasonable compatibility | 94 bool haveInterpreter() const; |
168 // with what JSC does as well. | |
169 ScriptController* windowShell() { return this; } | |
170 | 95 |
171 ScriptState* state() const { return m_scriptState.get(); } | 96 bool isEnabled() const; |
172 | 97 |
173 void disposeJSResult(JSResult result); | 98 // FIXME: void* is a compile hack. |
174 void collectGarbage(); | 99 void attachDebugger(void*); |
175 | 100 |
176 // Creates a property of the global object of a frame. | 101 // --- Static methods assume we are running VM in single thread, --- |
177 void BindToWindowObject(Frame*, const String& key, NPObject*); | 102 // --- and there is only one VM instance. --- |
178 | 103 |
179 NPRuntimeFunctions* functions(); | 104 // Returns the frame for the entered context. See comments in |
| 105 // V8Proxy::retrieveFrameForEnteredContext() for more information. |
| 106 static Frame* retrieveFrameForEnteredContext(); |
180 | 107 |
181 PassScriptInstance createScriptInstanceForWidget(Widget*); | 108 // Returns the frame for the current context. See comments in |
| 109 // V8Proxy::retrieveFrameForEnteredContext() for more information. |
| 110 static Frame* retrieveFrameForCurrentContext(); |
182 | 111 |
183 void disconnectFrame(); | 112 // Check whether it is safe to access a frame in another domain. |
| 113 static bool isSafeScript(Frame*); |
184 | 114 |
185 // Check if the javascript engine has been initialized. | 115 // Pass command-line flags to the JS engine. |
186 bool haveInterpreter() const; | 116 static void setFlags(const char* string, int length); |
187 | 117 |
188 bool isEnabled() const; | 118 // Protect and unprotect the JS wrapper from garbage collected. |
| 119 static void gcProtectJSWrapper(void*); |
| 120 static void gcUnprotectJSWrapper(void*); |
189 | 121 |
190 // TODO(eseidel): void* is a compile hack | 122 void finishedWithEvent(Event*); |
191 void attachDebugger(void*); | 123 void setEventHandlerLineNumber(int lineNumber); |
192 | 124 |
193 // Create a NPObject wrapper for a JSObject | 125 void setProcessingTimerCallback(bool processingTimerCallback) { m_proces
singTimerCallback = processingTimerCallback; } |
194 // NPObject *WrapScriptObject(NPP pluginId, JSObject* objectToWrap, | 126 bool processingUserGesture() const; |
195 // JSRootObject* originRootObject, | |
196 // JSRootObject* rootObject); | |
197 | 127 |
198 // --- Static methods assume we are running VM in single thread, --- | 128 void setPaused(bool paused) { m_paused = paused; } |
199 // --- and there is only one VM instance. --- | 129 bool isPaused() const { return m_paused; } |
200 | 130 |
201 // Returns the frame for the entered context. See comments in | 131 const String* sourceURL() const { return m_sourceURL; } // 0 if we are n
ot evaluating any script. |
202 // V8Proxy::retrieveFrameForEnteredContext() for more information. | |
203 static Frame* retrieveFrameForEnteredContext(); | |
204 | 132 |
205 // Returns the frame for the current context. See comments in | 133 void clearWindowShell(); |
206 // V8Proxy::retrieveFrameForEnteredContext() for more information. | 134 void updateDocument(); |
207 static Frame* retrieveFrameForCurrentContext(); | |
208 | 135 |
209 // Check whether it is safe to access a frame in another domain. | 136 void updateSecurityOrigin(); |
210 static bool isSafeScript(Frame* target); | 137 void clearScriptObjects(); |
211 | 138 void updatePlatformScriptObjects(); |
212 // Pass command-line flags to the JS engine | 139 void cleanupScriptObjectsForPlugin(void*); |
213 static void setFlags(const char* str, int length); | |
214 | |
215 // Protect and unprotect the JS wrapper from garbage collected. | |
216 static void gcProtectJSWrapper(void* object); | |
217 static void gcUnprotectJSWrapper(void* object); | |
218 | |
219 void finishedWithEvent(Event*); | |
220 void setEventHandlerLineNumber(int lineno); | |
221 | |
222 void setProcessingTimerCallback(bool b) { m_processingTimerCallback = b; } | |
223 bool processingUserGesture() const; | |
224 | |
225 void setPaused(bool b) { m_paused = b; } | |
226 bool isPaused() const { return m_paused; } | |
227 | |
228 const String* sourceURL() const { return m_sourceURL; } // 0 if we are not e
valuating any script | |
229 | |
230 void clearWindowShell(); | |
231 void updateDocument(); | |
232 | |
233 void updateSecurityOrigin(); | |
234 void clearScriptObjects(); | |
235 void updatePlatformScriptObjects(); | |
236 void cleanupScriptObjectsForPlugin(void*); | |
237 | 140 |
238 #if ENABLE(NETSCAPE_PLUGIN_API) | 141 #if ENABLE(NETSCAPE_PLUGIN_API) |
239 NPObject* createScriptObjectForPluginElement(HTMLPlugInElement*); | 142 NPObject* createScriptObjectForPluginElement(HTMLPlugInElement*); |
240 NPObject* windowScriptNPObject(); | 143 NPObject* windowScriptNPObject(); |
241 #endif | 144 #endif |
242 | 145 |
243 private: | 146 private: |
244 Frame* m_frame; | 147 Frame* m_frame; |
245 const String* m_sourceURL; | 148 const String* m_sourceURL; |
246 | 149 |
247 bool m_processingTimerCallback; | 150 bool m_processingTimerCallback; |
248 bool m_paused; | 151 bool m_paused; |
249 | 152 |
250 OwnPtr<ScriptState> m_scriptState; | 153 OwnPtr<ScriptState> m_scriptState; |
251 OwnPtr<V8Proxy> m_proxy; | 154 OwnPtr<V8Proxy> m_proxy; |
252 typedef HashMap<void*, NPObject*> PluginObjectMap; | 155 typedef HashMap<void*, NPObject*> PluginObjectMap; |
253 | 156 |
254 // A mapping between Widgets and their corresponding script object. | 157 // A mapping between Widgets and their corresponding script object. |
255 // This list is used so that when the plugin dies, we can immediately | 158 // This list is used so that when the plugin dies, we can immediately |
256 // invalidate all sub-objects which are associated with that plugin. | 159 // invalidate all sub-objects which are associated with that plugin. |
257 // The frame keeps a NPObject reference for each item on the list. | 160 // The frame keeps a NPObject reference for each item on the list. |
258 PluginObjectMap m_pluginObjects; | 161 PluginObjectMap m_pluginObjects; |
259 #if ENABLE(NETSCAPE_PLUGIN_API) | 162 #if ENABLE(NETSCAPE_PLUGIN_API) |
260 NPObject* m_windowScriptNPObject; | 163 NPObject* m_windowScriptNPObject; |
261 #endif | 164 #endif |
262 }; | 165 }; |
263 | 166 |
264 } // namespace WebCore | 167 } // namespace WebCore |
265 | 168 |
266 #endif // ScriptController_h | 169 #endif // ScriptController_h |
OLD | NEW |