From 198db2ebd9428ecf3e69d86a02d14cb373479d9d Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Mon, 6 Mar 2023 19:50:56 +0000 Subject: [PATCH] LibWeb/HTML: Port Window.crypto to IDL --- Userland/Libraries/LibWeb/HTML/Window.cpp | 16 ++++++++-------- Userland/Libraries/LibWeb/HTML/Window.h | 6 ++---- Userland/Libraries/LibWeb/HTML/Window.idl | 4 ++++ 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/Userland/Libraries/LibWeb/HTML/Window.cpp b/Userland/Libraries/LibWeb/HTML/Window.cpp index 33897561d7..b409f1a681 100644 --- a/Userland/Libraries/LibWeb/HTML/Window.cpp +++ b/Userland/Libraries/LibWeb/HTML/Window.cpp @@ -1038,14 +1038,12 @@ WebIDL::ExceptionOr Window::initialize_web_interfaces(Badge(realm, "Window")); - m_crypto = MUST_OR_THROW_OOM(heap().allocate(realm, realm)); m_location = MUST_OR_THROW_OOM(heap().allocate(realm, realm)); m_navigator = MUST_OR_THROW_OOM(heap().allocate(realm, realm)); MUST_OR_THROW_OOM(Bindings::WindowGlobalMixin::initialize(realm, *this)); // FIXME: These should be native accessors, not properties - define_native_accessor(realm, "crypto", crypto_getter, {}, JS::Attribute::Enumerable); define_native_accessor(realm, "screen", screen_getter, screen_setter, JS::Attribute::Enumerable | JS::Attribute::Configurable); define_native_accessor(realm, "innerWidth", inner_width_getter, {}, JS::Attribute::Enumerable); define_native_accessor(realm, "innerHeight", inner_height_getter, {}, JS::Attribute::Enumerable); @@ -1340,6 +1338,14 @@ WebIDL::ExceptionOr> Window::p return JS::NonnullGCPtr { *m_performance }; } +// https://w3c.github.io/webcrypto/#dom-windoworworkerglobalscope-crypto +WebIDL::ExceptionOr> Window::crypto() +{ + if (!m_crypto) + m_crypto = MUST_OR_THROW_OOM(heap().allocate(realm(), realm())); + return JS::NonnullGCPtr { *m_crypto }; +} + static JS::ThrowCompletionOr make_timer_handler(JS::VM& vm, JS::Value handler) { if (handler.is_function()) @@ -1531,12 +1537,6 @@ JS_DEFINE_NATIVE_FUNCTION(Window::location_setter) return JS::js_undefined(); } -JS_DEFINE_NATIVE_FUNCTION(Window::crypto_getter) -{ - auto* impl = TRY(impl_from(vm)); - return &impl->crypto(); -} - JS_DEFINE_NATIVE_FUNCTION(Window::inner_width_getter) { auto* impl = TRY(impl_from(vm)); diff --git a/Userland/Libraries/LibWeb/HTML/Window.h b/Userland/Libraries/LibWeb/HTML/Window.h index 5c70582186..032a943509 100644 --- a/Userland/Libraries/LibWeb/HTML/Window.h +++ b/Userland/Libraries/LibWeb/HTML/Window.h @@ -96,8 +96,6 @@ public: void deallocate_timer_id(Badge, i32); - Crypto::Crypto& crypto() { return *m_crypto; } - CSS::Screen& screen(); DOM::Event* current_event() { return m_current_event.ptr(); } @@ -169,6 +167,8 @@ public: WebIDL::ExceptionOr> performance(); + WebIDL::ExceptionOr> crypto(); + private: explicit Window(JS::Realm&); @@ -274,8 +274,6 @@ private: JS_DECLARE_NATIVE_FUNCTION(request_idle_callback); JS_DECLARE_NATIVE_FUNCTION(cancel_idle_callback); - JS_DECLARE_NATIVE_FUNCTION(crypto_getter); - HTML::Location* m_location { nullptr }; // [[CrossOriginPropertyDescriptorMap]], https://html.spec.whatwg.org/multipage/browsers.html#crossoriginpropertydescriptormap diff --git a/Userland/Libraries/LibWeb/HTML/Window.idl b/Userland/Libraries/LibWeb/HTML/Window.idl index afa1099125..19eb2a20a7 100644 --- a/Userland/Libraries/LibWeb/HTML/Window.idl +++ b/Userland/Libraries/LibWeb/HTML/Window.idl @@ -1,3 +1,4 @@ +#import #import #import #import @@ -44,6 +45,9 @@ interface Window : EventTarget { // FIXME: Everything from here on should be shared through WindowOrWorkerGlobalScope // https://w3c.github.io/hr-time/#the-performance-attribute [Replaceable] readonly attribute Performance performance; + + // https://w3c.github.io/webcrypto/#crypto-interface + [SameObject] readonly attribute Crypto crypto; }; Window includes GlobalEventHandlers; Window includes WindowEventHandlers;