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
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "mozilla/dom/PushSubscriptionOptions.h"
9 #include "MainThreadUtils.h"
10 #include "mozilla/dom/PushSubscriptionOptionsBinding.h"
11 #include "mozilla/ErrorResult.h"
12 #include "mozilla/HoldDropJSObjects.h"
13 #include "mozilla/dom/PushUtil.h"
14 #include "nsIGlobalObject.h"
19 PushSubscriptionOptions::PushSubscriptionOptions(
20 nsIGlobalObject
* aGlobal
, nsTArray
<uint8_t>&& aRawAppServerKey
)
22 mRawAppServerKey(std::move(aRawAppServerKey
)),
23 mAppServerKey(nullptr) {
24 // There's only one global on a worker, so we don't need to pass a global
25 // object to the constructor.
26 MOZ_ASSERT_IF(NS_IsMainThread(), mGlobal
);
27 mozilla::HoldJSObjects(this);
30 PushSubscriptionOptions::~PushSubscriptionOptions() {
31 mAppServerKey
= nullptr;
32 mozilla::DropJSObjects(this);
35 NS_IMPL_CYCLE_COLLECTION_CLASS(PushSubscriptionOptions
)
36 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(PushSubscriptionOptions
)
37 NS_IMPL_CYCLE_COLLECTION_UNLINK(mGlobal
)
38 NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
39 tmp
->mAppServerKey
= nullptr;
40 NS_IMPL_CYCLE_COLLECTION_UNLINK_END
41 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(PushSubscriptionOptions
)
42 NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mGlobal
)
43 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
44 NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(PushSubscriptionOptions
)
45 NS_IMPL_CYCLE_COLLECTION_TRACE_PRESERVED_WRAPPER
46 NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mAppServerKey
)
47 NS_IMPL_CYCLE_COLLECTION_TRACE_END
49 NS_IMPL_CYCLE_COLLECTING_ADDREF(PushSubscriptionOptions
)
50 NS_IMPL_CYCLE_COLLECTING_RELEASE(PushSubscriptionOptions
)
52 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(PushSubscriptionOptions
)
53 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
54 NS_INTERFACE_MAP_ENTRY(nsISupports
)
57 JSObject
* PushSubscriptionOptions::WrapObject(
58 JSContext
* aCx
, JS::Handle
<JSObject
*> aGivenProto
) {
59 return PushSubscriptionOptions_Binding::Wrap(aCx
, this, aGivenProto
);
62 void PushSubscriptionOptions::GetApplicationServerKey(
63 JSContext
* aCx
, JS::MutableHandle
<JSObject
*> aKey
, ErrorResult
& aRv
) {
64 if (!mRawAppServerKey
.IsEmpty() && !mAppServerKey
) {
65 JS::Rooted
<JSObject
*> appServerKey(aCx
);
66 PushUtil::CopyArrayToArrayBuffer(aCx
, mRawAppServerKey
, &appServerKey
, aRv
);
70 MOZ_ASSERT(appServerKey
);
71 mAppServerKey
= appServerKey
;
73 aKey
.set(mAppServerKey
);
77 } // namespace mozilla