Files
palemoon27/dom/push/PushManager.cpp
T
roytam1 f579c98b65 import changes from `dev' branch of rmottola/Arctic-Fox:
- Bug 1195755: Don't assert recursion depth sanity on Mac, because there is none. r=me (e25096acc1)
- Bug 1217940 - remove BindingUtils.h from CycleCollectedJSRuntime.cpp; r=mccr8 (012fad0b80)
- Bug 1118285 - The browser.newtab.url preference is abused and should be removed. (ca573649c6)
- Bug 1209591 - allow loadURI consumers to expose whether an error page was immediately loaded as result of an error, r=smaug,mak (c033d86f07)
- Bug 1167132 - Part 14: [NetworkManager] Move network information into a separate interface (NetStats). r=ethan (87acc048cc)
- Bug 1167132 - Part 15: [NetworkManager] Move network information into a separate interface (NetworkInterfaceList). r=echen (a2a96e481e)
- Bug 1205240 - Add JSON Validation code in order to prevent invalid file. r=seanlin (8c7261ba8c)
- Bug 1215429 - Add import statement in order to access file object in chrome code of TVSimulatorService. r=seanlin (5ba9e78581)
- Bug 1217093 - Remove for-each from dom/. r=smaug (5af3efbd62)
- var-let (576b2489ec)
- Bug 1183440 - Replaces Promise.defer() with the Promise constructor in push tests. r=kitcambridge (16dfaa59b3)
- Bug 1191453 - Drop subscriptions for a site when the user revokes push permissions. r=mt,MattN (5edd10e5ad)
- Bug 1159641, Part 1 - Skip the permission check in `pushManager.getSubscription()`. r=mt (d399c496d7)
- Bug 1159641, Part 2 - Use tasks in the Push permissions test. r=mt (132484c355)
- Bug 1206302 - Use DOMException for Push errors. r=mt (5a675714fa)
- Bug 1193365 - Disable push debug. r=kitcambridge (1dc20e69b0)
- Bug 1219063, Part 1 - Use transactions for updating Push subscription permissions. r=mt (8c28453942)
- Bug 1219063, Part 2 - Remove obsolete "push" permission. r=mt (84a36931cd)
- Bug 1217065 - Unconditionally ack incoming updates. r=dragana,benbangert (e0bfa4454f)
- Bug 1212593 - Fix PushService behavior when we are switching between push servers. r=kcambridge (0afa39e743)
- Bug 1206163 - Retry failed register requests on reconnect. r=dragana (6ed1258b15)
- Bug 1218591 - Reset the WebSocket retry counter when the server replies. r=dragana (64e800db60)
- Bug 1210943 - Drop subscriptions unconditionally if the UAID changes. r=benbangert (52f538a7de)
- Bug 1214366 - Part 1: Don't preprocess PushServiceWebSocket.jsm. r=kitcambridge (a78b9fc838)
- Bug 1214366 - Part 3: Use getLastVisited equivalent in PushService.jsm. r=kitcambridge,rnewman (bc7004ad32)
- Bug 1210896, Part 1 - Use Console.jsm to log Push errors. r=mt (04335cc37f)
- Bug 1216683 - For the WebSocket version unregister should return true even if we are offline. r=kitcambridge (0f6e397a03)
- Bug 1210896, Part 2 - Use JS errors to reject internal Push promises. r=mt (3546b2f7c8)
- Bug 1223481 - Use the "potentially trustworthy origin" helper to validate Push server URLs. r=dragana (0c21f551f3)
- Bug 1223202 - Only send subscription change events if the Push permission is granted. r=mt (afeaf0dceb)
- Bug 1201128 - Don't send channel IDs in the Push handshake. r=nsm (dbbadb5c16)
- var-let (a35cb6aeca)
- Bug 1210211 - Part 1: Delay updating push quota. r=kitcambridge (53f5735ff0)
- Bug 1210211 - Part 2: Notify Push service of visible notifications. r=baku (9182bcb7d1)
- Bug 1170115 - Use clear-origin-data to remove Push records. r=allstars.chh (47f1070bab)
- Bug 1211418 - Part 1: Ensure Data Consistency after Collision of SMS Segment. r=echen. (f2d5221984)
- Bug 1211418 - Part 2: Add Test Coverage for the Collision of SMS Segment. r=echen. (06f7ba7308)
- Bug 1159132 - Part 1: Use dun apn only when config ro.tethering.dun_required is set. r=echen (bbb4fd2798)
- Bug 1159132 - Part 2: Set ro.tethering.dun_required when running dun test case. r=echen (11fe9344be)
- Bug 1187262 - Let the flag 'Services.io.offline' reference the state of tethering. r=jjong (ee22fd9358)
- Bug 1148671 - ipv6 and dual stack support on Lollipop. r=hchang (a9f7dc570e)
- Bug 1173671 - just warn if we fail to remove old default routes. r=echen (b4ab24da9f)
- Bug 1175817 - [NetworkManager] remove old default routes explicitly. r=echen,smaug (3f9a0b98ab)
- Bug 1174998 - Part 1: add setMtu() support in NetworkService. r=echen,smaug (9621036470)
- Bug 1174998 - Part 2: Set MTU for connected network interfaces. r=echen (397c898942)
- Bug 1197667 - [NetworkManager] Part 1: add missing implementation for 'allNetworkInfo'. r=echen (a49fd3498b)
- Bug 1197667 - [NetworkManager] Part 2: add test case for 'allNetworkInfo'. r=echen (942a52b0d4)
- Bug 1057091 - Add USB tethring command supporting IPv6 outgoing interface. r=hchang (9210eb5a1d)
- Bug 1177236 - Usage alert doesn't work when tethering is enabled. r=ethan (4bdd8ae226)
- Bug 1168938 - Memory safety bug in NetworkUtils::postTetherInterfaceList. r=fabrice (97485ac95c)
- Bug 1138757 - Part 1: Fix the logic of checking invalid port in CDMA WAP Push. r=echen (68dac00e52)
- Bug 1138757 - Part 2: Add Test Coverage for CDMA Wap Push. r=echen (9d54278aa9)
- Bug 1209891 - Do Not Reply Read-Report if a MMS Message Was Marked from Unread to Read Multiple Times. r=echen (421550db06)
- var-let (2ed380bb64)
- bug 1175005: performance regression. backout_f081c464c1e2 (28e1ee74b9)
- Bug 1207665 - Block Intel GMA 3150 for d3d11/d2d on all drivers. (bug 1207665 part 1, r=jrmuizel). r=jrmuizel (bb8eac6fa8)
- Bug 1188105: Parse bad driver versions. r=botond (8c856cac36)
- Bug 1075089 - Move popup menu frame offset to LookAndFeel and fix default offset for OS X. r=Enn (e1f7d0c418)
- Bug 1134385. Delete main thread assertion in CompositorVsyncDispatcher. r=kats (0945e91185)
- some profiler stuff (d3d68abdad)
- Bug 1156283 - Avoid shutdown observer race when shutting down gfx on Mac. r=roc (f66195546b)
2023-01-21 00:07:37 +08:00

822 lines
22 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/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)
: mGlobal(aGlobal)
, mEndpoint(aEndpoint)
, mScope(aScope)
, mRawP256dhKey(aRawP256dhKey)
{
}
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*> aP256dhKey)
{
if (aType == PushEncryptionKeyName::P256dh && !mRawP256dhKey.IsEmpty()) {
aP256dhKey.set(ArrayBuffer::Create(aCx,
mRawP256dhKey.Length(),
mRawP256dhKey.Elements()));
} else {
aP256dhKey.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,
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.SetLength(key.Length());
rawKey.ReplaceElementsAt(0, key.Length(), key.Data(), key.Length());
}
RefPtr<PushSubscription> sub = new PushSubscription(global,
aEndpoint,
aScope,
rawKey);
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)
: mEndpoint(aEndpoint), mScope(aScope), mRawP256dhKey(aRawP256dhKey)
{
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,
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());
}
RefPtr<WorkerPushSubscription> sub = new WorkerPushSubscription(aEndpoint,
aScope,
rawKey);
return sub.forget();
}
void
WorkerPushSubscription::GetKey(JSContext* aCx,
PushEncryptionKeyName aType,
JS::MutableHandle<JSObject*> aP256dhKey)
{
if (aType == mozilla::dom::PushEncryptionKeyName::P256dh &&
!mRawP256dhKey.IsEmpty()) {
aP256dhKey.set(ArrayBuffer::Create(aCx,
mRawP256dhKey.Length(),
mRawP256dhKey.Elements()));
} else {
aP256dhKey.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)
: WorkerRunnable(aProxy->GetWorkerPrivate(), WorkerThreadModifyBusyCount)
, mProxy(aProxy)
, mStatus(aStatus)
, mEndpoint(aEndpoint)
, mScope(aScope)
, mRawP256dhKey(aRawP256dhKey)
{ }
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);
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;
};
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) 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);
RefPtr<GetSubscriptionResultRunnable> r =
new GetSubscriptionResultRunnable(proxy,
aStatus,
aEndpoint,
mScope,
rawP256dhKey);
r->Dispatch(jsapi.cx());
return NS_OK;
}
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->OnPushEndpoint(NS_ERROR_FAILURE, EmptyString(), 0, nullptr);
return NS_OK;
}
if (state != PushPermissionState::Granted) {
if (mAction == WorkerPushManager::GetSubscriptionAction) {
callback->OnPushEndpoint(NS_OK, EmptyString(), 0, nullptr);
return NS_OK;
}
callback->OnPushEndpoint(NS_ERROR_FAILURE, EmptyString(), 0, nullptr);
return NS_OK;
}
nsCOMPtr<nsIPushClient> client =
do_CreateInstance("@mozilla.org/push/PushClient;1");
if (!client) {
callback->OnPushEndpoint(NS_ERROR_FAILURE, EmptyString(), 0, nullptr);
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->OnPushEndpoint(NS_ERROR_FAILURE, EmptyString(), 0, nullptr);
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)) {
MOZ_ASSERT(uint32_t(mState) < ArrayLength(PushPermissionStateValues::strings));
nsAutoCString stringState(PushPermissionStateValues::strings[uint32_t(mState)].value,
PushPermissionStateValues::strings[uint32_t(mState)].length);
promise->MaybeResolve(NS_ConvertUTF8toUTF16(stringState));
} 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