initial import
[vuplus_webkit] / Source / WebCore / bindings / js / ScriptController.h
1 /*
2  *  Copyright (C) 1999 Harri Porten (porten@kde.org)
3  *  Copyright (C) 2001 Peter Kelly (pmk@post.com)
4  *  Copyright (C) 2008 Apple Inc. All rights reserved.
5  *  Copyright (C) 2008 Eric Seidel <eric@webkit.org>
6  *
7  *  This library is free software; you can redistribute it and/or
8  *  modify it under the terms of the GNU Lesser General Public
9  *  License as published by the Free Software Foundation; either
10  *  version 2 of the License, or (at your option) any later version.
11  *
12  *  This library is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  *  Lesser General Public License for more details.
16  *
17  *  You should have received a copy of the GNU Lesser General Public
18  *  License along with this library; if not, write to the Free Software
19  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
20  */
21
22 #ifndef ScriptController_h
23 #define ScriptController_h
24
25 #include "FrameLoaderTypes.h"
26 #include "JSDOMWindowShell.h"
27 #include "ScriptControllerBase.h"
28 #include "ScriptInstance.h"
29 #include <heap/Strong.h>
30 #include <wtf/Forward.h>
31 #include <wtf/RefPtr.h>
32
33 #if PLATFORM(MAC)
34 #include <wtf/RetainPtr.h>
35
36 #ifdef __OBJC__
37 @class WebScriptObject;
38 #else
39 class WebScriptObject;
40 #endif
41 #endif
42
43 struct NPObject;
44
45 namespace JSC {
46     class JSGlobalObject;
47
48     namespace Bindings {
49         class RootObject;
50     }
51 }
52
53 namespace WebCore {
54
55 class HTMLPlugInElement;
56 class Frame;
57 class ScriptSourceCode;
58 class ScriptValue;
59 class Widget;
60
61 typedef HashMap<void*, RefPtr<JSC::Bindings::RootObject> > RootObjectMap;
62
63 class ScriptController {
64     friend class ScriptCachedFrameData;
65     typedef WTF::HashMap< RefPtr<DOMWrapperWorld>, JSC::Strong<JSDOMWindowShell> > ShellMap;
66
67 public:
68     ScriptController(Frame*);
69     ~ScriptController();
70
71     static PassRefPtr<DOMWrapperWorld> createWorld();
72
73     JSDOMWindowShell* createWindowShell(DOMWrapperWorld*);
74     void destroyWindowShell(DOMWrapperWorld*);
75
76     JSDOMWindowShell* windowShell(DOMWrapperWorld* world)
77     {
78         ShellMap::iterator iter = m_windowShells.find(world);
79         return (iter != m_windowShells.end()) ? iter->second.get() : initScript(world);
80     }
81     JSDOMWindowShell* existingWindowShell(DOMWrapperWorld* world) const
82     {
83         ShellMap::const_iterator iter = m_windowShells.find(world);
84         return (iter != m_windowShells.end()) ? iter->second.get() : 0;
85     }
86     JSDOMWindow* globalObject(DOMWrapperWorld* world)
87     {
88         return windowShell(world)->window();
89     }
90
91     static void getAllWorlds(Vector<DOMWrapperWorld*>&);
92
93     ScriptValue executeScript(const ScriptSourceCode&);
94     ScriptValue executeScript(const String& script, bool forceUserGesture = false);
95     ScriptValue executeScriptInWorld(DOMWrapperWorld*, const String& script, bool forceUserGesture = false);
96
97     // Returns true if argument is a JavaScript URL.
98     bool executeIfJavaScriptURL(const KURL&, ShouldReplaceDocumentIfJavaScriptURL shouldReplaceDocumentIfJavaScriptURL = ReplaceDocumentIfJavaScriptURL);
99
100     // This function must be called from the main thread. It is safe to call it repeatedly.
101     // Darwin is an exception to this rule: it is OK to call this function from any thread, even reentrantly.
102     static void initializeThreading();
103
104     ScriptValue evaluate(const ScriptSourceCode&);
105     ScriptValue evaluateInWorld(const ScriptSourceCode&, DOMWrapperWorld*);
106
107     int eventHandlerLineNumber() const;
108
109     void disableEval();
110
111     static bool processingUserGesture();
112
113     static bool canAccessFromCurrentOrigin(Frame*);
114     bool canExecuteScripts(ReasonForCallingCanExecuteScripts);
115
116     // Debugger can be 0 to detach any existing Debugger.
117     void attachDebugger(JSC::Debugger*); // Attaches/detaches in all worlds/window shells.
118     void attachDebugger(JSDOMWindowShell*, JSC::Debugger*);
119
120     void setPaused(bool b) { m_paused = b; }
121     bool isPaused() const { return m_paused; }
122
123     const String* sourceURL() const { return m_sourceURL; } // 0 if we are not evaluating any script
124
125     void clearWindowShell(bool goingIntoPageCache = false);
126     void updateDocument();
127
128     void namedItemAdded(HTMLDocument*, const AtomicString&) { }
129     void namedItemRemoved(HTMLDocument*, const AtomicString&) { }
130
131     // Notifies the ScriptController that the securityOrigin of the current
132     // document was modified.  For example, this method is called when
133     // document.domain is set.  This method is *not* called when a new document
134     // is attached to a frame because updateDocument() is called instead.
135     void updateSecurityOrigin();
136
137     void clearScriptObjects();
138     void cleanupScriptObjectsForPlugin(void*);
139
140     void updatePlatformScriptObjects();
141
142     PassScriptInstance createScriptInstanceForWidget(Widget*);
143     JSC::Bindings::RootObject* bindingRootObject();
144     JSC::Bindings::RootObject* cacheableBindingRootObject();
145
146     PassRefPtr<JSC::Bindings::RootObject> createRootObject(void* nativeHandle);
147
148 #if ENABLE(INSPECTOR)
149     static void setCaptureCallStackForUncaughtExceptions(bool);
150 #endif
151
152 #if PLATFORM(MAC)
153 #if ENABLE(JAVA_BRIDGE)
154     static void initJavaJSBindings();
155 #endif
156     WebScriptObject* windowScriptObject();
157 #endif
158
159     JSC::JSObject* jsObjectForPluginElement(HTMLPlugInElement*);
160     
161 #if ENABLE(NETSCAPE_PLUGIN_API)
162     NPObject* createScriptObjectForPluginElement(HTMLPlugInElement*);
163     NPObject* windowScriptNPObject();
164 #endif
165
166 private:
167     JSDOMWindowShell* initScript(DOMWrapperWorld* world);
168
169     void disconnectPlatformScriptObjects();
170
171     ShellMap m_windowShells;
172     Frame* m_frame;
173     const String* m_sourceURL;
174
175     bool m_inExecuteScript;
176
177     bool m_paused;
178
179     // The root object used for objects bound outside the context of a plugin, such
180     // as NPAPI plugins. The plugins using these objects prevent a page from being cached so they
181     // are safe to invalidate() when WebKit navigates away from the page that contains them.
182     RefPtr<JSC::Bindings::RootObject> m_bindingRootObject;
183     // Unlike m_bindingRootObject these objects are used in pages that are cached, so they are not invalidate()'d.
184     // This ensures they are still available when the page is restored.
185     RefPtr<JSC::Bindings::RootObject> m_cacheableBindingRootObject;
186     RootObjectMap m_rootObjects;
187 #if ENABLE(NETSCAPE_PLUGIN_API)
188     NPObject* m_windowScriptNPObject;
189 #endif
190 #if PLATFORM(MAC)
191     RetainPtr<WebScriptObject> m_windowScriptObject;
192 #endif
193 };
194
195 } // namespace WebCore
196
197 #endif // ScriptController_h