Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / bindings / core / v8 / ScriptController.h
1 /*
2  * Copyright (C) 2008, 2009 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
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
13  * distribution.
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.
17  *
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.
29  */
30
31 #ifndef ScriptController_h
32 #define ScriptController_h
33
34 #include "bindings/core/v8/SharedPersistent.h"
35
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"
41 #include <v8.h>
42
43 struct NPObject;
44
45 namespace blink {
46
47 class DOMWrapperWorld;
48 class ExecutionContext;
49 class HTMLDocument;
50 class HTMLPlugInElement;
51 class KURL;
52 class LocalFrame;
53 class ScriptState;
54 class ScriptSourceCode;
55 class SecurityOrigin;
56 class WindowProxy;
57 class Widget;
58
59 typedef WTF::Vector<v8::Extension*> V8Extensions;
60
61 enum ReasonForCallingCanExecuteScripts {
62     AboutToExecuteScript,
63     NotAboutToExecuteScript
64 };
65
66 class ScriptController final : public NoBaseWillBeGarbageCollectedFinalized<ScriptController> {
67 public:
68     enum ExecuteScriptPolicy {
69         ExecuteScriptWhenScriptsDisabled,
70         DoNotExecuteScriptWhenScriptsDisabled
71     };
72
73     static PassOwnPtrWillBeRawPtr<ScriptController> create(LocalFrame* frame)
74     {
75         return adoptPtrWillBeNoop(new ScriptController(frame));
76     }
77
78     ~ScriptController();
79     void trace(Visitor*);
80
81     bool initializeMainWorld();
82     WindowProxy* windowProxy(DOMWrapperWorld&);
83     WindowProxy* existingWindowProxy(DOMWrapperWorld&);
84
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);
90
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.
94     //
95     // If an isolated world with the specified ID already exists, it is reused.
96     // Otherwise, a new world is created.
97     //
98     // FIXME: Get rid of extensionGroup here.
99     void executeScriptInIsolatedWorld(int worldID, const Vector<ScriptSourceCode>& sources, int extensionGroup, Vector<v8::Local<v8::Value> >* results);
100
101     // Returns true if argument is a JavaScript URL.
102     bool executeScriptIfJavaScriptURL(const KURL&);
103
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*);
106
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();
111
112     // Creates a property of the global object of a frame.
113     void bindToWindowObject(LocalFrame*, const String& key, NPObject*);
114
115     PassRefPtr<SharedPersistent<v8::Object> > createPluginWrapper(Widget*);
116
117     void enableEval();
118     void disableEval(const String& errorMessage);
119
120     static bool canAccessFromCurrentOrigin(LocalFrame*);
121
122     static void setCaptureCallStackForUncaughtExceptions(bool);
123     void collectIsolatedContexts(Vector<std::pair<ScriptState*, SecurityOrigin*> >&);
124
125     bool canExecuteScripts(ReasonForCallingCanExecuteScripts);
126
127     TextPosition eventHandlerPosition() const;
128
129     void clearWindowProxy();
130     void updateDocument();
131
132     void namedItemAdded(HTMLDocument*, const AtomicString&);
133     void namedItemRemoved(HTMLDocument*, const AtomicString&);
134
135     void updateSecurityOrigin(SecurityOrigin*);
136     void clearScriptObjects();
137     void cleanupScriptObjectsForPlugin(Widget*);
138
139     void clearForClose();
140
141     NPObject* createScriptObjectForPluginElement(HTMLPlugInElement*);
142     NPObject* windowScriptNPObject();
143
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();
149
150     void setWorldDebugId(int worldId, int debuggerId);
151
152     v8::Isolate* isolate() const { return m_isolate; }
153
154 private:
155     explicit ScriptController(LocalFrame*);
156
157     typedef WillBeHeapHashMap<int, OwnPtrWillBeMember<WindowProxy> > IsolatedWorldMap;
158     typedef HashMap<Widget*, NPObject*> PluginObjectMap;
159
160     v8::Local<v8::Value> evaluateScriptInMainWorld(const ScriptSourceCode&, AccessControlStatus, ExecuteScriptPolicy, double* compilationFinishTime = 0);
161
162     RawPtrWillBeMember<LocalFrame> m_frame;
163     const String* m_sourceURL;
164     v8::Isolate* m_isolate;
165
166     OwnPtrWillBeMember<WindowProxy> m_windowProxy;
167     IsolatedWorldMap m_isolatedWorlds;
168
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;
174
175     NPObject* m_windowScriptNPObject;
176 };
177
178 } // namespace blink
179
180 #endif // ScriptController_h