Bug 1264508 - Use generic helpers for converting ArrayBuffer or BufferSource to/from nsTArray in Push API code. r=webidl,saschanaz

Differential Revision: https://phabricator.services.mozilla.com/D207176
This commit is contained in:
Peter Van der Beken 2024-04-27 12:57:32 +00:00
parent 2ff3b0c056
commit 7f6c90ecc5
9 changed files with 27 additions and 108 deletions

View file

@ -8937,10 +8937,12 @@ def wrapTypeIntoCurrentCompartment(type, value, isMember=True):
return CGList(memberWraps) if len(memberWraps) != 0 else None
if type.isUnion():
memberWraps = []
origValue = value
origType = type
if type.nullable():
type = type.inner
value = "%s.Value()" % value
memberWraps = []
for member in type.flatMemberTypes:
memberName = getUnionMemberName(member)
memberWrap = wrapTypeIntoCurrentCompartment(
@ -8949,7 +8951,12 @@ def wrapTypeIntoCurrentCompartment(type, value, isMember=True):
if memberWrap:
memberWrap = CGIfWrapper(memberWrap, "%s.Is%s()" % (value, memberName))
memberWraps.append(memberWrap)
return CGList(memberWraps, "else ") if len(memberWraps) != 0 else None
if len(memberWraps) == 0:
return None
wrapCode = CGList(memberWraps, "else ")
if origType.nullable():
wrapCode = CGIfWrapper(wrapCode, "!%s.IsNull()" % origValue)
return wrapCode
if (
type.isUndefined()

View file

@ -14,7 +14,6 @@
#include "mozilla/dom/PushManagerBinding.h"
#include "mozilla/dom/PushSubscription.h"
#include "mozilla/dom/PushSubscriptionOptionsBinding.h"
#include "mozilla/dom/PushUtil.h"
#include "mozilla/dom/RootedDictionary.h"
#include "mozilla/dom/ServiceWorker.h"
#include "mozilla/dom/WorkerRunnable.h"

View file

@ -17,7 +17,7 @@
#include "mozilla/dom/Promise.h"
#include "mozilla/dom/PromiseWorkerProxy.h"
#include "mozilla/dom/PushSubscriptionOptions.h"
#include "mozilla/dom/PushUtil.h"
#include "mozilla/dom/TypedArray.h"
#include "mozilla/dom/WorkerCommon.h"
#include "mozilla/dom/WorkerPrivate.h"
#include "mozilla/dom/WorkerRunnable.h"
@ -230,30 +230,24 @@ already_AddRefed<PushSubscription> PushSubscription::Constructor(
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aGlobal.GetAsSupports());
nsTArray<uint8_t> rawKey;
if (aInitDict.mP256dhKey.WasPassed() &&
!aInitDict.mP256dhKey.Value().IsNull() &&
!aInitDict.mP256dhKey.Value().Value().AppendDataTo(rawKey)) {
if (!aInitDict.mP256dhKey.IsNull() &&
!aInitDict.mP256dhKey.Value().AppendDataTo(rawKey)) {
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
return nullptr;
}
nsTArray<uint8_t> authSecret;
if (aInitDict.mAuthSecret.WasPassed() &&
!aInitDict.mAuthSecret.Value().IsNull() &&
!aInitDict.mAuthSecret.Value().Value().AppendDataTo(authSecret)) {
if (!aInitDict.mAuthSecret.IsNull() &&
!aInitDict.mAuthSecret.Value().AppendDataTo(authSecret)) {
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
return nullptr;
}
nsTArray<uint8_t> appServerKey;
if (aInitDict.mAppServerKey.WasPassed() &&
!aInitDict.mAppServerKey.Value().IsNull()) {
const OwningArrayBufferViewOrArrayBuffer& bufferSource =
aInitDict.mAppServerKey.Value().Value();
if (!PushUtil::CopyBufferSourceToArray(bufferSource, appServerKey)) {
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
return nullptr;
}
if (!aInitDict.mAppServerKey.IsNull() &&
!AppendTypedArrayDataTo(aInitDict.mAppServerKey.Value(), appServerKey)) {
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
return nullptr;
}
Nullable<EpochTimeStamp> expirationTime;
@ -307,10 +301,10 @@ already_AddRefed<Promise> PushSubscription::Unsubscribe(ErrorResult& aRv) {
void PushSubscription::GetKey(JSContext* aCx, PushEncryptionKeyName aType,
JS::MutableHandle<JSObject*> aKey,
ErrorResult& aRv) {
if (aType == PushEncryptionKeyName::P256dh) {
PushUtil::CopyArrayToArrayBuffer(aCx, mRawP256dhKey, aKey, aRv);
} else if (aType == PushEncryptionKeyName::Auth) {
PushUtil::CopyArrayToArrayBuffer(aCx, mAuthSecret, aKey, aRv);
if (aType == PushEncryptionKeyName::P256dh && !mRawP256dhKey.IsEmpty()) {
aKey.set(ArrayBuffer::Create(aCx, mRawP256dhKey, aRv));
} else if (aType == PushEncryptionKeyName::Auth && !mAuthSecret.IsEmpty()) {
aKey.set(ArrayBuffer::Create(aCx, mAuthSecret, aRv));
} else {
aKey.set(nullptr);
}

View file

@ -8,9 +8,9 @@
#include "MainThreadUtils.h"
#include "mozilla/dom/PushSubscriptionOptionsBinding.h"
#include "mozilla/dom/TypedArray.h"
#include "mozilla/ErrorResult.h"
#include "mozilla/HoldDropJSObjects.h"
#include "mozilla/dom/PushUtil.h"
#include "nsIGlobalObject.h"
#include "nsWrapperCache.h"
@ -51,13 +51,10 @@ JSObject* PushSubscriptionOptions::WrapObject(
void PushSubscriptionOptions::GetApplicationServerKey(
JSContext* aCx, JS::MutableHandle<JSObject*> aKey, ErrorResult& aRv) {
if (!mRawAppServerKey.IsEmpty() && !mAppServerKey) {
JS::Rooted<JSObject*> appServerKey(aCx);
PushUtil::CopyArrayToArrayBuffer(aCx, mRawAppServerKey, &appServerKey, aRv);
mAppServerKey = ArrayBuffer::Create(aCx, mRawAppServerKey, aRv);
if (aRv.Failed()) {
return;
}
MOZ_ASSERT(appServerKey);
mAppServerKey = appServerKey;
}
aKey.set(mAppServerKey);
}

View file

@ -1,36 +0,0 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "mozilla/dom/PushUtil.h"
#include "mozilla/dom/UnionTypes.h"
namespace mozilla::dom {
/* static */
bool PushUtil::CopyBufferSourceToArray(
const OwningArrayBufferViewOrArrayBuffer& aSource,
nsTArray<uint8_t>& aArray) {
MOZ_ASSERT(aArray.IsEmpty());
return AppendTypedArrayDataTo(aSource, aArray);
}
/* static */
void PushUtil::CopyArrayToArrayBuffer(JSContext* aCx,
const nsTArray<uint8_t>& aArray,
JS::MutableHandle<JSObject*> aValue,
ErrorResult& aRv) {
if (aArray.IsEmpty()) {
aValue.set(nullptr);
return;
}
JS::Rooted<JSObject*> buffer(aCx, ArrayBuffer::Create(aCx, aArray, aRv));
if (NS_WARN_IF(aRv.Failed())) {
return;
}
aValue.set(buffer);
}
} // namespace mozilla::dom

View file

@ -1,39 +0,0 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef mozilla_dom_PushUtil_h
#define mozilla_dom_PushUtil_h
#include "nsTArray.h"
#include "mozilla/dom/TypedArray.h"
namespace mozilla {
class ErrorResult;
namespace dom {
class OwningArrayBufferViewOrArrayBuffer;
class PushUtil final {
private:
PushUtil() = delete;
public:
static bool CopyBufferSourceToArray(
const OwningArrayBufferViewOrArrayBuffer& aSource,
nsTArray<uint8_t>& aArray);
static void CopyArrayToArrayBuffer(JSContext* aCx,
const nsTArray<uint8_t>& aArray,
JS::MutableHandle<JSObject*> aValue,
ErrorResult& aRv);
};
} // namespace dom
} // namespace mozilla
#endif // mozilla_dom_PushUtil_h

View file

@ -44,7 +44,6 @@ EXPORTS.mozilla.dom += [
"PushNotifier.h",
"PushSubscription.h",
"PushSubscriptionOptions.h",
"PushUtil.h",
]
UNIFIED_SOURCES += [
@ -52,7 +51,6 @@ UNIFIED_SOURCES += [
"PushNotifier.cpp",
"PushSubscription.cpp",
"PushSubscriptionOptions.cpp",
"PushUtil.cpp",
]
TEST_DIRS += ["test/xpcshell"]

View file

@ -26,7 +26,6 @@
#include "mozilla/dom/PromiseNativeHandler.h"
#include "mozilla/dom/PushEventBinding.h"
#include "mozilla/dom/PushMessageDataBinding.h"
#include "mozilla/dom/PushUtil.h"
#include "mozilla/dom/Request.h"
#include "mozilla/dom/Response.h"
#include "mozilla/dom/ServiceWorkerOp.h"

View file

@ -35,9 +35,9 @@ dictionary PushSubscriptionInit
{
required USVString endpoint;
required USVString scope;
ArrayBuffer? p256dhKey;
ArrayBuffer? authSecret;
BufferSource? appServerKey;
ArrayBuffer? p256dhKey = null;
ArrayBuffer? authSecret = null;
BufferSource? appServerKey = null;
EpochTimeStamp? expirationTime = null;
};