1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-30 14:58:11 +00:00

LibWeb: Move cross-origin AOs from Bindings/ to HTML/CrossOrigin/

These are from the HTML spec and therefore belong in the HTML/ directory
in LibWeb. Bindings/ has become a bit of a dumping ground, so this is a
first step towards cleaning that up.
This commit is contained in:
Linus Groh 2022-09-24 14:02:47 +01:00
parent c3841e1667
commit dc44effd44
10 changed files with 111 additions and 97 deletions

View file

@ -1,61 +0,0 @@
/*
* Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Forward.h>
#include <AK/Traits.h>
#include <LibJS/Forward.h>
#include <LibJS/Runtime/PropertyKey.h>
#include <LibWeb/Forward.h>
namespace Web::Bindings {
struct CrossOriginProperty {
String property;
Optional<bool> needs_get {};
Optional<bool> needs_set {};
};
struct CrossOriginKey {
FlatPtr current_settings_object;
FlatPtr relevant_settings_object;
JS::PropertyKey property_key;
};
using CrossOriginPropertyDescriptorMap = HashMap<CrossOriginKey, JS::PropertyDescriptor>;
Vector<CrossOriginProperty> cross_origin_properties(Variant<LocationObject const*, HTML::Window const*> const&);
bool is_cross_origin_accessible_window_property_name(JS::PropertyKey const&);
JS::ThrowCompletionOr<JS::PropertyDescriptor> cross_origin_property_fallback(JS::VM&, JS::PropertyKey const&);
bool is_platform_object_same_origin(JS::Object const&);
Optional<JS::PropertyDescriptor> cross_origin_get_own_property_helper(Variant<LocationObject*, HTML::Window*> const&, JS::PropertyKey const&);
JS::ThrowCompletionOr<JS::Value> cross_origin_get(JS::VM&, JS::Object const&, JS::PropertyKey const&, JS::Value receiver);
JS::ThrowCompletionOr<bool> cross_origin_set(JS::VM&, JS::Object&, JS::PropertyKey const&, JS::Value, JS::Value receiver);
JS::MarkedVector<JS::Value> cross_origin_own_property_keys(Variant<LocationObject const*, HTML::Window const*> const&);
}
namespace AK {
template<>
struct Traits<Web::Bindings::CrossOriginKey> : public GenericTraits<Web::Bindings::CrossOriginKey> {
static unsigned hash(Web::Bindings::CrossOriginKey const& key)
{
return pair_int_hash(
Traits<JS::PropertyKey>::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<JS::PropertyKey>::equals(a.property_key, b.property_key);
}
};
}

View file

@ -11,11 +11,11 @@
#include <LibJS/Runtime/Completion.h>
#include <LibJS/Runtime/PropertyDescriptor.h>
#include <LibJS/Runtime/PropertyKey.h>
#include <LibWeb/Bindings/CrossOriginAbstractOperations.h>
#include <LibWeb/Bindings/LocationObject.h>
#include <LibWeb/Bindings/LocationPrototype.h>
#include <LibWeb/DOM/DOMException.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/HTML/CrossOrigin/AbstractOperations.h>
#include <LibWeb/HTML/Window.h>
namespace Web::Bindings {
@ -244,7 +244,7 @@ JS_DEFINE_NATIVE_FUNCTION(LocationObject::replace)
JS::ThrowCompletionOr<JS::Object*> 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<Optional<JS::PropertyDescriptor>> 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<Optional<JS::PropertyDescriptor>> LocationObject::internal
}
// 2. Let property be CrossOriginGetOwnPropertyHelper(this, P).
auto property = cross_origin_get_own_property_helper(const_cast<LocationObject*>(this), property_key);
auto property = HTML::cross_origin_get_own_property_helper(const_cast<LocationObject*>(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<bool> 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<JS::Value> 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<JS::Object const&>(*this), property_key, receiver);
return HTML::cross_origin_get(vm, static_cast<JS::Object const&>(*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<bool> 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<JS::Object&>(*this), property_key, value, receiver);
return HTML::cross_origin_set(vm, static_cast<JS::Object&>(*this), property_key, value, receiver);
}
// 7.10.5.9 [[Delete]] ( P ), https://html.spec.whatwg.org/multipage/history.html#location-delete
JS::ThrowCompletionOr<bool> 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<bool> LocationObject::internal_delete(JS::PropertyKey cons
JS::ThrowCompletionOr<JS::MarkedVector<JS::Value>> 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);
}
}

View file

@ -10,9 +10,9 @@
#include <AK/URL.h>
#include <LibJS/Forward.h>
#include <LibJS/Runtime/Completion.h>
#include <LibWeb/Bindings/CrossOriginAbstractOperations.h>
#include <LibWeb/Bindings/PlatformObject.h>
#include <LibWeb/Forward.h>
#include <LibWeb/HTML/CrossOrigin/CrossOriginPropertyDescriptorMap.h>
namespace Web {
namespace Bindings {
@ -34,8 +34,8 @@ public:
virtual JS::ThrowCompletionOr<bool> internal_delete(JS::PropertyKey const&) override;
virtual JS::ThrowCompletionOr<JS::MarkedVector<JS::Value>> 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<JS::Value> m_default_properties;

View file

@ -10,9 +10,9 @@
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/PropertyDescriptor.h>
#include <LibJS/Runtime/PropertyKey.h>
#include <LibWeb/Bindings/CrossOriginAbstractOperations.h>
#include <LibWeb/Bindings/WindowProxy.h>
#include <LibWeb/DOM/DOMException.h>
#include <LibWeb/HTML/CrossOrigin/AbstractOperations.h>
#include <LibWeb/HTML/CrossOrigin/Reporting.h>
#include <LibWeb/HTML/Scripting/Environments.h>
#include <LibWeb/HTML/Window.h>
@ -120,7 +120,7 @@ JS::ThrowCompletionOr<Optional<JS::PropertyDescriptor>> 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<JS::Value> 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<bool> 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

View file

@ -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

View file

@ -13,23 +13,23 @@
#include <LibJS/Runtime/Object.h>
#include <LibJS/Runtime/PropertyDescriptor.h>
#include <LibJS/Runtime/PropertyKey.h>
#include <LibWeb/Bindings/CrossOriginAbstractOperations.h>
#include <LibWeb/Bindings/LocationObject.h>
#include <LibWeb/Bindings/MainThreadVM.h>
#include <LibWeb/DOM/DOMException.h>
#include <LibWeb/HTML/CrossOrigin/AbstractOperations.h>
#include <LibWeb/HTML/Scripting/Environments.h>
#include <LibWeb/HTML/Window.h>
namespace Web::Bindings {
namespace Web::HTML {
// 7.2.3.1 CrossOriginProperties ( O ), https://html.spec.whatwg.org/multipage/browsers.html#crossoriginproperties-(-o-)
Vector<CrossOriginProperty> cross_origin_properties(Variant<LocationObject const*, HTML::Window const*> const& object)
Vector<CrossOriginProperty> cross_origin_properties(Variant<Bindings::LocationObject const*, HTML::Window const*> 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<CrossOriginProperty> {
[](Bindings::LocationObject const*) -> Vector<CrossOriginProperty> {
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<JS::PropertyDescriptor> cross_origin_get_own_property_helper(Variant<LocationObject*, HTML::Window*> const& object, JS::PropertyKey const& property_key)
Optional<JS::PropertyDescriptor> cross_origin_get_own_property_helper(Variant<Bindings::LocationObject*, HTML::Window*> 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<JS::Object const*>(o); });
auto const object_const_variant = object.visit([](auto* o) { return Variant<LocationObject const*, HTML::Window const*> { o }; });
auto const object_const_variant = object.visit([](auto* o) { return Variant<Bindings::LocationObject const*, HTML::Window const*> { 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<bool> 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<JS::Value> cross_origin_own_property_keys(Variant<LocationObject const*, HTML::Window const*> const& object)
JS::MarkedVector<JS::Value> cross_origin_own_property_keys(Variant<Bindings::LocationObject const*, HTML::Window const*> const& object)
{
auto& event_loop = HTML::main_thread_event_loop();
auto& vm = event_loop.vm();

View file

@ -0,0 +1,25 @@
/*
* Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Forward.h>
#include <LibJS/Forward.h>
#include <LibWeb/Forward.h>
#include <LibWeb/HTML/CrossOrigin/CrossOriginPropertyDescriptorMap.h>
namespace Web::HTML {
Vector<CrossOriginProperty> cross_origin_properties(Variant<Bindings::LocationObject const*, HTML::Window const*> const&);
bool is_cross_origin_accessible_window_property_name(JS::PropertyKey const&);
JS::ThrowCompletionOr<JS::PropertyDescriptor> cross_origin_property_fallback(JS::VM&, JS::PropertyKey const&);
bool is_platform_object_same_origin(JS::Object const&);
Optional<JS::PropertyDescriptor> cross_origin_get_own_property_helper(Variant<Bindings::LocationObject*, HTML::Window*> const&, JS::PropertyKey const&);
JS::ThrowCompletionOr<JS::Value> cross_origin_get(JS::VM&, JS::Object const&, JS::PropertyKey const&, JS::Value receiver);
JS::ThrowCompletionOr<bool> cross_origin_set(JS::VM&, JS::Object&, JS::PropertyKey const&, JS::Value, JS::Value receiver);
JS::MarkedVector<JS::Value> cross_origin_own_property_keys(Variant<Bindings::LocationObject const*, HTML::Window const*> const&);
}

View file

@ -0,0 +1,50 @@
/*
* Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Forward.h>
#include <AK/Traits.h>
#include <LibJS/Forward.h>
namespace Web::HTML {
struct CrossOriginProperty {
String property;
Optional<bool> needs_get {};
Optional<bool> needs_set {};
};
struct CrossOriginKey {
FlatPtr current_settings_object;
FlatPtr relevant_settings_object;
JS::PropertyKey property_key;
};
using CrossOriginPropertyDescriptorMap = HashMap<CrossOriginKey, JS::PropertyDescriptor>;
}
namespace AK {
template<>
struct Traits<Web::HTML::CrossOriginKey> : public GenericTraits<Web::HTML::CrossOriginKey> {
static unsigned hash(Web::HTML::CrossOriginKey const& key)
{
return pair_int_hash(
Traits<JS::PropertyKey>::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<JS::PropertyKey>::equals(a.property_key, b.property_key);
}
};
}

View file

@ -5,8 +5,8 @@
*/
#include <LibJS/Runtime/PropertyKey.h>
#include <LibWeb/Bindings/CrossOriginAbstractOperations.h>
#include <LibWeb/HTML/BrowsingContext.h>
#include <LibWeb/HTML/CrossOrigin/AbstractOperations.h>
#include <LibWeb/HTML/CrossOrigin/Reporting.h>
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.

View file

@ -13,10 +13,10 @@
#include <AK/TypeCasts.h>
#include <AK/URL.h>
#include <LibJS/Heap/Heap.h>
#include <LibWeb/Bindings/CrossOriginAbstractOperations.h>
#include <LibWeb/DOM/EventTarget.h>
#include <LibWeb/Forward.h>
#include <LibWeb/HTML/AnimationFrameCallbackDriver.h>
#include <LibWeb/HTML/CrossOrigin/CrossOriginPropertyDescriptorMap.h>
#include <LibWeb/HTML/GlobalEventHandlers.h>
#include <LibWeb/HTML/WindowEventHandlers.h>
@ -202,8 +202,8 @@ public:
virtual JS::ThrowCompletionOr<bool> 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<String, JS::NativeFunction*> 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);