diff options
author | Andrew Kaster <akaster@serenityos.org> | 2022-09-25 18:11:21 -0600 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2022-10-01 21:05:32 +0100 |
commit | 4bb6345b2ff348bd604b17c7dd4e81e61d52b658 (patch) | |
tree | 0f6c0a111e022358921f6c8912972aae3975e32f /Userland/Libraries/LibWeb/Crypto | |
parent | 4878a18ee7ebe0c9d50880f804bd2dc22b1c751c (diff) | |
download | serenity-4bb6345b2ff348bd604b17c7dd4e81e61d52b658.zip |
LibWeb: Remove unecessary dependence on Window from assorted classes
These classes only needed Window to get at its realm. Pass a realm
directly to construct Crypto, Encoding, HRT, IntersectionObserver,
NavigationTiming, Page, RequestIdleCallback, Selection, Streams, URL,
and XML classes.
Diffstat (limited to 'Userland/Libraries/LibWeb/Crypto')
-rw-r--r-- | Userland/Libraries/LibWeb/Crypto/Crypto.cpp | 20 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/Crypto/Crypto.h | 4 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/Crypto/SubtleCrypto.cpp | 22 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/Crypto/SubtleCrypto.h | 4 |
4 files changed, 25 insertions, 25 deletions
diff --git a/Userland/Libraries/LibWeb/Crypto/Crypto.cpp b/Userland/Libraries/LibWeb/Crypto/Crypto.cpp index a5c0dc17c2..672bfe6bc2 100644 --- a/Userland/Libraries/LibWeb/Crypto/Crypto.cpp +++ b/Userland/Libraries/LibWeb/Crypto/Crypto.cpp @@ -8,21 +8,21 @@ #include <AK/Random.h> #include <AK/StringBuilder.h> #include <LibJS/Runtime/TypedArray.h> +#include <LibWeb/Bindings/Intrinsics.h> #include <LibWeb/Crypto/Crypto.h> #include <LibWeb/Crypto/SubtleCrypto.h> -#include <LibWeb/HTML/Window.h> namespace Web::Crypto { -JS::NonnullGCPtr<Crypto> Crypto::create(HTML::Window& window) +JS::NonnullGCPtr<Crypto> Crypto::create(JS::Realm& realm) { - return *window.heap().allocate<Crypto>(window.realm(), window); + return *realm.heap().allocate<Crypto>(realm, realm); } -Crypto::Crypto(HTML::Window& window) - : PlatformObject(window.realm()) +Crypto::Crypto(JS::Realm& realm) + : PlatformObject(realm) { - set_prototype(&window.cached_web_prototype("Crypto")); + set_prototype(&Bindings::cached_web_prototype(realm, "Crypto")); } Crypto::~Crypto() = default; @@ -30,7 +30,7 @@ Crypto::~Crypto() = default; void Crypto::initialize(JS::Realm& realm) { Base::initialize(realm); - m_subtle = SubtleCrypto::create(global_object()); + m_subtle = SubtleCrypto::create(realm); } JS::NonnullGCPtr<SubtleCrypto> Crypto::subtle() const @@ -43,16 +43,16 @@ WebIDL::ExceptionOr<JS::Value> Crypto::get_random_values(JS::Value array) const { // 1. If array is not an Int8Array, Uint8Array, Uint8ClampedArray, Int16Array, Uint16Array, Int32Array, Uint32Array, BigInt64Array, or BigUint64Array, then throw a TypeMismatchError and terminate the algorithm. if (!array.is_object() || !(is<JS::Int8Array>(array.as_object()) || is<JS::Uint8Array>(array.as_object()) || is<JS::Uint8ClampedArray>(array.as_object()) || is<JS::Int16Array>(array.as_object()) || is<JS::Uint16Array>(array.as_object()) || is<JS::Int32Array>(array.as_object()) || is<JS::Uint32Array>(array.as_object()) || is<JS::BigInt64Array>(array.as_object()) || is<JS::BigUint64Array>(array.as_object()))) - return WebIDL::TypeMismatchError::create(global_object(), "array must be one of Int8Array, Uint8Array, Uint8ClampedArray, Int16Array, Uint16Array, Int32Array, Uint32Array, BigInt64Array, or BigUint64Array"); + return WebIDL::TypeMismatchError::create(realm(), "array must be one of Int8Array, Uint8Array, Uint8ClampedArray, Int16Array, Uint16Array, Int32Array, Uint32Array, BigInt64Array, or BigUint64Array"); auto& typed_array = static_cast<JS::TypedArrayBase&>(array.as_object()); // 2. If the byteLength of array is greater than 65536, throw a QuotaExceededError and terminate the algorithm. if (typed_array.byte_length() > 65536) - return WebIDL::QuotaExceededError::create(global_object(), "array's byteLength may not be greater than 65536"); + return WebIDL::QuotaExceededError::create(realm(), "array's byteLength may not be greater than 65536"); // IMPLEMENTATION DEFINED: If the viewed array buffer is detached, throw a InvalidStateError and terminate the algorithm. if (typed_array.viewed_array_buffer()->is_detached()) - return WebIDL::InvalidStateError::create(global_object(), "array is detached"); + return WebIDL::InvalidStateError::create(realm(), "array is detached"); // FIXME: Handle SharedArrayBuffers // 3. Overwrite all elements of array with cryptographically strong random values of the appropriate type. diff --git a/Userland/Libraries/LibWeb/Crypto/Crypto.h b/Userland/Libraries/LibWeb/Crypto/Crypto.h index dececef8b5..40fba4f071 100644 --- a/Userland/Libraries/LibWeb/Crypto/Crypto.h +++ b/Userland/Libraries/LibWeb/Crypto/Crypto.h @@ -16,7 +16,7 @@ class Crypto : public Bindings::PlatformObject { WEB_PLATFORM_OBJECT(Crypto, Bindings::PlatformObject); public: - static JS::NonnullGCPtr<Crypto> create(HTML::Window&); + static JS::NonnullGCPtr<Crypto> create(JS::Realm&); virtual ~Crypto() override; @@ -29,7 +29,7 @@ protected: virtual void visit_edges(Cell::Visitor&) override; private: - explicit Crypto(HTML::Window&); + explicit Crypto(JS::Realm&); virtual void initialize(JS::Realm&) override; JS::GCPtr<SubtleCrypto> m_subtle; diff --git a/Userland/Libraries/LibWeb/Crypto/SubtleCrypto.cpp b/Userland/Libraries/LibWeb/Crypto/SubtleCrypto.cpp index 0c67a0bc40..4fd53936d8 100644 --- a/Userland/Libraries/LibWeb/Crypto/SubtleCrypto.cpp +++ b/Userland/Libraries/LibWeb/Crypto/SubtleCrypto.cpp @@ -7,37 +7,37 @@ #include <LibCrypto/Hash/HashManager.h> #include <LibJS/Runtime/ArrayBuffer.h> #include <LibJS/Runtime/Promise.h> -#include <LibWeb/Bindings/MainThreadVM.h> +#include <LibWeb/Bindings/Intrinsics.h> #include <LibWeb/Crypto/SubtleCrypto.h> #include <LibWeb/WebIDL/AbstractOperations.h> #include <LibWeb/WebIDL/DOMException.h> namespace Web::Crypto { -JS::NonnullGCPtr<SubtleCrypto> SubtleCrypto::create(HTML::Window& window) +JS::NonnullGCPtr<SubtleCrypto> SubtleCrypto::create(JS::Realm& realm) { - return *window.heap().allocate<SubtleCrypto>(window.realm(), window); + return *realm.heap().allocate<SubtleCrypto>(realm, realm); } -SubtleCrypto::SubtleCrypto(HTML::Window& window) - : PlatformObject(window.realm()) +SubtleCrypto::SubtleCrypto(JS::Realm& realm) + : PlatformObject(realm) { - set_prototype(&window.cached_web_prototype("SubtleCrypto")); + set_prototype(&Bindings::cached_web_prototype(realm, "SubtleCrypto")); } SubtleCrypto::~SubtleCrypto() = default; +// https://w3c.github.io/webcrypto/#dfn-SubtleCrypto-method-digest JS::Promise* SubtleCrypto::digest(String const& algorithm, JS::Handle<JS::Object> const& data) { - auto& vm = Bindings::main_thread_vm(); - auto& realm = *vm.current_realm(); + auto& realm = this->realm(); // 1. Let algorithm be the algorithm parameter passed to the digest() method. // 2. Let data be the result of getting a copy of the bytes held by the data parameter passed to the digest() method. auto data_buffer_or_error = WebIDL::get_buffer_source_copy(*data.cell()); if (data_buffer_or_error.is_error()) { - auto error = WebIDL::OperationError::create(global_object(), "Failed to copy bytes from ArrayBuffer"); + auto error = WebIDL::OperationError::create(realm, "Failed to copy bytes from ArrayBuffer"); auto* promise = JS::Promise::create(realm); promise->reject(error.ptr()); return promise; @@ -58,7 +58,7 @@ JS::Promise* SubtleCrypto::digest(String const& algorithm, JS::Handle<JS::Object } // 4. If an error occurred, return a Promise rejected with normalizedAlgorithm. else { - auto error = WebIDL::NotSupportedError::create(global_object(), String::formatted("Invalid hash function '{}'", algorithm)); + auto error = WebIDL::NotSupportedError::create(realm, String::formatted("Invalid hash function '{}'", algorithm)); auto* promise = JS::Promise::create(realm); promise->reject(error.ptr()); return promise; @@ -79,7 +79,7 @@ JS::Promise* SubtleCrypto::digest(String const& algorithm, JS::Handle<JS::Object auto digest = hash.digest(); auto result_buffer = ByteBuffer::copy(digest.immutable_data(), hash.digest_size()); if (result_buffer.is_error()) { - auto error = WebIDL::OperationError::create(global_object(), "Failed to create result buffer"); + auto error = WebIDL::OperationError::create(realm, "Failed to create result buffer"); promise->reject(error.ptr()); return promise; } diff --git a/Userland/Libraries/LibWeb/Crypto/SubtleCrypto.h b/Userland/Libraries/LibWeb/Crypto/SubtleCrypto.h index 8ac7df8c27..9099500eb6 100644 --- a/Userland/Libraries/LibWeb/Crypto/SubtleCrypto.h +++ b/Userland/Libraries/LibWeb/Crypto/SubtleCrypto.h @@ -15,14 +15,14 @@ class SubtleCrypto final : public Bindings::PlatformObject { WEB_PLATFORM_OBJECT(SubtleCrypto, Bindings::PlatformObject); public: - static JS::NonnullGCPtr<SubtleCrypto> create(HTML::Window&); + static JS::NonnullGCPtr<SubtleCrypto> create(JS::Realm&); virtual ~SubtleCrypto() override; JS::Promise* digest(String const& algorithm, JS::Handle<JS::Object> const& data); private: - explicit SubtleCrypto(HTML::Window&); + explicit SubtleCrypto(JS::Realm&); }; } |