LibWeb: Add the SubtleCrypto interface

Just some boilerplate code to get started :^)
This adds both the SubtleCrypto constructor to the window object, as
well as the crypto.subtle instance attribute.
This commit is contained in:
Linus Groh 2021-12-13 20:48:27 +00:00 committed by Andreas Kling
parent ce6c515873
commit 615be9eb7c
Notes: sideshowbarker 2024-07-17 22:48:09 +09:00
8 changed files with 57 additions and 2 deletions

View file

@ -2798,6 +2798,7 @@ void generate_prototype_implementation(IDL::Interface const& interface)
#include <LibWeb/Bindings/PerformanceTimingWrapper.h>
#include <LibWeb/Bindings/RangeWrapper.h>
#include <LibWeb/Bindings/StyleSheetListWrapper.h>
#include <LibWeb/Bindings/SubtleCryptoWrapper.h>
#include <LibWeb/Bindings/TextWrapper.h>
#include <LibWeb/Bindings/URLSearchParamsWrapper.h>
#include <LibWeb/Bindings/WindowObject.h>
@ -2886,6 +2887,7 @@ void generate_prototype_implementation(IDL::Interface const& interface)
generator.append(R"~~~(
// FIXME: This is a total hack until we can figure out the namespace for a given type somehow.
using namespace Web::Crypto;
using namespace Web::CSS;
using namespace Web::DOM;
using namespace Web::Geometry;

View file

@ -261,6 +261,8 @@
#include <LibWeb/Bindings/StyleSheetPrototype.h>
#include <LibWeb/Bindings/SubmitEventConstructor.h>
#include <LibWeb/Bindings/SubmitEventPrototype.h>
#include <LibWeb/Bindings/SubtleCryptoConstructor.h>
#include <LibWeb/Bindings/SubtleCryptoPrototype.h>
#include <LibWeb/Bindings/TextConstructor.h>
#include <LibWeb/Bindings/TextEncoderConstructor.h>
#include <LibWeb/Bindings/TextEncoderPrototype.h>
@ -412,6 +414,7 @@
ADD_WINDOW_OBJECT_INTERFACE(StyleSheet) \
ADD_WINDOW_OBJECT_INTERFACE(StyleSheetList) \
ADD_WINDOW_OBJECT_INTERFACE(SubmitEvent) \
ADD_WINDOW_OBJECT_INTERFACE(SubtleCrypto) \
ADD_WINDOW_OBJECT_INTERFACE(SVGElement) \
ADD_WINDOW_OBJECT_INTERFACE(SVGGeometryElement) \
ADD_WINDOW_OBJECT_INTERFACE(SVGGraphicsElement) \

View file

@ -8,9 +8,15 @@
#include <LibJS/Runtime/TypedArray.h>
#include <LibWeb/Bindings/Wrapper.h>
#include <LibWeb/Crypto/Crypto.h>
#include <LibWeb/Crypto/SubtleCrypto.h>
namespace Web::Crypto {
Crypto::Crypto()
: m_subtle(SubtleCrypto::create())
{
}
DOM::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.

View file

@ -8,6 +8,7 @@
#include <LibJS/Runtime/Value.h>
#include <LibWeb/Bindings/Wrappable.h>
#include <LibWeb/Crypto/SubtleCrypto.h>
#include <LibWeb/DOM/ExceptionOr.h>
namespace Web::Crypto {
@ -23,10 +24,14 @@ public:
return adopt_ref(*new Crypto());
}
NonnullRefPtr<SubtleCrypto> subtle() const { return m_subtle; }
DOM::ExceptionOr<JS::Value> get_random_values(JS::Value array) const;
private:
Crypto() = default;
Crypto();
NonnullRefPtr<SubtleCrypto> m_subtle;
};
}

View file

@ -1,6 +1,6 @@
[Exposed=(Window,Worker)]
interface Crypto {
// TODO: [SecureContext] readonly attribute SubtleCrypto subtle;
[SecureContext] readonly attribute SubtleCrypto subtle;
// FIXME: the argument and the return value should be of type ArrayBufferView
any getRandomValues(any array);

View file

@ -0,0 +1,34 @@
/*
* Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibWeb/Bindings/Wrappable.h>
namespace Web::Crypto {
class SubtleCrypto
: public Bindings::Wrappable
, public RefCounted<SubtleCrypto> {
public:
using WrapperType = Bindings::SubtleCryptoWrapper;
static NonnullRefPtr<SubtleCrypto> create()
{
return adopt_ref(*new SubtleCrypto());
}
private:
SubtleCrypto() = default;
};
}
namespace Web::Bindings {
SubtleCryptoWrapper* wrap(JS::GlobalObject&, Crypto::SubtleCrypto&);
}

View file

@ -0,0 +1,3 @@
[SecureContext,Exposed=(Window,Worker)]
interface SubtleCrypto {
};

View file

@ -15,6 +15,7 @@ enum class Source;
namespace Web::Crypto {
class Crypto;
class SubtleCrypto;
}
namespace Web::CSS {
@ -431,6 +432,7 @@ class SelectionWrapper;
class StyleSheetListWrapper;
class StyleSheetWrapper;
class SubmitEventWrapper;
class SubtleCryptoWrapper;
class SVGElementWrapper;
class SVGGeometryElementWrapper;
class SVGGraphicsElementWrapper;