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/. */
7 #ifndef mozilla_dom_PushNotifier_h
8 #define mozilla_dom_PushNotifier_h
10 #include "nsIPushNotifier.h"
12 #include "nsCycleCollectionParticipant.h"
13 #include "nsIPrincipal.h"
16 #include "mozilla/Maybe.h"
18 namespace mozilla::dom
{
24 * `PushDispatcher` is a base class used to forward observer notifications and
25 * service worker events to the correct process.
27 class MOZ_STACK_CLASS PushDispatcher
{
29 // Fires an XPCOM observer notification. This method may be called from both
31 virtual nsresult
NotifyObservers() = 0;
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;
37 // A convenience method that calls `NotifyObservers` and `NotifyWorkers`.
38 nsresult
NotifyObserversAndWorkers();
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;
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;
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();
54 nsIPrincipal
* GetPrincipal() { return mPrincipal
; }
57 PushDispatcher(const nsACString
& aScope
, nsIPrincipal
* aPrincipal
);
59 virtual ~PushDispatcher();
61 bool ShouldNotifyWorkers();
62 nsresult
DoNotifyObservers(nsISupports
* aSubject
, const char* aTopic
,
63 const nsACString
& aScope
);
65 const nsCString mScope
;
66 nsCOMPtr
<nsIPrincipal
> mPrincipal
;
70 * `PushNotifier` implements the `nsIPushNotifier` interface. This service
71 * broadcasts XPCOM observer notifications for incoming push messages, then
72 * forwards incoming push messages to service workers.
74 * All scriptable methods on this interface may be called from the parent or
75 * content process. Observer notifications are broadcasted to both processes.
77 class PushNotifier final
: public nsIPushNotifier
{
81 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
82 NS_DECL_CYCLE_COLLECTION_CLASS_AMBIGUOUS(PushNotifier
, nsIPushNotifier
)
83 NS_DECL_NSIPUSHNOTIFIER
88 nsresult
Dispatch(PushDispatcher
& aDispatcher
);
92 * `PushData` provides methods for retrieving push message data in different
93 * formats. This class is similar to the `PushMessageData` WebIDL interface.
95 class PushData final
: public nsIPushData
{
97 explicit PushData(const nsTArray
<uint8_t>& aData
);
99 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
100 NS_DECL_CYCLE_COLLECTION_CLASS_AMBIGUOUS(PushData
, nsIPushData
)
106 nsresult
EnsureDecodedText();
108 nsTArray
<uint8_t> mData
;
109 nsString mDecodedText
;
113 * `PushMessage` exposes the subscription principal and data for a push
114 * message. Each `push-message` observer receives an instance of this class
117 class PushMessage final
: public nsIPushMessage
{
119 PushMessage(nsIPrincipal
* aPrincipal
, nsIPushData
* aData
);
121 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
122 NS_DECL_CYCLE_COLLECTION_CLASS_AMBIGUOUS(PushMessage
, nsIPushMessage
)
123 NS_DECL_NSIPUSHMESSAGE
128 nsCOMPtr
<nsIPrincipal
> mPrincipal
;
129 nsCOMPtr
<nsIPushData
> mData
;
132 class PushMessageDispatcher final
: public PushDispatcher
{
134 PushMessageDispatcher(const nsACString
& aScope
, nsIPrincipal
* aPrincipal
,
135 const nsAString
& aMessageId
,
136 const Maybe
<nsTArray
<uint8_t>>& aData
);
137 ~PushMessageDispatcher();
139 nsresult
NotifyObservers() override
;
140 nsresult
NotifyWorkers() override
;
141 bool SendToParent(ContentChild
* aParentActor
) override
;
142 bool SendToChild(ContentParent
* aContentActor
) override
;
145 const nsString mMessageId
;
146 const Maybe
<nsTArray
<uint8_t>> mData
;
149 class PushSubscriptionChangeDispatcher final
: public PushDispatcher
{
151 PushSubscriptionChangeDispatcher(const nsACString
& aScope
,
152 nsIPrincipal
* aPrincipal
);
153 ~PushSubscriptionChangeDispatcher();
155 nsresult
NotifyObservers() override
;
156 nsresult
NotifyWorkers() override
;
157 bool SendToParent(ContentChild
* aParentActor
) override
;
158 bool SendToChild(ContentParent
* aContentActor
) override
;
161 class PushSubscriptionModifiedDispatcher
: public PushDispatcher
{
163 PushSubscriptionModifiedDispatcher(const nsACString
& aScope
,
164 nsIPrincipal
* aPrincipal
);
165 ~PushSubscriptionModifiedDispatcher();
167 nsresult
NotifyObservers() override
;
168 nsresult
NotifyWorkers() override
;
169 bool SendToParent(ContentChild
* aParentActor
) override
;
170 bool SendToChild(ContentParent
* aContentActor
) override
;
173 class PushErrorDispatcher final
: public PushDispatcher
{
175 PushErrorDispatcher(const nsACString
& aScope
, nsIPrincipal
* aPrincipal
,
176 const nsAString
& aMessage
, uint32_t aFlags
);
177 ~PushErrorDispatcher();
179 nsresult
NotifyObservers() override
;
180 nsresult
NotifyWorkers() override
;
181 bool SendToParent(ContentChild
* aParentActor
) override
;
182 bool SendToChild(ContentParent
* aContentActor
) override
;
185 nsresult
HandleNoChildProcesses() override
;
187 const nsString mMessage
;
191 } // namespace mozilla::dom
193 #endif // mozilla_dom_PushNotifier_h