tizen beta release
[framework/web/webkit-efl.git] / Source / WebCore / bindings / v8 / ScriptController.cpp
1 /*
2  * Copyright (C) 2008, 2009 Google Inc. All rights reserved.
3  * Copyright (C) 2009 Apple Inc. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  *     * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *     * Redistributions in binary form must reproduce the above
12  * copyright notice, this list of conditions and the following disclaimer
13  * in the documentation and/or other materials provided with the
14  * distribution.
15  *     * Neither the name of Google Inc. nor the names of its
16  * contributors may be used to endorse or promote products derived from
17  * this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31
32 #include "config.h"
33 #include "ScriptController.h"
34
35 #include "PlatformSupport.h"
36 #include "Document.h"
37 #include "ScriptCallStack.h"
38 #include "ScriptCallStackFactory.h"
39 #include "ScriptableDocumentParser.h"
40 #include "DOMWindow.h"
41 #include "Event.h"
42 #include "EventListener.h"
43 #include "EventNames.h"
44 #include "Frame.h"
45 #include "FrameLoaderClient.h"
46 #include "Node.h"
47 #include "NotImplemented.h"
48 #include "NPObjectWrapper.h"
49 #include "npruntime_impl.h"
50 #include "npruntime_priv.h"
51 #include "NPV8Object.h"
52 #include "ScriptSourceCode.h"
53 #include "SecurityOrigin.h"
54 #include "Settings.h"
55 #include "UserGestureIndicator.h"
56 #include "V8Binding.h"
57 #include "V8BindingState.h"
58 #include "V8DOMWindow.h"
59 #include "V8Event.h"
60 #include "V8HiddenPropertyName.h"
61 #include "V8HTMLEmbedElement.h"
62 #include "V8IsolatedContext.h"
63 #include "V8NPObject.h"
64 #include "V8Proxy.h"
65 #include "Widget.h"
66 #include <wtf/StdLibExtras.h>
67 #include <wtf/text/CString.h>
68
69 #if PLATFORM(QT)
70 #include <QJSEngine>
71 #endif
72
73 namespace WebCore {
74
75 void ScriptController::initializeThreading()
76 {
77     static bool initializedThreading = false;
78     if (!initializedThreading) {
79         WTF::initializeThreading();
80         WTF::initializeMainThread();
81         initializedThreading = true;
82     }
83 }
84
85 void ScriptController::setFlags(const char* string, int length)
86 {
87     v8::V8::SetFlagsFromString(string, length);
88 }
89
90 Frame* ScriptController::retrieveFrameForEnteredContext()
91 {
92     return V8Proxy::retrieveFrameForEnteredContext();
93 }
94
95 Frame* ScriptController::retrieveFrameForCurrentContext()
96 {
97     return V8Proxy::retrieveFrameForCurrentContext();
98 }
99
100 bool ScriptController::canAccessFromCurrentOrigin(Frame *frame)
101 {
102     return !v8::Context::InContext() || V8BindingSecurity::canAccessFrame(V8BindingState::Only(), frame, true);
103 }
104
105 bool ScriptController::isSafeScript(Frame* target)
106 {
107     return V8BindingSecurity::canAccessFrame(V8BindingState::Only(), target, true);
108 }
109
110 ScriptController::ScriptController(Frame* frame)
111     : m_frame(frame)
112     , m_sourceURL(0)
113     , m_inExecuteScript(false)
114     , m_paused(false)
115     , m_proxy(adoptPtr(new V8Proxy(frame)))
116 #if ENABLE(NETSCAPE_PLUGIN_API)
117     , m_wrappedWindowScriptNPObject(0)
118 #endif
119 {
120 }
121
122 ScriptController::~ScriptController()
123 {
124 }
125
126 void ScriptController::clearScriptObjects()
127 {
128     PluginObjectMap::iterator it = m_pluginObjects.begin();
129     for (; it != m_pluginObjects.end(); ++it) {
130         _NPN_UnregisterObject(it->second);
131         _NPN_ReleaseObject(it->second);
132     }
133     m_pluginObjects.clear();
134
135 #if ENABLE(NETSCAPE_PLUGIN_API)
136     if (m_wrappedWindowScriptNPObject) {
137         NPObjectWrapper* windowScriptObjectWrapper = NPObjectWrapper::getWrapper(m_wrappedWindowScriptNPObject);
138         ASSERT(windowScriptObjectWrapper);
139
140         NPObject* windowScriptNPObject = NPObjectWrapper::getUnderlyingNPObject(m_wrappedWindowScriptNPObject);
141         ASSERT(windowScriptNPObject);
142         // Call _NPN_DeallocateObject() instead of _NPN_ReleaseObject() so that we don't leak if a plugin fails to release the window
143         // script object properly.
144         // This shouldn't cause any problems for plugins since they should have already been stopped and destroyed at this point.
145         _NPN_DeallocateObject(windowScriptNPObject);
146
147         // Clear out the wrapped window script object pointer held by the wrapper.
148         windowScriptObjectWrapper->clear();
149         _NPN_ReleaseObject(m_wrappedWindowScriptNPObject);
150         m_wrappedWindowScriptNPObject = 0;
151     }
152 #endif
153 }
154
155 void ScriptController::updateSecurityOrigin()
156 {
157     m_proxy->windowShell()->updateSecurityOrigin();
158 }
159
160 void ScriptController::updatePlatformScriptObjects()
161 {
162     notImplemented();
163 }
164
165 bool ScriptController::processingUserGesture()
166 {
167     return UserGestureIndicator::processingUserGesture();
168 }
169
170 void ScriptController::evaluateInIsolatedWorld(unsigned worldID, const Vector<ScriptSourceCode>& sources)
171 {
172     m_proxy->evaluateInIsolatedWorld(worldID, sources, 0);
173 }
174
175 void ScriptController::evaluateInIsolatedWorld(unsigned worldID, const Vector<ScriptSourceCode>& sources, int extensionGroup)
176 {
177     m_proxy->evaluateInIsolatedWorld(worldID, sources, extensionGroup);
178 }
179
180 void ScriptController::setIsolatedWorldSecurityOrigin(int worldID, PassRefPtr<SecurityOrigin> securityOrigin)
181 {
182     m_proxy->setIsolatedWorldSecurityOrigin(worldID, securityOrigin);
183 }
184
185 // Evaluate a script file in the environment of this proxy.
186 ScriptValue ScriptController::evaluate(const ScriptSourceCode& sourceCode)
187 {
188     String sourceURL = sourceCode.url();
189     const String* savedSourceURL = m_sourceURL;
190     m_sourceURL = &sourceURL;
191
192     v8::HandleScope handleScope;
193     v8::Handle<v8::Context> v8Context = V8Proxy::mainWorldContext(m_proxy->frame());
194     if (v8Context.IsEmpty())
195         return ScriptValue();
196
197     v8::Context::Scope scope(v8Context);
198
199     RefPtr<Frame> protect(m_frame);
200
201     v8::Local<v8::Value> object = m_proxy->evaluate(sourceCode, 0);
202
203     m_sourceURL = savedSourceURL;
204
205     if (object.IsEmpty())
206         return ScriptValue();
207
208     return ScriptValue(object);
209 }
210
211 TextPosition ScriptController::eventHandlerPosition() const
212 {
213     ScriptableDocumentParser* parser = m_frame->document()->scriptableDocumentParser();
214     if (parser)
215         return parser->textPosition();
216     return TextPosition::minimumPosition();
217 }
218
219 void ScriptController::finishedWithEvent(Event* event)
220 {
221     m_proxy->finishedWithEvent(event);
222 }
223
224 // Create a V8 object with an interceptor of NPObjectPropertyGetter.
225 void ScriptController::bindToWindowObject(Frame* frame, const String& key, NPObject* object)
226 {
227     v8::HandleScope handleScope;
228
229     v8::Handle<v8::Context> v8Context = V8Proxy::mainWorldContext(frame);
230     if (v8Context.IsEmpty())
231         return;
232
233     v8::Context::Scope scope(v8Context);
234
235     v8::Handle<v8::Object> value = createV8ObjectForNPObject(object, 0);
236
237     // Attach to the global object.
238     v8::Handle<v8::Object> global = v8Context->Global();
239     global->Set(v8String(key), value);
240 }
241
242 void ScriptController::collectGarbage()
243 {
244     v8::HandleScope handleScope;
245
246     v8::Persistent<v8::Context> v8Context = v8::Context::New();
247     if (v8Context.IsEmpty())
248         return;
249     {
250         v8::Context::Scope scope(v8Context);
251         v8::Local<v8::String> source = v8::String::New("if (gc) gc();");
252         v8::Local<v8::String> name = v8::String::New("gc");
253         v8::Handle<v8::Script> script = v8::Script::Compile(source, name);
254         if (!script.IsEmpty())
255             script->Run();
256     }
257     v8Context.Dispose();
258 }
259
260 void ScriptController::lowMemoryNotification()
261 {
262     v8::V8::LowMemoryNotification();
263 }
264
265 bool ScriptController::haveInterpreter() const
266 {
267     return m_proxy->windowShell()->isContextInitialized();
268 }
269
270 void ScriptController::disableEval()
271 {
272     if (!m_proxy->windowShell()->initContextIfNeeded())
273         return;
274
275     v8::HandleScope handleScope;
276     v8::Handle<v8::Context> v8Context = V8Proxy::mainWorldContext(m_frame);
277     if (v8Context.IsEmpty())
278         return;
279
280     v8Context->AllowCodeGenerationFromStrings(false);
281 }
282
283 PassScriptInstance ScriptController::createScriptInstanceForWidget(Widget* widget)
284 {
285     ASSERT(widget);
286
287     if (widget->isFrameView())
288         return 0;
289
290     NPObject* npObject = PlatformSupport::pluginScriptableObject(widget);
291
292     if (!npObject)
293         return 0;
294
295     // Frame Memory Management for NPObjects
296     // -------------------------------------
297     // NPObjects are treated differently than other objects wrapped by JS.
298     // NPObjects can be created either by the browser (e.g. the main
299     // window object) or by the plugin (the main plugin object
300     // for a HTMLEmbedElement). Further, unlike most DOM Objects, the frame
301     // is especially careful to ensure NPObjects terminate at frame teardown because
302     // if a plugin leaks a reference, it could leak its objects (or the browser's objects).
303     //
304     // The Frame maintains a list of plugin objects (m_pluginObjects)
305     // which it can use to quickly find the wrapped embed object.
306     //
307     // Inside the NPRuntime, we've added a few methods for registering
308     // wrapped NPObjects. The purpose of the registration is because
309     // javascript garbage collection is non-deterministic, yet we need to
310     // be able to tear down the plugin objects immediately. When an object
311     // is registered, javascript can use it. When the object is destroyed,
312     // or when the object's "owning" object is destroyed, the object will
313     // be un-registered, and the javascript engine must not use it.
314     //
315     // Inside the javascript engine, the engine can keep a reference to the
316     // NPObject as part of its wrapper. However, before accessing the object
317     // it must consult the _NPN_Registry.
318
319     v8::Local<v8::Object> wrapper = createV8ObjectForNPObject(npObject, 0);
320
321     // Track the plugin object. We've been given a reference to the object.
322     m_pluginObjects.set(widget, npObject);
323
324     return V8ScriptInstance::create(wrapper);
325 }
326
327 void ScriptController::cleanupScriptObjectsForPlugin(Widget* nativeHandle)
328 {
329     PluginObjectMap::iterator it = m_pluginObjects.find(nativeHandle);
330     if (it == m_pluginObjects.end())
331         return;
332     _NPN_UnregisterObject(it->second);
333     _NPN_ReleaseObject(it->second);
334     m_pluginObjects.remove(it);
335 }
336
337 void ScriptController::getAllWorlds(Vector<DOMWrapperWorld*>& worlds)
338 {
339     worlds.append(mainThreadNormalWorld());
340 }
341
342 void ScriptController::evaluateInWorld(const ScriptSourceCode& source,
343                                        DOMWrapperWorld* world)
344 {
345     Vector<ScriptSourceCode> sources;
346     sources.append(source);
347     // FIXME: Get an ID from the world param.
348     evaluateInIsolatedWorld(0, sources);
349 }
350
351 static NPObject* createNoScriptObject()
352 {
353     notImplemented();
354     return 0;
355 }
356
357 static NPObject* createScriptObject(Frame* frame)
358 {
359     v8::HandleScope handleScope;
360     v8::Handle<v8::Context> v8Context = V8Proxy::mainWorldContext(frame);
361     if (v8Context.IsEmpty())
362         return createNoScriptObject();
363
364     v8::Context::Scope scope(v8Context);
365     DOMWindow* window = frame->domWindow();
366     v8::Handle<v8::Value> global = toV8(window);
367     ASSERT(global->IsObject());
368     return npCreateV8ScriptObject(0, v8::Handle<v8::Object>::Cast(global), window);
369 }
370
371 NPObject* ScriptController::windowScriptNPObject()
372 {
373     if (m_wrappedWindowScriptNPObject)
374         return m_wrappedWindowScriptNPObject;
375
376     NPObject* windowScriptNPObject = 0;
377     if (canExecuteScripts(NotAboutToExecuteScript)) {
378         // JavaScript is enabled, so there is a JavaScript window object.
379         // Return an NPObject bound to the window object.
380         windowScriptNPObject = createScriptObject(m_frame);
381         _NPN_RegisterObject(windowScriptNPObject, 0);
382     } else {
383         // JavaScript is not enabled, so we cannot bind the NPObject to the
384         // JavaScript window object. Instead, we create an NPObject of a
385         // different class, one which is not bound to a JavaScript object.
386         windowScriptNPObject = createNoScriptObject();
387     }
388
389     m_wrappedWindowScriptNPObject = NPObjectWrapper::create(windowScriptNPObject);
390     return m_wrappedWindowScriptNPObject;
391 }
392
393 NPObject* ScriptController::createScriptObjectForPluginElement(HTMLPlugInElement* plugin)
394 {
395     // Can't create NPObjects when JavaScript is disabled.
396     if (!canExecuteScripts(NotAboutToExecuteScript))
397         return createNoScriptObject();
398
399     v8::HandleScope handleScope;
400     v8::Handle<v8::Context> v8Context = V8Proxy::mainWorldContext(m_frame);
401     if (v8Context.IsEmpty())
402         return createNoScriptObject();
403     v8::Context::Scope scope(v8Context);
404
405     DOMWindow* window = m_frame->domWindow();
406     v8::Handle<v8::Value> v8plugin = toV8(static_cast<HTMLEmbedElement*>(plugin));
407     if (!v8plugin->IsObject())
408         return createNoScriptObject();
409
410     return npCreateV8ScriptObject(0, v8::Handle<v8::Object>::Cast(v8plugin), window);
411 }
412
413
414 void ScriptController::clearWindowShell(bool)
415 {
416     // V8 binding expects ScriptController::clearWindowShell only be called
417     // when a frame is loading a new page. V8Proxy::clearForNavigation
418     // creates a new context for the new page.
419     m_proxy->clearForNavigation();
420 }
421
422 #if ENABLE(INSPECTOR)
423 void ScriptController::setCaptureCallStackForUncaughtExceptions(bool value)
424 {
425     v8::V8::SetCaptureStackTraceForUncaughtExceptions(value, ScriptCallStack::maxCallStackSizeToCapture, stackTraceOptions);
426 }
427 #endif
428
429 void ScriptController::attachDebugger(void*)
430 {
431     notImplemented();
432 }
433
434 void ScriptController::updateDocument()
435 {
436     m_proxy->windowShell()->updateDocument();
437 }
438
439 void ScriptController::namedItemAdded(HTMLDocument* doc, const AtomicString& name)
440 {
441     m_proxy->windowShell()->namedItemAdded(doc, name);
442 }
443
444 void ScriptController::namedItemRemoved(HTMLDocument* doc, const AtomicString& name)
445 {
446     m_proxy->windowShell()->namedItemRemoved(doc, name);
447 }
448
449 } // namespace WebCore