2 * Copyright (C) 2008, 2009 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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.
31 #ifndef ScriptController_h
32 #define ScriptController_h
34 #include "bindings/v8/ScriptValue.h"
35 #include "bindings/v8/SharedPersistent.h"
37 #include "core/fetch/CrossOriginAccessControl.h"
38 #include "wtf/Forward.h"
39 #include "wtf/HashMap.h"
40 #include "wtf/RefCounted.h"
41 #include "wtf/Vector.h"
42 #include "wtf/text/TextPosition.h"
49 class DOMWrapperWorld;
50 class ExecutionContext;
54 class HTMLPlugInElement;
56 class ScriptSourceCode;
62 typedef WTF::Vector<v8::Extension*> V8Extensions;
64 enum ReasonForCallingCanExecuteScripts {
66 NotAboutToExecuteScript
69 class ScriptController {
71 enum ExecuteScriptPolicy {
72 ExecuteScriptWhenScriptsDisabled,
73 DoNotExecuteScriptWhenScriptsDisabled
76 ScriptController(Frame*);
79 bool initializeMainWorld();
80 V8WindowShell* windowShell(DOMWrapperWorld*);
81 V8WindowShell* existingWindowShell(DOMWrapperWorld*);
83 // Evaluate JavaScript in the main world.
84 void executeScriptInMainWorld(const String&, ExecuteScriptPolicy = DoNotExecuteScriptWhenScriptsDisabled);
85 void executeScriptInMainWorld(const ScriptSourceCode&, AccessControlStatus = NotSharableCrossOrigin);
86 ScriptValue executeScriptInMainWorldAndReturnValue(const ScriptSourceCode&);
87 v8::Local<v8::Value> executeScriptAndReturnValue(v8::Handle<v8::Context>, const ScriptSourceCode&, AccessControlStatus = NotSharableCrossOrigin);
89 // Executes JavaScript in an isolated world. The script gets its own global scope,
90 // its own prototypes for intrinsic JavaScript objects (String, Array, and so-on),
91 // and its own wrappers for all DOM nodes and DOM constructors.
93 // If an isolated world with the specified ID already exists, it is reused.
94 // Otherwise, a new world is created.
96 // FIXME: Get rid of extensionGroup here.
97 void executeScriptInIsolatedWorld(int worldID, const Vector<ScriptSourceCode>& sources, int extensionGroup, Vector<ScriptValue>* results);
99 // Returns true if argument is a JavaScript URL.
100 bool executeScriptIfJavaScriptURL(const KURL&);
102 v8::Local<v8::Value> callFunction(v8::Handle<v8::Function>, v8::Handle<v8::Object>, int argc, v8::Handle<v8::Value> argv[]);
103 static v8::Local<v8::Value> callFunction(ExecutionContext*, v8::Handle<v8::Function>, v8::Handle<v8::Object> receiver, int argc, v8::Handle<v8::Value> info[], v8::Isolate*);
105 // Returns true if the current world is isolated, and has its own Content
106 // Security Policy. In this case, the policy of the main world should be
107 // ignored when evaluating resources injected into the DOM.
108 bool shouldBypassMainWorldContentSecurityPolicy();
110 // Creates a property of the global object of a frame.
111 void bindToWindowObject(Frame*, const String& key, NPObject*);
113 PassRefPtr<SharedPersistent<v8::Object> > createPluginWrapper(Widget*);
116 void disableEval(const String& errorMessage);
118 static bool canAccessFromCurrentOrigin(Frame*);
120 static void setCaptureCallStackForUncaughtExceptions(bool);
121 void collectIsolatedContexts(Vector<std::pair<ScriptState*, SecurityOrigin*> >&);
123 bool canExecuteScripts(ReasonForCallingCanExecuteScripts);
125 TextPosition eventHandlerPosition() const;
127 void clearWindowShell();
128 void updateDocument();
130 void namedItemAdded(HTMLDocument*, const AtomicString&);
131 void namedItemRemoved(HTMLDocument*, const AtomicString&);
133 void updateSecurityOrigin(SecurityOrigin*);
134 void clearScriptObjects();
135 void cleanupScriptObjectsForPlugin(Widget*);
137 void clearForClose();
138 void clearForOutOfMemory();
140 NPObject* createScriptObjectForPluginElement(HTMLPlugInElement*);
141 NPObject* windowScriptNPObject();
143 // Registers a v8 extension to be available on webpages. Will only
144 // affect v8 contexts initialized after this call. Takes ownership of
145 // the v8::Extension object passed.
146 static void registerExtensionIfNeeded(v8::Extension*);
147 static V8Extensions& registeredExtensions();
149 bool setContextDebugId(int);
150 static int contextDebugId(v8::Handle<v8::Context>);
152 v8::Isolate* isolate() const { return m_isolate; }
155 typedef HashMap<int, OwnPtr<V8WindowShell> > IsolatedWorldMap;
156 typedef HashMap<Widget*, NPObject*> PluginObjectMap;
158 ScriptValue evaluateScriptInMainWorld(const ScriptSourceCode&, AccessControlStatus, ExecuteScriptPolicy);
159 void clearForClose(bool destroyGlobal);
162 const String* m_sourceURL;
163 v8::Isolate* m_isolate;
165 OwnPtr<V8WindowShell> m_windowShell;
166 IsolatedWorldMap m_isolatedWorlds;
168 // A mapping between Widgets and their corresponding script object.
169 // This list is used so that when the plugin dies, we can immediately
170 // invalidate all sub-objects which are associated with that plugin.
171 // The frame keeps a NPObject reference for each item on the list.
172 PluginObjectMap m_pluginObjects;
174 NPObject* m_windowScriptNPObject;
177 } // namespace WebCore
179 #endif // ScriptController_h