Bug 1885602 - Part 5: Implement navigating to the SUMO help topic from the menu heade...
[gecko.git] / dom / push / PushManager.h
blobf951e0a2baa7963d1de9ac74bca70d3df4cec27b
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/. */
7 /**
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"
35 #include "nsCOMPtr.h"
36 #include "mozilla/RefPtr.h"
38 class nsIGlobalObject;
39 class nsIPrincipal;
41 namespace mozilla {
42 class ErrorResult;
44 namespace dom {
46 class OwningArrayBufferViewOrArrayBufferOrString;
47 class Promise;
48 class PushManagerImpl;
49 struct PushSubscriptionOptionsInit;
50 class WorkerPrivate;
52 class PushManager final : public nsISupports, public nsWrapperCache {
53 public:
54 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
55 NS_DECL_CYCLE_COLLECTION_WRAPPERCACHE_CLASS(PushManager)
57 enum SubscriptionAction {
58 SubscribeAction,
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,
75 ErrorResult& aRv);
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,
84 ErrorResult& aRv);
86 already_AddRefed<Promise> Subscribe(
87 const PushSubscriptionOptionsInit& aOptions, ErrorResult& aRv);
89 already_AddRefed<Promise> GetSubscription(ErrorResult& aRv);
91 already_AddRefed<Promise> PermissionState(
92 const PushSubscriptionOptionsInit& aOptions, ErrorResult& aRv);
94 private:
95 ~PushManager();
97 nsresult NormalizeAppServerKey(
98 const OwningArrayBufferViewOrArrayBufferOrString& aSource,
99 nsTArray<uint8_t>& aAppServerKey);
101 // The following are only set and accessed on the main thread.
102 nsCOMPtr<nsIGlobalObject> mGlobal;
103 RefPtr<PushManagerImpl> mImpl;
105 // Only used on the worker thread.
106 nsString mScope;
108 } // namespace dom
109 } // namespace mozilla
111 #endif // mozilla_dom_PushManager_h