2 * Copyright (C) 2008, 2009 Google Inc. All rights reserved.
3 * Copyright (C) 2009 Apple Inc. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
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
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.
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.
33 #include "ScriptController.h"
35 #include "PlatformSupport.h"
37 #include "ScriptCallStack.h"
38 #include "ScriptCallStackFactory.h"
39 #include "ScriptableDocumentParser.h"
40 #include "DOMWindow.h"
42 #include "EventListener.h"
43 #include "EventNames.h"
45 #include "FrameLoaderClient.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"
55 #include "UserGestureIndicator.h"
56 #include "V8Binding.h"
57 #include "V8BindingState.h"
58 #include "V8DOMWindow.h"
60 #include "V8HiddenPropertyName.h"
61 #include "V8HTMLEmbedElement.h"
62 #include "V8IsolatedContext.h"
63 #include "V8NPObject.h"
66 #include <wtf/StdLibExtras.h>
67 #include <wtf/text/CString.h>
75 void ScriptController::initializeThreading()
77 static bool initializedThreading = false;
78 if (!initializedThreading) {
79 WTF::initializeThreading();
80 WTF::initializeMainThread();
81 initializedThreading = true;
85 void ScriptController::setFlags(const char* string, int length)
87 v8::V8::SetFlagsFromString(string, length);
90 Frame* ScriptController::retrieveFrameForEnteredContext()
92 return V8Proxy::retrieveFrameForEnteredContext();
95 Frame* ScriptController::retrieveFrameForCurrentContext()
97 return V8Proxy::retrieveFrameForCurrentContext();
100 bool ScriptController::canAccessFromCurrentOrigin(Frame *frame)
102 return !v8::Context::InContext() || V8BindingSecurity::canAccessFrame(V8BindingState::Only(), frame, true);
105 bool ScriptController::isSafeScript(Frame* target)
107 return V8BindingSecurity::canAccessFrame(V8BindingState::Only(), target, true);
110 ScriptController::ScriptController(Frame* frame)
113 , m_inExecuteScript(false)
115 , m_proxy(adoptPtr(new V8Proxy(frame)))
116 #if ENABLE(NETSCAPE_PLUGIN_API)
117 , m_wrappedWindowScriptNPObject(0)
122 ScriptController::~ScriptController()
126 void ScriptController::clearScriptObjects()
128 PluginObjectMap::iterator it = m_pluginObjects.begin();
129 for (; it != m_pluginObjects.end(); ++it) {
130 _NPN_UnregisterObject(it->second);
131 _NPN_ReleaseObject(it->second);
133 m_pluginObjects.clear();
135 #if ENABLE(NETSCAPE_PLUGIN_API)
136 if (m_wrappedWindowScriptNPObject) {
137 NPObjectWrapper* windowScriptObjectWrapper = NPObjectWrapper::getWrapper(m_wrappedWindowScriptNPObject);
138 ASSERT(windowScriptObjectWrapper);
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);
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;
155 void ScriptController::updateSecurityOrigin()
157 m_proxy->windowShell()->updateSecurityOrigin();
160 void ScriptController::updatePlatformScriptObjects()
165 bool ScriptController::processingUserGesture()
167 return UserGestureIndicator::processingUserGesture();
170 void ScriptController::evaluateInIsolatedWorld(unsigned worldID, const Vector<ScriptSourceCode>& sources)
172 m_proxy->evaluateInIsolatedWorld(worldID, sources, 0);
175 void ScriptController::evaluateInIsolatedWorld(unsigned worldID, const Vector<ScriptSourceCode>& sources, int extensionGroup)
177 m_proxy->evaluateInIsolatedWorld(worldID, sources, extensionGroup);
180 void ScriptController::setIsolatedWorldSecurityOrigin(int worldID, PassRefPtr<SecurityOrigin> securityOrigin)
182 m_proxy->setIsolatedWorldSecurityOrigin(worldID, securityOrigin);
185 // Evaluate a script file in the environment of this proxy.
186 ScriptValue ScriptController::evaluate(const ScriptSourceCode& sourceCode)
188 String sourceURL = sourceCode.url();
189 const String* savedSourceURL = m_sourceURL;
190 m_sourceURL = &sourceURL;
192 v8::HandleScope handleScope;
193 v8::Handle<v8::Context> v8Context = V8Proxy::mainWorldContext(m_proxy->frame());
194 if (v8Context.IsEmpty())
195 return ScriptValue();
197 v8::Context::Scope scope(v8Context);
199 RefPtr<Frame> protect(m_frame);
201 v8::Local<v8::Value> object = m_proxy->evaluate(sourceCode, 0);
203 m_sourceURL = savedSourceURL;
205 if (object.IsEmpty())
206 return ScriptValue();
208 return ScriptValue(object);
211 TextPosition ScriptController::eventHandlerPosition() const
213 ScriptableDocumentParser* parser = m_frame->document()->scriptableDocumentParser();
215 return parser->textPosition();
216 return TextPosition::minimumPosition();
219 void ScriptController::finishedWithEvent(Event* event)
221 m_proxy->finishedWithEvent(event);
224 // Create a V8 object with an interceptor of NPObjectPropertyGetter.
225 void ScriptController::bindToWindowObject(Frame* frame, const String& key, NPObject* object)
227 v8::HandleScope handleScope;
229 v8::Handle<v8::Context> v8Context = V8Proxy::mainWorldContext(frame);
230 if (v8Context.IsEmpty())
233 v8::Context::Scope scope(v8Context);
235 v8::Handle<v8::Object> value = createV8ObjectForNPObject(object, 0);
237 // Attach to the global object.
238 v8::Handle<v8::Object> global = v8Context->Global();
239 global->Set(v8String(key), value);
242 void ScriptController::collectGarbage()
244 v8::HandleScope handleScope;
246 v8::Persistent<v8::Context> v8Context = v8::Context::New();
247 if (v8Context.IsEmpty())
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())
260 void ScriptController::lowMemoryNotification()
262 v8::V8::LowMemoryNotification();
265 bool ScriptController::haveInterpreter() const
267 return m_proxy->windowShell()->isContextInitialized();
270 void ScriptController::disableEval()
272 if (!m_proxy->windowShell()->initContextIfNeeded())
275 v8::HandleScope handleScope;
276 v8::Handle<v8::Context> v8Context = V8Proxy::mainWorldContext(m_frame);
277 if (v8Context.IsEmpty())
280 v8Context->AllowCodeGenerationFromStrings(false);
283 PassScriptInstance ScriptController::createScriptInstanceForWidget(Widget* widget)
287 if (widget->isFrameView())
290 NPObject* npObject = PlatformSupport::pluginScriptableObject(widget);
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).
304 // The Frame maintains a list of plugin objects (m_pluginObjects)
305 // which it can use to quickly find the wrapped embed object.
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.
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.
319 v8::Local<v8::Object> wrapper = createV8ObjectForNPObject(npObject, 0);
321 // Track the plugin object. We've been given a reference to the object.
322 m_pluginObjects.set(widget, npObject);
324 return V8ScriptInstance::create(wrapper);
327 void ScriptController::cleanupScriptObjectsForPlugin(Widget* nativeHandle)
329 PluginObjectMap::iterator it = m_pluginObjects.find(nativeHandle);
330 if (it == m_pluginObjects.end())
332 _NPN_UnregisterObject(it->second);
333 _NPN_ReleaseObject(it->second);
334 m_pluginObjects.remove(it);
337 void ScriptController::getAllWorlds(Vector<DOMWrapperWorld*>& worlds)
339 worlds.append(mainThreadNormalWorld());
342 void ScriptController::evaluateInWorld(const ScriptSourceCode& source,
343 DOMWrapperWorld* world)
345 Vector<ScriptSourceCode> sources;
346 sources.append(source);
347 // FIXME: Get an ID from the world param.
348 evaluateInIsolatedWorld(0, sources);
351 static NPObject* createNoScriptObject()
357 static NPObject* createScriptObject(Frame* frame)
359 v8::HandleScope handleScope;
360 v8::Handle<v8::Context> v8Context = V8Proxy::mainWorldContext(frame);
361 if (v8Context.IsEmpty())
362 return createNoScriptObject();
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);
371 NPObject* ScriptController::windowScriptNPObject()
373 if (m_wrappedWindowScriptNPObject)
374 return m_wrappedWindowScriptNPObject;
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);
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();
389 m_wrappedWindowScriptNPObject = NPObjectWrapper::create(windowScriptNPObject);
390 return m_wrappedWindowScriptNPObject;
393 NPObject* ScriptController::createScriptObjectForPluginElement(HTMLPlugInElement* plugin)
395 // Can't create NPObjects when JavaScript is disabled.
396 if (!canExecuteScripts(NotAboutToExecuteScript))
397 return createNoScriptObject();
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);
405 DOMWindow* window = m_frame->domWindow();
406 v8::Handle<v8::Value> v8plugin = toV8(static_cast<HTMLEmbedElement*>(plugin));
407 if (!v8plugin->IsObject())
408 return createNoScriptObject();
410 return npCreateV8ScriptObject(0, v8::Handle<v8::Object>::Cast(v8plugin), window);
414 void ScriptController::clearWindowShell(bool)
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();
422 #if ENABLE(INSPECTOR)
423 void ScriptController::setCaptureCallStackForUncaughtExceptions(bool value)
425 v8::V8::SetCaptureStackTraceForUncaughtExceptions(value, ScriptCallStack::maxCallStackSizeToCapture, stackTraceOptions);
429 void ScriptController::attachDebugger(void*)
434 void ScriptController::updateDocument()
436 m_proxy->windowShell()->updateDocument();
439 void ScriptController::namedItemAdded(HTMLDocument* doc, const AtomicString& name)
441 m_proxy->windowShell()->namedItemAdded(doc, name);
444 void ScriptController::namedItemRemoved(HTMLDocument* doc, const AtomicString& name)
446 m_proxy->windowShell()->namedItemRemoved(doc, name);
449 } // namespace WebCore