LibWeb: Make SubtleCrypto GC-allocated

This commit is contained in:
Andreas Kling 2022-09-03 19:59:53 +02:00
parent 2ac8e3db3a
commit 7a9b8ced38
Notes: sideshowbarker 2024-07-17 07:25:45 +09:00
8 changed files with 38 additions and 29 deletions

View file

@ -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<SubtleCrypto> Crypto::subtle() const
{
return *m_subtle;
}
// https://w3c.github.io/webcrypto/#dfn-Crypto-method-getRandomValues
DOM::ExceptionOr<JS::Value> Crypto::get_random_values(JS::Value array) const
{

View file

@ -19,20 +19,20 @@ class Crypto : public Bindings::Wrappable
public:
using WrapperType = Bindings::CryptoWrapper;
static NonnullRefPtr<Crypto> create()
static NonnullRefPtr<Crypto> create(HTML::Window& window)
{
return adopt_ref(*new Crypto());
return adopt_ref(*new Crypto(window));
}
NonnullRefPtr<SubtleCrypto> subtle() const { return m_subtle; }
JS::NonnullGCPtr<SubtleCrypto> subtle() const;
DOM::ExceptionOr<JS::Value> get_random_values(JS::Value array) const;
String random_uuid() const;
private:
Crypto();
explicit Crypto(HTML::Window&);
NonnullRefPtr<SubtleCrypto> m_subtle;
JS::Handle<SubtleCrypto> m_subtle;
};
}

View file

@ -10,12 +10,24 @@
#include <LibWeb/Bindings/DOMExceptionWrapper.h>
#include <LibWeb/Bindings/IDLAbstractOperations.h>
#include <LibWeb/Bindings/MainThreadVM.h>
#include <LibWeb/Bindings/Wrapper.h>
#include <LibWeb/Crypto/SubtleCrypto.h>
#include <LibWeb/DOM/DOMException.h>
namespace Web::Crypto {
JS::NonnullGCPtr<SubtleCrypto> SubtleCrypto::create(HTML::Window& window)
{
return *window.heap().allocate<SubtleCrypto>(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<JS::Object> const& data)
{
auto& vm = Bindings::main_thread_vm();

View file

@ -7,31 +7,24 @@
#pragma once
#include <LibJS/Forward.h>
#include <LibWeb/Bindings/Wrappable.h>
#include <LibWeb/Bindings/PlatformObject.h>
namespace Web::Crypto {
class SubtleCrypto
: public Bindings::Wrappable
, public RefCounted<SubtleCrypto> {
public:
using WrapperType = Bindings::SubtleCryptoWrapper;
class SubtleCrypto final : public Bindings::PlatformObject {
WEB_PLATFORM_OBJECT(SubtleCrypto, Bindings::PlatformObject);
static NonnullRefPtr<SubtleCrypto> create()
{
return adopt_ref(*new SubtleCrypto());
}
public:
static JS::NonnullGCPtr<SubtleCrypto> create(HTML::Window&);
virtual ~SubtleCrypto() override;
JS::Promise* digest(String const& algorithm, JS::Handle<JS::Object> const& data);
private:
SubtleCrypto() = default;
explicit SubtleCrypto(HTML::Window&);
};
}
namespace Web::Bindings {
SubtleCryptoWrapper* wrap(JS::Realm&, Crypto::SubtleCrypto&);
}
WRAPPER_HACK(SubtleCrypto, Web::Crypto)

View file

@ -466,7 +466,6 @@ class OptionConstructor;
class RangePrototype;
class ResizeObserverWrapper;
class SelectionWrapper;
class SubtleCryptoWrapper;
class TextDecoderWrapper;
class TextEncoderWrapper;
class URLSearchParamsIteratorWrapper;

View file

@ -92,7 +92,6 @@ JS::NonnullGCPtr<Window> 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<Bindings::WindowPrototype>("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);

View file

@ -147,7 +147,7 @@ private:
HashMap<int, JS::NonnullGCPtr<Timer>> m_timers;
JS::GCPtr<HighResolutionTime::Performance> m_performance;
NonnullRefPtr<Crypto::Crypto> m_crypto;
RefPtr<Crypto::Crypto> m_crypto;
JS::GCPtr<CSS::Screen> m_screen;
AnimationFrameCallbackDriver m_animation_frame_callback_driver;

View file

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