1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "jsfriendapi.h"
8 #include "js/experimental/TypedData.h" // JS_GetArrayBufferViewType
10 #include "nsIRandomGenerator.h"
11 #include "MainThreadUtils.h"
12 #include "nsXULAppAPI.h"
14 #include "mozilla/dom/ContentChild.h"
15 #include "mozilla/dom/CryptoBinding.h"
16 #include "nsServiceManagerUtils.h"
18 using mozilla::dom::ContentChild
;
23 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(Crypto
)
24 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
25 NS_INTERFACE_MAP_ENTRY(nsISupports
)
28 NS_IMPL_CYCLE_COLLECTING_ADDREF(Crypto
)
29 NS_IMPL_CYCLE_COLLECTING_RELEASE(Crypto
)
31 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(Crypto
, mParent
, mSubtle
)
33 Crypto::Crypto(nsIGlobalObject
* aParent
) : mParent(aParent
) {}
35 Crypto::~Crypto() = default;
38 JSObject
* Crypto::WrapObject(JSContext
* aCx
,
39 JS::Handle
<JSObject
*> aGivenProto
) {
40 return Crypto_Binding::Wrap(aCx
, this, aGivenProto
);
43 void Crypto::GetRandomValues(JSContext
* aCx
, const ArrayBufferView
& aArray
,
44 JS::MutableHandle
<JSObject
*> aRetval
,
46 JS::Rooted
<JSObject
*> view(aCx
, aArray
.Obj());
48 // Throw if the wrong type of ArrayBufferView is passed in
49 // (Part of the Web Crypto API spec)
50 switch (JS_GetArrayBufferViewType(view
)) {
51 case js::Scalar::Int8
:
52 case js::Scalar::Uint8
:
53 case js::Scalar::Uint8Clamped
:
54 case js::Scalar::Int16
:
55 case js::Scalar::Uint16
:
56 case js::Scalar::Int32
:
57 case js::Scalar::Uint32
:
60 aRv
.Throw(NS_ERROR_DOM_TYPE_MISMATCH_ERR
);
64 aArray
.ComputeState();
65 uint32_t dataLen
= aArray
.Length();
67 NS_WARNING("ArrayBufferView length is 0, cannot continue");
70 } else if (dataLen
> 65536) {
71 aRv
.Throw(NS_ERROR_DOM_QUOTA_EXCEEDED_ERR
);
75 nsCOMPtr
<nsIRandomGenerator
> randomGenerator
=
76 do_GetService("@mozilla.org/security/random-generator;1");
77 if (!randomGenerator
) {
78 aRv
.Throw(NS_ERROR_DOM_OPERATION_ERR
);
83 nsresult rv
= randomGenerator
->GenerateRandomBytes(dataLen
, &buf
);
84 if (NS_FAILED(rv
) || !buf
) {
85 aRv
.Throw(NS_ERROR_DOM_OPERATION_ERR
);
89 // Copy random bytes to ABV.
90 memcpy(aArray
.Data(), buf
, dataLen
);
96 SubtleCrypto
* Crypto::Subtle() {
98 mSubtle
= new SubtleCrypto(GetParentObject());
104 } // namespace mozilla