summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMoonchild <moonchild@palemoon.org>2023-10-25 20:48:01 +0000
committerMoonchild <moonchild@palemoon.org>2023-10-25 20:48:01 +0000
commita653f74576ccf7e6dd26f4af63a7ca238f63dbc3 (patch)
tree863676ae9a157993a74124aa80634282eab0a12a
parentccb41c645cac3b3dd183c06099433143650c7431 (diff)
parent3d92b8212595769c40da7ccfbafb3898cfaaf7bd (diff)
downloaduxp-a653f74576ccf7e6dd26f4af63a7ca238f63dbc3.tar.gz
Merge pull request 'No Issue - Implement crypto.randomUUID' (#2359) from Basilisk-Dev/UXP-contrib:crypto-randomuuid into master
Reviewed-on: https://repo.palemoon.org/MoonchildProductions/UXP/pulls/2359
-rw-r--r--dom/base/Crypto.cpp21
-rw-r--r--dom/base/Crypto.h3
-rw-r--r--dom/webidl/Crypto.webidl3
3 files changed, 27 insertions, 0 deletions
diff --git a/dom/base/Crypto.cpp b/dom/base/Crypto.cpp
index 863c26c902..2c72fb6692 100644
--- a/dom/base/Crypto.cpp
+++ b/dom/base/Crypto.cpp
@@ -115,6 +115,27 @@ Crypto::GetRandomValues(JSContext* aCx, const ArrayBufferView& aArray,
aRetval.set(view);
}
+void Crypto::RandomUUID(nsAString& aRetVal)
+{
+ // NSID_LENGTH == 39 == 36 UUID chars + 2 curly braces + 1 NUL byte
+ static_assert(NSID_LENGTH == 39);
+
+ nsCOMPtr<nsIUUIDGenerator> uuidgen =
+ do_GetService("@mozilla.org/uuid-generator;1");
+
+ nsID uuid;
+
+ nsresult rv = uuidgen->GenerateUUIDInPlace(&uuid);
+
+ char uuidBuffer[NSID_LENGTH];
+ uuid.ToProvidedString(uuidBuffer);
+
+ nsCString asciiString(uuidBuffer);
+
+ // Convert UUID chars to UTF-16 retval, omitting the curly braces and NUL
+ CopyASCIItoUTF16(Substring(asciiString, 1, NSID_LENGTH - 3), aRetVal);
+}
+
SubtleCrypto*
Crypto::Subtle()
{
diff --git a/dom/base/Crypto.h b/dom/base/Crypto.h
index 318eba5592..2893d299c0 100644
--- a/dom/base/Crypto.h
+++ b/dom/base/Crypto.h
@@ -9,6 +9,7 @@
#include "mozilla/dom/SubtleCrypto.h"
#include "nsIGlobalObject.h"
+#include "nsString.h"
#include "nsWrapperCache.h"
#include "mozilla/dom/TypedArray.h"
#define NS_DOMCRYPTO_CID \
@@ -39,6 +40,8 @@ public:
JS::MutableHandle<JSObject*> aRetval,
ErrorResult& aRv);
+void RandomUUID(nsAString& aRetVal);
+
SubtleCrypto*
Subtle();
diff --git a/dom/webidl/Crypto.webidl b/dom/webidl/Crypto.webidl
index 31574a219c..2ef7b5d21d 100644
--- a/dom/webidl/Crypto.webidl
+++ b/dom/webidl/Crypto.webidl
@@ -18,4 +18,7 @@ interface Crypto {
[Throws]
ArrayBufferView getRandomValues(ArrayBufferView array);
+
+ [SecureContext]
+ DOMString randomUUID();
};