Bug 1874684 - Part 4: Prefer const references instead of copying Instant values....
[gecko.git] / dom / push / PushNotifier.h
blob3718c7674e62fc6836c905273e58e4ed5c0b1be5
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"
14 #include "nsString.h"
16 #include "mozilla/Maybe.h"
18 namespace mozilla::dom {
20 class ContentChild;
21 class ContentParent;
23 /**
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 {
28 public:
29 // Fires an XPCOM observer notification. This method may be called from both
30 // processes.
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; }
56 protected:
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;
69 /**
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 {
78 public:
79 PushNotifier();
81 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
82 NS_DECL_CYCLE_COLLECTION_CLASS_AMBIGUOUS(PushNotifier, nsIPushNotifier)
83 NS_DECL_NSIPUSHNOTIFIER
85 private:
86 ~PushNotifier();
88 nsresult Dispatch(PushDispatcher& aDispatcher);
91 /**
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 {
96 public:
97 explicit PushData(const nsTArray<uint8_t>& aData);
99 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
100 NS_DECL_CYCLE_COLLECTION_CLASS_AMBIGUOUS(PushData, nsIPushData)
101 NS_DECL_NSIPUSHDATA
103 private:
104 ~PushData();
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
115 * as the subject.
117 class PushMessage final : public nsIPushMessage {
118 public:
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
125 private:
126 ~PushMessage();
128 nsCOMPtr<nsIPrincipal> mPrincipal;
129 nsCOMPtr<nsIPushData> mData;
132 class PushMessageDispatcher final : public PushDispatcher {
133 public:
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;
144 private:
145 const nsString mMessageId;
146 const Maybe<nsTArray<uint8_t>> mData;
149 class PushSubscriptionChangeDispatcher final : public PushDispatcher {
150 public:
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 {
162 public:
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 {
174 public:
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;
184 private:
185 nsresult HandleNoChildProcesses() override;
187 const nsString mMessage;
188 uint32_t mFlags;
191 } // namespace mozilla::dom
193 #endif // mozilla_dom_PushNotifier_h