diff --git a/Userland/Libraries/LibWeb/Crypto/Crypto.cpp b/Userland/Libraries/LibWeb/Crypto/Crypto.cpp index b6008f9aca..fd6ffc274b 100644 --- a/Userland/Libraries/LibWeb/Crypto/Crypto.cpp +++ b/Userland/Libraries/LibWeb/Crypto/Crypto.cpp @@ -14,11 +14,16 @@ namespace Web::Crypto { -Crypto::Crypto() - : m_subtle(SubtleCrypto::create()) +Crypto::Crypto(HTML::Window& window) + : m_subtle(*SubtleCrypto::create(window)) { } +JS::NonnullGCPtr Crypto::subtle() const +{ + return *m_subtle; +} + // https://w3c.github.io/webcrypto/#dfn-Crypto-method-getRandomValues DOM::ExceptionOr Crypto::get_random_values(JS::Value array) const { diff --git a/Userland/Libraries/LibWeb/Crypto/Crypto.h b/Userland/Libraries/LibWeb/Crypto/Crypto.h index 0ae8890981..04d8ef40fd 100644 --- a/Userland/Libraries/LibWeb/Crypto/Crypto.h +++ b/Userland/Libraries/LibWeb/Crypto/Crypto.h @@ -19,20 +19,20 @@ class Crypto : public Bindings::Wrappable public: using WrapperType = Bindings::CryptoWrapper; - static NonnullRefPtr create() + static NonnullRefPtr create(HTML::Window& window) { - return adopt_ref(*new Crypto()); + return adopt_ref(*new Crypto(window)); } - NonnullRefPtr subtle() const { return m_subtle; } + JS::NonnullGCPtr subtle() const; DOM::ExceptionOr get_random_values(JS::Value array) const; String random_uuid() const; private: - Crypto(); + explicit Crypto(HTML::Window&); - NonnullRefPtr m_subtle; + JS::Handle m_subtle; }; } diff --git a/Userland/Libraries/LibWeb/Crypto/SubtleCrypto.cpp b/Userland/Libraries/LibWeb/Crypto/SubtleCrypto.cpp index ffbebe75b1..bcad3cf09d 100644 --- a/Userland/Libraries/LibWeb/Crypto/SubtleCrypto.cpp +++ b/Userland/Libraries/LibWeb/Crypto/SubtleCrypto.cpp @@ -10,12 +10,24 @@ #include #include #include -#include #include #include namespace Web::Crypto { +JS::NonnullGCPtr SubtleCrypto::create(HTML::Window& window) +{ + return *window.heap().allocate(window.realm(), window); +} + +SubtleCrypto::SubtleCrypto(HTML::Window& window) + : PlatformObject(window.realm()) +{ + set_prototype(&window.cached_web_prototype("SubtleCrypto")); +} + +SubtleCrypto::~SubtleCrypto() = default; + JS::Promise* SubtleCrypto::digest(String const& algorithm, JS::Handle const& data) { auto& vm = Bindings::main_thread_vm(); diff --git a/Userland/Libraries/LibWeb/Crypto/SubtleCrypto.h b/Userland/Libraries/LibWeb/Crypto/SubtleCrypto.h index b1ae67badd..6866409a1e 100644 --- a/Userland/Libraries/LibWeb/Crypto/SubtleCrypto.h +++ b/Userland/Libraries/LibWeb/Crypto/SubtleCrypto.h @@ -7,31 +7,24 @@ #pragma once #include -#include +#include namespace Web::Crypto { -class SubtleCrypto - : public Bindings::Wrappable - , public RefCounted { -public: - using WrapperType = Bindings::SubtleCryptoWrapper; +class SubtleCrypto final : public Bindings::PlatformObject { + WEB_PLATFORM_OBJECT(SubtleCrypto, Bindings::PlatformObject); - static NonnullRefPtr create() - { - return adopt_ref(*new SubtleCrypto()); - } +public: + static JS::NonnullGCPtr create(HTML::Window&); + + virtual ~SubtleCrypto() override; JS::Promise* digest(String const& algorithm, JS::Handle const& data); private: - SubtleCrypto() = default; + explicit SubtleCrypto(HTML::Window&); }; } -namespace Web::Bindings { - -SubtleCryptoWrapper* wrap(JS::Realm&, Crypto::SubtleCrypto&); - -} +WRAPPER_HACK(SubtleCrypto, Web::Crypto) diff --git a/Userland/Libraries/LibWeb/Forward.h b/Userland/Libraries/LibWeb/Forward.h index 578363e420..3ac0532c63 100644 --- a/Userland/Libraries/LibWeb/Forward.h +++ b/Userland/Libraries/LibWeb/Forward.h @@ -466,7 +466,6 @@ class OptionConstructor; class RangePrototype; class ResizeObserverWrapper; class SelectionWrapper; -class SubtleCryptoWrapper; class TextDecoderWrapper; class TextEncoderWrapper; class URLSearchParamsIteratorWrapper; diff --git a/Userland/Libraries/LibWeb/HTML/Window.cpp b/Userland/Libraries/LibWeb/HTML/Window.cpp index 9fce75d890..a48a873526 100644 --- a/Userland/Libraries/LibWeb/HTML/Window.cpp +++ b/Userland/Libraries/LibWeb/HTML/Window.cpp @@ -92,7 +92,6 @@ JS::NonnullGCPtr Window::create_with_document(DOM::Document& document) Window::Window(JS::Realm& realm) : DOM::EventTarget(realm) - , m_crypto(Crypto::Crypto::create()) { // FIXME: Should this be WindowPrototype? } @@ -100,7 +99,6 @@ Window::Window(JS::Realm& realm) Window::Window(DOM::Document& document) : DOM::EventTarget(document.shape().realm()) , m_associated_document(document) - , m_crypto(Crypto::Crypto::create()) { } @@ -766,6 +764,8 @@ void Window::initialize(JS::Realm& realm) Object::set_prototype(&ensure_web_prototype("Window")); + m_crypto = Crypto::Crypto::create(*this); + // FIXME: These should be native accessors, not properties define_direct_property("window", this, JS::Attribute::Enumerable); define_direct_property("frames", this, JS::Attribute::Enumerable); diff --git a/Userland/Libraries/LibWeb/HTML/Window.h b/Userland/Libraries/LibWeb/HTML/Window.h index 28b227efa1..109a8206cb 100644 --- a/Userland/Libraries/LibWeb/HTML/Window.h +++ b/Userland/Libraries/LibWeb/HTML/Window.h @@ -147,7 +147,7 @@ private: HashMap> m_timers; JS::GCPtr m_performance; - NonnullRefPtr m_crypto; + RefPtr m_crypto; JS::GCPtr m_screen; AnimationFrameCallbackDriver m_animation_frame_callback_driver; diff --git a/Userland/Libraries/LibWeb/idl_files.cmake b/Userland/Libraries/LibWeb/idl_files.cmake index b4708c395d..223c62c0a7 100644 --- a/Userland/Libraries/LibWeb/idl_files.cmake +++ b/Userland/Libraries/LibWeb/idl_files.cmake @@ -2,7 +2,7 @@ # It is defined here so that there is no need to go to the Meta directory when adding new idl files libweb_js_wrapper(Crypto/Crypto) -libweb_js_wrapper(Crypto/SubtleCrypto) +libweb_js_wrapper(Crypto/SubtleCrypto NO_INSTANCE) libweb_js_wrapper(CSS/CSSConditionRule NO_INSTANCE) libweb_js_wrapper(CSS/CSSFontFaceRule NO_INSTANCE) libweb_js_wrapper(CSS/CSSGroupingRule NO_INSTANCE)