diff --git a/Userland/Libraries/LibWeb/Bindings/CrossOriginAbstractOperations.h b/Userland/Libraries/LibWeb/Bindings/CrossOriginAbstractOperations.h deleted file mode 100644 index a385b74d6c..0000000000 --- a/Userland/Libraries/LibWeb/Bindings/CrossOriginAbstractOperations.h +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Copyright (c) 2022, Linus Groh - * - * SPDX-License-Identifier: BSD-2-Clause - */ - -#pragma once - -#include -#include -#include -#include -#include - -namespace Web::Bindings { - -struct CrossOriginProperty { - String property; - Optional needs_get {}; - Optional needs_set {}; -}; - -struct CrossOriginKey { - FlatPtr current_settings_object; - FlatPtr relevant_settings_object; - JS::PropertyKey property_key; -}; - -using CrossOriginPropertyDescriptorMap = HashMap; - -Vector cross_origin_properties(Variant const&); -bool is_cross_origin_accessible_window_property_name(JS::PropertyKey const&); -JS::ThrowCompletionOr cross_origin_property_fallback(JS::VM&, JS::PropertyKey const&); -bool is_platform_object_same_origin(JS::Object const&); -Optional cross_origin_get_own_property_helper(Variant const&, JS::PropertyKey const&); -JS::ThrowCompletionOr cross_origin_get(JS::VM&, JS::Object const&, JS::PropertyKey const&, JS::Value receiver); -JS::ThrowCompletionOr cross_origin_set(JS::VM&, JS::Object&, JS::PropertyKey const&, JS::Value, JS::Value receiver); -JS::MarkedVector cross_origin_own_property_keys(Variant const&); - -} - -namespace AK { - -template<> -struct Traits : public GenericTraits { - static unsigned hash(Web::Bindings::CrossOriginKey const& key) - { - return pair_int_hash( - Traits::hash(key.property_key), - pair_int_hash(ptr_hash(key.current_settings_object), ptr_hash(key.relevant_settings_object))); - } - - static bool equals(Web::Bindings::CrossOriginKey const& a, Web::Bindings::CrossOriginKey const& b) - { - return a.current_settings_object == b.current_settings_object - && a.relevant_settings_object == b.relevant_settings_object - && Traits::equals(a.property_key, b.property_key); - } -}; - -} diff --git a/Userland/Libraries/LibWeb/Bindings/LocationObject.cpp b/Userland/Libraries/LibWeb/Bindings/LocationObject.cpp index 15851ee259..c163f375c3 100644 --- a/Userland/Libraries/LibWeb/Bindings/LocationObject.cpp +++ b/Userland/Libraries/LibWeb/Bindings/LocationObject.cpp @@ -11,11 +11,11 @@ #include #include #include -#include #include #include #include #include +#include #include namespace Web::Bindings { @@ -244,7 +244,7 @@ JS_DEFINE_NATIVE_FUNCTION(LocationObject::replace) JS::ThrowCompletionOr LocationObject::internal_get_prototype_of() const { // 1. If IsPlatformObjectSameOrigin(this) is true, then return ! OrdinaryGetPrototypeOf(this). - if (is_platform_object_same_origin(*this)) + if (HTML::is_platform_object_same_origin(*this)) return MUST(JS::Object::internal_get_prototype_of()); // 2. Return null. @@ -278,7 +278,7 @@ JS::ThrowCompletionOr> LocationObject::internal auto& vm = this->vm(); // 1. If IsPlatformObjectSameOrigin(this) is true, then: - if (is_platform_object_same_origin(*this)) { + if (HTML::is_platform_object_same_origin(*this)) { // 1. Let desc be OrdinaryGetOwnProperty(this, P). auto descriptor = MUST(Object::internal_get_own_property(property_key)); @@ -294,21 +294,21 @@ JS::ThrowCompletionOr> LocationObject::internal } // 2. Let property be CrossOriginGetOwnPropertyHelper(this, P). - auto property = cross_origin_get_own_property_helper(const_cast(this), property_key); + auto property = HTML::cross_origin_get_own_property_helper(const_cast(this), property_key); // 3. If property is not undefined, then return property. if (property.has_value()) return property; // 4. Return ? CrossOriginPropertyFallback(P). - return TRY(cross_origin_property_fallback(vm, property_key)); + return TRY(HTML::cross_origin_property_fallback(vm, property_key)); } // 7.10.5.6 [[DefineOwnProperty]] ( P, Desc ), https://html.spec.whatwg.org/multipage/history.html#location-defineownproperty JS::ThrowCompletionOr LocationObject::internal_define_own_property(JS::PropertyKey const& property_key, JS::PropertyDescriptor const& descriptor) { // 1. If IsPlatformObjectSameOrigin(this) is true, then: - if (is_platform_object_same_origin(*this)) { + if (HTML::is_platform_object_same_origin(*this)) { // 1. If the value of the [[DefaultProperties]] internal slot of this contains P, then return false. // 2. Return ? OrdinaryDefineOwnProperty(this, P, Desc). return JS::Object::internal_define_own_property(property_key, descriptor); @@ -324,11 +324,11 @@ JS::ThrowCompletionOr LocationObject::internal_get(JS::PropertyKey co auto& vm = this->vm(); // 1. If IsPlatformObjectSameOrigin(this) is true, then return ? OrdinaryGet(this, P, Receiver). - if (is_platform_object_same_origin(*this)) + if (HTML::is_platform_object_same_origin(*this)) return JS::Object::internal_get(property_key, receiver); // 2. Return ? CrossOriginGet(this, P, Receiver). - return cross_origin_get(vm, static_cast(*this), property_key, receiver); + return HTML::cross_origin_get(vm, static_cast(*this), property_key, receiver); } // 7.10.5.8 [[Set]] ( P, V, Receiver ), https://html.spec.whatwg.org/multipage/history.html#location-set @@ -337,18 +337,18 @@ JS::ThrowCompletionOr LocationObject::internal_set(JS::PropertyKey const& auto& vm = this->vm(); // 1. If IsPlatformObjectSameOrigin(this) is true, then return ? OrdinarySet(this, P, V, Receiver). - if (is_platform_object_same_origin(*this)) + if (HTML::is_platform_object_same_origin(*this)) return JS::Object::internal_set(property_key, value, receiver); // 2. Return ? CrossOriginSet(this, P, V, Receiver). - return cross_origin_set(vm, static_cast(*this), property_key, value, receiver); + return HTML::cross_origin_set(vm, static_cast(*this), property_key, value, receiver); } // 7.10.5.9 [[Delete]] ( P ), https://html.spec.whatwg.org/multipage/history.html#location-delete JS::ThrowCompletionOr LocationObject::internal_delete(JS::PropertyKey const& property_key) { // 1. If IsPlatformObjectSameOrigin(this) is true, then return ? OrdinaryDelete(this, P). - if (is_platform_object_same_origin(*this)) + if (HTML::is_platform_object_same_origin(*this)) return JS::Object::internal_delete(property_key); // 2. Throw a "SecurityError" DOMException. @@ -359,11 +359,11 @@ JS::ThrowCompletionOr LocationObject::internal_delete(JS::PropertyKey cons JS::ThrowCompletionOr> LocationObject::internal_own_property_keys() const { // 1. If IsPlatformObjectSameOrigin(this) is true, then return OrdinaryOwnPropertyKeys(this). - if (is_platform_object_same_origin(*this)) + if (HTML::is_platform_object_same_origin(*this)) return JS::Object::internal_own_property_keys(); // 2. Return CrossOriginOwnPropertyKeys(this). - return cross_origin_own_property_keys(this); + return HTML::cross_origin_own_property_keys(this); } } diff --git a/Userland/Libraries/LibWeb/Bindings/LocationObject.h b/Userland/Libraries/LibWeb/Bindings/LocationObject.h index 6bb3716f25..7bbd11f28c 100644 --- a/Userland/Libraries/LibWeb/Bindings/LocationObject.h +++ b/Userland/Libraries/LibWeb/Bindings/LocationObject.h @@ -10,9 +10,9 @@ #include #include #include -#include #include #include +#include namespace Web { namespace Bindings { @@ -34,8 +34,8 @@ public: virtual JS::ThrowCompletionOr internal_delete(JS::PropertyKey const&) override; virtual JS::ThrowCompletionOr> internal_own_property_keys() const override; - CrossOriginPropertyDescriptorMap const& cross_origin_property_descriptor_map() const { return m_cross_origin_property_descriptor_map; } - CrossOriginPropertyDescriptorMap& cross_origin_property_descriptor_map() { return m_cross_origin_property_descriptor_map; } + HTML::CrossOriginPropertyDescriptorMap const& cross_origin_property_descriptor_map() const { return m_cross_origin_property_descriptor_map; } + HTML::CrossOriginPropertyDescriptorMap& cross_origin_property_descriptor_map() { return m_cross_origin_property_descriptor_map; } private: explicit LocationObject(JS::Realm&); @@ -61,7 +61,7 @@ private: JS_DECLARE_NATIVE_FUNCTION(port_getter); // [[CrossOriginPropertyDescriptorMap]], https://html.spec.whatwg.org/multipage/browsers.html#crossoriginpropertydescriptormap - CrossOriginPropertyDescriptorMap m_cross_origin_property_descriptor_map; + HTML::CrossOriginPropertyDescriptorMap m_cross_origin_property_descriptor_map; // [[DefaultProperties]], https://html.spec.whatwg.org/multipage/history.html#defaultproperties Vector m_default_properties; diff --git a/Userland/Libraries/LibWeb/Bindings/WindowProxy.cpp b/Userland/Libraries/LibWeb/Bindings/WindowProxy.cpp index 98e42cf524..84fe0dddef 100644 --- a/Userland/Libraries/LibWeb/Bindings/WindowProxy.cpp +++ b/Userland/Libraries/LibWeb/Bindings/WindowProxy.cpp @@ -10,9 +10,9 @@ #include #include #include -#include #include #include +#include #include #include #include @@ -120,7 +120,7 @@ JS::ThrowCompletionOr> WindowProxy::internal_ge } // 7. Return ? CrossOriginPropertyFallback(P). - return TRY(cross_origin_property_fallback(vm, property_key)); + return TRY(HTML::cross_origin_property_fallback(vm, property_key)); } // 7.4.6 [[DefineOwnProperty]] ( P, Desc ), https://html.spec.whatwg.org/multipage/window-object.html#windowproxy-defineownproperty @@ -160,7 +160,7 @@ JS::ThrowCompletionOr WindowProxy::internal_get(JS::PropertyKey const // 4. Return ? CrossOriginGet(this, P, Receiver). // NOTE: this is passed rather than W as OrdinaryGet and CrossOriginGet will invoke the [[GetOwnProperty]] internal method. - return cross_origin_get(vm, *this, property_key, receiver); + return HTML::cross_origin_get(vm, *this, property_key, receiver); } // 7.4.8 [[Set]] ( P, V, Receiver ), https://html.spec.whatwg.org/multipage/window-object.html#windowproxy-set @@ -185,7 +185,7 @@ JS::ThrowCompletionOr WindowProxy::internal_set(JS::PropertyKey const& pro // 4. Return ? CrossOriginSet(this, P, V, Receiver). // NOTE: this is passed rather than W as CrossOriginSet will invoke the [[GetOwnProperty]] internal method. - return cross_origin_set(vm, *this, property_key, value, receiver); + return HTML::cross_origin_set(vm, *this, property_key, value, receiver); } // 7.4.9 [[Delete]] ( P ), https://html.spec.whatwg.org/multipage/window-object.html#windowproxy-delete diff --git a/Userland/Libraries/LibWeb/CMakeLists.txt b/Userland/Libraries/LibWeb/CMakeLists.txt index 71c94e0fdc..4b8b280e74 100644 --- a/Userland/Libraries/LibWeb/CMakeLists.txt +++ b/Userland/Libraries/LibWeb/CMakeLists.txt @@ -4,7 +4,6 @@ set(SOURCES Bindings/AudioConstructor.cpp Bindings/CSSNamespace.cpp Bindings/CallbackType.cpp - Bindings/CrossOriginAbstractOperations.cpp Bindings/IDLAbstractOperations.cpp Bindings/IDLOverloadResolution.cpp Bindings/ImageConstructor.cpp @@ -152,6 +151,7 @@ set(SOURCES HTML/CanvasGradient.cpp HTML/CanvasRenderingContext2D.cpp HTML/CloseEvent.cpp + HTML/CrossOrigin/AbstractOperations.cpp HTML/CrossOrigin/Reporting.cpp HTML/DOMParser.cpp HTML/DOMStringMap.cpp diff --git a/Userland/Libraries/LibWeb/Bindings/CrossOriginAbstractOperations.cpp b/Userland/Libraries/LibWeb/HTML/CrossOrigin/AbstractOperations.cpp similarity index 95% rename from Userland/Libraries/LibWeb/Bindings/CrossOriginAbstractOperations.cpp rename to Userland/Libraries/LibWeb/HTML/CrossOrigin/AbstractOperations.cpp index ab1b1512d8..6909effd5e 100644 --- a/Userland/Libraries/LibWeb/Bindings/CrossOriginAbstractOperations.cpp +++ b/Userland/Libraries/LibWeb/HTML/CrossOrigin/AbstractOperations.cpp @@ -13,23 +13,23 @@ #include #include #include -#include #include #include #include +#include #include #include -namespace Web::Bindings { +namespace Web::HTML { // 7.2.3.1 CrossOriginProperties ( O ), https://html.spec.whatwg.org/multipage/browsers.html#crossoriginproperties-(-o-) -Vector cross_origin_properties(Variant const& object) +Vector cross_origin_properties(Variant const& object) { // 1. Assert: O is a Location or Window object. return object.visit( // 2. If O is a Location object, then return « { [[Property]]: "href", [[NeedsGet]]: false, [[NeedsSet]]: true }, { [[Property]]: "replace" } ». - [](LocationObject const*) -> Vector { + [](Bindings::LocationObject const*) -> Vector { return { { .property = "href"sv, .needs_get = false, .needs_set = true }, { .property = "replace"sv }, @@ -89,11 +89,11 @@ bool is_platform_object_same_origin(JS::Object const& object) } // 7.2.3.4 CrossOriginGetOwnPropertyHelper ( O, P ), https://html.spec.whatwg.org/multipage/browsers.html#crossorigingetownpropertyhelper-(-o,-p-) -Optional cross_origin_get_own_property_helper(Variant const& object, JS::PropertyKey const& property_key) +Optional cross_origin_get_own_property_helper(Variant const& object, JS::PropertyKey const& property_key) { - auto& realm = *main_thread_vm().current_realm(); + auto& realm = *Bindings::main_thread_vm().current_realm(); auto const* object_ptr = object.visit([](auto* o) { return static_cast(o); }); - auto const object_const_variant = object.visit([](auto* o) { return Variant { o }; }); + auto const object_const_variant = object.visit([](auto* o) { return Variant { o }; }); // 1. Let crossOriginKey be a tuple consisting of the current settings object, O's relevant settings object, and P. auto cross_origin_key = CrossOriginKey { @@ -226,7 +226,7 @@ JS::ThrowCompletionOr cross_origin_set(JS::VM& vm, JS::Object& object, JS: } // 7.2.3.7 CrossOriginOwnPropertyKeys ( O ), https://html.spec.whatwg.org/multipage/browsers.html#crossoriginownpropertykeys-(-o-) -JS::MarkedVector cross_origin_own_property_keys(Variant const& object) +JS::MarkedVector cross_origin_own_property_keys(Variant const& object) { auto& event_loop = HTML::main_thread_event_loop(); auto& vm = event_loop.vm(); diff --git a/Userland/Libraries/LibWeb/HTML/CrossOrigin/AbstractOperations.h b/Userland/Libraries/LibWeb/HTML/CrossOrigin/AbstractOperations.h new file mode 100644 index 0000000000..3e68e34761 --- /dev/null +++ b/Userland/Libraries/LibWeb/HTML/CrossOrigin/AbstractOperations.h @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2022, Linus Groh + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include +#include +#include +#include + +namespace Web::HTML { + +Vector cross_origin_properties(Variant const&); +bool is_cross_origin_accessible_window_property_name(JS::PropertyKey const&); +JS::ThrowCompletionOr cross_origin_property_fallback(JS::VM&, JS::PropertyKey const&); +bool is_platform_object_same_origin(JS::Object const&); +Optional cross_origin_get_own_property_helper(Variant const&, JS::PropertyKey const&); +JS::ThrowCompletionOr cross_origin_get(JS::VM&, JS::Object const&, JS::PropertyKey const&, JS::Value receiver); +JS::ThrowCompletionOr cross_origin_set(JS::VM&, JS::Object&, JS::PropertyKey const&, JS::Value, JS::Value receiver); +JS::MarkedVector cross_origin_own_property_keys(Variant const&); + +} diff --git a/Userland/Libraries/LibWeb/HTML/CrossOrigin/CrossOriginPropertyDescriptorMap.h b/Userland/Libraries/LibWeb/HTML/CrossOrigin/CrossOriginPropertyDescriptorMap.h new file mode 100644 index 0000000000..1aebdc0385 --- /dev/null +++ b/Userland/Libraries/LibWeb/HTML/CrossOrigin/CrossOriginPropertyDescriptorMap.h @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2022, Linus Groh + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include +#include +#include + +namespace Web::HTML { + +struct CrossOriginProperty { + String property; + Optional needs_get {}; + Optional needs_set {}; +}; + +struct CrossOriginKey { + FlatPtr current_settings_object; + FlatPtr relevant_settings_object; + JS::PropertyKey property_key; +}; + +using CrossOriginPropertyDescriptorMap = HashMap; + +} + +namespace AK { + +template<> +struct Traits : public GenericTraits { + static unsigned hash(Web::HTML::CrossOriginKey const& key) + { + return pair_int_hash( + Traits::hash(key.property_key), + pair_int_hash(ptr_hash(key.current_settings_object), ptr_hash(key.relevant_settings_object))); + } + + static bool equals(Web::HTML::CrossOriginKey const& a, Web::HTML::CrossOriginKey const& b) + { + return a.current_settings_object == b.current_settings_object + && a.relevant_settings_object == b.relevant_settings_object + && Traits::equals(a.property_key, b.property_key); + } +}; + +} diff --git a/Userland/Libraries/LibWeb/HTML/CrossOrigin/Reporting.cpp b/Userland/Libraries/LibWeb/HTML/CrossOrigin/Reporting.cpp index ab801138af..b3b940fbfe 100644 --- a/Userland/Libraries/LibWeb/HTML/CrossOrigin/Reporting.cpp +++ b/Userland/Libraries/LibWeb/HTML/CrossOrigin/Reporting.cpp @@ -5,8 +5,8 @@ */ #include -#include #include +#include #include namespace Web::HTML { @@ -15,7 +15,7 @@ namespace Web::HTML { void check_if_access_between_two_browsing_contexts_should_be_reported(BrowsingContext const& accessor, BrowsingContext const& accessed, JS::PropertyKey const& property_key, EnvironmentSettingsObject const& environment) { // 1. If P is not a cross-origin accessible window property name, then return. - if (!Bindings::is_cross_origin_accessible_window_property_name(property_key)) + if (!is_cross_origin_accessible_window_property_name(property_key)) return; // FIXME: 2. If accessor's active document's origin or any of its ancestors' active document's origins are not same origin with accessor's top-level browsing context's active document's origin, or if accessed's active document's origin or any of its ancestors' active document's origins are not same origin with accessed's top-level browsing context's active document's origin, then return. diff --git a/Userland/Libraries/LibWeb/HTML/Window.h b/Userland/Libraries/LibWeb/HTML/Window.h index f44f048be1..85a50d2213 100644 --- a/Userland/Libraries/LibWeb/HTML/Window.h +++ b/Userland/Libraries/LibWeb/HTML/Window.h @@ -13,10 +13,10 @@ #include #include #include -#include #include #include #include +#include #include #include @@ -202,8 +202,8 @@ public: virtual JS::ThrowCompletionOr internal_set_prototype_of(JS::Object* prototype) override; - Bindings::CrossOriginPropertyDescriptorMap const& cross_origin_property_descriptor_map() const { return m_cross_origin_property_descriptor_map; } - Bindings::CrossOriginPropertyDescriptorMap& cross_origin_property_descriptor_map() { return m_cross_origin_property_descriptor_map; } + CrossOriginPropertyDescriptorMap const& cross_origin_property_descriptor_map() const { return m_cross_origin_property_descriptor_map; } + CrossOriginPropertyDescriptorMap& cross_origin_property_descriptor_map() { return m_cross_origin_property_descriptor_map; } private: JS_DECLARE_NATIVE_FUNCTION(length_getter); @@ -287,7 +287,7 @@ private: HashMap m_constructors; // [[CrossOriginPropertyDescriptorMap]], https://html.spec.whatwg.org/multipage/browsers.html#crossoriginpropertydescriptormap - Bindings::CrossOriginPropertyDescriptorMap m_cross_origin_property_descriptor_map; + CrossOriginPropertyDescriptorMap m_cross_origin_property_descriptor_map; }; void run_animation_frame_callbacks(DOM::Document&, double now);