PushSubscriptionOptions.cpp (2192B)
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/. */ 6 7 #include "mozilla/dom/PushSubscriptionOptions.h" 8 9 #include "MainThreadUtils.h" 10 #include "mozilla/ErrorResult.h" 11 #include "mozilla/HoldDropJSObjects.h" 12 #include "mozilla/dom/PushSubscriptionOptionsBinding.h" 13 #include "mozilla/dom/TypedArray.h" 14 #include "nsIGlobalObject.h" 15 #include "nsWrapperCache.h" 16 17 namespace mozilla::dom { 18 19 PushSubscriptionOptions::PushSubscriptionOptions( 20 nsIGlobalObject* aGlobal, nsTArray<uint8_t>&& aRawAppServerKey) 21 : mGlobal(aGlobal), 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); 28 } 29 30 PushSubscriptionOptions::~PushSubscriptionOptions() { 31 mozilla::DropJSObjects(this); 32 } 33 34 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_WITH_JS_MEMBERS(PushSubscriptionOptions, 35 (mGlobal), 36 (mAppServerKey)) 37 38 NS_IMPL_CYCLE_COLLECTING_ADDREF(PushSubscriptionOptions) 39 NS_IMPL_CYCLE_COLLECTING_RELEASE(PushSubscriptionOptions) 40 41 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(PushSubscriptionOptions) 42 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY 43 NS_INTERFACE_MAP_ENTRY(nsISupports) 44 NS_INTERFACE_MAP_END 45 46 JSObject* PushSubscriptionOptions::WrapObject( 47 JSContext* aCx, JS::Handle<JSObject*> aGivenProto) { 48 return PushSubscriptionOptions_Binding::Wrap(aCx, this, aGivenProto); 49 } 50 51 void PushSubscriptionOptions::GetApplicationServerKey( 52 JSContext* aCx, JS::MutableHandle<JSObject*> aKey, ErrorResult& aRv) { 53 if (!mRawAppServerKey.IsEmpty() && !mAppServerKey) { 54 mAppServerKey = ArrayBuffer::Create(aCx, mRawAppServerKey, aRv); 55 if (aRv.Failed()) { 56 return; 57 } 58 } 59 aKey.set(mAppServerKey); 60 } 61 62 } // namespace mozilla::dom