mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 23:47:45 +00:00
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.
This commit is contained in:
parent
4878a18ee7
commit
4bb6345b2f
30 changed files with 125 additions and 126 deletions
|
@ -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.
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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&);
|
||||
};
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue