-
Notifications
You must be signed in to change notification settings - Fork 919
/
Copy pathsw-controller.ts
390 lines (337 loc) · 12.2 KB
/
sw-controller.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/**
* @license
* Copyright 2017 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { deleteToken, getToken } from '../core/token-management';
import { FirebaseInternalDependencies } from '../interfaces/internal-dependencies';
import { FirebaseMessaging } from '@firebase/messaging-types';
import { ERROR_FACTORY, ErrorCode } from '../util/errors';
import {
MessagePayload,
NotificationDetails
} from '../interfaces/message-payload';
import { FCM_MSG, DEFAULT_VAPID_KEY } from '../util/constants';
import { MessageType, InternalMessage } from '../interfaces/internal-message';
import { dbGet } from '../helpers/idb-manager';
import { Unsubscribe, getBrowserExtensionRuntime } from '@firebase/util';
import { sleep } from '../helpers/sleep';
import { FirebaseApp } from '@firebase/app-types';
import { isConsoleMessage } from '../helpers/is-console-message';
import { FirebaseService } from '@firebase/app-types/private';
// Let TS know that this is a service worker
declare const self: ServiceWorkerGlobalScope;
export type BgMessageHandler = (payload: MessagePayload) => unknown;
export class SwController implements FirebaseMessaging, FirebaseService {
private vapidKey: string | null = null;
private bgMessageHandler: BgMessageHandler | null = null;
constructor(
private readonly firebaseDependencies: FirebaseInternalDependencies
) {
self.addEventListener('push', e => {
e.waitUntil(this.onPush(e));
});
self.addEventListener('pushsubscriptionchange', e => {
e.waitUntil(this.onSubChange(e));
});
self.addEventListener('notificationclick', e => {
e.waitUntil(this.onNotificationClick(e));
});
}
get app(): FirebaseApp {
return this.firebaseDependencies.app;
}
/**
* Calling setBackgroundMessageHandler will opt in to some specific
* behaviours.
* 1.) If a notification doesn't need to be shown due to a window already
* being visible, then push messages will be sent to the page.
* 2.) If a notification needs to be shown, and the message contains no
* notification data this method will be called
* and the promise it returns will be passed to event.waitUntil.
* If you do not set this callback then all push messages will let and the
* developer can handle them in a their own 'push' event callback
*
* @param callback The callback to be called when a push message is received
* and a notification must be shown. The callback will be given the data from
* the push message.
*/
setBackgroundMessageHandler(callback: BgMessageHandler): void {
if (!callback || typeof callback !== 'function') {
throw ERROR_FACTORY.create(ErrorCode.INVALID_BG_HANDLER);
}
this.bgMessageHandler = callback;
}
// TODO: Remove getToken from SW Controller.
// Calling this from an old SW can cause all kinds of trouble.
async getToken(): Promise<string> {
if (!this.vapidKey) {
// Call getToken using the current VAPID key if there already is a token.
// This is needed because usePublicVapidKey was not available in SW.
// It will be removed when vapidKey becomes a parameter of getToken, or
// when getToken is removed from SW.
const tokenDetails = await dbGet(this.firebaseDependencies);
this.vapidKey =
tokenDetails?.subscriptionOptions?.vapidKey ?? DEFAULT_VAPID_KEY;
}
return getToken(
this.firebaseDependencies,
self.registration,
this.vapidKey
);
}
// TODO: Remove deleteToken from SW Controller.
// Calling this from an old SW can cause all kinds of trouble.
deleteToken(): Promise<boolean> {
return deleteToken(this.firebaseDependencies, self.registration);
}
requestPermission(): Promise<void> {
throw ERROR_FACTORY.create(ErrorCode.AVAILABLE_IN_WINDOW);
}
// TODO: Deprecate this and make VAPID key a parameter in getToken.
// TODO: Remove this together with getToken from SW Controller.
usePublicVapidKey(vapidKey: string): void {
if (this.vapidKey !== null) {
throw ERROR_FACTORY.create(ErrorCode.USE_VAPID_KEY_AFTER_GET_TOKEN);
}
if (typeof vapidKey !== 'string' || vapidKey.length === 0) {
throw ERROR_FACTORY.create(ErrorCode.INVALID_VAPID_KEY);
}
this.vapidKey = vapidKey;
}
useServiceWorker(): void {
throw ERROR_FACTORY.create(ErrorCode.AVAILABLE_IN_WINDOW);
}
onMessage(): Unsubscribe {
throw ERROR_FACTORY.create(ErrorCode.AVAILABLE_IN_WINDOW);
}
onTokenRefresh(): Unsubscribe {
throw ERROR_FACTORY.create(ErrorCode.AVAILABLE_IN_WINDOW);
}
/**
* A handler for push events that shows notifications based on the content of
* the payload.
*
* The payload must be a JSON-encoded Object with a `notification` key. The
* value of the `notification` property will be used as the NotificationOptions
* object passed to showNotification. Additionally, the `title` property of the
* notification object will be used as the title.
*
* If there is no notification data in the payload then no notification will be
* shown.
*/
async onPush(event: PushEvent): Promise<void> {
const payload = getMessagePayload(event);
if (!payload) {
return;
}
const clientList = await getClientList();
if (await hasVisibleClients(clientList)) {
// App in foreground. Send to page.
return sendMessageToWindowClients(clientList, payload);
}
const notificationDetails = getNotificationData(payload);
if (notificationDetails) {
await showNotification(notificationDetails);
} else if (this.bgMessageHandler) {
await this.bgMessageHandler(payload);
}
}
async onSubChange(event: PushSubscriptionChangeEvent): Promise<void> {
const { newSubscription } = event;
if (!newSubscription) {
// Subscription revoked, delete token
await deleteToken(this.firebaseDependencies, self.registration);
return;
}
const tokenDetails = await dbGet(this.firebaseDependencies);
await deleteToken(this.firebaseDependencies, self.registration);
await getToken(
this.firebaseDependencies,
self.registration,
tokenDetails?.subscriptionOptions?.vapidKey ?? DEFAULT_VAPID_KEY
);
}
async onNotificationClick(event: NotificationEvent): Promise<void> {
const payload: MessagePayload = event.notification?.data?.[FCM_MSG];
if (!payload) {
// Not an FCM notification, do nothing.
return;
} else if (event.action) {
// User clicked on an action button.
// This will allow devs to act on action button clicks by using a custom
// onNotificationClick listener that they define.
return;
}
// Prevent other listeners from receiving the event
event.stopImmediatePropagation();
event.notification.close();
const link = getLink(payload);
if (!link) {
return;
}
let client = await getWindowClient(link);
if (!client) {
// Unable to find window client so need to open one.
// This also focuses the opened client.
client = await self.clients.openWindow(link);
// Wait three seconds for the client to initialize and set up the message
// handler so that it can receive the message.
await sleep(3000);
} else {
client = await client.focus();
}
if (!client) {
// Window Client will not be returned if it's for a third party origin.
return;
}
const message = createNewMessage(MessageType.NOTIFICATION_CLICKED, payload);
return client.postMessage(message);
}
}
function getMessagePayload({ data }: PushEvent): MessagePayload | null {
if (!data) {
return null;
}
try {
return data.json();
} catch (err) {
// Not JSON so not an FCM message.
return null;
}
}
function getNotificationData(
payload: MessagePayload
): NotificationDetails | undefined {
if (!payload || typeof payload.notification !== 'object') {
return;
}
const notificationInformation: NotificationDetails = {
...payload.notification
};
// Put the message payload under FCM_MSG name so we can identify the
// notification as being an FCM notification vs a notification from
// somewhere else (i.e. normal web push or developer generated
// notification).
notificationInformation.data = {
...payload.notification.data,
[FCM_MSG]: payload
};
return notificationInformation;
}
/**
* @param url The URL to look for when focusing a client.
* @return Returns an existing window client or a newly opened WindowClient.
*/
async function getWindowClient(url: string): Promise<WindowClient | null> {
// Use URL to normalize the URL when comparing to windowClients.
// This at least handles whether to include trailing slashes or not
const parsedURL = new URL(url, self.location.href).href;
const clientList = await getClientList();
for (const client of clientList) {
const parsedClientUrl = new URL(client.url, self.location.href).href;
if (parsedClientUrl === parsedURL) {
return client;
}
}
return null;
}
/**
* @returns If there is currently a visible WindowClient, this method will
* resolve to true, otherwise false.
*/
async function hasVisibleClients(clientList: WindowClient[]): Promise<boolean> {
const checkedClientList = await Promise.all(
clientList.map(
async client =>
client.visibilityState === 'visible' &&
// Ignore browser extension clients as that matches the background pages
// of extensions, which are always considered visible for some reason.
!(await isBackgroundClient(client))
)
);
return checkedClientList.some(item => item);
}
/**
* @returns If client is the background page of browser extension, this method will
* resolve to true, otherwise false.
*/
async function isBackgroundClient(client: WindowClient): Promise<boolean> {
const runtime = getBrowserExtensionRuntime();
if (runtime) {
try {
const backgroundClient = await runtime.getBackgroundClient();
return client === backgroundClient;
} catch {
return false;
}
}
return false;
}
/**
* @param payload The data from the push event that should be sent to all
* available pages.
* @returns Returns a promise that resolves once the message has been sent to
* all WindowClients.
*/
function sendMessageToWindowClients(
clientList: WindowClient[],
payload: MessagePayload
): void {
const message = createNewMessage(MessageType.PUSH_RECEIVED, payload);
for (const client of clientList) {
client.postMessage(message);
}
}
function getClientList(): Promise<WindowClient[]> {
return self.clients.matchAll({
type: 'window',
includeUncontrolled: true
// TS doesn't know that "type: 'window'" means it'll return WindowClient[]
}) as Promise<WindowClient[]>;
}
function createNewMessage(
type: MessageType,
payload: MessagePayload
): InternalMessage {
return {
firebaseMessaging: { type, payload }
};
}
function showNotification(details: NotificationDetails): Promise<void> {
const title = details.title ?? '';
const { actions } = details;
// Note: Firefox does not support the maxActions property.
// https://developer.mozilla.org/en-US/docs/Web/API/notification/maxActions
const { maxActions } = Notification;
if (actions && maxActions && actions.length > maxActions) {
console.warn(
`This browser only supports ${maxActions} actions. The remaining actions will not be displayed.`
);
}
return self.registration.showNotification(title, details);
}
function getLink(payload: MessagePayload): string | null {
// eslint-disable-next-line camelcase
const link = payload.fcmOptions?.link ?? payload.notification?.click_action;
if (link) {
return link;
}
if (isConsoleMessage(payload.data)) {
// Notification created in the Firebase Console. Redirect to origin.
return self.location.origin;
} else {
return null;
}
}