From 89b6cd3fb1b9a3058dbada9e12606950b466936a Mon Sep 17 00:00:00 2001 From: Tim Ledbetter Date: Tue, 17 Sep 2024 16:44:42 +0100 Subject: [PATCH] LibWeb: Expose `crypto` object to workers This change moves the `crypto()` getter from `Window` to `WorkerOrWindowGlobalScope`. This aligns our implementation with the WebCrypto specification. --- Tests/LibWeb/Text/expected/Worker/Worker-crypto.txt | 1 + Tests/LibWeb/Text/input/Worker/Worker-crypto.html | 10 ++++++++++ Tests/LibWeb/Text/input/Worker/worker-crypto.js | 1 + Userland/Libraries/LibWeb/HTML/Window.cpp | 12 ------------ Userland/Libraries/LibWeb/HTML/Window.h | 3 --- Userland/Libraries/LibWeb/HTML/Window.idl | 4 ---- .../LibWeb/HTML/WindowOrWorkerGlobalScope.cpp | 13 +++++++++++++ .../LibWeb/HTML/WindowOrWorkerGlobalScope.h | 4 ++++ .../LibWeb/HTML/WindowOrWorkerGlobalScope.idl | 4 ++++ 9 files changed, 33 insertions(+), 19 deletions(-) create mode 100644 Tests/LibWeb/Text/expected/Worker/Worker-crypto.txt create mode 100644 Tests/LibWeb/Text/input/Worker/Worker-crypto.html create mode 100644 Tests/LibWeb/Text/input/Worker/worker-crypto.js diff --git a/Tests/LibWeb/Text/expected/Worker/Worker-crypto.txt b/Tests/LibWeb/Text/expected/Worker/Worker-crypto.txt new file mode 100644 index 00000000000..5dbe9d1ce8a --- /dev/null +++ b/Tests/LibWeb/Text/expected/Worker/Worker-crypto.txt @@ -0,0 +1 @@ +Type of self.crypto: [object Crypto] diff --git a/Tests/LibWeb/Text/input/Worker/Worker-crypto.html b/Tests/LibWeb/Text/input/Worker/Worker-crypto.html new file mode 100644 index 00000000000..c8b59e57508 --- /dev/null +++ b/Tests/LibWeb/Text/input/Worker/Worker-crypto.html @@ -0,0 +1,10 @@ + + diff --git a/Tests/LibWeb/Text/input/Worker/worker-crypto.js b/Tests/LibWeb/Text/input/Worker/worker-crypto.js new file mode 100644 index 00000000000..e5da6d590a0 --- /dev/null +++ b/Tests/LibWeb/Text/input/Worker/worker-crypto.js @@ -0,0 +1 @@ +postMessage(self.crypto.toString()); diff --git a/Userland/Libraries/LibWeb/HTML/Window.cpp b/Userland/Libraries/LibWeb/HTML/Window.cpp index a8a0a05674d..ff51a1ce9a7 100644 --- a/Userland/Libraries/LibWeb/HTML/Window.cpp +++ b/Userland/Libraries/LibWeb/HTML/Window.cpp @@ -24,7 +24,6 @@ #include #include #include -#include #include #include #include @@ -120,7 +119,6 @@ void Window::visit_edges(JS::Cell::Visitor& visitor) visitor.visit(m_current_event); visitor.visit(m_screen); visitor.visit(m_location); - visitor.visit(m_crypto); visitor.visit(m_navigator); visitor.visit(m_navigation); visitor.visit(m_custom_element_registry); @@ -1553,16 +1551,6 @@ JS::GCPtr Window::get_selection() const return associated_document().get_selection(); } -// https://w3c.github.io/webcrypto/#dom-windoworworkerglobalscope-crypto -JS::NonnullGCPtr Window::crypto() -{ - auto& realm = this->realm(); - - if (!m_crypto) - m_crypto = heap().allocate(realm, realm); - return JS::NonnullGCPtr { *m_crypto }; -} - // https://html.spec.whatwg.org/multipage/obsolete.html#dom-window-captureevents void Window::capture_events() { diff --git a/Userland/Libraries/LibWeb/HTML/Window.h b/Userland/Libraries/LibWeb/HTML/Window.h index 06809d8a010..3c3aba1fee0 100644 --- a/Userland/Libraries/LibWeb/HTML/Window.h +++ b/Userland/Libraries/LibWeb/HTML/Window.h @@ -210,8 +210,6 @@ public: JS::GCPtr get_selection() const; - [[nodiscard]] JS::NonnullGCPtr crypto(); - void capture_events(); void release_events(); @@ -270,7 +268,6 @@ private: // https://html.spec.whatwg.org/multipage/webappapis.html#import-maps-allowed bool m_import_maps_allowed { true }; - JS::GCPtr m_crypto; JS::GCPtr m_screen; JS::GCPtr m_navigator; JS::GCPtr m_location; diff --git a/Userland/Libraries/LibWeb/HTML/Window.idl b/Userland/Libraries/LibWeb/HTML/Window.idl index 5121df93aea..c3111850433 100644 --- a/Userland/Libraries/LibWeb/HTML/Window.idl +++ b/Userland/Libraries/LibWeb/HTML/Window.idl @@ -1,4 +1,3 @@ -#import #import #import #import @@ -108,9 +107,6 @@ interface Window : EventTarget { // https://w3c.github.io/selection-api/#extensions-to-window-interface Selection? getSelection(); - // https://w3c.github.io/webcrypto/#crypto-interface - [SameObject] readonly attribute Crypto crypto; - undefined captureEvents(); undefined releaseEvents(); diff --git a/Userland/Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.cpp b/Userland/Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.cpp index 5c4fcb82cdd..82bce7125a5 100644 --- a/Userland/Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.cpp +++ b/Userland/Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.cpp @@ -15,6 +15,7 @@ #include #include #include +#include #include #include #include @@ -72,6 +73,7 @@ void WindowOrWorkerGlobalScopeMixin::visit_edges(JS::Cell::Visitor& visitor) for (auto& entry : m_performance_entry_buffer_map) entry.value.visit_edges(visitor); visitor.visit(m_registered_event_sources); + visitor.visit(m_crypto); } void WindowOrWorkerGlobalScopeMixin::finalize() @@ -838,4 +840,15 @@ void WindowOrWorkerGlobalScopeMixin::report_error(JS::Value e) } } +// https://w3c.github.io/webcrypto/#dom-windoworworkerglobalscope-crypto +JS::NonnullGCPtr WindowOrWorkerGlobalScopeMixin::crypto() +{ + auto& platform_object = this_impl(); + auto& realm = platform_object.realm(); + + if (!m_crypto) + m_crypto = platform_object.heap().allocate(realm, realm); + return JS::NonnullGCPtr { *m_crypto }; +} + } diff --git a/Userland/Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.h b/Userland/Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.h index 2e008ac107c..88635bc66c6 100644 --- a/Userland/Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.h +++ b/Userland/Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.h @@ -78,6 +78,8 @@ public: void report_error(JS::Value e); + [[nodiscard]] JS::NonnullGCPtr crypto(); + protected: void initialize(JS::Realm&); void visit_edges(JS::Cell::Visitor&); @@ -117,6 +119,8 @@ private: mutable JS::GCPtr m_supported_entry_types_array; + JS::GCPtr m_crypto; + bool m_error_reporting_mode { false }; }; diff --git a/Userland/Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.idl b/Userland/Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.idl index 6ec57d8596a..c0fa1e1a33b 100644 --- a/Userland/Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.idl +++ b/Userland/Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.idl @@ -1,3 +1,4 @@ +#import #import #import #import @@ -48,4 +49,7 @@ interface mixin WindowOrWorkerGlobalScope { // https://w3c.github.io/IndexedDB/#factory-interface [SameObject] readonly attribute IDBFactory indexedDB; + + // https://w3c.github.io/webcrypto/#crypto-interface + [SameObject] readonly attribute Crypto crypto; };