/src/mozilla-central/dom/push/PushNotifier.h
Line | Count | Source (jump to first uncovered line) |
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 |
5 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
6 | | |
7 | | #ifndef mozilla_dom_PushNotifier_h |
8 | | #define mozilla_dom_PushNotifier_h |
9 | | |
10 | | #include "nsIPushNotifier.h" |
11 | | |
12 | | #include "nsCycleCollectionParticipant.h" |
13 | | #include "nsIPrincipal.h" |
14 | | #include "nsString.h" |
15 | | |
16 | | #include "mozilla/Maybe.h" |
17 | | |
18 | | namespace mozilla { |
19 | | namespace dom { |
20 | | |
21 | | class ContentChild; |
22 | | class ContentParent; |
23 | | |
24 | | /** |
25 | | * `PushDispatcher` is a base class used to forward observer notifications and |
26 | | * service worker events to the correct process. |
27 | | */ |
28 | | class MOZ_STACK_CLASS PushDispatcher |
29 | | { |
30 | | public: |
31 | | // Fires an XPCOM observer notification. This method may be called from both |
32 | | // processes. |
33 | | virtual nsresult NotifyObservers() = 0; |
34 | | |
35 | | // Fires a service worker event. This method is called from the content |
36 | | // process if e10s is enabled, or the parent otherwise. |
37 | | virtual nsresult NotifyWorkers() = 0; |
38 | | |
39 | | // A convenience method that calls `NotifyObservers` and `NotifyWorkers`. |
40 | | nsresult NotifyObserversAndWorkers(); |
41 | | |
42 | | // Sends an IPDL message to fire an observer notification in the parent |
43 | | // process. This method is only called from the content process, and only |
44 | | // if e10s is enabled. |
45 | | virtual bool SendToParent(ContentChild* aParentActor) = 0; |
46 | | |
47 | | // Sends an IPDL message to fire an observer notification and a service worker |
48 | | // event in the content process. This method is only called from the parent, |
49 | | // and only if e10s is enabled. |
50 | | virtual bool SendToChild(ContentParent* aContentActor) = 0; |
51 | | |
52 | | // An optional method, called from the parent if e10s is enabled and there |
53 | | // are no active content processes. The default behavior is a no-op. |
54 | | virtual nsresult HandleNoChildProcesses(); |
55 | | |
56 | 0 | nsIPrincipal* GetPrincipal() { |
57 | 0 | return mPrincipal; |
58 | 0 | } |
59 | | |
60 | | protected: |
61 | | PushDispatcher(const nsACString& aScope, |
62 | | nsIPrincipal* aPrincipal); |
63 | | |
64 | | virtual ~PushDispatcher(); |
65 | | |
66 | | bool ShouldNotifyWorkers(); |
67 | | nsresult DoNotifyObservers(nsISupports *aSubject, const char *aTopic, |
68 | | const nsACString& aScope); |
69 | | |
70 | | const nsCString mScope; |
71 | | nsCOMPtr<nsIPrincipal> mPrincipal; |
72 | | }; |
73 | | |
74 | | /** |
75 | | * `PushNotifier` implements the `nsIPushNotifier` interface. This service |
76 | | * broadcasts XPCOM observer notifications for incoming push messages, then |
77 | | * forwards incoming push messages to service workers. |
78 | | * |
79 | | * All scriptable methods on this interface may be called from the parent or |
80 | | * content process. Observer notifications are broadcasted to both processes. |
81 | | */ |
82 | | class PushNotifier final : public nsIPushNotifier |
83 | | { |
84 | | public: |
85 | | PushNotifier(); |
86 | | |
87 | | NS_DECL_CYCLE_COLLECTING_ISUPPORTS |
88 | | NS_DECL_CYCLE_COLLECTION_CLASS_AMBIGUOUS(PushNotifier, nsIPushNotifier) |
89 | | NS_DECL_NSIPUSHNOTIFIER |
90 | | |
91 | | private: |
92 | | ~PushNotifier(); |
93 | | |
94 | | nsresult Dispatch(PushDispatcher& aDispatcher); |
95 | | }; |
96 | | |
97 | | /** |
98 | | * `PushData` provides methods for retrieving push message data in different |
99 | | * formats. This class is similar to the `PushMessageData` WebIDL interface. |
100 | | */ |
101 | | class PushData final : public nsIPushData |
102 | | { |
103 | | public: |
104 | | explicit PushData(const nsTArray<uint8_t>& aData); |
105 | | |
106 | | NS_DECL_CYCLE_COLLECTING_ISUPPORTS |
107 | | NS_DECL_CYCLE_COLLECTION_CLASS_AMBIGUOUS(PushData, nsIPushData) |
108 | | NS_DECL_NSIPUSHDATA |
109 | | |
110 | | private: |
111 | | ~PushData(); |
112 | | |
113 | | nsresult EnsureDecodedText(); |
114 | | |
115 | | nsTArray<uint8_t> mData; |
116 | | nsString mDecodedText; |
117 | | }; |
118 | | |
119 | | /** |
120 | | * `PushMessage` exposes the subscription principal and data for a push |
121 | | * message. Each `push-message` observer receives an instance of this class |
122 | | * as the subject. |
123 | | */ |
124 | | class PushMessage final : public nsIPushMessage |
125 | | { |
126 | | public: |
127 | | PushMessage(nsIPrincipal* aPrincipal, nsIPushData* aData); |
128 | | |
129 | | NS_DECL_CYCLE_COLLECTING_ISUPPORTS |
130 | | NS_DECL_CYCLE_COLLECTION_CLASS_AMBIGUOUS(PushMessage, nsIPushMessage) |
131 | | NS_DECL_NSIPUSHMESSAGE |
132 | | |
133 | | private: |
134 | | ~PushMessage(); |
135 | | |
136 | | nsCOMPtr<nsIPrincipal> mPrincipal; |
137 | | nsCOMPtr<nsIPushData> mData; |
138 | | }; |
139 | | |
140 | | class PushMessageDispatcher final : public PushDispatcher |
141 | | { |
142 | | public: |
143 | | PushMessageDispatcher(const nsACString& aScope, |
144 | | nsIPrincipal* aPrincipal, |
145 | | const nsAString& aMessageId, |
146 | | const Maybe<nsTArray<uint8_t>>& aData); |
147 | | ~PushMessageDispatcher(); |
148 | | |
149 | | nsresult NotifyObservers() override; |
150 | | nsresult NotifyWorkers() override; |
151 | | bool SendToParent(ContentChild* aParentActor) override; |
152 | | bool SendToChild(ContentParent* aContentActor) override; |
153 | | |
154 | | private: |
155 | | const nsString mMessageId; |
156 | | const Maybe<nsTArray<uint8_t>> mData; |
157 | | }; |
158 | | |
159 | | class PushSubscriptionChangeDispatcher final : public PushDispatcher |
160 | | { |
161 | | public: |
162 | | PushSubscriptionChangeDispatcher(const nsACString& aScope, |
163 | | nsIPrincipal* aPrincipal); |
164 | | ~PushSubscriptionChangeDispatcher(); |
165 | | |
166 | | nsresult NotifyObservers() override; |
167 | | nsresult NotifyWorkers() override; |
168 | | bool SendToParent(ContentChild* aParentActor) override; |
169 | | bool SendToChild(ContentParent* aContentActor) override; |
170 | | }; |
171 | | |
172 | | class PushSubscriptionModifiedDispatcher : public PushDispatcher |
173 | | { |
174 | | public: |
175 | | PushSubscriptionModifiedDispatcher(const nsACString& aScope, |
176 | | nsIPrincipal* aPrincipal); |
177 | | ~PushSubscriptionModifiedDispatcher(); |
178 | | |
179 | | nsresult NotifyObservers() override; |
180 | | nsresult NotifyWorkers() override; |
181 | | bool SendToParent(ContentChild* aParentActor) override; |
182 | | bool SendToChild(ContentParent* aContentActor) override; |
183 | | }; |
184 | | |
185 | | class PushErrorDispatcher final : public PushDispatcher |
186 | | { |
187 | | public: |
188 | | PushErrorDispatcher(const nsACString& aScope, |
189 | | nsIPrincipal* aPrincipal, |
190 | | const nsAString& aMessage, |
191 | | uint32_t aFlags); |
192 | | ~PushErrorDispatcher(); |
193 | | |
194 | | nsresult NotifyObservers() override; |
195 | | nsresult NotifyWorkers() override; |
196 | | bool SendToParent(ContentChild* aParentActor) override; |
197 | | bool SendToChild(ContentParent* aContentActor) override; |
198 | | |
199 | | private: |
200 | | nsresult HandleNoChildProcesses() override; |
201 | | |
202 | | const nsString mMessage; |
203 | | uint32_t mFlags; |
204 | | }; |
205 | | |
206 | | } // namespace dom |
207 | | } // namespace mozilla |
208 | | |
209 | | #endif // mozilla_dom_PushNotifier_h |