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