Line data Source code
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/. */
6 :
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.
11 : *
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.
17 : *
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.
21 : *
22 : * PushSubscription is in C++ on both threads since it isn't particularly
23 : * verbose to implement in C++ compared to JS.
24 : */
25 :
26 : #ifndef mozilla_dom_PushManager_h
27 : #define mozilla_dom_PushManager_h
28 :
29 : #include "nsWrapperCache.h"
30 :
31 : #include "mozilla/AlreadyAddRefed.h"
32 : #include "mozilla/ErrorResult.h"
33 : #include "mozilla/dom/BindingDeclarations.h"
34 : #include "mozilla/dom/TypedArray.h"
35 :
36 : #include "nsCOMPtr.h"
37 : #include "nsContentUtils.h" // Required for nsContentUtils::PushEnabled
38 : #include "mozilla/RefPtr.h"
39 :
40 : class nsIGlobalObject;
41 : class nsIPrincipal;
42 :
43 : namespace mozilla {
44 : namespace dom {
45 :
46 : namespace workers {
47 : class WorkerPrivate;
48 : }
49 :
50 : class OwningArrayBufferViewOrArrayBufferOrString;
51 : class Promise;
52 : class PushManagerImpl;
53 : struct PushSubscriptionOptionsInit;
54 :
55 : class PushManager final : public nsISupports
56 : , public nsWrapperCache
57 : {
58 : public:
59 : NS_DECL_CYCLE_COLLECTING_ISUPPORTS
60 0 : NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(PushManager)
61 :
62 : enum SubscriptionAction {
63 : SubscribeAction,
64 : GetSubscriptionAction,
65 : };
66 :
67 : // The main thread constructor.
68 : PushManager(nsIGlobalObject* aGlobal, PushManagerImpl* aImpl);
69 :
70 : // The worker thread constructor.
71 : explicit PushManager(const nsAString& aScope);
72 :
73 : nsIGlobalObject*
74 0 : GetParentObject() const
75 : {
76 0 : return mGlobal;
77 : }
78 :
79 : JSObject*
80 : WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
81 :
82 : static already_AddRefed<PushManager>
83 : Constructor(GlobalObject& aGlobal, const nsAString& aScope,
84 : ErrorResult& aRv);
85 :
86 : already_AddRefed<Promise>
87 : PerformSubscriptionActionFromWorker(SubscriptionAction aAction,
88 : ErrorResult& aRv);
89 :
90 : already_AddRefed<Promise>
91 : PerformSubscriptionActionFromWorker(SubscriptionAction aAction,
92 : const PushSubscriptionOptionsInit& aOptions,
93 : ErrorResult& aRv);
94 :
95 : already_AddRefed<Promise>
96 : Subscribe(const PushSubscriptionOptionsInit& aOptions, ErrorResult& aRv);
97 :
98 : already_AddRefed<Promise>
99 : GetSubscription(ErrorResult& aRv);
100 :
101 : already_AddRefed<Promise>
102 : PermissionState(const PushSubscriptionOptionsInit& aOptions,
103 : ErrorResult& aRv);
104 :
105 : private:
106 : ~PushManager();
107 :
108 : nsresult
109 : NormalizeAppServerKey(const OwningArrayBufferViewOrArrayBufferOrString& aSource,
110 : nsTArray<uint8_t>& aAppServerKey);
111 :
112 : // The following are only set and accessed on the main thread.
113 : nsCOMPtr<nsIGlobalObject> mGlobal;
114 : RefPtr<PushManagerImpl> mImpl;
115 :
116 : // Only used on the worker thread.
117 : nsString mScope;
118 : };
119 : } // namespace dom
120 : } // namespace mozilla
121 :
122 : #endif // mozilla_dom_PushManager_h
|