mirror of
https://github.com/roytam1/palemoon27.git
synced 2026-05-26 14:18:48 +00:00
1dfd83ddde
- Bug 1225603. Codegen ToJSValue overloads for Web IDL enums. r=smaug,jib (8c21e0b148) - Bug 1194978 - Renable RequestSync tests in b2g, r=nsm (e99a2fb4f5) - Bug 1182358 - patch 2 - RequestSyncService.jsm should not write debug messages (1fcc99d65c) - Bug 1147804 - Facebook auto sync will not happen if you change the time 24 hours later. r=fabrice (936b25f586) - Bug 1181489 - Enable debug for AlarmService.jsm. r=fabrice (a92673c2f4) - Bug 1181489 - Adding sanity checks into the alarm service for preventing from the alarm works erroneously. r=fabrice (dd75f12425) - Bug 1213169 - requestsync assumes all in-memory mozAlarms will never be purged (and these alarms get persisted anyways), but they are purged on timezone/clock changes, r=asuth (00924ed778) - Bug 1147804 followup: Add missing 'override' annotation to AlarmHalService::Notify declaration. rs=ehsan (8e797f2a9a) - Bug 1211469 - JavaScript error: resource://gre/modules/RequestSyncService.jsm, line 104: TypeError: this.addRegistration is not a function. r=baku (bbcefdb093) - Bug 1216002 - "JavaScript error: resource://gre/modules/RequestSyncService.jsm, line 228: TypeError: 'continue' called on an object that does not implement interface IDBCursor." r=bz (153d8a610b) - Bug 1226708 - part 1 - Use importInMainProcess in requestsync tests. r=baku (7638d77db3) - Bug 1140275 - System messages shouldn't be sent to app pages not registered in manifests. r=fabrice (3e0d7ae440) - var-let (e74f0b4a2c) - Bug 1174683 - [Secure Element] Fix TypeError in UiccConnector.unregisterListener. r=allstars.chh (9dd333d909) - Bug 1156710 - [Secure Element] Allow SE access without full ACE checks if certified apps debug is enabled. r=allstars.chh (f1d0b8d5b0) - Bug 1216822 - Make sure that using an invalid Content-Type when constructing a Response object doesn't throw; r=bkelly (232af6f665) - Bug 1227030 - Change log module to LazyLogModule with a more self-explantory name. r=seanlin (17f85691fb) - Bug 1225873 - micro-optimize creating event target chains; r=smaug (a38d507a61) - Bug 1194525 - Gecko should ignore |postResult| calls for WebActivities with no returnValue. r=fabrice,sicking (1549718723) - Bug 1174071 - Remove 'required' keyword for Bluetooth*EventInit dictionary members. r=btian, r=bz (f8c02ed9e3) - Bug 1181483: Implement GATT server characteristic notification; r=jocelyn, r=mrbkap (d5170b650e) - Bug 1215525: Replace strings with Bluetooth addresses and UUIDs in GATT mid-layer, r=joliu (5949fa2a39) - Bug 1223720: Support UUIDs and addresses for Bluetooth signal paths, r=joliu (1d98490eac) - Bug 1217778 - Ensure sBluetoothGattService is not null before accessing GattInterface in GattManager. r=jocelyn (40c4cde2b3) - Bug 1146355: Update Bluetooth backend interface for bluetooth2, r=brsun (f384d2f1dc) - Bug 1211948: Introduce |enum BluetoothSetupServiceId|, r=brsun (2c96581c45) - fix (3de8d6c033) - Bug 1211948: Register Bluetooth GATT module in GATT manager, r=joliu (491d1e0408) - Bug 1209469: Expose |BluetoothPropertyType| in Bluetooth backend interface, r=brsun (027a57df73) - Bug 1209469: Replace |BluetoothNamedValue| with |BluetoothProperty| in Bluetooth backend, r=brsun (c14311d277) - Bug 1207649: Convert Bluetooth AVRCP backend to |BluetoothAddress|, r=shuang (0acc2e7f51) - Bug 1223806: Add Bluetooth Core interface, notification and result handler, r=btian (d6ca2c4beb) - Bug1209085: Replace simple init ops by |UnpackInitOp| in Bluetooth Core backend, r=joliu (a37e6cc579) - Bug 1223806: Convert Bluetooth to |BluetoothCoreNotificationHandler|, r=btian (a85b9302f5) - Bug 1220121: Convert IPDL of Bluetooth Core API to |BluetoothAddress|, r=brsun (d105f3eab0) - Bug 1220121: Convert IPDL of Bluetooth Core API to |BluetoothPinCode|, r=brsun (c3a2b4f30a) - Bug 1220121: Convert IPDL of Bluetooth OPP API to |BluetoothAddress|, r=btian (560c056efc) - Bug 1220121: Convert IPDL of Bluetooth AVRCP API to |ControlPlayStatus|, r=shuang (0aaac4c9c8) - Bug 1215525: Use strong typing in Bluetooth GATT mid-layer interfaces, r=joliu (7ad219094f) - Bug 1223806: Convert Bluetooth to |BluetoothCoreResultHandler|, r=btian (e2c96b7f90) - Bug 1220121: Convert IPDL of Bluetooth GATT API to |BluetoothAddress|, r=joliu (a426ffd63d) - Bug 1220121: Convert IPDL of Bluetooth GATT API to |BluetoothUuid|, r=joliu (2343063b18) - Bug 1225787: Fix wrong arguments of BluetoothGattInterface::AddCharacteristic; r=jocelyn (b19bb89fb3) - Bug 1228519 - Fix RequestReadNotification for GATT Server API. r=brsun (25cc876d88) - Bug 1225785: Fix WriteRequested mismatch; r=jocelyn (89d9b243a3) - Bug 1223806: Add |BluetoothDaemonCoreInterface|, r=btian (c6b2cd9fba) - Bug 1220121: Prepare IPDL support for additional Bluetooth types, r=brsun (8cb996570b) - Bug 1211948: Register Bluetooth Handsfree module in Handsfree manager, r=btian (af91aa226d) - Bug 1211435: Rename some internal classes of Bluetooth's A2DP manager, r=shuang (e834e2ee87) - Bug 1211948: Register Bluetooth A2DP module in A2DP manager, r=brsun (f9fcb0115f) - Bug 1211435: Rename some internal classes of Bluetooth's AVRCP manager, r=shuang (15a23ffe31) - Bug 1211948: Register Bluetooth AVRCP module in AVRCP manager, r=brsun (68e51afcca) - Bug 1223806: Convert Bluetooth to |BluetoothCoreInterface|, r=btian (7bff6267e1) - Bug 1186840 - [MAP] Implement MessageUpdate function, r=btian (6439c7497b) - Bug 1207998 - Convert nsCString to nsString for BluetoothVCardListingEvent.mSearchValue, r=shuang (37effbf4d2) - cleanup (721fb92ec3) - fix misspatch (5e94030fcd) - Bug 1230066 - Add missing STATUS_AUTH_REJECTED into enum BluetoothStatus,r=tzimmermann (17b45140a5) - Bug 1225340 - Refine nullity check macros in BluetoothServiceBluedroid, r=tzimmermann (5175c6eacf) - Bug 1227440 - Reject pin reply by cancelling bond to trigger BondStateChangedNotification, in order to inform gaia setting app, r=shuang (772401f943) - Bug1209085: Replace simple init ops by |UnpackPDUInitOp| in Bluetooth GATT backend, r=joliu (392d81748c) - Bug 1229290 - Pack/unpack UUIDs in a reverse order when adding characteristics and descriptors in GATT server API. r=brsun (850fb0eeb9) - Bug 1197815: Add missing |BluetoothHfpManager::IsNrecEnabled|, r=shuang (968089bef0) - Bug 1216179: Fix broken interface of fall-back Bluetooth HFP manager, r=btian (13b602a861) - Bug 1166469 - Add HandleBackendError for hfp-fallback product, r=btian (309dd1a92b) - Bug 1224240 - fix memory leak in Bluetooth*Manager::CompareHeaderTarget; r=btian (c6cfc95bac) - Bug 1224166: Build BluetoothCommon.cpp unconditionally, r=shuang (8efa02b83d) - Bug1209085: Replace trivial init ops by |UnpackPDUInitOP| in Bluetooth A2DP backend, r=joliu (4ccaab6bd6) - Bug 1142408 - Add data length parameter for Register Notification Response Command. r=tzimmermann (28706dcc16) - Bug 1228521 - Fix PDU packing in |ServerSendResponseCmd| for GATT Server API. r=brsun (290c3739c3) - Bug1209085: Replace trivial init ops by |UnpackPDUInitOp| in Bluetooth AVRCP backend, r=joliu (639f5b6143) - Bug 1199653 - Correctly set |sInShutdown| in BluetoothService for content processes. f=tzimmermann, r=shuang (5fed461709) - Bug 1224166: Define Bluetooth debug flag in BluetoothCommon.cpp, r=shuang (9602455405)
874 lines
23 KiB
C++
874 lines
23 KiB
C++
/* -*- 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/PushManager.h"
|
|
|
|
#include "mozilla/Preferences.h"
|
|
#include "mozilla/Services.h"
|
|
#include "mozilla/unused.h"
|
|
#include "mozilla/dom/PushManagerBinding.h"
|
|
#include "mozilla/dom/PushSubscriptionBinding.h"
|
|
#include "mozilla/dom/ServiceWorkerGlobalScopeBinding.h"
|
|
|
|
#include "mozilla/dom/Promise.h"
|
|
#include "mozilla/dom/PromiseWorkerProxy.h"
|
|
|
|
#include "nsIGlobalObject.h"
|
|
#include "nsIPermissionManager.h"
|
|
#include "nsIPrincipal.h"
|
|
#include "nsIPushClient.h"
|
|
|
|
#include "nsComponentManagerUtils.h"
|
|
#include "nsFrameMessageManager.h"
|
|
#include "nsContentCID.h"
|
|
|
|
#include "WorkerRunnable.h"
|
|
#include "WorkerPrivate.h"
|
|
#include "WorkerScope.h"
|
|
|
|
namespace mozilla {
|
|
namespace dom {
|
|
|
|
using namespace workers;
|
|
|
|
namespace {
|
|
|
|
nsresult
|
|
GetPermissionState(nsIPrincipal* aPrincipal,
|
|
PushPermissionState& aState)
|
|
{
|
|
nsCOMPtr<nsIPermissionManager> permManager =
|
|
mozilla::services::GetPermissionManager();
|
|
|
|
if (!permManager) {
|
|
return NS_ERROR_FAILURE;
|
|
}
|
|
uint32_t permission = nsIPermissionManager::UNKNOWN_ACTION;
|
|
nsresult rv = permManager->TestExactPermissionFromPrincipal(
|
|
aPrincipal,
|
|
"desktop-notification",
|
|
&permission);
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
return rv;
|
|
}
|
|
|
|
if (permission == nsIPermissionManager::ALLOW_ACTION) {
|
|
aState = PushPermissionState::Granted;
|
|
} else if (permission == nsIPermissionManager::DENY_ACTION) {
|
|
aState = PushPermissionState::Denied;
|
|
} else {
|
|
aState = PushPermissionState::Prompt;
|
|
}
|
|
return NS_OK;
|
|
}
|
|
|
|
} // anonymous namespace
|
|
|
|
class UnsubscribeResultCallback final : public nsIUnsubscribeResultCallback
|
|
{
|
|
public:
|
|
NS_DECL_ISUPPORTS
|
|
|
|
explicit UnsubscribeResultCallback(Promise* aPromise)
|
|
: mPromise(aPromise)
|
|
{
|
|
AssertIsOnMainThread();
|
|
}
|
|
|
|
NS_IMETHOD
|
|
OnUnsubscribe(nsresult aStatus, bool aSuccess) override
|
|
{
|
|
if (NS_SUCCEEDED(aStatus)) {
|
|
mPromise->MaybeResolve(aSuccess);
|
|
} else {
|
|
mPromise->MaybeReject(NS_ERROR_DOM_NETWORK_ERR);
|
|
}
|
|
|
|
return NS_OK;
|
|
}
|
|
|
|
private:
|
|
~UnsubscribeResultCallback()
|
|
{}
|
|
|
|
RefPtr<Promise> mPromise;
|
|
};
|
|
|
|
NS_IMPL_ISUPPORTS(UnsubscribeResultCallback, nsIUnsubscribeResultCallback)
|
|
|
|
already_AddRefed<Promise>
|
|
PushSubscription::Unsubscribe(ErrorResult& aRv)
|
|
{
|
|
MOZ_ASSERT(mPrincipal);
|
|
|
|
nsCOMPtr<nsIPushClient> client =
|
|
do_CreateInstance("@mozilla.org/push/PushClient;1");
|
|
if (NS_WARN_IF(!client)) {
|
|
aRv = NS_ERROR_FAILURE;
|
|
return nullptr;
|
|
}
|
|
|
|
RefPtr<Promise> p = Promise::Create(mGlobal, aRv);
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
return nullptr;
|
|
}
|
|
|
|
RefPtr<UnsubscribeResultCallback> callback =
|
|
new UnsubscribeResultCallback(p);
|
|
client->Unsubscribe(mScope, mPrincipal, callback);
|
|
return p.forget();
|
|
}
|
|
|
|
PushSubscription::PushSubscription(nsIGlobalObject* aGlobal,
|
|
const nsAString& aEndpoint,
|
|
const nsAString& aScope,
|
|
const nsTArray<uint8_t>& aRawP256dhKey,
|
|
const nsTArray<uint8_t>& aAuthSecret)
|
|
: mGlobal(aGlobal)
|
|
, mEndpoint(aEndpoint)
|
|
, mScope(aScope)
|
|
, mRawP256dhKey(aRawP256dhKey)
|
|
, mAuthSecret(aAuthSecret)
|
|
{
|
|
}
|
|
|
|
PushSubscription::~PushSubscription()
|
|
{
|
|
}
|
|
|
|
JSObject*
|
|
PushSubscription::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
|
|
{
|
|
return PushSubscriptionBinding::Wrap(aCx, this, aGivenProto);
|
|
}
|
|
|
|
void
|
|
PushSubscription::GetKey(JSContext* aCx,
|
|
PushEncryptionKeyName aType,
|
|
JS::MutableHandle<JSObject*> aKey)
|
|
{
|
|
if (aType == PushEncryptionKeyName::P256dh && !mRawP256dhKey.IsEmpty()) {
|
|
aKey.set(ArrayBuffer::Create(aCx,
|
|
mRawP256dhKey.Length(),
|
|
mRawP256dhKey.Elements()));
|
|
} else if (aType == PushEncryptionKeyName::Auth && !mAuthSecret.IsEmpty()) {
|
|
aKey.set(ArrayBuffer::Create(aCx,
|
|
mAuthSecret.Length(),
|
|
mAuthSecret.Elements()));
|
|
} else {
|
|
aKey.set(nullptr);
|
|
}
|
|
}
|
|
|
|
void
|
|
PushSubscription::SetPrincipal(nsIPrincipal* aPrincipal)
|
|
{
|
|
MOZ_ASSERT(!mPrincipal);
|
|
mPrincipal = aPrincipal;
|
|
}
|
|
|
|
// static
|
|
already_AddRefed<PushSubscription>
|
|
PushSubscription::Constructor(GlobalObject& aGlobal,
|
|
const nsAString& aEndpoint,
|
|
const nsAString& aScope,
|
|
const Nullable<ArrayBuffer>& aP256dhKey,
|
|
const Nullable<ArrayBuffer>& aAuthSecret,
|
|
ErrorResult& aRv)
|
|
{
|
|
MOZ_ASSERT(!aEndpoint.IsEmpty());
|
|
MOZ_ASSERT(!aScope.IsEmpty());
|
|
|
|
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aGlobal.GetAsSupports());
|
|
|
|
nsTArray<uint8_t> rawKey;
|
|
if (!aP256dhKey.IsNull()) {
|
|
const ArrayBuffer& key = aP256dhKey.Value();
|
|
key.ComputeLengthAndData();
|
|
rawKey.InsertElementsAt(0, key.Data(), key.Length());
|
|
}
|
|
|
|
nsTArray<uint8_t> authSecret;
|
|
if (!aAuthSecret.IsNull()) {
|
|
const ArrayBuffer& sekrit = aAuthSecret.Value();
|
|
sekrit.ComputeLengthAndData();
|
|
authSecret.InsertElementsAt(0, sekrit.Data(), sekrit.Length());
|
|
}
|
|
RefPtr<PushSubscription> sub = new PushSubscription(global,
|
|
aEndpoint,
|
|
aScope,
|
|
rawKey,
|
|
authSecret);
|
|
|
|
return sub.forget();
|
|
}
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(PushSubscription, mGlobal, mPrincipal)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(PushSubscription)
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(PushSubscription)
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(PushSubscription)
|
|
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
NS_INTERFACE_MAP_END
|
|
|
|
PushManager::PushManager(nsIGlobalObject* aGlobal, const nsAString& aScope)
|
|
: mGlobal(aGlobal), mScope(aScope)
|
|
{
|
|
AssertIsOnMainThread();
|
|
}
|
|
|
|
PushManager::~PushManager()
|
|
{}
|
|
|
|
JSObject*
|
|
PushManager::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
|
|
{
|
|
// XXXnsm I don't know if this is the right way to do it, but I want to assert
|
|
// that an implementation has been set before this object gets exposed to JS.
|
|
MOZ_ASSERT(mImpl);
|
|
return PushManagerBinding::Wrap(aCx, this, aGivenProto);
|
|
}
|
|
|
|
void
|
|
PushManager::SetPushManagerImpl(PushManagerImpl& foo, ErrorResult& aRv)
|
|
{
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
MOZ_ASSERT(!mImpl);
|
|
mImpl = &foo;
|
|
}
|
|
|
|
already_AddRefed<Promise>
|
|
PushManager::Subscribe(ErrorResult& aRv)
|
|
{
|
|
MOZ_ASSERT(mImpl);
|
|
return mImpl->Subscribe(aRv);
|
|
}
|
|
|
|
already_AddRefed<Promise>
|
|
PushManager::GetSubscription(ErrorResult& aRv)
|
|
{
|
|
MOZ_ASSERT(mImpl);
|
|
return mImpl->GetSubscription(aRv);
|
|
}
|
|
|
|
already_AddRefed<Promise>
|
|
PushManager::PermissionState(ErrorResult& aRv)
|
|
{
|
|
MOZ_ASSERT(mImpl);
|
|
return mImpl->PermissionState(aRv);
|
|
}
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(PushManager, mGlobal, mImpl)
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(PushManager)
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(PushManager)
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(PushManager)
|
|
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
NS_INTERFACE_MAP_END
|
|
|
|
// WorkerPushSubscription
|
|
|
|
WorkerPushSubscription::WorkerPushSubscription(const nsAString& aEndpoint,
|
|
const nsAString& aScope,
|
|
const nsTArray<uint8_t>& aRawP256dhKey,
|
|
const nsTArray<uint8_t>& aAuthSecret)
|
|
: mEndpoint(aEndpoint)
|
|
, mScope(aScope)
|
|
, mRawP256dhKey(aRawP256dhKey)
|
|
, mAuthSecret(aAuthSecret)
|
|
{
|
|
MOZ_ASSERT(!aScope.IsEmpty());
|
|
MOZ_ASSERT(!aEndpoint.IsEmpty());
|
|
}
|
|
|
|
WorkerPushSubscription::~WorkerPushSubscription()
|
|
{}
|
|
|
|
JSObject*
|
|
WorkerPushSubscription::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
|
|
{
|
|
return PushSubscriptionBinding_workers::Wrap(aCx, this, aGivenProto);
|
|
}
|
|
|
|
// static
|
|
already_AddRefed<WorkerPushSubscription>
|
|
WorkerPushSubscription::Constructor(GlobalObject& aGlobal,
|
|
const nsAString& aEndpoint,
|
|
const nsAString& aScope,
|
|
const Nullable<ArrayBuffer>& aP256dhKey,
|
|
const Nullable<ArrayBuffer>& aAuthSecret,
|
|
ErrorResult& aRv)
|
|
{
|
|
WorkerPrivate* worker = GetCurrentThreadWorkerPrivate();
|
|
MOZ_ASSERT(worker);
|
|
worker->AssertIsOnWorkerThread();
|
|
|
|
nsTArray<uint8_t> rawKey;
|
|
if (!aP256dhKey.IsNull()) {
|
|
const ArrayBuffer& key = aP256dhKey.Value();
|
|
key.ComputeLengthAndData();
|
|
rawKey.SetLength(key.Length());
|
|
rawKey.ReplaceElementsAt(0, key.Length(), key.Data(), key.Length());
|
|
}
|
|
|
|
nsTArray<uint8_t> authSecret;
|
|
if (!aAuthSecret.IsNull()) {
|
|
const ArrayBuffer& sekrit = aAuthSecret.Value();
|
|
sekrit.ComputeLengthAndData();
|
|
authSecret.SetLength(sekrit.Length());
|
|
authSecret.ReplaceElementsAt(0, sekrit.Length(),
|
|
sekrit.Data(), sekrit.Length());
|
|
}
|
|
RefPtr<WorkerPushSubscription> sub = new WorkerPushSubscription(aEndpoint,
|
|
aScope,
|
|
rawKey,
|
|
authSecret);
|
|
|
|
return sub.forget();
|
|
}
|
|
|
|
void
|
|
WorkerPushSubscription::GetKey(JSContext* aCx,
|
|
PushEncryptionKeyName aType,
|
|
JS::MutableHandle<JSObject*> aKey)
|
|
{
|
|
if (aType == mozilla::dom::PushEncryptionKeyName::P256dh &&
|
|
!mRawP256dhKey.IsEmpty()) {
|
|
aKey.set(ArrayBuffer::Create(aCx,
|
|
mRawP256dhKey.Length(),
|
|
mRawP256dhKey.Elements()));
|
|
} else if (aType == mozilla::dom::PushEncryptionKeyName::Auth &&
|
|
!mAuthSecret.IsEmpty()) {
|
|
aKey.set(ArrayBuffer::Create(aCx,
|
|
mAuthSecret.Length(),
|
|
mAuthSecret.Elements()));
|
|
} else {
|
|
aKey.set(nullptr);
|
|
}
|
|
}
|
|
|
|
class UnsubscribeResultRunnable final : public WorkerRunnable
|
|
{
|
|
public:
|
|
UnsubscribeResultRunnable(PromiseWorkerProxy* aProxy,
|
|
nsresult aStatus,
|
|
bool aSuccess)
|
|
: WorkerRunnable(aProxy->GetWorkerPrivate(), WorkerThreadModifyBusyCount)
|
|
, mProxy(aProxy)
|
|
, mStatus(aStatus)
|
|
, mSuccess(aSuccess)
|
|
{
|
|
AssertIsOnMainThread();
|
|
}
|
|
|
|
bool
|
|
WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override
|
|
{
|
|
MOZ_ASSERT(aWorkerPrivate);
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
RefPtr<Promise> promise = mProxy->WorkerPromise();
|
|
if (NS_SUCCEEDED(mStatus)) {
|
|
promise->MaybeResolve(mSuccess);
|
|
} else {
|
|
promise->MaybeReject(NS_ERROR_DOM_NETWORK_ERR);
|
|
}
|
|
|
|
mProxy->CleanUp(aCx);
|
|
return true;
|
|
}
|
|
private:
|
|
~UnsubscribeResultRunnable()
|
|
{}
|
|
|
|
RefPtr<PromiseWorkerProxy> mProxy;
|
|
nsresult mStatus;
|
|
bool mSuccess;
|
|
};
|
|
|
|
class WorkerUnsubscribeResultCallback final : public nsIUnsubscribeResultCallback
|
|
{
|
|
public:
|
|
NS_DECL_ISUPPORTS
|
|
|
|
explicit WorkerUnsubscribeResultCallback(PromiseWorkerProxy* aProxy)
|
|
: mProxy(aProxy)
|
|
{
|
|
AssertIsOnMainThread();
|
|
}
|
|
|
|
NS_IMETHOD
|
|
OnUnsubscribe(nsresult aStatus, bool aSuccess) override
|
|
{
|
|
AssertIsOnMainThread();
|
|
MOZ_ASSERT(mProxy, "OnUnsubscribe() called twice?");
|
|
|
|
RefPtr<PromiseWorkerProxy> proxy = mProxy.forget();
|
|
|
|
MutexAutoLock lock(proxy->Lock());
|
|
if (proxy->CleanedUp()) {
|
|
return NS_OK;
|
|
}
|
|
|
|
AutoJSAPI jsapi;
|
|
jsapi.Init();
|
|
|
|
RefPtr<UnsubscribeResultRunnable> r =
|
|
new UnsubscribeResultRunnable(proxy, aStatus, aSuccess);
|
|
r->Dispatch(jsapi.cx());
|
|
return NS_OK;
|
|
}
|
|
|
|
private:
|
|
~WorkerUnsubscribeResultCallback()
|
|
{
|
|
}
|
|
|
|
RefPtr<PromiseWorkerProxy> mProxy;
|
|
};
|
|
|
|
NS_IMPL_ISUPPORTS(WorkerUnsubscribeResultCallback, nsIUnsubscribeResultCallback)
|
|
|
|
class UnsubscribeRunnable final : public nsRunnable
|
|
{
|
|
public:
|
|
UnsubscribeRunnable(PromiseWorkerProxy* aProxy,
|
|
const nsAString& aScope)
|
|
: mProxy(aProxy)
|
|
, mScope(aScope)
|
|
{
|
|
MOZ_ASSERT(aProxy);
|
|
MOZ_ASSERT(!aScope.IsEmpty());
|
|
}
|
|
|
|
NS_IMETHOD
|
|
Run() override
|
|
{
|
|
AssertIsOnMainThread();
|
|
MutexAutoLock lock(mProxy->Lock());
|
|
if (mProxy->CleanedUp()) {
|
|
return NS_OK;
|
|
}
|
|
|
|
RefPtr<WorkerUnsubscribeResultCallback> callback =
|
|
new WorkerUnsubscribeResultCallback(mProxy);
|
|
|
|
nsCOMPtr<nsIPushClient> client =
|
|
do_CreateInstance("@mozilla.org/push/PushClient;1");
|
|
if (!client) {
|
|
callback->OnUnsubscribe(NS_ERROR_FAILURE, false);
|
|
return NS_OK;
|
|
}
|
|
|
|
nsCOMPtr<nsIPrincipal> principal = mProxy->GetWorkerPrivate()->GetPrincipal();
|
|
if (NS_WARN_IF(NS_FAILED(client->Unsubscribe(mScope, principal, callback)))) {
|
|
callback->OnUnsubscribe(NS_ERROR_FAILURE, false);
|
|
return NS_OK;
|
|
}
|
|
return NS_OK;
|
|
}
|
|
|
|
private:
|
|
~UnsubscribeRunnable()
|
|
{}
|
|
|
|
RefPtr<PromiseWorkerProxy> mProxy;
|
|
nsString mScope;
|
|
};
|
|
|
|
already_AddRefed<Promise>
|
|
WorkerPushSubscription::Unsubscribe(ErrorResult &aRv)
|
|
{
|
|
WorkerPrivate* worker = GetCurrentThreadWorkerPrivate();
|
|
MOZ_ASSERT(worker);
|
|
worker->AssertIsOnWorkerThread();
|
|
|
|
nsCOMPtr<nsIGlobalObject> global = worker->GlobalScope();
|
|
RefPtr<Promise> p = Promise::Create(global, aRv);
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
return nullptr;
|
|
}
|
|
|
|
RefPtr<PromiseWorkerProxy> proxy = PromiseWorkerProxy::Create(worker, p);
|
|
if (!proxy) {
|
|
p->MaybeReject(NS_ERROR_DOM_NETWORK_ERR);
|
|
return p.forget();
|
|
}
|
|
|
|
RefPtr<UnsubscribeRunnable> r =
|
|
new UnsubscribeRunnable(proxy, mScope);
|
|
MOZ_ALWAYS_TRUE(NS_SUCCEEDED(NS_DispatchToMainThread(r)));
|
|
|
|
return p.forget();
|
|
}
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_0(WorkerPushSubscription)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(WorkerPushSubscription)
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(WorkerPushSubscription)
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(WorkerPushSubscription)
|
|
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
NS_INTERFACE_MAP_END
|
|
|
|
// WorkerPushManager
|
|
|
|
WorkerPushManager::WorkerPushManager(const nsAString& aScope)
|
|
: mScope(aScope)
|
|
{
|
|
}
|
|
|
|
JSObject*
|
|
WorkerPushManager::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
|
|
{
|
|
return PushManagerBinding_workers::Wrap(aCx, this, aGivenProto);
|
|
}
|
|
|
|
class GetSubscriptionResultRunnable final : public WorkerRunnable
|
|
{
|
|
public:
|
|
GetSubscriptionResultRunnable(PromiseWorkerProxy* aProxy,
|
|
nsresult aStatus,
|
|
const nsAString& aEndpoint,
|
|
const nsAString& aScope,
|
|
const nsTArray<uint8_t>& aRawP256dhKey,
|
|
const nsTArray<uint8_t>& aAuthSecret)
|
|
: WorkerRunnable(aProxy->GetWorkerPrivate(), WorkerThreadModifyBusyCount)
|
|
, mProxy(aProxy)
|
|
, mStatus(aStatus)
|
|
, mEndpoint(aEndpoint)
|
|
, mScope(aScope)
|
|
, mRawP256dhKey(aRawP256dhKey)
|
|
, mAuthSecret(aAuthSecret)
|
|
{ }
|
|
|
|
bool
|
|
WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override
|
|
{
|
|
RefPtr<Promise> promise = mProxy->WorkerPromise();
|
|
if (NS_SUCCEEDED(mStatus)) {
|
|
if (mEndpoint.IsEmpty()) {
|
|
promise->MaybeResolve(JS::NullHandleValue);
|
|
} else {
|
|
RefPtr<WorkerPushSubscription> sub =
|
|
new WorkerPushSubscription(mEndpoint, mScope,
|
|
mRawP256dhKey, mAuthSecret);
|
|
promise->MaybeResolve(sub);
|
|
}
|
|
} else {
|
|
promise->MaybeReject(NS_ERROR_DOM_PUSH_ABORT_ERR);
|
|
}
|
|
|
|
mProxy->CleanUp(aCx);
|
|
return true;
|
|
}
|
|
private:
|
|
~GetSubscriptionResultRunnable()
|
|
{}
|
|
|
|
RefPtr<PromiseWorkerProxy> mProxy;
|
|
nsresult mStatus;
|
|
nsString mEndpoint;
|
|
nsString mScope;
|
|
nsTArray<uint8_t> mRawP256dhKey;
|
|
nsTArray<uint8_t> mAuthSecret;
|
|
};
|
|
|
|
class GetSubscriptionCallback final : public nsIPushEndpointCallback
|
|
{
|
|
public:
|
|
NS_DECL_ISUPPORTS
|
|
|
|
explicit GetSubscriptionCallback(PromiseWorkerProxy* aProxy,
|
|
const nsAString& aScope)
|
|
: mProxy(aProxy)
|
|
, mScope(aScope)
|
|
{}
|
|
|
|
NS_IMETHOD
|
|
OnPushEndpoint(nsresult aStatus,
|
|
const nsAString& aEndpoint,
|
|
uint32_t aKeyLen,
|
|
uint8_t* aKey,
|
|
uint32_t aAuthSecretLen,
|
|
uint8_t* aAuthSecret) override
|
|
{
|
|
AssertIsOnMainThread();
|
|
MOZ_ASSERT(mProxy, "OnPushEndpoint() called twice?");
|
|
|
|
RefPtr<PromiseWorkerProxy> proxy = mProxy.forget();
|
|
|
|
MutexAutoLock lock(proxy->Lock());
|
|
if (proxy->CleanedUp()) {
|
|
return NS_OK;
|
|
}
|
|
|
|
AutoJSAPI jsapi;
|
|
jsapi.Init();
|
|
|
|
nsTArray<uint8_t> rawP256dhKey(aKeyLen);
|
|
rawP256dhKey.ReplaceElementsAt(0, aKeyLen, aKey, aKeyLen);
|
|
|
|
nsTArray<uint8_t> authSecret(aAuthSecretLen);
|
|
authSecret.ReplaceElementsAt(0, aAuthSecretLen,
|
|
aAuthSecret, aAuthSecretLen);
|
|
|
|
RefPtr<GetSubscriptionResultRunnable> r =
|
|
new GetSubscriptionResultRunnable(proxy,
|
|
aStatus,
|
|
aEndpoint,
|
|
mScope,
|
|
rawP256dhKey,
|
|
authSecret);
|
|
r->Dispatch(jsapi.cx());
|
|
return NS_OK;
|
|
}
|
|
|
|
// Convenience method for use in this file.
|
|
void
|
|
OnPushEndpointError(nsresult aStatus)
|
|
{
|
|
Unused << NS_WARN_IF(NS_FAILED(
|
|
OnPushEndpoint(aStatus, EmptyString(), 0, nullptr, 0, nullptr)));
|
|
}
|
|
|
|
|
|
protected:
|
|
~GetSubscriptionCallback()
|
|
{}
|
|
|
|
private:
|
|
RefPtr<PromiseWorkerProxy> mProxy;
|
|
nsString mScope;
|
|
};
|
|
|
|
NS_IMPL_ISUPPORTS(GetSubscriptionCallback, nsIPushEndpointCallback)
|
|
|
|
class GetSubscriptionRunnable final : public nsRunnable
|
|
{
|
|
public:
|
|
GetSubscriptionRunnable(PromiseWorkerProxy* aProxy,
|
|
const nsAString& aScope,
|
|
WorkerPushManager::SubscriptionAction aAction)
|
|
: mProxy(aProxy)
|
|
, mScope(aScope), mAction(aAction)
|
|
{}
|
|
|
|
NS_IMETHOD
|
|
Run() override
|
|
{
|
|
AssertIsOnMainThread();
|
|
MutexAutoLock lock(mProxy->Lock());
|
|
if (mProxy->CleanedUp()) {
|
|
return NS_OK;
|
|
}
|
|
|
|
RefPtr<GetSubscriptionCallback> callback = new GetSubscriptionCallback(mProxy, mScope);
|
|
|
|
nsCOMPtr<nsIPrincipal> principal = mProxy->GetWorkerPrivate()->GetPrincipal();
|
|
|
|
PushPermissionState state;
|
|
nsresult rv = GetPermissionState(principal, state);
|
|
if (NS_FAILED(rv)) {
|
|
callback->OnPushEndpointError(NS_ERROR_FAILURE);
|
|
return NS_OK;
|
|
}
|
|
|
|
if (state != PushPermissionState::Granted) {
|
|
if (mAction == WorkerPushManager::GetSubscriptionAction) {
|
|
callback->OnPushEndpointError(NS_OK);
|
|
return NS_OK;
|
|
}
|
|
callback->OnPushEndpointError(NS_ERROR_FAILURE);
|
|
return NS_OK;
|
|
}
|
|
|
|
nsCOMPtr<nsIPushClient> client =
|
|
do_CreateInstance("@mozilla.org/push/PushClient;1");
|
|
if (!client) {
|
|
callback->OnPushEndpointError(NS_ERROR_FAILURE);
|
|
return NS_OK;
|
|
}
|
|
|
|
if (mAction == WorkerPushManager::SubscribeAction) {
|
|
rv = client->Subscribe(mScope, principal, callback);
|
|
} else {
|
|
MOZ_ASSERT(mAction == WorkerPushManager::GetSubscriptionAction);
|
|
rv = client->GetSubscription(mScope, principal, callback);
|
|
}
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
callback->OnPushEndpointError(NS_ERROR_FAILURE);
|
|
return NS_OK;
|
|
}
|
|
|
|
return NS_OK;
|
|
}
|
|
|
|
private:
|
|
~GetSubscriptionRunnable()
|
|
{}
|
|
|
|
RefPtr<PromiseWorkerProxy> mProxy;
|
|
nsString mScope;
|
|
WorkerPushManager::SubscriptionAction mAction;
|
|
};
|
|
|
|
already_AddRefed<Promise>
|
|
WorkerPushManager::PerformSubscriptionAction(SubscriptionAction aAction, ErrorResult& aRv)
|
|
{
|
|
WorkerPrivate* worker = GetCurrentThreadWorkerPrivate();
|
|
MOZ_ASSERT(worker);
|
|
worker->AssertIsOnWorkerThread();
|
|
|
|
nsCOMPtr<nsIGlobalObject> global = worker->GlobalScope();
|
|
RefPtr<Promise> p = Promise::Create(global, aRv);
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
return nullptr;
|
|
}
|
|
|
|
RefPtr<PromiseWorkerProxy> proxy = PromiseWorkerProxy::Create(worker, p);
|
|
if (!proxy) {
|
|
p->MaybeReject(NS_ERROR_DOM_PUSH_ABORT_ERR);
|
|
return p.forget();
|
|
}
|
|
|
|
RefPtr<GetSubscriptionRunnable> r =
|
|
new GetSubscriptionRunnable(proxy, mScope, aAction);
|
|
MOZ_ALWAYS_TRUE(NS_SUCCEEDED(NS_DispatchToMainThread(r)));
|
|
|
|
return p.forget();
|
|
}
|
|
|
|
already_AddRefed<Promise>
|
|
WorkerPushManager::Subscribe(ErrorResult& aRv)
|
|
{
|
|
return PerformSubscriptionAction(SubscribeAction, aRv);
|
|
}
|
|
|
|
already_AddRefed<Promise>
|
|
WorkerPushManager::GetSubscription(ErrorResult& aRv)
|
|
{
|
|
return PerformSubscriptionAction(GetSubscriptionAction, aRv);
|
|
}
|
|
|
|
class PermissionResultRunnable final : public WorkerRunnable
|
|
{
|
|
public:
|
|
PermissionResultRunnable(PromiseWorkerProxy *aProxy,
|
|
nsresult aStatus,
|
|
PushPermissionState aState)
|
|
: WorkerRunnable(aProxy->GetWorkerPrivate(), WorkerThreadModifyBusyCount)
|
|
, mProxy(aProxy)
|
|
, mStatus(aStatus)
|
|
, mState(aState)
|
|
{
|
|
AssertIsOnMainThread();
|
|
}
|
|
|
|
bool
|
|
WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override
|
|
{
|
|
MOZ_ASSERT(aWorkerPrivate);
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
RefPtr<Promise> promise = mProxy->WorkerPromise();
|
|
if (NS_SUCCEEDED(mStatus)) {
|
|
promise->MaybeResolve(mState);
|
|
} else {
|
|
promise->MaybeReject(aCx, JS::UndefinedHandleValue);
|
|
}
|
|
|
|
mProxy->CleanUp(aCx);
|
|
return true;
|
|
}
|
|
|
|
private:
|
|
~PermissionResultRunnable()
|
|
{}
|
|
|
|
RefPtr<PromiseWorkerProxy> mProxy;
|
|
nsresult mStatus;
|
|
PushPermissionState mState;
|
|
};
|
|
|
|
class PermissionStateRunnable final : public nsRunnable
|
|
{
|
|
public:
|
|
explicit PermissionStateRunnable(PromiseWorkerProxy* aProxy)
|
|
: mProxy(aProxy)
|
|
{}
|
|
|
|
NS_IMETHOD
|
|
Run() override
|
|
{
|
|
AssertIsOnMainThread();
|
|
MutexAutoLock lock(mProxy->Lock());
|
|
if (mProxy->CleanedUp()) {
|
|
return NS_OK;
|
|
}
|
|
|
|
PushPermissionState state;
|
|
nsresult rv = GetPermissionState(
|
|
mProxy->GetWorkerPrivate()->GetPrincipal(),
|
|
state
|
|
);
|
|
|
|
AutoJSAPI jsapi;
|
|
jsapi.Init();
|
|
RefPtr<PermissionResultRunnable> r =
|
|
new PermissionResultRunnable(mProxy, rv, state);
|
|
r->Dispatch(jsapi.cx());
|
|
return NS_OK;
|
|
}
|
|
|
|
private:
|
|
~PermissionStateRunnable()
|
|
{}
|
|
|
|
RefPtr<PromiseWorkerProxy> mProxy;
|
|
};
|
|
|
|
already_AddRefed<Promise>
|
|
WorkerPushManager::PermissionState(ErrorResult& aRv)
|
|
{
|
|
WorkerPrivate* worker = GetCurrentThreadWorkerPrivate();
|
|
MOZ_ASSERT(worker);
|
|
worker->AssertIsOnWorkerThread();
|
|
|
|
nsCOMPtr<nsIGlobalObject> global = worker->GlobalScope();
|
|
RefPtr<Promise> p = Promise::Create(global, aRv);
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
return nullptr;
|
|
}
|
|
|
|
RefPtr<PromiseWorkerProxy> proxy = PromiseWorkerProxy::Create(worker, p);
|
|
if (!proxy) {
|
|
p->MaybeReject(worker->GetJSContext(), JS::UndefinedHandleValue);
|
|
return p.forget();
|
|
}
|
|
|
|
RefPtr<PermissionStateRunnable> r =
|
|
new PermissionStateRunnable(proxy);
|
|
NS_DispatchToMainThread(r);
|
|
|
|
return p.forget();
|
|
}
|
|
|
|
WorkerPushManager::~WorkerPushManager()
|
|
{}
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_0(WorkerPushManager)
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(WorkerPushManager)
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(WorkerPushManager)
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(WorkerPushManager)
|
|
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
NS_INTERFACE_MAP_END
|
|
} // namespace dom
|
|
} // namespace mozilla
|