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/core/v8/SharedPersistent.h"
36 #include "core/fetch/CrossOriginAccessControl.h"
37 #include "platform/heap/Handle.h"
38 #include "wtf/HashMap.h"
39 #include "wtf/Vector.h"
40 #include "wtf/text/TextPosition.h"
47 class DOMWrapperWorld;
48 class ExecutionContext;
50 class HTMLPlugInElement;
54 class ScriptSourceCode;
59 typedef WTF::Vector<v8::Extension*> V8Extensions;
61 enum ReasonForCallingCanExecuteScripts {
63 NotAboutToExecuteScript
66 class ScriptController final : public NoBaseWillBeGarbageCollectedFinalized<ScriptController> {
68 enum ExecuteScriptPolicy {
69 ExecuteScriptWhenScriptsDisabled,
70 DoNotExecuteScriptWhenScriptsDisabled
73 static PassOwnPtrWillBeRawPtr<ScriptController> create(LocalFrame* frame)
75 return adoptPtrWillBeNoop(new ScriptController(frame));
81 bool initializeMainWorld();
82 WindowProxy* windowProxy(DOMWrapperWorld&);
83 WindowProxy* existingWindowProxy(DOMWrapperWorld&);
85 // Evaluate JavaScript in the main world.
86 void executeScriptInMainWorld(const String&, ExecuteScriptPolicy = DoNotExecuteScriptWhenScriptsDisabled);
87 void executeScriptInMainWorld(const ScriptSourceCode&, AccessControlStatus = NotSharableCrossOrigin, double* compilationFinishTime = 0);
88 v8::Local<v8::Value> executeScriptInMainWorldAndReturnValue(const ScriptSourceCode&);
89 v8::Local<v8::Value> executeScriptAndReturnValue(v8::Handle<v8::Context>, const ScriptSourceCode&, AccessControlStatus = NotSharableCrossOrigin, double* compilationFinishTime = 0);
91 // Executes JavaScript in an isolated world. The script gets its own global scope,
92 // its own prototypes for intrinsic JavaScript objects (String, Array, and so-on),
93 // and its own wrappers for all DOM nodes and DOM constructors.
95 // If an isolated world with the specified ID already exists, it is reused.
96 // Otherwise, a new world is created.
98 // FIXME: Get rid of extensionGroup here.
99 void executeScriptInIsolatedWorld(int worldID, const Vector<ScriptSourceCode>& sources, int extensionGroup, Vector<v8::Local<v8::Value> >* results);
101 // Returns true if argument is a JavaScript URL.
102 bool executeScriptIfJavaScriptURL(const KURL&);
104 v8::Local<v8::Value> callFunction(v8::Handle<v8::Function>, v8::Handle<v8::Value>, int argc, v8::Handle<v8::Value> argv[]);
105 static v8::Local<v8::Value> callFunction(ExecutionContext*, v8::Handle<v8::Function>, v8::Handle<v8::Value> receiver, int argc, v8::Handle<v8::Value> info[], v8::Isolate*);
107 // Returns true if the current world is isolated, and has its own Content
108 // Security Policy. In this case, the policy of the main world should be
109 // ignored when evaluating resources injected into the DOM.
110 bool shouldBypassMainWorldCSP();
112 // Creates a property of the global object of a frame.
113 void bindToWindowObject(LocalFrame*, const String& key, NPObject*);
115 PassRefPtr<SharedPersistent<v8::Object> > createPluginWrapper(Widget*);
118 void disableEval(const String& errorMessage);
120 static bool canAccessFromCurrentOrigin(LocalFrame*);
122 static void setCaptureCallStackForUncaughtExceptions(bool);
123 void collectIsolatedContexts(Vector<std::pair<ScriptState*, SecurityOrigin*> >&);
125 bool canExecuteScripts(ReasonForCallingCanExecuteScripts);
127 TextPosition eventHandlerPosition() const;
129 void clearWindowProxy();
130 void updateDocument();
132 void namedItemAdded(HTMLDocument*, const AtomicString&);
133 void namedItemRemoved(HTMLDocument*, const AtomicString&);
135 void updateSecurityOrigin(SecurityOrigin*);
136 void clearScriptObjects();
137 void cleanupScriptObjectsForPlugin(Widget*);
139 void clearForClose();
141 NPObject* createScriptObjectForPluginElement(HTMLPlugInElement*);
142 NPObject* windowScriptNPObject();
144 // Registers a v8 extension to be available on webpages. Will only
145 // affect v8 contexts initialized after this call. Takes ownership of
146 // the v8::Extension object passed.
147 static void registerExtensionIfNeeded(v8::Extension*);
148 static V8Extensions& registeredExtensions();
150 void setWorldDebugId(int worldId, int debuggerId);
152 v8::Isolate* isolate() const { return m_isolate; }
155 explicit ScriptController(LocalFrame*);
157 typedef WillBeHeapHashMap<int, OwnPtrWillBeMember<WindowProxy> > IsolatedWorldMap;
158 typedef HashMap<Widget*, NPObject*> PluginObjectMap;
160 v8::Local<v8::Value> evaluateScriptInMainWorld(const ScriptSourceCode&, AccessControlStatus, ExecuteScriptPolicy, double* compilationFinishTime = 0);
162 RawPtrWillBeMember<LocalFrame> m_frame;
163 const String* m_sourceURL;
164 v8::Isolate* m_isolate;
166 OwnPtrWillBeMember<WindowProxy> m_windowProxy;
167 IsolatedWorldMap m_isolatedWorlds;
169 // A mapping between Widgets and their corresponding script object.
170 // This list is used so that when the plugin dies, we can immediately
171 // invalidate all sub-objects which are associated with that plugin.
172 // The frame keeps a NPObject reference for each item on the list.
173 PluginObjectMap m_pluginObjects;
175 NPObject* m_windowScriptNPObject;
180 #endif // ScriptController_h