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/. */
|
|
|
|
|
|
|
|
/**
|
2016-04-02 01:25:49 +03:00
|
|
|
* PushManager and PushSubscription are exposed on the main and worker threads.
|
|
|
|
* The main thread version is implemented in Push.js. The JS implementation
|
|
|
|
* makes it easier to use certain APIs like the permission prompt and Promises.
|
2015-07-23 18:30:15 +03:00
|
|
|
*
|
2016-04-02 01:25:49 +03:00
|
|
|
* Unfortunately, JS-implemented WebIDL is not supported off the main thread.
|
|
|
|
* To work around this, we use a chain of runnables to query the JS-implemented
|
|
|
|
* nsIPushService component for subscription information, and return the
|
|
|
|
* results to the worker. We don't have to deal with permission prompts, since
|
|
|
|
* we just reject calls if the principal does not have permission.
|
2015-07-23 18:30:15 +03:00
|
|
|
*
|
2016-04-02 01:25:49 +03:00
|
|
|
* On the main thread, PushManager wraps a JS-implemented PushManagerImpl
|
|
|
|
* instance. The C++ wrapper is necessary because our bindings code cannot
|
|
|
|
* accomodate "JS-implemented on the main thread, C++ on the worker" bindings.
|
2015-07-23 18:30:15 +03:00
|
|
|
*
|
|
|
|
* 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"
|
2015-09-11 17:51:32 +03:00
|
|
|
#include "mozilla/dom/TypedArray.h"
|
2015-07-23 18:30:15 +03:00
|
|
|
|
|
|
|
#include "nsCOMPtr.h"
|
2016-05-05 13:21:00 +03:00
|
|
|
#include "nsContentUtils.h" // Required for nsContentUtils::PushEnabled
|
2015-10-18 08:24:48 +03:00
|
|
|
#include "mozilla/RefPtr.h"
|
2015-07-23 18:30:15 +03:00
|
|
|
|
|
|
|
class nsIGlobalObject;
|
|
|
|
class nsIPrincipal;
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
namespace workers {
|
|
|
|
class WorkerPrivate;
|
|
|
|
}
|
|
|
|
|
|
|
|
class Promise;
|
|
|
|
class PushManagerImpl;
|
2016-03-22 23:38:03 +03:00
|
|
|
struct PushSubscriptionOptionsInit;
|
2015-07-23 18:30:15 +03:00
|
|
|
|
|
|
|
class PushManager final : public nsISupports
|
|
|
|
, public nsWrapperCache
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
|
|
|
|
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(PushManager)
|
|
|
|
|
|
|
|
enum SubscriptionAction {
|
|
|
|
SubscribeAction,
|
|
|
|
GetSubscriptionAction,
|
|
|
|
};
|
|
|
|
|
2016-04-02 01:25:49 +03:00
|
|
|
// The main thread constructor.
|
|
|
|
PushManager(nsIGlobalObject* aGlobal, PushManagerImpl* aImpl);
|
|
|
|
|
|
|
|
// The worker thread constructor.
|
|
|
|
explicit PushManager(const nsAString& aScope);
|
2015-07-23 18:30:15 +03:00
|
|
|
|
|
|
|
nsIGlobalObject*
|
|
|
|
GetParentObject() const
|
|
|
|
{
|
2016-04-02 01:25:49 +03:00
|
|
|
return mGlobal;
|
2015-07-23 18:30:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
JSObject*
|
|
|
|
WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
|
|
|
|
|
2016-04-02 01:25:49 +03:00
|
|
|
static already_AddRefed<PushManager>
|
|
|
|
Constructor(GlobalObject& aGlobal, const nsAString& aScope,
|
|
|
|
ErrorResult& aRv);
|
|
|
|
|
2015-07-23 18:30:15 +03:00
|
|
|
already_AddRefed<Promise>
|
2016-04-02 01:25:49 +03:00
|
|
|
PerformSubscriptionActionFromWorker(SubscriptionAction aAction,
|
|
|
|
ErrorResult& aRv);
|
2015-07-23 18:30:15 +03:00
|
|
|
|
|
|
|
already_AddRefed<Promise>
|
2016-03-22 23:38:03 +03:00
|
|
|
PerformSubscriptionActionFromWorker(SubscriptionAction aAction,
|
|
|
|
const PushSubscriptionOptionsInit& aOptions,
|
|
|
|
ErrorResult& aRv);
|
|
|
|
|
|
|
|
already_AddRefed<Promise>
|
|
|
|
Subscribe(const PushSubscriptionOptionsInit& aOptions, ErrorResult& aRv);
|
2015-07-23 18:30:15 +03:00
|
|
|
|
|
|
|
already_AddRefed<Promise>
|
|
|
|
GetSubscription(ErrorResult& aRv);
|
|
|
|
|
|
|
|
already_AddRefed<Promise>
|
2016-03-22 23:38:03 +03:00
|
|
|
PermissionState(const PushSubscriptionOptionsInit& aOptions,
|
|
|
|
ErrorResult& aRv);
|
2015-07-23 18:30:15 +03:00
|
|
|
|
2016-03-22 23:38:03 +03:00
|
|
|
private:
|
2016-04-02 01:25:49 +03:00
|
|
|
~PushManager();
|
2015-07-23 18:30:15 +03:00
|
|
|
|
2016-04-02 01:25:49 +03:00
|
|
|
// The following are only set and accessed on the main thread.
|
|
|
|
nsCOMPtr<nsIGlobalObject> mGlobal;
|
|
|
|
RefPtr<PushManagerImpl> mImpl;
|
|
|
|
|
|
|
|
// Only used on the worker thread.
|
2015-07-23 18:30:15 +03:00
|
|
|
nsString mScope;
|
|
|
|
};
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // mozilla_dom_PushManager_h
|