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"
|
|
|
|
|
2016-05-17 18:32:05 +03:00
|
|
|
#include "mozilla/Preferences.h"
|
2015-07-23 18:30:15 +03:00
|
|
|
#include "mozilla/Services.h"
|
2016-08-23 07:09:32 +03:00
|
|
|
#include "mozilla/Unused.h"
|
2015-07-23 18:30:15 +03:00
|
|
|
#include "mozilla/dom/PushManagerBinding.h"
|
2016-03-31 20:49:07 +03:00
|
|
|
#include "mozilla/dom/PushSubscription.h"
|
2016-03-22 23:38:03 +03:00
|
|
|
#include "mozilla/dom/PushSubscriptionOptionsBinding.h"
|
|
|
|
#include "mozilla/dom/PushUtil.h"
|
2015-07-23 18:30:15 +03:00
|
|
|
|
|
|
|
#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"
|
2016-04-02 01:25:49 +03:00
|
|
|
#include "nsContentUtils.h"
|
2015-07-23 18:30:15 +03:00
|
|
|
|
|
|
|
#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,
|
2016-04-02 01:25:49 +03:00
|
|
|
PushPermissionState& aState)
|
2015-10-30 19:37:22 +03:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-05-17 18:32:05 +03:00
|
|
|
if (permission == nsIPermissionManager::ALLOW_ACTION ||
|
|
|
|
Preferences::GetBool("dom.push.testing.ignorePermission", false)) {
|
2015-10-30 19:37:22 +03:00
|
|
|
aState = PushPermissionState::Granted;
|
|
|
|
} else if (permission == nsIPermissionManager::DENY_ACTION) {
|
|
|
|
aState = PushPermissionState::Denied;
|
|
|
|
} else {
|
|
|
|
aState = PushPermissionState::Prompt;
|
|
|
|
}
|
2015-07-23 18:30:15 +03:00
|
|
|
|
2016-04-02 01:25:49 +03:00
|
|
|
return NS_OK;
|
2015-07-23 18:30:15 +03:00
|
|
|
}
|
|
|
|
|
2016-03-22 23:38:03 +03:00
|
|
|
// A helper class that frees an `nsIPushSubscription` key buffer when it
|
|
|
|
// goes out of scope.
|
|
|
|
class MOZ_RAII AutoFreeKeyBuffer final
|
|
|
|
{
|
|
|
|
uint8_t** mKeyBuffer;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit AutoFreeKeyBuffer(uint8_t** aKeyBuffer)
|
|
|
|
: mKeyBuffer(aKeyBuffer)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mKeyBuffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
~AutoFreeKeyBuffer()
|
|
|
|
{
|
|
|
|
NS_Free(*mKeyBuffer);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Copies a subscription key buffer into an array.
|
|
|
|
nsresult
|
|
|
|
CopySubscriptionKeyToArray(nsIPushSubscription* aSubscription,
|
|
|
|
const nsAString& aKeyName,
|
|
|
|
nsTArray<uint8_t>& aKey)
|
|
|
|
{
|
|
|
|
uint8_t* keyBuffer = nullptr;
|
|
|
|
AutoFreeKeyBuffer autoFree(&keyBuffer);
|
|
|
|
|
|
|
|
uint32_t keyLen;
|
|
|
|
nsresult rv = aSubscription->GetKey(aKeyName, &keyLen, &keyBuffer);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2016-04-23 06:54:22 +03:00
|
|
|
if (!aKey.SetCapacity(keyLen, fallible) ||
|
|
|
|
!aKey.InsertElementsAt(0, keyBuffer, keyLen, fallible)) {
|
2016-03-22 23:38:03 +03:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
GetSubscriptionParams(nsIPushSubscription* aSubscription,
|
|
|
|
nsAString& aEndpoint,
|
|
|
|
nsTArray<uint8_t>& aRawP256dhKey,
|
|
|
|
nsTArray<uint8_t>& aAuthSecret,
|
|
|
|
nsTArray<uint8_t>& aAppServerKey)
|
|
|
|
{
|
|
|
|
if (!aSubscription) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv = aSubscription->GetEndpoint(aEndpoint);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = CopySubscriptionKeyToArray(aSubscription, NS_LITERAL_STRING("p256dh"),
|
|
|
|
aRawP256dhKey);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
rv = CopySubscriptionKeyToArray(aSubscription, NS_LITERAL_STRING("auth"),
|
|
|
|
aAuthSecret);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
rv = CopySubscriptionKeyToArray(aSubscription, NS_LITERAL_STRING("appServer"),
|
|
|
|
aAppServerKey);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-07-23 18:30:15 +03:00
|
|
|
class GetSubscriptionResultRunnable final : public WorkerRunnable
|
|
|
|
{
|
|
|
|
public:
|
2016-04-02 01:25:49 +03:00
|
|
|
GetSubscriptionResultRunnable(WorkerPrivate* aWorkerPrivate,
|
|
|
|
already_AddRefed<PromiseWorkerProxy>&& aProxy,
|
2015-07-23 18:30:15 +03:00
|
|
|
nsresult aStatus,
|
|
|
|
const nsAString& aEndpoint,
|
2015-09-11 17:51:32 +03:00
|
|
|
const nsAString& aScope,
|
2016-04-02 01:25:49 +03:00
|
|
|
nsTArray<uint8_t>&& aRawP256dhKey,
|
2016-03-22 23:38:03 +03:00
|
|
|
nsTArray<uint8_t>&& aAuthSecret,
|
|
|
|
nsTArray<uint8_t>&& aAppServerKey)
|
2016-06-28 20:28:13 +03:00
|
|
|
: WorkerRunnable(aWorkerPrivate)
|
2016-04-02 01:25:49 +03:00
|
|
|
, mProxy(Move(aProxy))
|
2015-07-23 18:30:15 +03:00
|
|
|
, mStatus(aStatus)
|
|
|
|
, mEndpoint(aEndpoint)
|
|
|
|
, mScope(aScope)
|
2016-04-02 01:25:49 +03:00
|
|
|
, mRawP256dhKey(Move(aRawP256dhKey))
|
|
|
|
, mAuthSecret(Move(aAuthSecret))
|
2016-03-22 23:38:03 +03:00
|
|
|
, mAppServerKey(Move(aAppServerKey))
|
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 {
|
2016-03-31 20:49:07 +03:00
|
|
|
RefPtr<PushSubscription> sub =
|
2016-04-02 01:25:49 +03:00
|
|
|
new PushSubscription(nullptr, mEndpoint, mScope,
|
2016-03-22 23:38:03 +03:00
|
|
|
Move(mRawP256dhKey), Move(mAuthSecret),
|
|
|
|
Move(mAppServerKey));
|
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
|
|
|
}
|
|
|
|
|
2016-02-29 22:52:42 +03:00
|
|
|
mProxy->CleanUp();
|
2016-04-02 01:25:49 +03:00
|
|
|
|
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;
|
2016-03-22 23:38:03 +03:00
|
|
|
nsTArray<uint8_t> mAppServerKey;
|
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
|
|
|
|
2016-04-02 01:25:49 +03:00
|
|
|
MutexAutoLock lock(mProxy->Lock());
|
|
|
|
if (mProxy->CleanedUp()) {
|
2015-07-23 18:30:15 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-12-08 23:41:41 +03:00
|
|
|
nsAutoString endpoint;
|
2016-03-22 23:38:03 +03:00
|
|
|
nsTArray<uint8_t> rawP256dhKey, authSecret, appServerKey;
|
2015-12-08 23:41:41 +03:00
|
|
|
if (NS_SUCCEEDED(aStatus)) {
|
|
|
|
aStatus = GetSubscriptionParams(aSubscription, endpoint, rawP256dhKey,
|
2016-03-22 23:38:03 +03:00
|
|
|
authSecret, appServerKey);
|
2015-12-08 23:41:41 +03:00
|
|
|
}
|
2015-12-08 23:26:42 +03:00
|
|
|
|
2016-04-02 01:25:49 +03:00
|
|
|
WorkerPrivate* worker = mProxy->GetWorkerPrivate();
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<GetSubscriptionResultRunnable> r =
|
2016-04-02 01:25:49 +03:00
|
|
|
new GetSubscriptionResultRunnable(worker,
|
|
|
|
mProxy.forget(),
|
2015-09-11 17:51:32 +03:00
|
|
|
aStatus,
|
2015-12-08 23:41:41 +03:00
|
|
|
endpoint,
|
2015-09-11 17:51:32 +03:00
|
|
|
mScope,
|
2016-04-02 01:25:49 +03:00
|
|
|
Move(rawP256dhKey),
|
2016-03-22 23:38:03 +03:00
|
|
|
Move(authSecret),
|
|
|
|
Move(appServerKey));
|
2016-04-02 01:25:49 +03:00
|
|
|
MOZ_ALWAYS_TRUE(r->Dispatch());
|
|
|
|
|
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-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
|
|
|
|
2016-04-26 03:23:21 +03:00
|
|
|
class GetSubscriptionRunnable final : public Runnable
|
2015-07-23 18:30:15 +03:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
GetSubscriptionRunnable(PromiseWorkerProxy* aProxy,
|
|
|
|
const nsAString& aScope,
|
2016-03-22 23:38:03 +03:00
|
|
|
PushManager::SubscriptionAction aAction,
|
|
|
|
nsTArray<uint8_t>&& aAppServerKey)
|
2015-07-23 18:30:15 +03:00
|
|
|
: mProxy(aProxy)
|
2016-03-22 23:38:03 +03:00
|
|
|
, mScope(aScope)
|
|
|
|
, mAction(aAction)
|
|
|
|
, mAppServerKey(Move(aAppServerKey))
|
2015-07-23 18:30:15 +03:00
|
|
|
{}
|
|
|
|
|
|
|
|
NS_IMETHOD
|
|
|
|
Run() override
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
2016-01-26 23:16:32 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsIPrincipal> principal;
|
2016-04-02 01:25:49 +03:00
|
|
|
|
2016-01-26 23:16:32 +03:00
|
|
|
{
|
|
|
|
// 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-04-02 01:25:49 +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) {
|
2016-04-02 01:25:49 +03:00
|
|
|
if (mAction == PushManager::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");
|
2016-04-02 01:25:49 +03:00
|
|
|
if (NS_WARN_IF(!service)) {
|
2015-12-08 23:41:41 +03:00
|
|
|
callback->OnPushSubscriptionError(NS_ERROR_FAILURE);
|
2015-07-23 18:30:15 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-04-02 01:25:49 +03:00
|
|
|
if (mAction == PushManager::SubscribeAction) {
|
2016-03-22 23:38:03 +03:00
|
|
|
if (mAppServerKey.IsEmpty()) {
|
|
|
|
rv = service->Subscribe(mScope, principal, callback);
|
|
|
|
} else {
|
|
|
|
rv = service->SubscribeWithKey(mScope, principal,
|
|
|
|
mAppServerKey.Length(),
|
|
|
|
mAppServerKey.Elements(), callback);
|
|
|
|
}
|
2015-07-23 18:30:15 +03:00
|
|
|
} else {
|
2016-04-02 01:25:49 +03:00
|
|
|
MOZ_ASSERT(mAction == PushManager::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;
|
2016-04-02 01:25:49 +03:00
|
|
|
PushManager::SubscriptionAction mAction;
|
2016-03-22 23:38:03 +03:00
|
|
|
nsTArray<uint8_t> mAppServerKey;
|
2015-07-23 18:30:15 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
class PermissionResultRunnable final : public WorkerRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
PermissionResultRunnable(PromiseWorkerProxy *aProxy,
|
|
|
|
nsresult aStatus,
|
|
|
|
PushPermissionState aState)
|
2016-06-28 20:28:13 +03:00
|
|
|
: WorkerRunnable(aProxy->GetWorkerPrivate())
|
2015-07-23 18:30:15 +03:00
|
|
|
, 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);
|
|
|
|
}
|
|
|
|
|
2016-02-29 22:52:42 +03:00
|
|
|
mProxy->CleanUp();
|
2016-04-02 01:25:49 +03:00
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2016-04-26 03:23:21 +03:00
|
|
|
class PermissionStateRunnable final : public Runnable
|
2015-07-23 18:30:15 +03:00
|
|
|
{
|
|
|
|
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-10-18 08:24:48 +03:00
|
|
|
RefPtr<PermissionResultRunnable> r =
|
2015-07-23 18:30:15 +03:00
|
|
|
new PermissionResultRunnable(mProxy, rv, state);
|
2016-04-02 01:25:49 +03:00
|
|
|
MOZ_ALWAYS_TRUE(r->Dispatch());
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2016-04-02 01:25:49 +03:00
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
PushManager::PushManager(nsIGlobalObject* aGlobal, PushManagerImpl* aImpl)
|
|
|
|
: mGlobal(aGlobal)
|
|
|
|
, mImpl(aImpl)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
MOZ_ASSERT(aImpl);
|
|
|
|
}
|
|
|
|
|
|
|
|
PushManager::PushManager(const nsAString& aScope)
|
|
|
|
: mScope(aScope)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
|
|
|
// There's only one global on a worker, so we don't need to pass a global
|
|
|
|
// object to the constructor.
|
|
|
|
WorkerPrivate* worker = GetCurrentThreadWorkerPrivate();
|
|
|
|
MOZ_ASSERT(worker);
|
|
|
|
worker->AssertIsOnWorkerThread();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
PushManager::~PushManager()
|
|
|
|
{}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
JSObject*
|
|
|
|
PushManager::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
|
|
|
|
{
|
|
|
|
return PushManagerBinding::Wrap(aCx, this, aGivenProto);
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
already_AddRefed<PushManager>
|
|
|
|
PushManager::Constructor(GlobalObject& aGlobal,
|
|
|
|
const nsAString& aScope,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
if (!NS_IsMainThread()) {
|
|
|
|
RefPtr<PushManager> ret = new PushManager(aScope);
|
|
|
|
return ret.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<PushManagerImpl> impl = PushManagerImpl::Constructor(aGlobal,
|
|
|
|
aGlobal.Context(),
|
|
|
|
aScope, aRv);
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aGlobal.GetAsSupports());
|
|
|
|
RefPtr<PushManager> ret = new PushManager(global, impl);
|
|
|
|
|
|
|
|
return ret.forget();
|
|
|
|
}
|
|
|
|
|
2015-07-23 18:30:15 +03:00
|
|
|
already_AddRefed<Promise>
|
2016-03-22 23:38:03 +03:00
|
|
|
PushManager::Subscribe(const PushSubscriptionOptionsInit& aOptions,
|
|
|
|
ErrorResult& aRv)
|
2016-04-02 01:25:49 +03:00
|
|
|
{
|
|
|
|
if (mImpl) {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2016-03-22 23:38:03 +03:00
|
|
|
return mImpl->Subscribe(aOptions, aRv);
|
2016-04-02 01:25:49 +03:00
|
|
|
}
|
|
|
|
|
2016-03-22 23:38:03 +03:00
|
|
|
return PerformSubscriptionActionFromWorker(SubscribeAction, aOptions, aRv);
|
2016-04-02 01:25:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<Promise>
|
|
|
|
PushManager::GetSubscription(ErrorResult& aRv)
|
2015-07-23 18:30:15 +03:00
|
|
|
{
|
2016-04-02 01:25:49 +03:00
|
|
|
if (mImpl) {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
return mImpl->GetSubscription(aRv);
|
|
|
|
}
|
|
|
|
|
|
|
|
return PerformSubscriptionActionFromWorker(GetSubscriptionAction, aRv);
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<Promise>
|
2016-03-22 23:38:03 +03:00
|
|
|
PushManager::PermissionState(const PushSubscriptionOptionsInit& aOptions,
|
|
|
|
ErrorResult& aRv)
|
2016-04-02 01:25:49 +03:00
|
|
|
{
|
|
|
|
if (mImpl) {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2016-03-22 23:38:03 +03:00
|
|
|
return mImpl->PermissionState(aOptions, aRv);
|
2016-04-02 01:25:49 +03:00
|
|
|
}
|
|
|
|
|
2015-07-23 18:30:15 +03:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2016-04-02 01:25:49 +03:00
|
|
|
already_AddRefed<Promise>
|
2016-03-22 23:38:03 +03:00
|
|
|
PushManager::PerformSubscriptionActionFromWorker(SubscriptionAction aAction,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
PushSubscriptionOptionsInit options;
|
|
|
|
return PerformSubscriptionActionFromWorker(aAction, options, aRv);
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<Promise>
|
|
|
|
PushManager::PerformSubscriptionActionFromWorker(SubscriptionAction aAction,
|
|
|
|
const PushSubscriptionOptionsInit& aOptions,
|
|
|
|
ErrorResult& aRv)
|
2016-04-02 01:25:49 +03:00
|
|
|
{
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2016-03-22 23:38:03 +03:00
|
|
|
nsTArray<uint8_t> appServerKey;
|
|
|
|
if (!aOptions.mApplicationServerKey.IsNull()) {
|
|
|
|
const OwningArrayBufferViewOrArrayBuffer& bufferSource =
|
|
|
|
aOptions.mApplicationServerKey.Value();
|
|
|
|
if (!PushUtil::CopyBufferSourceToArray(bufferSource, appServerKey) ||
|
|
|
|
appServerKey.IsEmpty()) {
|
|
|
|
p->MaybeReject(NS_ERROR_DOM_PUSH_INVALID_KEY_ERR);
|
|
|
|
return p.forget();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-02 01:25:49 +03:00
|
|
|
RefPtr<GetSubscriptionRunnable> r =
|
2016-03-22 23:38:03 +03:00
|
|
|
new GetSubscriptionRunnable(proxy, mScope, aAction, Move(appServerKey));
|
2016-04-02 01:25:49 +03:00
|
|
|
MOZ_ALWAYS_SUCCEEDS(NS_DispatchToMainThread(r));
|
|
|
|
|
|
|
|
return p.forget();
|
|
|
|
}
|
2015-07-23 18:30:15 +03:00
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|