2015-07-23 18:30:15 +03:00
|
|
|
/* -*- 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"
|
|
|
|
|
2015-12-15 03:28:19 +03:00
|
|
|
#include "mozilla/Base64.h"
|
2015-07-23 18:30:15 +03:00
|
|
|
#include "mozilla/Preferences.h"
|
|
|
|
#include "mozilla/Services.h"
|
2015-12-08 23:26:42 +03:00
|
|
|
#include "mozilla/unused.h"
|
2015-07-23 18:30:15 +03:00
|
|
|
#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"
|
2015-12-08 23:41:41 +03:00
|
|
|
#include "nsIPushService.h"
|
2015-07-23 18:30:15 +03:00
|
|
|
|
2015-09-02 19:20:30 +03:00
|
|
|
#include "nsComponentManagerUtils.h"
|
2015-07-23 18:30:15 +03:00
|
|
|
#include "nsFrameMessageManager.h"
|
|
|
|
#include "nsContentCID.h"
|
|
|
|
|
|
|
|
#include "WorkerRunnable.h"
|
|
|
|
#include "WorkerPrivate.h"
|
|
|
|
#include "WorkerScope.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
using namespace workers;
|
|
|
|
|
2015-10-30 19:37:22 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-12-15 03:28:19 +03:00
|
|
|
void
|
|
|
|
SubscriptionToJSON(PushSubscriptionJSON& aJSON, const nsString& aEndpoint,
|
|
|
|
const nsTArray<uint8_t>& aRawP256dhKey,
|
|
|
|
const nsTArray<uint8_t>& aAuthSecret)
|
|
|
|
{
|
|
|
|
aJSON.mEndpoint.Construct();
|
|
|
|
aJSON.mEndpoint.Value() = aEndpoint;
|
|
|
|
|
|
|
|
aJSON.mKeys.mP256dh.Construct();
|
|
|
|
nsresult rv = Base64URLEncode(aRawP256dhKey.Length(),
|
|
|
|
aRawP256dhKey.Elements(),
|
|
|
|
aJSON.mKeys.mP256dh.Value());
|
|
|
|
Unused << NS_WARN_IF(NS_FAILED(rv));
|
|
|
|
|
|
|
|
aJSON.mKeys.mAuth.Construct();
|
|
|
|
rv = Base64URLEncode(aAuthSecret.Length(), aAuthSecret.Elements(),
|
|
|
|
aJSON.mKeys.mAuth.Value());
|
|
|
|
Unused << NS_WARN_IF(NS_FAILED(rv));
|
|
|
|
}
|
|
|
|
|
2015-10-30 19:37:22 +03:00
|
|
|
} // anonymous namespace
|
|
|
|
|
2015-07-23 18:30:15 +03:00
|
|
|
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 {
|
2016-01-28 19:23:29 +03:00
|
|
|
mPromise->MaybeReject(NS_ERROR_DOM_PUSH_SERVICE_UNREACHABLE);
|
2015-07-23 18:30:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
~UnsubscribeResultCallback()
|
|
|
|
{}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Promise> mPromise;
|
2015-07-23 18:30:15 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(UnsubscribeResultCallback, nsIUnsubscribeResultCallback)
|
|
|
|
|
|
|
|
already_AddRefed<Promise>
|
|
|
|
PushSubscription::Unsubscribe(ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mPrincipal);
|
|
|
|
|
2015-12-08 23:41:41 +03:00
|
|
|
nsCOMPtr<nsIPushService> service =
|
|
|
|
do_GetService("@mozilla.org/push/Service;1");
|
|
|
|
if (NS_WARN_IF(!service)) {
|
2015-07-23 18:30:15 +03:00
|
|
|
aRv = NS_ERROR_FAILURE;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Promise> p = Promise::Create(mGlobal, aRv);
|
2015-07-23 18:30:15 +03:00
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<UnsubscribeResultCallback> callback =
|
2015-07-23 18:30:15 +03:00
|
|
|
new UnsubscribeResultCallback(p);
|
2015-12-08 23:41:41 +03:00
|
|
|
Unused << NS_WARN_IF(NS_FAILED(
|
|
|
|
service->Unsubscribe(mScope, mPrincipal, callback)));
|
2015-07-23 18:30:15 +03:00
|
|
|
return p.forget();
|
|
|
|
}
|
|
|
|
|
2015-12-15 03:28:19 +03:00
|
|
|
void
|
|
|
|
PushSubscription::ToJSON(PushSubscriptionJSON& aJSON)
|
|
|
|
{
|
|
|
|
SubscriptionToJSON(aJSON, mEndpoint, mRawP256dhKey, mAuthSecret);
|
|
|
|
}
|
|
|
|
|
2015-07-23 18:30:15 +03:00
|
|
|
PushSubscription::PushSubscription(nsIGlobalObject* aGlobal,
|
|
|
|
const nsAString& aEndpoint,
|
2015-09-11 17:51:32 +03:00
|
|
|
const nsAString& aScope,
|
2015-12-08 23:26:42 +03:00
|
|
|
const nsTArray<uint8_t>& aRawP256dhKey,
|
|
|
|
const nsTArray<uint8_t>& aAuthSecret)
|
2015-09-11 17:51:32 +03:00
|
|
|
: mGlobal(aGlobal)
|
|
|
|
, mEndpoint(aEndpoint)
|
|
|
|
, mScope(aScope)
|
|
|
|
, mRawP256dhKey(aRawP256dhKey)
|
2015-12-08 23:26:42 +03:00
|
|
|
, mAuthSecret(aAuthSecret)
|
2015-07-23 18:30:15 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
PushSubscription::~PushSubscription()
|
2015-09-11 17:51:32 +03:00
|
|
|
{
|
|
|
|
}
|
2015-07-23 18:30:15 +03:00
|
|
|
|
|
|
|
JSObject*
|
|
|
|
PushSubscription::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
|
|
|
|
{
|
|
|
|
return PushSubscriptionBinding::Wrap(aCx, this, aGivenProto);
|
|
|
|
}
|
|
|
|
|
2015-09-11 17:51:32 +03:00
|
|
|
void
|
|
|
|
PushSubscription::GetKey(JSContext* aCx,
|
|
|
|
PushEncryptionKeyName aType,
|
2015-12-08 23:26:42 +03:00
|
|
|
JS::MutableHandle<JSObject*> aKey)
|
2015-09-11 17:51:32 +03:00
|
|
|
{
|
|
|
|
if (aType == PushEncryptionKeyName::P256dh && !mRawP256dhKey.IsEmpty()) {
|
2015-12-08 23:26:42 +03:00
|
|
|
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()));
|
2015-09-11 17:51:32 +03:00
|
|
|
} else {
|
2015-12-08 23:26:42 +03:00
|
|
|
aKey.set(nullptr);
|
2015-09-11 17:51:32 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-23 18:30:15 +03:00
|
|
|
void
|
|
|
|
PushSubscription::SetPrincipal(nsIPrincipal* aPrincipal)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!mPrincipal);
|
|
|
|
mPrincipal = aPrincipal;
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
already_AddRefed<PushSubscription>
|
2015-09-11 17:51:32 +03:00
|
|
|
PushSubscription::Constructor(GlobalObject& aGlobal,
|
|
|
|
const nsAString& aEndpoint,
|
|
|
|
const nsAString& aScope,
|
|
|
|
const Nullable<ArrayBuffer>& aP256dhKey,
|
2015-12-08 23:26:42 +03:00
|
|
|
const Nullable<ArrayBuffer>& aAuthSecret,
|
2015-09-11 17:51:32 +03:00
|
|
|
ErrorResult& aRv)
|
2015-07-23 18:30:15 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(!aEndpoint.IsEmpty());
|
|
|
|
MOZ_ASSERT(!aScope.IsEmpty());
|
2015-09-11 17:51:32 +03:00
|
|
|
|
2015-07-23 18:30:15 +03:00
|
|
|
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aGlobal.GetAsSupports());
|
2015-09-11 17:51:32 +03:00
|
|
|
|
|
|
|
nsTArray<uint8_t> rawKey;
|
|
|
|
if (!aP256dhKey.IsNull()) {
|
|
|
|
const ArrayBuffer& key = aP256dhKey.Value();
|
|
|
|
key.ComputeLengthAndData();
|
2015-12-08 23:26:42 +03:00
|
|
|
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());
|
2015-09-11 17:51:32 +03:00
|
|
|
}
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<PushSubscription> sub = new PushSubscription(global,
|
2015-12-08 23:26:42 +03:00
|
|
|
aEndpoint,
|
|
|
|
aScope,
|
|
|
|
rawKey,
|
|
|
|
authSecret);
|
2015-09-11 17:51:32 +03:00
|
|
|
|
2015-07-23 18:30:15 +03:00
|
|
|
return sub.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(PushSubscription, mGlobal, mPrincipal)
|
2015-09-11 17:51:32 +03:00
|
|
|
|
2015-07-23 18:30:15 +03:00
|
|
|
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,
|
2015-09-11 17:51:32 +03:00
|
|
|
const nsAString& aScope,
|
2015-12-08 23:26:42 +03:00
|
|
|
const nsTArray<uint8_t>& aRawP256dhKey,
|
|
|
|
const nsTArray<uint8_t>& aAuthSecret)
|
|
|
|
: mEndpoint(aEndpoint)
|
|
|
|
, mScope(aScope)
|
|
|
|
, mRawP256dhKey(aRawP256dhKey)
|
|
|
|
, mAuthSecret(aAuthSecret)
|
2015-07-23 18:30:15 +03:00
|
|
|
{
|
|
|
|
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>
|
2015-09-11 17:51:32 +03:00
|
|
|
WorkerPushSubscription::Constructor(GlobalObject& aGlobal,
|
|
|
|
const nsAString& aEndpoint,
|
|
|
|
const nsAString& aScope,
|
|
|
|
const Nullable<ArrayBuffer>& aP256dhKey,
|
2015-12-08 23:26:42 +03:00
|
|
|
const Nullable<ArrayBuffer>& aAuthSecret,
|
2015-09-11 17:51:32 +03:00
|
|
|
ErrorResult& aRv)
|
2015-07-23 18:30:15 +03:00
|
|
|
{
|
|
|
|
WorkerPrivate* worker = GetCurrentThreadWorkerPrivate();
|
|
|
|
MOZ_ASSERT(worker);
|
|
|
|
worker->AssertIsOnWorkerThread();
|
|
|
|
|
2015-09-11 17:51:32 +03:00
|
|
|
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());
|
|
|
|
}
|
2015-12-08 23:26:42 +03:00
|
|
|
|
|
|
|
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());
|
|
|
|
}
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<WorkerPushSubscription> sub = new WorkerPushSubscription(aEndpoint,
|
2015-12-08 23:26:42 +03:00
|
|
|
aScope,
|
|
|
|
rawKey,
|
|
|
|
authSecret);
|
2015-09-11 17:51:32 +03:00
|
|
|
|
2015-07-23 18:30:15 +03:00
|
|
|
return sub.forget();
|
|
|
|
}
|
|
|
|
|
2015-09-11 17:51:32 +03:00
|
|
|
void
|
|
|
|
WorkerPushSubscription::GetKey(JSContext* aCx,
|
|
|
|
PushEncryptionKeyName aType,
|
2015-12-08 23:26:42 +03:00
|
|
|
JS::MutableHandle<JSObject*> aKey)
|
2015-09-11 17:51:32 +03:00
|
|
|
{
|
|
|
|
if (aType == mozilla::dom::PushEncryptionKeyName::P256dh &&
|
|
|
|
!mRawP256dhKey.IsEmpty()) {
|
2015-12-08 23:26:42 +03:00
|
|
|
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()));
|
2015-09-11 17:51:32 +03:00
|
|
|
} else {
|
2015-12-08 23:26:42 +03:00
|
|
|
aKey.set(nullptr);
|
2015-09-11 17:51:32 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-23 18:30:15 +03:00
|
|
|
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();
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Promise> promise = mProxy->WorkerPromise();
|
2015-07-23 18:30:15 +03:00
|
|
|
if (NS_SUCCEEDED(mStatus)) {
|
|
|
|
promise->MaybeResolve(mSuccess);
|
|
|
|
} else {
|
2016-01-28 19:23:29 +03:00
|
|
|
promise->MaybeReject(NS_ERROR_DOM_PUSH_SERVICE_UNREACHABLE);
|
2015-07-23 18:30:15 +03:00
|
|
|
}
|
|
|
|
|
2015-09-02 20:07:26 +03:00
|
|
|
mProxy->CleanUp(aCx);
|
2015-07-23 18:30:15 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
~UnsubscribeResultRunnable()
|
2015-08-04 07:47:16 +03:00
|
|
|
{}
|
2015-07-23 18:30:15 +03:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<PromiseWorkerProxy> mProxy;
|
2015-07-23 18:30:15 +03:00
|
|
|
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();
|
2015-09-02 20:07:26 +03:00
|
|
|
MOZ_ASSERT(mProxy, "OnUnsubscribe() called twice?");
|
2015-07-23 18:30:15 +03:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<PromiseWorkerProxy> proxy = mProxy.forget();
|
2015-09-02 20:07:26 +03:00
|
|
|
|
|
|
|
MutexAutoLock lock(proxy->Lock());
|
|
|
|
if (proxy->CleanedUp()) {
|
2015-07-23 18:30:15 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
AutoJSAPI jsapi;
|
|
|
|
jsapi.Init();
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<UnsubscribeResultRunnable> r =
|
2015-09-02 20:07:26 +03:00
|
|
|
new UnsubscribeResultRunnable(proxy, aStatus, aSuccess);
|
|
|
|
r->Dispatch(jsapi.cx());
|
2015-07-23 18:30:15 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
~WorkerUnsubscribeResultCallback()
|
2015-08-06 01:18:05 +03:00
|
|
|
{
|
|
|
|
}
|
2015-07-23 18:30:15 +03:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<PromiseWorkerProxy> mProxy;
|
2015-07-23 18:30:15 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
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();
|
2016-01-26 23:16:32 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsIPrincipal> principal;
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mProxy->Lock());
|
|
|
|
if (mProxy->CleanedUp()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
principal = mProxy->GetWorkerPrivate()->GetPrincipal();
|
2015-07-23 18:30:15 +03:00
|
|
|
}
|
2016-01-26 23:16:32 +03:00
|
|
|
MOZ_ASSERT(principal);
|
2015-07-23 18:30:15 +03:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<WorkerUnsubscribeResultCallback> callback =
|
2015-08-04 07:47:16 +03:00
|
|
|
new WorkerUnsubscribeResultCallback(mProxy);
|
|
|
|
|
2015-12-08 23:41:41 +03:00
|
|
|
nsCOMPtr<nsIPushService> service =
|
|
|
|
do_GetService("@mozilla.org/push/Service;1");
|
|
|
|
if (!service) {
|
2015-08-04 07:47:16 +03:00
|
|
|
callback->OnUnsubscribe(NS_ERROR_FAILURE, false);
|
2015-11-05 21:38:35 +03:00
|
|
|
return NS_OK;
|
2015-07-23 18:30:15 +03:00
|
|
|
}
|
|
|
|
|
2015-12-08 23:41:41 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(service->Unsubscribe(mScope, principal, callback)))) {
|
2015-08-04 07:47:16 +03:00
|
|
|
callback->OnUnsubscribe(NS_ERROR_FAILURE, false);
|
2015-11-05 21:38:35 +03:00
|
|
|
return NS_OK;
|
2015-08-04 07:47:16 +03:00
|
|
|
}
|
2015-07-23 18:30:15 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
~UnsubscribeRunnable()
|
2015-08-04 07:47:16 +03:00
|
|
|
{}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<PromiseWorkerProxy> mProxy;
|
2015-07-23 18:30:15 +03:00
|
|
|
nsString mScope;
|
|
|
|
};
|
|
|
|
|
|
|
|
already_AddRefed<Promise>
|
|
|
|
WorkerPushSubscription::Unsubscribe(ErrorResult &aRv)
|
|
|
|
{
|
|
|
|
WorkerPrivate* worker = GetCurrentThreadWorkerPrivate();
|
|
|
|
MOZ_ASSERT(worker);
|
|
|
|
worker->AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
nsCOMPtr<nsIGlobalObject> global = worker->GlobalScope();
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Promise> p = Promise::Create(global, aRv);
|
2015-07-23 18:30:15 +03:00
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<PromiseWorkerProxy> proxy = PromiseWorkerProxy::Create(worker, p);
|
2015-07-23 18:30:15 +03:00
|
|
|
if (!proxy) {
|
2016-01-28 19:23:29 +03:00
|
|
|
p->MaybeReject(NS_ERROR_DOM_PUSH_SERVICE_UNREACHABLE);
|
2015-07-23 18:30:15 +03:00
|
|
|
return p.forget();
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<UnsubscribeRunnable> r =
|
2015-07-23 18:30:15 +03:00
|
|
|
new UnsubscribeRunnable(proxy, mScope);
|
2015-08-04 07:47:16 +03:00
|
|
|
MOZ_ALWAYS_TRUE(NS_SUCCEEDED(NS_DispatchToMainThread(r)));
|
2015-07-23 18:30:15 +03:00
|
|
|
|
|
|
|
return p.forget();
|
|
|
|
}
|
|
|
|
|
2015-12-15 03:28:19 +03:00
|
|
|
void
|
|
|
|
WorkerPushSubscription::ToJSON(PushSubscriptionJSON& aJSON)
|
|
|
|
{
|
|
|
|
SubscriptionToJSON(aJSON, mEndpoint, mRawP256dhKey, mAuthSecret);
|
|
|
|
}
|
|
|
|
|
2015-07-23 18:30:15 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_0(WorkerPushSubscription)
|
2015-09-11 17:51:32 +03:00
|
|
|
|
2015-07-23 18:30:15 +03:00
|
|
|
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,
|
2015-09-11 17:51:32 +03:00
|
|
|
const nsAString& aScope,
|
2015-12-08 23:26:42 +03:00
|
|
|
const nsTArray<uint8_t>& aRawP256dhKey,
|
|
|
|
const nsTArray<uint8_t>& aAuthSecret)
|
2015-07-23 18:30:15 +03:00
|
|
|
: WorkerRunnable(aProxy->GetWorkerPrivate(), WorkerThreadModifyBusyCount)
|
|
|
|
, mProxy(aProxy)
|
|
|
|
, mStatus(aStatus)
|
|
|
|
, mEndpoint(aEndpoint)
|
|
|
|
, mScope(aScope)
|
2015-09-11 17:51:32 +03:00
|
|
|
, mRawP256dhKey(aRawP256dhKey)
|
2015-12-08 23:26:42 +03:00
|
|
|
, mAuthSecret(aAuthSecret)
|
2015-07-23 18:30:15 +03:00
|
|
|
{ }
|
|
|
|
|
|
|
|
bool
|
|
|
|
WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override
|
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Promise> promise = mProxy->WorkerPromise();
|
2015-07-23 18:30:15 +03:00
|
|
|
if (NS_SUCCEEDED(mStatus)) {
|
2015-08-06 20:15:00 +03:00
|
|
|
if (mEndpoint.IsEmpty()) {
|
|
|
|
promise->MaybeResolve(JS::NullHandleValue);
|
|
|
|
} else {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<WorkerPushSubscription> sub =
|
2015-12-08 23:26:42 +03:00
|
|
|
new WorkerPushSubscription(mEndpoint, mScope,
|
|
|
|
mRawP256dhKey, mAuthSecret);
|
2015-08-06 20:15:00 +03:00
|
|
|
promise->MaybeResolve(sub);
|
|
|
|
}
|
2016-01-28 19:23:29 +03:00
|
|
|
} else if (NS_ERROR_GET_MODULE(mStatus) == NS_ERROR_MODULE_DOM_PUSH ) {
|
|
|
|
promise->MaybeReject(mStatus);
|
2015-07-23 18:30:15 +03:00
|
|
|
} else {
|
2015-11-05 21:38:35 +03:00
|
|
|
promise->MaybeReject(NS_ERROR_DOM_PUSH_ABORT_ERR);
|
2015-07-23 18:30:15 +03:00
|
|
|
}
|
|
|
|
|
2015-09-02 20:07:26 +03:00
|
|
|
mProxy->CleanUp(aCx);
|
2015-07-23 18:30:15 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
~GetSubscriptionResultRunnable()
|
2015-08-04 07:47:16 +03:00
|
|
|
{}
|
2015-07-23 18:30:15 +03:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<PromiseWorkerProxy> mProxy;
|
2015-07-23 18:30:15 +03:00
|
|
|
nsresult mStatus;
|
|
|
|
nsString mEndpoint;
|
|
|
|
nsString mScope;
|
2015-09-11 17:51:32 +03:00
|
|
|
nsTArray<uint8_t> mRawP256dhKey;
|
2015-12-08 23:26:42 +03:00
|
|
|
nsTArray<uint8_t> mAuthSecret;
|
2015-07-23 18:30:15 +03:00
|
|
|
};
|
|
|
|
|
2015-12-08 23:41:41 +03:00
|
|
|
class GetSubscriptionCallback final : public nsIPushSubscriptionCallback
|
2015-07-23 18:30:15 +03:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
explicit GetSubscriptionCallback(PromiseWorkerProxy* aProxy,
|
|
|
|
const nsAString& aScope)
|
|
|
|
: mProxy(aProxy)
|
|
|
|
, mScope(aScope)
|
|
|
|
{}
|
|
|
|
|
|
|
|
NS_IMETHOD
|
2015-12-08 23:41:41 +03:00
|
|
|
OnPushSubscription(nsresult aStatus,
|
|
|
|
nsIPushSubscription* aSubscription) override
|
2015-07-23 18:30:15 +03:00
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
2015-12-08 23:41:41 +03:00
|
|
|
MOZ_ASSERT(mProxy, "OnPushSubscription() called twice?");
|
2015-08-04 07:47:16 +03:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<PromiseWorkerProxy> proxy = mProxy.forget();
|
2015-07-23 18:30:15 +03:00
|
|
|
|
2015-09-02 20:07:26 +03:00
|
|
|
MutexAutoLock lock(proxy->Lock());
|
|
|
|
if (proxy->CleanedUp()) {
|
2015-07-23 18:30:15 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
AutoJSAPI jsapi;
|
|
|
|
jsapi.Init();
|
|
|
|
|
2015-12-08 23:41:41 +03:00
|
|
|
nsAutoString endpoint;
|
|
|
|
nsTArray<uint8_t> rawP256dhKey, authSecret;
|
|
|
|
if (NS_SUCCEEDED(aStatus)) {
|
|
|
|
aStatus = GetSubscriptionParams(aSubscription, endpoint, rawP256dhKey,
|
|
|
|
authSecret);
|
|
|
|
}
|
2015-12-08 23:26:42 +03:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<GetSubscriptionResultRunnable> r =
|
2015-09-11 17:51:32 +03:00
|
|
|
new GetSubscriptionResultRunnable(proxy,
|
|
|
|
aStatus,
|
2015-12-08 23:41:41 +03:00
|
|
|
endpoint,
|
2015-09-11 17:51:32 +03:00
|
|
|
mScope,
|
2015-12-08 23:26:42 +03:00
|
|
|
rawP256dhKey,
|
|
|
|
authSecret);
|
2015-09-02 20:07:26 +03:00
|
|
|
r->Dispatch(jsapi.cx());
|
2015-07-23 18:30:15 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-12-08 23:26:42 +03:00
|
|
|
// Convenience method for use in this file.
|
|
|
|
void
|
2015-12-08 23:41:41 +03:00
|
|
|
OnPushSubscriptionError(nsresult aStatus)
|
2015-12-08 23:26:42 +03:00
|
|
|
{
|
|
|
|
Unused << NS_WARN_IF(NS_FAILED(
|
2015-12-08 23:41:41 +03:00
|
|
|
OnPushSubscription(aStatus, nullptr)));
|
2015-12-08 23:26:42 +03:00
|
|
|
}
|
|
|
|
|
2015-07-23 18:30:15 +03:00
|
|
|
protected:
|
|
|
|
~GetSubscriptionCallback()
|
2015-09-02 20:07:26 +03:00
|
|
|
{}
|
2015-07-23 18:30:15 +03:00
|
|
|
|
|
|
|
private:
|
2015-12-08 23:41:41 +03:00
|
|
|
inline nsresult
|
|
|
|
FreeKeys(nsresult aStatus, uint8_t* aKey, uint8_t* aAuthSecret)
|
|
|
|
{
|
|
|
|
NS_Free(aKey);
|
|
|
|
NS_Free(aAuthSecret);
|
|
|
|
return aStatus;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
GetSubscriptionParams(nsIPushSubscription* aSubscription,
|
|
|
|
nsAString& aEndpoint,
|
|
|
|
nsTArray<uint8_t>& aRawP256dhKey,
|
|
|
|
nsTArray<uint8_t>& aAuthSecret)
|
|
|
|
{
|
|
|
|
if (!aSubscription) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv = aSubscription->GetEndpoint(aEndpoint);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t* key = nullptr;
|
|
|
|
uint8_t* authSecret = nullptr;
|
|
|
|
|
|
|
|
uint32_t keyLen;
|
|
|
|
rv = aSubscription->GetKey(NS_LITERAL_STRING("p256dh"), &keyLen, &key);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return FreeKeys(rv, key, authSecret);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t authSecretLen;
|
|
|
|
rv = aSubscription->GetKey(NS_LITERAL_STRING("auth"), &authSecretLen,
|
|
|
|
&authSecret);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return FreeKeys(rv, key, authSecret);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!aRawP256dhKey.SetLength(keyLen, fallible) ||
|
|
|
|
!aRawP256dhKey.ReplaceElementsAt(0, keyLen, key, keyLen, fallible) ||
|
|
|
|
!aAuthSecret.SetLength(authSecretLen, fallible) ||
|
|
|
|
!aAuthSecret.ReplaceElementsAt(0, authSecretLen, authSecret,
|
|
|
|
authSecretLen, fallible)) {
|
|
|
|
|
|
|
|
return FreeKeys(NS_ERROR_OUT_OF_MEMORY, key, authSecret);
|
|
|
|
}
|
|
|
|
|
|
|
|
return FreeKeys(NS_OK, key, authSecret);
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<PromiseWorkerProxy> mProxy;
|
2015-07-23 18:30:15 +03:00
|
|
|
nsString mScope;
|
|
|
|
};
|
|
|
|
|
2015-12-08 23:41:41 +03:00
|
|
|
NS_IMPL_ISUPPORTS(GetSubscriptionCallback, nsIPushSubscriptionCallback)
|
2015-07-23 18:30:15 +03:00
|
|
|
|
|
|
|
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();
|
2016-01-26 23:16:32 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsIPrincipal> principal;
|
|
|
|
{
|
|
|
|
// Bug 1228723: If permission is revoked or an error occurs, the
|
|
|
|
// subscription callback will be called synchronously. This causes
|
|
|
|
// `GetSubscriptionCallback::OnPushSubscription` to deadlock when
|
|
|
|
// it tries to acquire the lock.
|
|
|
|
MutexAutoLock lock(mProxy->Lock());
|
|
|
|
if (mProxy->CleanedUp()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
principal = mProxy->GetWorkerPrivate()->GetPrincipal();
|
2015-07-23 18:30:15 +03:00
|
|
|
}
|
2016-01-26 23:16:32 +03:00
|
|
|
MOZ_ASSERT(principal);
|
2015-07-23 18:30:15 +03:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<GetSubscriptionCallback> callback = new GetSubscriptionCallback(mProxy, mScope);
|
2015-08-04 07:47:16 +03:00
|
|
|
|
2015-10-30 19:37:22 +03:00
|
|
|
PushPermissionState state;
|
|
|
|
nsresult rv = GetPermissionState(principal, state);
|
|
|
|
if (NS_FAILED(rv)) {
|
2015-12-08 23:41:41 +03:00
|
|
|
callback->OnPushSubscriptionError(NS_ERROR_FAILURE);
|
2015-07-23 18:30:15 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-10-30 19:37:22 +03:00
|
|
|
if (state != PushPermissionState::Granted) {
|
|
|
|
if (mAction == WorkerPushManager::GetSubscriptionAction) {
|
2015-12-08 23:41:41 +03:00
|
|
|
callback->OnPushSubscriptionError(NS_OK);
|
2015-10-30 19:37:22 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2016-01-28 19:23:29 +03:00
|
|
|
callback->OnPushSubscriptionError(NS_ERROR_DOM_PUSH_DENIED_ERR);
|
2015-07-23 18:30:15 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-12-08 23:41:41 +03:00
|
|
|
nsCOMPtr<nsIPushService> service =
|
|
|
|
do_GetService("@mozilla.org/push/Service;1");
|
|
|
|
if (!service) {
|
|
|
|
callback->OnPushSubscriptionError(NS_ERROR_FAILURE);
|
2015-07-23 18:30:15 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mAction == WorkerPushManager::SubscribeAction) {
|
2015-12-08 23:41:41 +03:00
|
|
|
rv = service->Subscribe(mScope, principal, callback);
|
2015-07-23 18:30:15 +03:00
|
|
|
} else {
|
|
|
|
MOZ_ASSERT(mAction == WorkerPushManager::GetSubscriptionAction);
|
2015-12-08 23:41:41 +03:00
|
|
|
rv = service->GetSubscription(mScope, principal, callback);
|
2015-07-23 18:30:15 +03:00
|
|
|
}
|
|
|
|
|
2015-08-04 07:47:16 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2015-12-08 23:41:41 +03:00
|
|
|
callback->OnPushSubscriptionError(NS_ERROR_FAILURE);
|
2015-11-05 21:38:35 +03:00
|
|
|
return NS_OK;
|
2015-08-04 07:47:16 +03:00
|
|
|
}
|
2015-07-23 18:30:15 +03:00
|
|
|
|
2015-08-04 07:47:16 +03:00
|
|
|
return NS_OK;
|
2015-07-23 18:30:15 +03:00
|
|
|
}
|
|
|
|
|
2015-08-04 07:47:16 +03:00
|
|
|
private:
|
2015-07-23 18:30:15 +03:00
|
|
|
~GetSubscriptionRunnable()
|
2015-08-04 07:47:16 +03:00
|
|
|
{}
|
2015-07-23 18:30:15 +03:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<PromiseWorkerProxy> mProxy;
|
2015-07-23 18:30:15 +03:00
|
|
|
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();
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Promise> p = Promise::Create(global, aRv);
|
2015-07-23 18:30:15 +03:00
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<PromiseWorkerProxy> proxy = PromiseWorkerProxy::Create(worker, p);
|
2015-07-23 18:30:15 +03:00
|
|
|
if (!proxy) {
|
2015-11-05 21:38:35 +03:00
|
|
|
p->MaybeReject(NS_ERROR_DOM_PUSH_ABORT_ERR);
|
2015-07-23 18:30:15 +03:00
|
|
|
return p.forget();
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<GetSubscriptionRunnable> r =
|
2015-07-23 18:30:15 +03:00
|
|
|
new GetSubscriptionRunnable(proxy, mScope, aAction);
|
2015-08-04 07:47:16 +03:00
|
|
|
MOZ_ALWAYS_TRUE(NS_SUCCEEDED(NS_DispatchToMainThread(r)));
|
2015-07-23 18:30:15 +03:00
|
|
|
|
|
|
|
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();
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Promise> promise = mProxy->WorkerPromise();
|
2015-07-23 18:30:15 +03:00
|
|
|
if (NS_SUCCEEDED(mStatus)) {
|
2015-11-21 20:15:11 +03:00
|
|
|
promise->MaybeResolve(mState);
|
2015-07-23 18:30:15 +03:00
|
|
|
} else {
|
|
|
|
promise->MaybeReject(aCx, JS::UndefinedHandleValue);
|
|
|
|
}
|
|
|
|
|
2015-09-02 20:07:26 +03:00
|
|
|
mProxy->CleanUp(aCx);
|
2015-07-23 18:30:15 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
~PermissionResultRunnable()
|
2015-08-04 07:47:16 +03:00
|
|
|
{}
|
2015-07-23 18:30:15 +03:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<PromiseWorkerProxy> mProxy;
|
2015-07-23 18:30:15 +03:00
|
|
|
nsresult mStatus;
|
|
|
|
PushPermissionState mState;
|
|
|
|
};
|
|
|
|
|
|
|
|
class PermissionStateRunnable final : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit PermissionStateRunnable(PromiseWorkerProxy* aProxy)
|
|
|
|
: mProxy(aProxy)
|
|
|
|
{}
|
|
|
|
|
|
|
|
NS_IMETHOD
|
|
|
|
Run() override
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
2015-09-02 20:07:26 +03:00
|
|
|
MutexAutoLock lock(mProxy->Lock());
|
|
|
|
if (mProxy->CleanedUp()) {
|
2015-07-23 18:30:15 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-10-30 19:37:22 +03:00
|
|
|
PushPermissionState state;
|
|
|
|
nsresult rv = GetPermissionState(
|
|
|
|
mProxy->GetWorkerPrivate()->GetPrincipal(),
|
|
|
|
state
|
|
|
|
);
|
2015-07-23 18:30:15 +03:00
|
|
|
|
2015-08-04 07:47:16 +03:00
|
|
|
AutoJSAPI jsapi;
|
|
|
|
jsapi.Init();
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<PermissionResultRunnable> r =
|
2015-07-23 18:30:15 +03:00
|
|
|
new PermissionResultRunnable(mProxy, rv, state);
|
2015-09-02 20:07:26 +03:00
|
|
|
r->Dispatch(jsapi.cx());
|
2015-07-23 18:30:15 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
~PermissionStateRunnable()
|
2015-08-04 07:47:16 +03:00
|
|
|
{}
|
2015-07-23 18:30:15 +03:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<PromiseWorkerProxy> mProxy;
|
2015-07-23 18:30:15 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
already_AddRefed<Promise>
|
|
|
|
WorkerPushManager::PermissionState(ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
WorkerPrivate* worker = GetCurrentThreadWorkerPrivate();
|
|
|
|
MOZ_ASSERT(worker);
|
|
|
|
worker->AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
nsCOMPtr<nsIGlobalObject> global = worker->GlobalScope();
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Promise> p = Promise::Create(global, aRv);
|
2015-07-23 18:30:15 +03:00
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<PromiseWorkerProxy> proxy = PromiseWorkerProxy::Create(worker, p);
|
2015-07-23 18:30:15 +03:00
|
|
|
if (!proxy) {
|
|
|
|
p->MaybeReject(worker->GetJSContext(), JS::UndefinedHandleValue);
|
|
|
|
return p.forget();
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<PermissionStateRunnable> r =
|
2015-07-23 18:30:15 +03:00
|
|
|
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
|