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>
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.
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.
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
22 #ifndef ScriptController_h
23 #define ScriptController_h
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>
34 #include <wtf/RetainPtr.h>
37 @class WebScriptObject;
39 class WebScriptObject;
55 class HTMLPlugInElement;
57 class ScriptSourceCode;
61 typedef HashMap<void*, RefPtr<JSC::Bindings::RootObject> > RootObjectMap;
63 class ScriptController {
64 friend class ScriptCachedFrameData;
65 typedef WTF::HashMap< RefPtr<DOMWrapperWorld>, JSC::Strong<JSDOMWindowShell> > ShellMap;
68 ScriptController(Frame*);
71 static PassRefPtr<DOMWrapperWorld> createWorld();
73 JSDOMWindowShell* createWindowShell(DOMWrapperWorld*);
74 void destroyWindowShell(DOMWrapperWorld*);
76 JSDOMWindowShell* windowShell(DOMWrapperWorld* world)
78 ShellMap::iterator iter = m_windowShells.find(world);
79 return (iter != m_windowShells.end()) ? iter->second.get() : initScript(world);
81 JSDOMWindowShell* existingWindowShell(DOMWrapperWorld* world) const
83 ShellMap::const_iterator iter = m_windowShells.find(world);
84 return (iter != m_windowShells.end()) ? iter->second.get() : 0;
86 JSDOMWindow* globalObject(DOMWrapperWorld* world)
88 return windowShell(world)->window();
91 static void getAllWorlds(Vector<DOMWrapperWorld*>&);
93 ScriptValue executeScript(const ScriptSourceCode&);
94 ScriptValue executeScript(const String& script, bool forceUserGesture = false);
95 ScriptValue executeScriptInWorld(DOMWrapperWorld*, const String& script, bool forceUserGesture = false);
97 // Returns true if argument is a JavaScript URL.
98 bool executeIfJavaScriptURL(const KURL&, ShouldReplaceDocumentIfJavaScriptURL shouldReplaceDocumentIfJavaScriptURL = ReplaceDocumentIfJavaScriptURL);
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();
104 ScriptValue evaluate(const ScriptSourceCode&);
105 ScriptValue evaluateInWorld(const ScriptSourceCode&, DOMWrapperWorld*);
107 int eventHandlerLineNumber() const;
111 static bool processingUserGesture();
113 static bool canAccessFromCurrentOrigin(Frame*);
114 bool canExecuteScripts(ReasonForCallingCanExecuteScripts);
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*);
120 void setPaused(bool b) { m_paused = b; }
121 bool isPaused() const { return m_paused; }
123 const String* sourceURL() const { return m_sourceURL; } // 0 if we are not evaluating any script
125 void clearWindowShell(bool goingIntoPageCache = false);
126 void updateDocument();
128 void namedItemAdded(HTMLDocument*, const AtomicString&) { }
129 void namedItemRemoved(HTMLDocument*, const AtomicString&) { }
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();
137 void clearScriptObjects();
138 void cleanupScriptObjectsForPlugin(void*);
140 void updatePlatformScriptObjects();
142 PassScriptInstance createScriptInstanceForWidget(Widget*);
143 JSC::Bindings::RootObject* bindingRootObject();
144 JSC::Bindings::RootObject* cacheableBindingRootObject();
146 PassRefPtr<JSC::Bindings::RootObject> createRootObject(void* nativeHandle);
148 #if ENABLE(INSPECTOR)
149 static void setCaptureCallStackForUncaughtExceptions(bool);
153 #if ENABLE(JAVA_BRIDGE)
154 static void initJavaJSBindings();
156 WebScriptObject* windowScriptObject();
159 JSC::JSObject* jsObjectForPluginElement(HTMLPlugInElement*);
161 #if ENABLE(NETSCAPE_PLUGIN_API)
162 NPObject* createScriptObjectForPluginElement(HTMLPlugInElement*);
163 NPObject* windowScriptNPObject();
167 JSDOMWindowShell* initScript(DOMWrapperWorld* world);
169 void disconnectPlatformScriptObjects();
171 ShellMap m_windowShells;
173 const String* m_sourceURL;
175 bool m_inExecuteScript;
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;
191 RetainPtr<WebScriptObject> m_windowScriptObject;
195 } // namespace WebCore
197 #endif // ScriptController_h