1/*
2 * Copyright (C) 2000 Harri Porten (porten@kde.org)
3 * Copyright (C) 2003-2017 Apple Inc. All rights reseved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20#pragma once
21
22#include "DOMWindow.h"
23#include "JSDOMGlobalObject.h"
24#include "JSDOMWrapperCache.h"
25#include <JavaScriptCore/AuxiliaryBarrierInlines.h>
26#include <JavaScriptCore/HeapInlines.h>
27#include <JavaScriptCore/JSArray.h>
28#include <JavaScriptCore/JSCJSValueInlines.h>
29#include <JavaScriptCore/JSCellInlines.h>
30#include <JavaScriptCore/JSObjectInlines.h>
31#include <JavaScriptCore/Lookup.h>
32#include <JavaScriptCore/ObjectConstructor.h>
33#include <JavaScriptCore/SlotVisitorInlines.h>
34#include <JavaScriptCore/StructureInlines.h>
35#include <JavaScriptCore/WriteBarrier.h>
36#include <cstddef>
37#include <wtf/Forward.h>
38#include <wtf/GetPtr.h>
39#include <wtf/Vector.h>
40
41namespace WebCore {
42
43class DOMWrapperWorld;
44class Frame;
45class FetchResponse;
46class JSDOMWindow;
47class JSDOMWindowBasePrivate;
48class JSWindowProxy;
49
50class WEBCORE_EXPORT JSDOMWindowBase : public JSDOMGlobalObject {
51 typedef JSDOMGlobalObject Base;
52protected:
53 JSDOMWindowBase(JSC::VM&, JSC::Structure*, RefPtr<DOMWindow>&&, JSWindowProxy*);
54 void finishCreation(JSC::VM&, JSWindowProxy*);
55
56 static void destroy(JSCell*);
57
58public:
59 void updateDocument();
60
61 DOMWindow& wrapped() const { return *m_wrapped; }
62 ScriptExecutionContext* scriptExecutionContext() const;
63
64 // Called just before removing this window from the JSWindowProxy.
65 void willRemoveFromWindowProxy();
66
67 DECLARE_INFO;
68
69 static JSC::Structure* createStructure(JSC::VM& vm, JSC::JSValue prototype)
70 {
71 return JSC::Structure::create(vm, 0, prototype, JSC::TypeInfo(JSC::GlobalObjectType, StructureFlags), info());
72 }
73
74 static const JSC::GlobalObjectMethodTable s_globalObjectMethodTable;
75
76 static bool supportsRichSourceInfo(const JSC::JSGlobalObject*);
77 static bool shouldInterruptScript(const JSC::JSGlobalObject*);
78 static bool shouldInterruptScriptBeforeTimeout(const JSC::JSGlobalObject*);
79 static JSC::RuntimeFlags javaScriptRuntimeFlags(const JSC::JSGlobalObject*);
80 static void queueTaskToEventLoop(JSC::JSGlobalObject&, Ref<JSC::Microtask>&&);
81
82 void printErrorMessage(const String&) const;
83
84 JSWindowProxy* proxy() const;
85
86 static void fireFrameClearedWatchpointsForWindow(DOMWindow*);
87
88protected:
89 JSC::WatchpointSet m_windowCloseWatchpoints;
90
91private:
92 using ResponseCallback = WTF::Function<void(const char*, size_t)>;
93
94 static JSC::Identifier moduleLoaderResolve(JSC::JSGlobalObject*, JSC::ExecState*, JSC::JSModuleLoader*, JSC::JSValue, JSC::JSValue, JSC::JSValue);
95 static JSC::JSInternalPromise* moduleLoaderFetch(JSC::JSGlobalObject*, JSC::ExecState*, JSC::JSModuleLoader*, JSC::JSValue, JSC::JSValue, JSC::JSValue);
96 static JSC::JSValue moduleLoaderEvaluate(JSC::JSGlobalObject*, JSC::ExecState*, JSC::JSModuleLoader*, JSC::JSValue, JSC::JSValue, JSC::JSValue);
97 static JSC::JSInternalPromise* moduleLoaderImportModule(JSC::JSGlobalObject*, JSC::ExecState*, JSC::JSModuleLoader*, JSC::JSString*, JSC::JSValue, const JSC::SourceOrigin&);
98 static JSC::JSObject* moduleLoaderCreateImportMetaProperties(JSC::JSGlobalObject*, JSC::ExecState*, JSC::JSModuleLoader*, JSC::JSValue, JSC::JSModuleRecord*, JSC::JSValue);
99
100#if ENABLE(WEBASSEMBLY)
101 static void compileStreaming(JSC::JSGlobalObject*, JSC::ExecState*, JSC::JSPromiseDeferred*, JSC::JSValue);
102 static void instantiateStreaming(JSC::JSGlobalObject*, JSC::ExecState*, JSC::JSPromiseDeferred*, JSC::JSValue, JSC::JSObject*);
103#endif
104
105 RefPtr<DOMWindow> m_wrapped;
106 JSWindowProxy* m_proxy;
107};
108
109WEBCORE_EXPORT JSC::JSValue toJS(JSC::ExecState*, DOMWindow&);
110// The following return a JSWindowProxy or jsNull()
111// JSDOMGlobalObject* is ignored, accessing a window in any context will use that DOMWindow's prototype chain.
112inline JSC::JSValue toJS(JSC::ExecState* state, JSDOMGlobalObject*, DOMWindow& window) { return toJS(state, window); }
113inline JSC::JSValue toJS(JSC::ExecState* state, JSDOMGlobalObject* globalObject, DOMWindow* window) { return window ? toJS(state, globalObject, *window) : JSC::jsNull(); }
114inline JSC::JSValue toJS(JSC::ExecState* state, DOMWindow* window) { return window ? toJS(state, *window) : JSC::jsNull(); }
115
116// The following return a JSDOMWindow or nullptr.
117JSDOMWindow* toJSDOMWindow(Frame&, DOMWrapperWorld&);
118inline JSDOMWindow* toJSDOMWindow(Frame* frame, DOMWrapperWorld& world) { return frame ? toJSDOMWindow(*frame, world) : nullptr; }
119WEBCORE_EXPORT JSDOMWindow* toJSDOMWindow(JSC::VM&, JSC::JSValue);
120
121// DOMWindow associated with global object of the "most-recently-entered author function or script
122// on the stack, or the author function or script that originally scheduled the currently-running callback."
123// (<https://html.spec.whatwg.org/multipage/webappapis.html#concept-incumbent-everything>, 27 April 2017)
124// FIXME: Make this work for an "author function or script that originally scheduled the currently-running callback."
125// See <https://bugs.webkit.org/show_bug.cgi?id=163412>.
126DOMWindow& incumbentDOMWindow(JSC::ExecState&);
127
128DOMWindow& activeDOMWindow(JSC::ExecState&);
129DOMWindow& firstDOMWindow(JSC::ExecState&);
130
131// FIXME: This should probably be removed in favor of one of the other DOMWindow accessors. It is intended
132// to provide the document specfied as the 'responsible document' in the algorithm for document.open()
133// (https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#document-open-steps steps 4
134// and 23 and https://html.spec.whatwg.org/multipage/webappapis.html#responsible-document). It is only
135// used by JSDocument.
136Document* responsibleDocument(JSC::ExecState&);
137
138} // namespace WebCore
139