From 2ea31fad32cc9e8ddc8bc64076349136b7dbf51c Mon Sep 17 00:00:00 2001 From: Shannon Booth Date: Fri, 8 Nov 2024 16:03:45 +1300 Subject: [PATCH] LibWeb: Add support for interfaces being exposed for shadow realm --- Meta/CMake/libweb_generators.cmake | 3 +++ .../GenerateWindowOrWorkerInterfaces.cpp | 21 ++++++++++++++----- .../Text/expected/all-window-properties.txt | 1 - .../LibWeb/Bindings/MainThreadVM.cpp | 3 +++ .../LibWeb/HTML/ShadowRealmGlobalScope.cpp | 6 ++++++ .../LibWeb/HTML/ShadowRealmGlobalScope.h | 2 ++ .../LibWeb/HTML/ShadowRealmGlobalScope.idl | 3 +-- 7 files changed, 31 insertions(+), 8 deletions(-) diff --git a/Meta/CMake/libweb_generators.cmake b/Meta/CMake/libweb_generators.cmake index f08c98e5935..76425440fc2 100644 --- a/Meta/CMake/libweb_generators.cmake +++ b/Meta/CMake/libweb_generators.cmake @@ -195,6 +195,7 @@ function (generate_js_bindings target) IntrinsicDefinitions.cpp DedicatedWorkerExposedInterfaces.cpp DedicatedWorkerExposedInterfaces.h SharedWorkerExposedInterfaces.cpp SharedWorkerExposedInterfaces.h + ShadowRealmExposedInterfaces.cpp ShadowRealmExposedInterfaces.h WindowExposedInterfaces.cpp WindowExposedInterfaces.h) list(TRANSFORM exposed_interface_sources PREPEND "Bindings/") add_custom_command( @@ -206,6 +207,8 @@ function (generate_js_bindings target) COMMAND "${CMAKE_COMMAND}" -E copy_if_different tmp/DedicatedWorkerExposedInterfaces.cpp "Bindings/DedicatedWorkerExposedInterfaces.cpp" COMMAND "${CMAKE_COMMAND}" -E copy_if_different tmp/SharedWorkerExposedInterfaces.h "Bindings/SharedWorkerExposedInterfaces.h" COMMAND "${CMAKE_COMMAND}" -E copy_if_different tmp/SharedWorkerExposedInterfaces.cpp "Bindings/SharedWorkerExposedInterfaces.cpp" + COMMAND "${CMAKE_COMMAND}" -E copy_if_different tmp/ShadowRealmExposedInterfaces.h "Bindings/ShadowRealmExposedInterfaces.h" + COMMAND "${CMAKE_COMMAND}" -E copy_if_different tmp/ShadowRealmExposedInterfaces.cpp "Bindings/ShadowRealmExposedInterfaces.cpp" COMMAND "${CMAKE_COMMAND}" -E copy_if_different tmp/WindowExposedInterfaces.h "Bindings/WindowExposedInterfaces.h" COMMAND "${CMAKE_COMMAND}" -E copy_if_different tmp/WindowExposedInterfaces.cpp "Bindings/WindowExposedInterfaces.cpp" COMMAND "${CMAKE_COMMAND}" -E remove_directory "${CMAKE_CURRENT_BINARY_DIR}/tmp" diff --git a/Meta/Lagom/Tools/CodeGenerators/LibWeb/GenerateWindowOrWorkerInterfaces.cpp b/Meta/Lagom/Tools/CodeGenerators/LibWeb/GenerateWindowOrWorkerInterfaces.cpp index 218776f4e72..38a38dc9ab1 100644 --- a/Meta/Lagom/Tools/CodeGenerators/LibWeb/GenerateWindowOrWorkerInterfaces.cpp +++ b/Meta/Lagom/Tools/CodeGenerators/LibWeb/GenerateWindowOrWorkerInterfaces.cpp @@ -14,7 +14,7 @@ #include #include -static ErrorOr add_to_interface_sets(IDL::Interface&, Vector& intrinsics, Vector& window_exposed, Vector& dedicated_worker_exposed, Vector& shared_worker_exposed); +static ErrorOr add_to_interface_sets(IDL::Interface&, Vector& intrinsics, Vector& window_exposed, Vector& dedicated_worker_exposed, Vector& shared_worker_exposed, Vector& shadow_realm_exposed); static ByteString s_error_string; struct LegacyConstructor { @@ -367,6 +367,7 @@ ErrorOr serenity_main(Main::Arguments arguments) Vector window_exposed; Vector dedicated_worker_exposed; Vector shared_worker_exposed; + Vector shadow_realm_exposed; // TODO: service_worker_exposed for (size_t i = 0; i < paths.size(); ++i) { @@ -378,7 +379,7 @@ ErrorOr serenity_main(Main::Arguments arguments) return Error::from_string_view(s_error_string.view()); } - TRY(add_to_interface_sets(interface, intrinsics, window_exposed, dedicated_worker_exposed, shared_worker_exposed)); + TRY(add_to_interface_sets(interface, intrinsics, window_exposed, dedicated_worker_exposed, shared_worker_exposed, shadow_realm_exposed)); parsers.append(move(parser)); } @@ -387,11 +388,13 @@ ErrorOr serenity_main(Main::Arguments arguments) TRY(generate_exposed_interface_header("Window"sv, output_path)); TRY(generate_exposed_interface_header("DedicatedWorker"sv, output_path)); TRY(generate_exposed_interface_header("SharedWorker"sv, output_path)); + TRY(generate_exposed_interface_header("ShadowRealm"sv, output_path)); // TODO: ServiceWorkerExposed.h TRY(generate_exposed_interface_implementation("Window"sv, output_path, window_exposed)); TRY(generate_exposed_interface_implementation("DedicatedWorker"sv, output_path, dedicated_worker_exposed)); TRY(generate_exposed_interface_implementation("SharedWorker"sv, output_path, shared_worker_exposed)); + TRY(generate_exposed_interface_implementation("ShadowRealm"sv, output_path, shadow_realm_exposed)); // TODO: ServiceWorkerExposed.cpp return 0; @@ -404,8 +407,9 @@ enum ExposedTo { ServiceWorker = 0x4, AudioWorklet = 0x8, Window = 0x10, - AllWorkers = 0xF, // FIXME: Is "AudioWorklet" a Worker? We'll assume it is for now - All = 0x1F, + ShadowRealm = 0x20, + AllWorkers = DedicatedWorker | SharedWorker | ServiceWorker | AudioWorklet, // FIXME: Is "AudioWorklet" a Worker? We'll assume it is for now (here, and line below) + All = AllWorkers | Window | ShadowRealm }; AK_ENUM_BITWISE_OPERATORS(ExposedTo); @@ -436,6 +440,8 @@ static ErrorOr parse_exposure_set(IDL::Interface& interface) return ExposedTo::ServiceWorker; if (exposed == "AudioWorklet"sv) return ExposedTo::AudioWorklet; + if (exposed == "ShadowRealm"sv) + return ExposedTo::ShadowRealm; if (exposed[0] == '(') { ExposedTo whom = Nobody; @@ -453,6 +459,8 @@ static ErrorOr parse_exposure_set(IDL::Interface& interface) whom |= ExposedTo::ServiceWorker; } else if (candidate == "AudioWorklet"sv) { whom |= ExposedTo::AudioWorklet; + } else if (candidate == "ShadowRealm"sv) { + whom |= ExposedTo::ShadowRealm; } else { s_error_string = ByteString::formatted("Unknown Exposed attribute candidate {} in {} in {}", candidate, exposed, interface.name); return Error::from_string_view(s_error_string.view()); @@ -469,7 +477,7 @@ static ErrorOr parse_exposure_set(IDL::Interface& interface) return Error::from_string_view(s_error_string.view()); } -ErrorOr add_to_interface_sets(IDL::Interface& interface, Vector& intrinsics, Vector& window_exposed, Vector& dedicated_worker_exposed, Vector& shared_worker_exposed) +ErrorOr add_to_interface_sets(IDL::Interface& interface, Vector& intrinsics, Vector& window_exposed, Vector& dedicated_worker_exposed, Vector& shared_worker_exposed, Vector& shadow_realm_exposed) { // TODO: Add service worker exposed and audio worklet exposed auto whom = TRY(parse_exposure_set(interface)); @@ -485,5 +493,8 @@ ErrorOr add_to_interface_sets(IDL::Interface& interface, Vector initialize_main_thread_vm(HTML::EventLoop::Type type) // 11. Perform ? SetDefaultGlobalBindings(realm). set_default_global_bindings(realm); + // NOTE: This needs to be done after initialization so that the realm has an intrinsics in its [[HostDefined]] + global_object->initialize_web_interfaces(); + // 12. Return NormalCompletion(unused). return {}; }; diff --git a/Userland/Libraries/LibWeb/HTML/ShadowRealmGlobalScope.cpp b/Userland/Libraries/LibWeb/HTML/ShadowRealmGlobalScope.cpp index 8083d4298cd..8101549de33 100644 --- a/Userland/Libraries/LibWeb/HTML/ShadowRealmGlobalScope.cpp +++ b/Userland/Libraries/LibWeb/HTML/ShadowRealmGlobalScope.cpp @@ -4,6 +4,7 @@ * SPDX-License-Identifier: BSD-2-Clause */ +#include #include #include @@ -30,6 +31,11 @@ void ShadowRealmGlobalScope::initialize(JS::Realm&) // WEB_SET_PROTOTYPE_FOR_INTERFACE(ShadowRealmGlobalScope); } +void ShadowRealmGlobalScope::initialize_web_interfaces() +{ + add_shadow_realm_exposed_interfaces(*this); +} + void ShadowRealmGlobalScope::visit_edges(Cell::Visitor& visitor) { Base::visit_edges(visitor); diff --git a/Userland/Libraries/LibWeb/HTML/ShadowRealmGlobalScope.h b/Userland/Libraries/LibWeb/HTML/ShadowRealmGlobalScope.h index 931764f8803..c01485369b1 100644 --- a/Userland/Libraries/LibWeb/HTML/ShadowRealmGlobalScope.h +++ b/Userland/Libraries/LibWeb/HTML/ShadowRealmGlobalScope.h @@ -28,6 +28,8 @@ public: return *this; } + void initialize_web_interfaces(); + protected: explicit ShadowRealmGlobalScope(JS::Realm&); diff --git a/Userland/Libraries/LibWeb/HTML/ShadowRealmGlobalScope.idl b/Userland/Libraries/LibWeb/HTML/ShadowRealmGlobalScope.idl index 043b16981c5..0c604a4bb9a 100644 --- a/Userland/Libraries/LibWeb/HTML/ShadowRealmGlobalScope.idl +++ b/Userland/Libraries/LibWeb/HTML/ShadowRealmGlobalScope.idl @@ -1,6 +1,5 @@ // https://whatpr.org/html/9893/webappapis.html#shadowrealmglobalscope -// FIXME: This is not correct! We should not have Exposed=Window here. -[Global, Exposed=Window] +[Global, Exposed=ShadowRealm] interface ShadowRealmGlobalScope : EventTarget { readonly attribute ShadowRealmGlobalScope self; };