2 * Copyright (C) 1999 Harri Porten (porten@kde.org)
3 * Copyright (C) 2001 Peter Kelly (pmk@post.com)
4 * Copyright (C) 2008-2019 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 "SerializedScriptValue.h"
27 #include "WindowProxy.h"
28 #include <JavaScriptCore/JSBase.h>
29 #include <JavaScriptCore/Strong.h>
30 #include <wtf/Forward.h>
31 #include <wtf/Optional.h>
32 #include <wtf/RefPtr.h>
33 #include <wtf/WeakPtr.h>
34 #include <wtf/text/TextPosition.h>
37 #include <wtf/RetainPtr.h>
39 OBJC_CLASS WebScriptObject;
47 class JSInternalPromise;
58 class CachedScriptFetcher;
61 class HTMLPlugInElement;
62 class LoadableModuleScript;
63 class ModuleFetchParameters;
64 class ScriptSourceCode;
68 enum class RunAsAsyncFunction : bool;
70 struct ExceptionDetails;
71 struct RunJavaScriptParameters;
73 enum ReasonForCallingCanExecuteScripts {
74 AboutToCreateEventListener,
76 NotAboutToExecuteScript
79 using ValueOrException = Expected<JSC::JSValue, ExceptionDetails>;
81 class ScriptController : public CanMakeWeakPtr<ScriptController> {
82 WTF_MAKE_FAST_ALLOCATED;
84 using RootObjectMap = HashMap<void*, Ref<JSC::Bindings::RootObject>>;
87 explicit ScriptController(Frame&);
90 enum class WorldType { User, Internal };
91 WEBCORE_EXPORT static Ref<DOMWrapperWorld> createWorld(const String& name, WorldType = WorldType::Internal);
93 JSDOMWindow* globalObject(DOMWrapperWorld& world)
95 return JSC::jsCast<JSDOMWindow*>(jsWindowProxy(world).window());
98 static void getAllWorlds(Vector<Ref<DOMWrapperWorld>>&);
100 using ResolveFunction = CompletionHandler<void(ValueOrException)>;
102 WEBCORE_EXPORT JSC::JSValue executeScriptIgnoringException(const String& script, bool forceUserGesture = false);
103 WEBCORE_EXPORT JSC::JSValue executeScriptInWorldIgnoringException(DOMWrapperWorld&, const String& script, bool forceUserGesture = false);
104 WEBCORE_EXPORT JSC::JSValue executeUserAgentScriptInWorldIgnoringException(DOMWrapperWorld&, const String& script, bool forceUserGesture);
105 WEBCORE_EXPORT ValueOrException executeUserAgentScriptInWorld(DOMWrapperWorld&, const String& script, bool forceUserGesture);
106 WEBCORE_EXPORT void executeAsynchronousUserAgentScriptInWorld(DOMWrapperWorld&, RunJavaScriptParameters&&, ResolveFunction&&);
107 JSC::JSValue evaluateIgnoringException(const ScriptSourceCode&);
108 JSC::JSValue evaluateInWorldIgnoringException(const ScriptSourceCode&, DOMWrapperWorld&);
110 Expected<void, ExceptionDetails> shouldAllowUserAgentScripts(Document&) const;
112 // This asserts that URL argument is a JavaScript URL.
113 void executeJavaScriptURL(const URL&, RefPtr<SecurityOrigin> = nullptr, ShouldReplaceDocumentIfJavaScriptURL = ReplaceDocumentIfJavaScriptURL);
115 static void initializeMainThread();
117 void loadModuleScriptInWorld(LoadableModuleScript&, const String& moduleName, Ref<ModuleFetchParameters>&&, DOMWrapperWorld&);
118 void loadModuleScript(LoadableModuleScript&, const String& moduleName, Ref<ModuleFetchParameters>&&);
119 void loadModuleScriptInWorld(LoadableModuleScript&, const ScriptSourceCode&, DOMWrapperWorld&);
120 void loadModuleScript(LoadableModuleScript&, const ScriptSourceCode&);
122 JSC::JSValue linkAndEvaluateModuleScriptInWorld(LoadableModuleScript& , DOMWrapperWorld&);
123 JSC::JSValue linkAndEvaluateModuleScript(LoadableModuleScript&);
125 JSC::JSValue evaluateModule(const URL&, JSC::JSModuleRecord&, DOMWrapperWorld&);
126 JSC::JSValue evaluateModule(const URL&, JSC::JSModuleRecord&);
128 WTF::TextPosition eventHandlerPosition() const;
131 void enableWebAssembly();
132 void disableEval(const String& errorMessage);
133 void disableWebAssembly(const String& errorMessage);
135 static bool canAccessFromCurrentOrigin(Frame*, Document& accessingDocument);
136 WEBCORE_EXPORT bool canExecuteScripts(ReasonForCallingCanExecuteScripts);
138 void setPaused(bool b) { m_paused = b; }
139 bool isPaused() const { return m_paused; }
141 const URL* sourceURL() const { return m_sourceURL; } // nullptr if we are not evaluating any script
143 void updateDocument();
145 void namedItemAdded(HTMLDocument*, const AtomString&) { }
146 void namedItemRemoved(HTMLDocument*, const AtomString&) { }
148 void clearScriptObjects();
149 WEBCORE_EXPORT void cleanupScriptObjectsForPlugin(void*);
151 void updatePlatformScriptObjects();
153 RefPtr<JSC::Bindings::Instance> createScriptInstanceForWidget(Widget*);
154 WEBCORE_EXPORT JSC::Bindings::RootObject* bindingRootObject();
155 JSC::Bindings::RootObject* cacheableBindingRootObject();
156 JSC::Bindings::RootObject* existingCacheableBindingRootObject() const { return m_cacheableBindingRootObject.get(); }
158 WEBCORE_EXPORT Ref<JSC::Bindings::RootObject> createRootObject(void* nativeHandle);
160 void collectIsolatedContexts(Vector<std::pair<JSC::JSGlobalObject*, SecurityOrigin*>>&);
163 WEBCORE_EXPORT WebScriptObject* windowScriptObject();
164 WEBCORE_EXPORT JSContext *javaScriptContext();
167 WEBCORE_EXPORT JSC::JSObject* jsObjectForPluginElement(HTMLPlugInElement*);
169 #if ENABLE(NETSCAPE_PLUGIN_API)
170 WEBCORE_EXPORT NPObject* windowScriptNPObject();
173 void initScriptForWindowProxy(JSWindowProxy&);
175 bool willReplaceWithResultOfExecutingJavascriptURL() const { return m_willReplaceWithResultOfExecutingJavascriptURL; }
178 ValueOrException executeUserAgentScriptInWorldInternal(DOMWrapperWorld&, RunJavaScriptParameters&&);
179 ValueOrException executeScriptInWorld(DOMWrapperWorld&, RunJavaScriptParameters&&);
180 ValueOrException evaluateInWorld(const ScriptSourceCode&, DOMWrapperWorld&);
181 ValueOrException callInWorld(RunJavaScriptParameters&&, DOMWrapperWorld&);
183 void setupModuleScriptHandlers(LoadableModuleScript&, JSC::JSInternalPromise&, DOMWrapperWorld&);
185 void disconnectPlatformScriptObjects();
187 WEBCORE_EXPORT WindowProxy& windowProxy();
188 WEBCORE_EXPORT JSWindowProxy& jsWindowProxy(DOMWrapperWorld&);
191 const URL* m_sourceURL { nullptr };
194 bool m_willReplaceWithResultOfExecutingJavascriptURL { false };
196 // The root object used for objects bound outside the context of a plugin, such
197 // as NPAPI plugins. The plugins using these objects prevent a page from being cached so they
198 // are safe to invalidate() when WebKit navigates away from the page that contains them.
199 RefPtr<JSC::Bindings::RootObject> m_bindingRootObject;
200 // Unlike m_bindingRootObject these objects are used in pages that are cached, so they are not invalidate()'d.
201 // This ensures they are still available when the page is restored.
202 RefPtr<JSC::Bindings::RootObject> m_cacheableBindingRootObject;
203 RootObjectMap m_rootObjects;
204 #if ENABLE(NETSCAPE_PLUGIN_API)
205 NPObject* m_windowScriptNPObject;
208 RetainPtr<WebScriptObject> m_windowScriptObject;
212 } // namespace WebCore