2 * Copyright (C) 1999 Harri Porten (porten@kde.org)
3 * Copyright (C) 2001 Peter Kelly (pmk@post.com)
4 * Copyright (C) 2008-2017 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
24 #include "FrameLoaderTypes.h"
25 #include "JSWindowProxy.h"
26 #include "WindowProxy.h"
27 #include <JavaScriptCore/JSBase.h>
28 #include <JavaScriptCore/Strong.h>
29 #include <wtf/Forward.h>
30 #include <wtf/RefPtr.h>
31 #include <wtf/text/TextPosition.h>
34 #include <wtf/RetainPtr.h>
36 OBJC_CLASS WebScriptObject;
44 class JSInternalPromise;
55 class CachedScriptFetcher;
58 class HTMLPlugInElement;
59 class LoadableModuleScript;
60 class ModuleFetchParameters;
61 class ScriptSourceCode;
64 struct ExceptionDetails;
66 enum ReasonForCallingCanExecuteScripts {
67 AboutToCreateEventListener,
69 NotAboutToExecuteScript
72 class ScriptController {
73 WTF_MAKE_FAST_ALLOCATED;
75 using RootObjectMap = HashMap<void*, Ref<JSC::Bindings::RootObject>>;
78 explicit ScriptController(Frame&);
81 WEBCORE_EXPORT static Ref<DOMWrapperWorld> createWorld();
83 JSDOMWindow* globalObject(DOMWrapperWorld& world)
85 return JSC::jsCast<JSDOMWindow*>(jsWindowProxy(world).window());
88 static void getAllWorlds(Vector<Ref<DOMWrapperWorld>>&);
90 JSC::JSValue executeScript(const ScriptSourceCode&, ExceptionDetails* = nullptr);
91 WEBCORE_EXPORT JSC::JSValue executeScript(const String& script, bool forceUserGesture = false, ExceptionDetails* = nullptr);
92 WEBCORE_EXPORT JSC::JSValue executeScriptInWorld(DOMWrapperWorld&, const String& script, bool forceUserGesture = false, ExceptionDetails* = nullptr);
94 // Returns true if argument is a JavaScript URL.
95 bool executeIfJavaScriptURL(const URL&, ShouldReplaceDocumentIfJavaScriptURL shouldReplaceDocumentIfJavaScriptURL = ReplaceDocumentIfJavaScriptURL);
97 // This function must be called from the main thread. It is safe to call it repeatedly.
98 // Darwin is an exception to this rule: it is OK to call this function from any thread, even reentrantly.
99 static void initializeThreading();
101 JSC::JSValue evaluate(const ScriptSourceCode&, ExceptionDetails* = nullptr);
102 JSC::JSValue evaluateInWorld(const ScriptSourceCode&, DOMWrapperWorld&, ExceptionDetails* = nullptr);
104 void loadModuleScriptInWorld(LoadableModuleScript&, const String& moduleName, Ref<ModuleFetchParameters>&&, DOMWrapperWorld&);
105 void loadModuleScript(LoadableModuleScript&, const String& moduleName, Ref<ModuleFetchParameters>&&);
106 void loadModuleScriptInWorld(LoadableModuleScript&, const ScriptSourceCode&, DOMWrapperWorld&);
107 void loadModuleScript(LoadableModuleScript&, const ScriptSourceCode&);
109 JSC::JSValue linkAndEvaluateModuleScriptInWorld(LoadableModuleScript& , DOMWrapperWorld&);
110 JSC::JSValue linkAndEvaluateModuleScript(LoadableModuleScript&);
112 JSC::JSValue evaluateModule(const URL&, JSC::JSModuleRecord&, DOMWrapperWorld&);
113 JSC::JSValue evaluateModule(const URL&, JSC::JSModuleRecord&);
115 WTF::TextPosition eventHandlerPosition() const;
118 void enableWebAssembly();
119 void disableEval(const String& errorMessage);
120 void disableWebAssembly(const String& errorMessage);
122 static bool canAccessFromCurrentOrigin(Frame*);
123 WEBCORE_EXPORT bool canExecuteScripts(ReasonForCallingCanExecuteScripts);
125 void setPaused(bool b) { m_paused = b; }
126 bool isPaused() const { return m_paused; }
128 const String* sourceURL() const { return m_sourceURL; } // 0 if we are not evaluating any script
130 void updateDocument();
132 void namedItemAdded(HTMLDocument*, const AtomicString&) { }
133 void namedItemRemoved(HTMLDocument*, const AtomicString&) { }
135 void clearScriptObjects();
136 WEBCORE_EXPORT void cleanupScriptObjectsForPlugin(void*);
138 void updatePlatformScriptObjects();
140 RefPtr<JSC::Bindings::Instance> createScriptInstanceForWidget(Widget*);
141 WEBCORE_EXPORT JSC::Bindings::RootObject* bindingRootObject();
142 JSC::Bindings::RootObject* cacheableBindingRootObject();
143 JSC::Bindings::RootObject* existingCacheableBindingRootObject() const { return m_cacheableBindingRootObject.get(); }
145 WEBCORE_EXPORT Ref<JSC::Bindings::RootObject> createRootObject(void* nativeHandle);
147 void collectIsolatedContexts(Vector<std::pair<JSC::ExecState*, SecurityOrigin*>>&);
150 WEBCORE_EXPORT WebScriptObject* windowScriptObject();
151 WEBCORE_EXPORT JSContext *javaScriptContext();
154 WEBCORE_EXPORT JSC::JSObject* jsObjectForPluginElement(HTMLPlugInElement*);
156 #if ENABLE(NETSCAPE_PLUGIN_API)
157 WEBCORE_EXPORT NPObject* windowScriptNPObject();
160 void initScriptForWindowProxy(JSWindowProxy&);
163 void setupModuleScriptHandlers(LoadableModuleScript&, JSC::JSInternalPromise&, DOMWrapperWorld&);
165 void disconnectPlatformScriptObjects();
167 WEBCORE_EXPORT WindowProxy& windowProxy();
168 WEBCORE_EXPORT JSWindowProxy& jsWindowProxy(DOMWrapperWorld&);
171 const String* m_sourceURL;
175 // The root object used for objects bound outside the context of a plugin, such
176 // as NPAPI plugins. The plugins using these objects prevent a page from being cached so they
177 // are safe to invalidate() when WebKit navigates away from the page that contains them.
178 RefPtr<JSC::Bindings::RootObject> m_bindingRootObject;
179 // Unlike m_bindingRootObject these objects are used in pages that are cached, so they are not invalidate()'d.
180 // This ensures they are still available when the page is restored.
181 RefPtr<JSC::Bindings::RootObject> m_cacheableBindingRootObject;
182 RootObjectMap m_rootObjects;
183 #if ENABLE(NETSCAPE_PLUGIN_API)
184 NPObject* m_windowScriptNPObject;
187 RetainPtr<WebScriptObject> m_windowScriptObject;
191 } // namespace WebCore