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 
windowShell(DOMWrapperWorld * world)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     }
existingWindowShell(DOMWrapperWorld * world)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     }
globalObject(DOMWrapperWorld * world)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 
setProcessingTimerCallback(bool b)111     void setProcessingTimerCallback(bool b) { m_processingTimerCallback = b; }
112     static bool processingUserGesture();
113     bool anyPageIsProcessingUserGesture() const;
114 
115     static bool canAccessFromCurrentOrigin(Frame*);
116     bool canExecuteScripts(ReasonForCallingCanExecuteScripts);
117 
118     // Debugger can be 0 to detach any existing Debugger.
119     void attachDebugger(JSC::Debugger*); // Attaches/detaches in all worlds/window shells.
120     void attachDebugger(JSDOMWindowShell*, JSC::Debugger*);
121 
setPaused(bool b)122     void setPaused(bool b) { m_paused = b; }
isPaused()123     bool isPaused() const { return m_paused; }
124 
setAllowPopupsFromPlugin(bool allowPopupsFromPlugin)125     void setAllowPopupsFromPlugin(bool allowPopupsFromPlugin) { m_allowPopupsFromPlugin = allowPopupsFromPlugin; }
allowPopupsFromPlugin()126     bool allowPopupsFromPlugin() const { return m_allowPopupsFromPlugin; }
127 
sourceURL()128     const String* sourceURL() const { return m_sourceURL; } // 0 if we are not evaluating any script
129 
130     void clearWindowShell(bool goingIntoPageCache = false);
131     void updateDocument();
132 
namedItemAdded(HTMLDocument *,const AtomicString &)133     void namedItemAdded(HTMLDocument*, const AtomicString&) { }
namedItemRemoved(HTMLDocument *,const AtomicString &)134     void namedItemRemoved(HTMLDocument*, const AtomicString&) { }
135 
136     // Notifies the ScriptController that the securityOrigin of the current
137     // document was modified.  For example, this method is called when
138     // document.domain is set.  This method is *not* called when a new document
139     // is attached to a frame because updateDocument() is called instead.
140     void updateSecurityOrigin();
141 
142     void clearScriptObjects();
143     void cleanupScriptObjectsForPlugin(void*);
144 
145     void updatePlatformScriptObjects();
146 
147     PassScriptInstance createScriptInstanceForWidget(Widget*);
148     JSC::Bindings::RootObject* bindingRootObject();
149     JSC::Bindings::RootObject* cacheableBindingRootObject();
150 
151     PassRefPtr<JSC::Bindings::RootObject> createRootObject(void* nativeHandle);
152 
153 #if ENABLE(INSPECTOR)
154     static void setCaptureCallStackForUncaughtExceptions(bool);
155 #endif
156 
157 #if PLATFORM(MAC)
158 #if ENABLE(JAVA_BRIDGE)
159     static void initJavaJSBindings();
160 #endif
161     WebScriptObject* windowScriptObject();
162 #endif
163 
164     JSC::JSObject* jsObjectForPluginElement(HTMLPlugInElement*);
165 
166 #if ENABLE(NETSCAPE_PLUGIN_API)
167     NPObject* createScriptObjectForPluginElement(HTMLPlugInElement*);
168     NPObject* windowScriptNPObject();
169 #endif
170 
171 private:
172     JSDOMWindowShell* initScript(DOMWrapperWorld* world);
173 
174     void disconnectPlatformScriptObjects();
175 
176     bool isJavaScriptAnchorNavigation() const;
177 
178     ShellMap m_windowShells;
179     Frame* m_frame;
180     const String* m_sourceURL;
181 
182     bool m_inExecuteScript;
183 
184     bool m_processingTimerCallback;
185     bool m_paused;
186     bool m_allowPopupsFromPlugin;
187 
188     // The root object used for objects bound outside the context of a plugin, such
189     // as NPAPI plugins. The plugins using these objects prevent a page from being cached so they
190     // are safe to invalidate() when WebKit navigates away from the page that contains them.
191     RefPtr<JSC::Bindings::RootObject> m_bindingRootObject;
192     // Unlike m_bindingRootObject these objects are used in pages that are cached, so they are not invalidate()'d.
193     // This ensures they are still available when the page is restored.
194     RefPtr<JSC::Bindings::RootObject> m_cacheableBindingRootObject;
195     RootObjectMap m_rootObjects;
196 #if ENABLE(NETSCAPE_PLUGIN_API)
197     NPObject* m_windowScriptNPObject;
198 #endif
199 #if PLATFORM(MAC)
200     RetainPtr<WebScriptObject> m_windowScriptObject;
201 #endif
202 };
203 
204 } // namespace WebCore
205 
206 #endif // ScriptController_h
207