1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
8 * PushManager and PushSubscription are exposed on the main and worker threads.
9 * The main thread version is implemented in Push.js. The JS implementation
10 * makes it easier to use certain APIs like the permission prompt and Promises.
12 * Unfortunately, JS-implemented WebIDL is not supported off the main thread.
13 * To work around this, we use a chain of runnables to query the JS-implemented
14 * nsIPushService component for subscription information, and return the
15 * results to the worker. We don't have to deal with permission prompts, since
16 * we just reject calls if the principal does not have permission.
18 * On the main thread, PushManager wraps a JS-implemented PushManagerImpl
19 * instance. The C++ wrapper is necessary because our bindings code cannot
20 * accomodate "JS-implemented on the main thread, C++ on the worker" bindings.
22 * PushSubscription is in C++ on both threads since it isn't particularly
23 * verbose to implement in C++ compared to JS.
26 #ifndef mozilla_dom_PushManager_h
27 #define mozilla_dom_PushManager_h
29 #include "nsWrapperCache.h"
31 #include "mozilla/AlreadyAddRefed.h"
32 #include "mozilla/dom/BindingDeclarations.h"
33 #include "mozilla/dom/TypedArray.h"
36 #include "mozilla/RefPtr.h"
38 class nsIGlobalObject
;
46 class OwningArrayBufferViewOrArrayBufferOrString
;
48 class PushManagerImpl
;
49 struct PushSubscriptionOptionsInit
;
52 class PushManager final
: public nsISupports
, public nsWrapperCache
{
54 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
55 NS_DECL_CYCLE_COLLECTION_WRAPPERCACHE_CLASS(PushManager
)
57 enum SubscriptionAction
{
59 GetSubscriptionAction
,
62 // The main thread constructor.
63 PushManager(nsIGlobalObject
* aGlobal
, PushManagerImpl
* aImpl
);
65 // The worker thread constructor.
66 explicit PushManager(const nsAString
& aScope
);
68 nsIGlobalObject
* GetParentObject() const { return mGlobal
; }
70 JSObject
* WrapObject(JSContext
* aCx
,
71 JS::Handle
<JSObject
*> aGivenProto
) override
;
73 static already_AddRefed
<PushManager
> Constructor(GlobalObject
& aGlobal
,
74 const nsAString
& aScope
,
77 static bool IsEnabled(JSContext
* aCx
, JSObject
* aGlobal
);
79 already_AddRefed
<Promise
> PerformSubscriptionActionFromWorker(
80 SubscriptionAction aAction
, ErrorResult
& aRv
);
82 already_AddRefed
<Promise
> PerformSubscriptionActionFromWorker(
83 SubscriptionAction aAction
, const PushSubscriptionOptionsInit
& aOptions
,
88 static void GetSupportedContentEncodings(
89 GlobalObject
& aGlobal
, JS::MutableHandle
<JSObject
*> aEncodings
,
92 already_AddRefed
<Promise
> Subscribe(
93 const PushSubscriptionOptionsInit
& aOptions
, ErrorResult
& aRv
);
95 already_AddRefed
<Promise
> GetSubscription(ErrorResult
& aRv
);
97 already_AddRefed
<Promise
> PermissionState(
98 const PushSubscriptionOptionsInit
& aOptions
, ErrorResult
& aRv
);
103 nsresult
NormalizeAppServerKey(
104 const OwningArrayBufferViewOrArrayBufferOrString
& aSource
,
105 nsTArray
<uint8_t>& aAppServerKey
);
107 // The following are only set and accessed on the main thread.
108 nsCOMPtr
<nsIGlobalObject
> mGlobal
;
109 RefPtr
<PushManagerImpl
> mImpl
;
111 // Only used on the worker thread.
115 } // namespace mozilla
117 #endif // mozilla_dom_PushManager_h