Skip to content

Commit 03c46a2

Browse files
committed
LibWeb: Implement the WindowProxy exotic object
A couple steps requiring working relationships between browsing contexts are currently FIXME'd - see #12917.
1 parent 6f94143 commit 03c46a2

File tree

4 files changed

+317
-0
lines changed

4 files changed

+317
-0
lines changed
Lines changed: 267 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,267 @@
1+
/*
2+
* Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
3+
*
4+
* SPDX-License-Identifier: BSD-2-Clause
5+
*/
6+
7+
#include <AK/Optional.h>
8+
#include <LibJS/Heap/MarkedVector.h>
9+
#include <LibJS/Runtime/Completion.h>
10+
#include <LibJS/Runtime/GlobalObject.h>
11+
#include <LibJS/Runtime/PropertyDescriptor.h>
12+
#include <LibJS/Runtime/PropertyKey.h>
13+
#include <LibWeb/Bindings/CrossOriginAbstractOperations.h>
14+
#include <LibWeb/Bindings/DOMExceptionWrapper.h>
15+
#include <LibWeb/Bindings/WindowObject.h>
16+
#include <LibWeb/Bindings/WindowProxy.h>
17+
#include <LibWeb/DOM/DOMException.h>
18+
#include <LibWeb/HTML/CrossOrigin/Reporting.h>
19+
#include <LibWeb/HTML/Scripting/Environments.h>
20+
#include <LibWeb/HTML/Window.h>
21+
22+
namespace Web::Bindings {
23+
24+
// 7.4 The WindowProxy exotic object, https://html.spec.whatwg.org/multipage/window-object.html#the-windowproxy-exotic-object
25+
WindowProxy::WindowProxy(JS::GlobalObject& global_object, WindowObject& window)
26+
: JS::Object(global_object, nullptr)
27+
, m_window(&window)
28+
{
29+
}
30+
31+
// 7.4.1 [[GetPrototypeOf]] ( ), https://html.spec.whatwg.org/multipage/window-object.html#windowproxy-getprototypeof
32+
JS::ThrowCompletionOr<JS::Object*> WindowProxy::internal_get_prototype_of() const
33+
{
34+
// 1. Let W be the value of the [[Window]] internal slot of this.
35+
36+
// 2. If IsPlatformObjectSameOrigin(W) is true, then return ! OrdinaryGetPrototypeOf(W).
37+
if (is_platform_object_same_origin(*m_window))
38+
return MUST(m_window->internal_get_prototype_of());
39+
40+
// 3. Return null.
41+
return nullptr;
42+
}
43+
44+
// 7.4.2 [[SetPrototypeOf]] ( V ), https://html.spec.whatwg.org/multipage/window-object.html#windowproxy-setprototypeof
45+
JS::ThrowCompletionOr<bool> WindowProxy::internal_set_prototype_of(Object* prototype)
46+
{
47+
// 1. Return ! SetImmutablePrototype(this, V).
48+
return MUST(set_immutable_prototype(prototype));
49+
}
50+
51+
// 7.4.3 [[IsExtensible]] ( ), https://html.spec.whatwg.org/multipage/window-object.html#windowproxy-isextensible
52+
JS::ThrowCompletionOr<bool> WindowProxy::internal_is_extensible() const
53+
{
54+
// 1. Return true.
55+
return true;
56+
}
57+
58+
// 7.4.4 [[PreventExtensions]] ( ), https://html.spec.whatwg.org/multipage/window-object.html#windowproxy-preventextensions
59+
JS::ThrowCompletionOr<bool> WindowProxy::internal_prevent_extensions()
60+
{
61+
// 1. Return false.
62+
return false;
63+
}
64+
65+
// 7.4.5 [[GetOwnProperty]] ( P ), https://html.spec.whatwg.org/multipage/window-object.html#windowproxy-getownproperty
66+
JS::ThrowCompletionOr<Optional<JS::PropertyDescriptor>> WindowProxy::internal_get_own_property(JS::PropertyKey const& property_key) const
67+
{
68+
auto& global_object = HTML::current_global_object();
69+
auto& vm = global_object.vm();
70+
71+
// 1. Let W be the value of the [[Window]] internal slot of this.
72+
73+
// 2. If P is an array index property name, then:
74+
if (property_key.is_number()) {
75+
// 1. Let index be ! ToUint32(P).
76+
auto index = property_key.as_number();
77+
78+
// FIXME: 2. Let maxProperties be the number of document-tree child browsing contexts of W.
79+
size_t max_properties = 0;
80+
81+
// 3. Let value be undefined.
82+
Optional<JS::Value> value;
83+
84+
// 4. If maxProperties is greater than 0 and index is less than maxProperties, then set value to the WindowProxy object of the indexth document-tree child browsing context of W's browsing context, sorted in the order that their browsing context container elements were most recently inserted into W's associated Document, the WindowProxy object of the most recently inserted browsing context container's nested browsing context being last.
85+
if (max_properties > 0 && index < max_properties) {
86+
// FIXME: Implement this.
87+
}
88+
89+
// 5. If value is undefined, then:
90+
if (!value.has_value()) {
91+
// 1. If IsPlatformObjectSameOrigin(W) is true, then return undefined.
92+
if (is_platform_object_same_origin(*m_window))
93+
return Optional<JS::PropertyDescriptor> {};
94+
95+
// 2. Throw a "SecurityError" DOMException.
96+
return vm.throw_completion<DOMExceptionWrapper>(global_object, DOM::SecurityError::create(String::formatted("Can't access property '{}' on cross-origin object", property_key)));
97+
}
98+
99+
// 6. Return PropertyDescriptor{ [[Value]]: value, [[Writable]]: false, [[Enumerable]]: true, [[Configurable]]: true }.
100+
return JS::PropertyDescriptor { .value = move(value), .writable = false, .enumerable = true, .configurable = true };
101+
}
102+
103+
// 3. If IsPlatformObjectSameOrigin(W) is true, then return ! OrdinaryGetOwnProperty(W, P).
104+
// NOTE: This is a willful violation of the JavaScript specification's invariants of the essential internal methods to maintain compatibility with existing web content. See tc39/ecma262 issue #672 for more information.
105+
if (is_platform_object_same_origin(*m_window))
106+
return m_window->internal_get_own_property(property_key);
107+
108+
// 4. Let property be CrossOriginGetOwnPropertyHelper(W, P).
109+
auto property = cross_origin_get_own_property_helper(m_window, property_key);
110+
111+
// 5. If property is not undefined, then return property.
112+
if (property.has_value())
113+
return property;
114+
115+
// FIXME: 6. If property is undefined and P is in W's document-tree child browsing context name property set, then:
116+
if (false) {
117+
// FIXME: 1. Let value be the WindowProxy object of the named object of W with the name P.
118+
auto value = JS::js_undefined();
119+
120+
// 2. Return PropertyDescriptor{ [[Value]]: value, [[Enumerable]]: false, [[Writable]]: false, [[Configurable]]: true }.
121+
// NOTE: The reason the property descriptors are non-enumerable, despite this mismatching the same-origin behavior, is for compatibility with existing web content. See issue #3183 for details.
122+
return JS::PropertyDescriptor { .value = value, .writable = false, .enumerable = false, .configurable = true };
123+
}
124+
125+
// 7. Return ? CrossOriginPropertyFallback(P).
126+
return TRY(cross_origin_property_fallback(global_object, property_key));
127+
}
128+
129+
// 7.4.6 [[DefineOwnProperty]] ( P, Desc ), https://html.spec.whatwg.org/multipage/window-object.html#windowproxy-defineownproperty
130+
JS::ThrowCompletionOr<bool> WindowProxy::internal_define_own_property(JS::PropertyKey const& property_key, JS::PropertyDescriptor const& descriptor)
131+
{
132+
auto& global_object = HTML::current_global_object();
133+
auto& vm = global_object.vm();
134+
135+
// 1. Let W be the value of the [[Window]] internal slot of this.
136+
137+
// 2. If IsPlatformObjectSameOrigin(W) is true, then:
138+
if (is_platform_object_same_origin(*m_window)) {
139+
// 1. If P is an array index property name, return false.
140+
if (property_key.is_number())
141+
return false;
142+
143+
// 2. Return ? OrdinaryDefineOwnProperty(W, P, Desc).
144+
// NOTE: This is a willful violation of the JavaScript specification's invariants of the essential internal methods to maintain compatibility with existing web content. See tc39/ecma262 issue #672 for more information.
145+
return m_window->internal_define_own_property(property_key, descriptor);
146+
}
147+
148+
// 3. Throw a "SecurityError" DOMException.
149+
return vm.throw_completion<DOMExceptionWrapper>(global_object, DOM::SecurityError::create(String::formatted("Can't define property '{}' on cross-origin object", property_key)));
150+
}
151+
152+
// 7.4.7 [[Get]] ( P, Receiver ), https://html.spec.whatwg.org/multipage/window-object.html#windowproxy-get
153+
JS::ThrowCompletionOr<JS::Value> WindowProxy::internal_get(JS::PropertyKey const& property_key, JS::Value receiver) const
154+
{
155+
auto& global_object = HTML::current_global_object();
156+
157+
// 1. Let W be the value of the [[Window]] internal slot of this.
158+
159+
// 2. Check if an access between two browsing contexts should be reported, given the current global object's browsing context, W's browsing context, P, and the current settings object.
160+
HTML::check_if_access_between_two_browsing_contexts_should_be_reported(*static_cast<WindowObject&>(HTML::current_global_object()).impl().browsing_context(), *m_window->impl().browsing_context(), property_key, HTML::current_settings_object());
161+
162+
// 3. If IsPlatformObjectSameOrigin(W) is true, then return ? OrdinaryGet(this, P, Receiver).
163+
// NOTE: this is passed rather than W as OrdinaryGet and CrossOriginGet will invoke the [[GetOwnProperty]] internal method.
164+
if (is_platform_object_same_origin(*m_window))
165+
return JS::Object::internal_get(property_key, receiver);
166+
167+
// 4. Return ? CrossOriginGet(this, P, Receiver).
168+
// NOTE: this is passed rather than W as OrdinaryGet and CrossOriginGet will invoke the [[GetOwnProperty]] internal method.
169+
return cross_origin_get(global_object, *this, property_key, receiver);
170+
}
171+
172+
// 7.4.8 [[Set]] ( P, V, Receiver ), https://html.spec.whatwg.org/multipage/window-object.html#windowproxy-set
173+
JS::ThrowCompletionOr<bool> WindowProxy::internal_set(JS::PropertyKey const& property_key, JS::Value value, JS::Value receiver)
174+
{
175+
auto& global_object = HTML::current_global_object();
176+
177+
// 1. Let W be the value of the [[Window]] internal slot of this.
178+
179+
// 2. Check if an access between two browsing contexts should be reported, given the current global object's browsing context, W's browsing context, P, and the current settings object.
180+
HTML::check_if_access_between_two_browsing_contexts_should_be_reported(*static_cast<WindowObject&>(HTML::current_global_object()).impl().browsing_context(), *m_window->impl().browsing_context(), property_key, HTML::current_settings_object());
181+
182+
// 3. If IsPlatformObjectSameOrigin(W) is true, then:
183+
if (is_platform_object_same_origin(*m_window)) {
184+
// 1. If P is an array index property name, then return false.
185+
if (property_key.is_number())
186+
return false;
187+
188+
// 2. Return ? OrdinarySet(W, P, V, Receiver).
189+
return m_window->internal_set(property_key, value, receiver);
190+
}
191+
192+
// 4. Return ? CrossOriginSet(this, P, V, Receiver).
193+
// NOTE: this is passed rather than W as CrossOriginSet will invoke the [[GetOwnProperty]] internal method.
194+
return cross_origin_set(global_object, *this, property_key, value, receiver);
195+
}
196+
197+
// 7.4.9 [[Delete]] ( P ), https://html.spec.whatwg.org/multipage/window-object.html#windowproxy-delete
198+
JS::ThrowCompletionOr<bool> WindowProxy::internal_delete(JS::PropertyKey const& property_key)
199+
{
200+
auto& global_object = HTML::current_global_object();
201+
auto& vm = global_object.vm();
202+
203+
// 1. Let W be the value of the [[Window]] internal slot of this.
204+
205+
// 2. If IsPlatformObjectSameOrigin(W) is true, then:
206+
if (is_platform_object_same_origin(*m_window)) {
207+
// 1. If P is an array index property name, then:
208+
if (property_key.is_number()) {
209+
// 2. Let desc be ! this.[[GetOwnProperty]](P).
210+
auto descriptor = MUST(internal_get_own_property(property_key));
211+
212+
// 2. If desc is undefined, then return true.
213+
if (!descriptor.has_value())
214+
return true;
215+
216+
// 3. Return false.
217+
return false;
218+
}
219+
220+
// 2. Return ? OrdinaryDelete(W, P).
221+
return m_window->internal_delete(property_key);
222+
}
223+
224+
// 3. Throw a "SecurityError" DOMException.
225+
return vm.throw_completion<DOMExceptionWrapper>(global_object, DOM::SecurityError::create(String::formatted("Can't delete property '{}' on cross-origin object", property_key)));
226+
}
227+
228+
// 7.4.10 [[OwnPropertyKeys]] ( ), https://html.spec.whatwg.org/multipage/window-object.html#windowproxy-ownpropertykeys
229+
JS::ThrowCompletionOr<JS::MarkedVector<JS::Value>> WindowProxy::internal_own_property_keys() const
230+
{
231+
auto& event_loop = HTML::main_thread_event_loop();
232+
auto& vm = event_loop.vm();
233+
234+
// 1. Let W be the value of the [[Window]] internal slot of this.
235+
236+
// 2. Let keys be a new empty List.
237+
auto keys = JS::MarkedVector<JS::Value> { vm.heap() };
238+
239+
// FIXME: 3. Let maxProperties be the number of document-tree child browsing contexts of W.
240+
size_t max_properties = 0;
241+
242+
// 4. Let index be 0.
243+
// 5. Repeat while index < maxProperties,
244+
for (size_t i = 0; i < max_properties; ++i) {
245+
// 1. Add ! ToString(index) as the last element of keys.
246+
keys.append(JS::js_string(vm, String::number(i)));
247+
248+
// 2. Increment index by 1.
249+
}
250+
251+
// 6. If IsPlatformObjectSameOrigin(W) is true, then return the concatenation of keys and OrdinaryOwnPropertyKeys(W).
252+
if (is_platform_object_same_origin(*m_window)) {
253+
keys.extend(MUST(m_window->internal_own_property_keys()));
254+
return keys;
255+
}
256+
257+
// 7. Return the concatenation of keys and ! CrossOriginOwnPropertyKeys(W).
258+
keys.extend(cross_origin_own_property_keys(m_window));
259+
return keys;
260+
}
261+
262+
void WindowProxy::visit_edges(JS::Cell::Visitor& visitor)
263+
{
264+
visitor.visit(m_window);
265+
}
266+
267+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
3+
*
4+
* SPDX-License-Identifier: BSD-2-Clause
5+
*/
6+
7+
#pragma once
8+
9+
#include <AK/Forward.h>
10+
#include <LibJS/Forward.h>
11+
#include <LibJS/Runtime/Object.h>
12+
#include <LibWeb/Forward.h>
13+
14+
namespace Web::Bindings {
15+
16+
class WindowProxy final : public JS::Object {
17+
JS_OBJECT(WindowProxy, JS::Object);
18+
19+
public:
20+
WindowProxy(JS::GlobalObject&, WindowObject&);
21+
virtual ~WindowProxy() override = default;
22+
23+
virtual JS::ThrowCompletionOr<JS::Object*> internal_get_prototype_of() const override;
24+
virtual JS::ThrowCompletionOr<bool> internal_set_prototype_of(Object* prototype) override;
25+
virtual JS::ThrowCompletionOr<bool> internal_is_extensible() const override;
26+
virtual JS::ThrowCompletionOr<bool> internal_prevent_extensions() override;
27+
virtual JS::ThrowCompletionOr<Optional<JS::PropertyDescriptor>> internal_get_own_property(JS::PropertyKey const&) const override;
28+
virtual JS::ThrowCompletionOr<bool> internal_define_own_property(JS::PropertyKey const&, JS::PropertyDescriptor const&) override;
29+
virtual JS::ThrowCompletionOr<JS::Value> internal_get(JS::PropertyKey const&, JS::Value receiver) const override;
30+
virtual JS::ThrowCompletionOr<bool> internal_set(JS::PropertyKey const&, JS::Value value, JS::Value receiver) override;
31+
virtual JS::ThrowCompletionOr<bool> internal_delete(JS::PropertyKey const&) override;
32+
virtual JS::ThrowCompletionOr<JS::MarkedVector<JS::Value>> internal_own_property_keys() const override;
33+
34+
WindowObject& window() { return *m_window; }
35+
WindowObject const& window() const { return *m_window; }
36+
37+
// NOTE: Someone will have to replace the wrapped window object as well:
38+
// "When the browsing context is navigated, the Window object wrapped by the browsing context's associated WindowProxy object is changed."
39+
// I haven't found where that actually happens yet. Make sure to use a Badge<T> guarded setter.
40+
41+
private:
42+
virtual void visit_edges(JS::Cell::Visitor&) override;
43+
44+
// [[Window]], https://html.spec.whatwg.org/multipage/window-object.html#concept-windowproxy-window
45+
WindowObject* m_window { nullptr };
46+
};
47+
48+
}

Userland/Libraries/LibWeb/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ set(SOURCES
1414
Bindings/NavigatorObject.cpp
1515
Bindings/NodeWrapperFactory.cpp
1616
Bindings/WindowObject.cpp
17+
Bindings/WindowProxy.cpp
1718
Bindings/Wrappable.cpp
1819
Crypto/Crypto.cpp
1920
Crypto/SubtleCrypto.cpp

Userland/Libraries/LibWeb/Forward.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,7 @@ class URLSearchParamsWrapper;
515515
class URLWrapper;
516516
class WebSocketWrapper;
517517
class WindowObject;
518+
class WindowProxy;
518519
class WorkerWrapper;
519520
class WorkerGlobalScopeWrapper;
520521
class WorkerLocationWrapper;

0 commit comments

Comments
 (0)