Files
palemoon27/dom/push/PushManager.h
T
roytam1 9e4ed9857d import changes from `dev' branch of rmottola/Arctic-Fox:
- Bug 1183825 - Hide PushMessageData methods until we support sending push data. r=mt,smaug (c07f6b6c9)
- Bug 1186880 - Performance timing api in workers should output entries if preference is enabled. r=baku (642bd335a)
- Bug 1188091 - Fix the exposure of Push interfaces; r=dougt,bzbarsky,nsm Currently we don't check the dom.push.enabled pref in some cases for some of these interfaces.  This patch unifies how all of these interfaces are exposed to Window, Worker, and ServiceWorker. (dbe4ebfcc)
- Bug 1188062 - Unship Request.context; r=baku (6ca2ebcf6)
- Bug 1162714 - Don't let YCM generate machc. r=ehsan (7c5f36c12)
- Bug 1160897 - Fixing .ycm_extra_conf for Fennec. r=ehsan (22adf0705)
- goanna->gecko (94b69bd6c)
- improve (e87b53162)
- Bug 1147939 - The expression decompiler can't assume that it's called directly from script. (r=bhackett) (4759a5210)
- Bug 1188609 - Remove mirroring support from RokuApp (Toolkit) r=snorp (81a0bb66a)
- Bug 1050749 - Expose BatteryManager via getBattery() returning a Promise. r=bz, r=baku, r=jgraham (67d2dd502)
- Bug 1182347 - Remove nsIPrincipal::cookieJar. r=sicking (855d0e8ce)
- remove double method, probably misspach (3c256c5b4)
- Bug 1182357 - Add an API to mint nsExpandedPrincipals. r=mrbkap (42de17cfd)
- Bug 1172080 - Part 1: Throw when requesting origin for poorly behaved URIs, r=bholley (c8410f3c6)
- Bug 1124126 - Retry database connection for the case of corrupted permissions.sqlite. r=bsmedberg (748e9205a)
2021-12-23 09:44:29 +08:00

225 lines
5.6 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/. */
/**
* We would like to expose PushManager and PushSubscription on window and
* workers. Parts of the Push API is implemented in JS out of necessity due to:
* 1) Using frame message managers, in which
* nsIMessageListener::receiveMessage() must be in JS.
* 2) It is easier to use certain APIs like the permission prompt and Promises
* from JS.
*
* Unfortunately, JS-implemented WebIDL is not supported off the main thread. To
* aid in fixing this, the nsIPushClient is introduced which deals with part (1)
* above. Part (2) is handled by PushManagerImpl on the main thread. PushManager
* wraps this in C++ since our bindings code cannot accomodate "JS-implemented
* on the main thread, C++ on the worker" bindings. PushManager simply forwards
* the calls to the JS component.
*
* On the worker threads, we don't have to deal with permission prompts, instead
* we just reject calls if the principal does not have permission. On workers
* WorkerPushManager dispatches runnables to the main thread which directly call
* nsIPushClient.
*
* PushSubscription is in C++ on both threads since it isn't particularly
* verbose to implement in C++ compared to JS.
*/
#ifndef mozilla_dom_PushManager_h
#define mozilla_dom_PushManager_h
#include "nsWrapperCache.h"
#include "mozilla/AlreadyAddRefed.h"
#include "mozilla/ErrorResult.h"
#include "mozilla/dom/BindingDeclarations.h"
#include "nsCOMPtr.h"
#include "mozilla/nsRefPtr.h"
#include "jsapi.h"
class nsIGlobalObject;
class nsIPrincipal;
namespace mozilla {
namespace dom {
namespace workers {
class WorkerPrivate;
}
class Promise;
class PushManagerImpl;
class PushSubscription final : public nsISupports
, public nsWrapperCache
{
public:
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(PushSubscription)
explicit PushSubscription(nsIGlobalObject* aGlobal,
const nsAString& aEndpoint,
const nsAString& aScope);
JSObject*
WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
nsIGlobalObject*
GetParentObject() const
{
return mGlobal;
}
void
GetEndpoint(nsAString& aEndpoint) const
{
aEndpoint = mEndpoint;
}
static already_AddRefed<PushSubscription>
Constructor(GlobalObject& aGlobal, const nsAString& aEndpoint, const nsAString& aScope, ErrorResult& aRv);
void
SetPrincipal(nsIPrincipal* aPrincipal);
already_AddRefed<Promise>
Unsubscribe(ErrorResult& aRv);
protected:
~PushSubscription();
private:
nsCOMPtr<nsIGlobalObject> mGlobal;
nsCOMPtr<nsIPrincipal> mPrincipal;
nsString mEndpoint;
nsString mScope;
};
class PushManager final : public nsISupports
, public nsWrapperCache
{
public:
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(PushManager)
explicit PushManager(nsIGlobalObject* aGlobal, const nsAString& aScope);
nsIGlobalObject*
GetParentObject() const
{
return mGlobal;
}
JSObject*
WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
already_AddRefed<Promise>
Subscribe(ErrorResult& aRv);
already_AddRefed<Promise>
GetSubscription(ErrorResult& aRv);
already_AddRefed<Promise>
PermissionState(ErrorResult& aRv);
void
SetPushManagerImpl(PushManagerImpl& foo, ErrorResult& aRv);
protected:
~PushManager();
private:
nsCOMPtr<nsIGlobalObject> mGlobal;
nsRefPtr<PushManagerImpl> mImpl;
nsString mScope;
};
class WorkerPushSubscription final : public nsISupports
, public nsWrapperCache
{
public:
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(WorkerPushSubscription)
explicit WorkerPushSubscription(const nsAString& aEndpoint,
const nsAString& aScope);
nsIGlobalObject*
GetParentObject() const
{
return nullptr;
}
JSObject*
WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
static already_AddRefed<WorkerPushSubscription>
Constructor(GlobalObject& aGlobal, const nsAString& aEndpoint, const nsAString& aScope, ErrorResult& aRv);
void
GetEndpoint(nsAString& aEndpoint) const
{
aEndpoint = mEndpoint;
}
already_AddRefed<Promise>
Unsubscribe(ErrorResult& aRv);
protected:
~WorkerPushSubscription();
private:
nsString mEndpoint;
nsString mScope;
};
class WorkerPushManager final : public nsISupports
, public nsWrapperCache
{
public:
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(WorkerPushManager)
enum SubscriptionAction {
SubscribeAction,
GetSubscriptionAction,
};
explicit WorkerPushManager(const nsAString& aScope);
nsIGlobalObject*
GetParentObject() const
{
return nullptr;
}
JSObject*
WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
already_AddRefed<Promise>
PerformSubscriptionAction(SubscriptionAction aAction, ErrorResult& aRv);
already_AddRefed<Promise>
Subscribe(ErrorResult& aRv);
already_AddRefed<Promise>
GetSubscription(ErrorResult& aRv);
already_AddRefed<Promise>
PermissionState(ErrorResult& aRv);
protected:
~WorkerPushManager();
private:
nsString mScope;
};
} // namespace dom
} // namespace mozilla
#endif // mozilla_dom_PushManager_h