@@ -34,6 +34,10 @@ import {
3434} from '../models/worker-page-message' ;
3535import { ControllerInterface } from './controller-interface' ;
3636
37+ interface ManifestContent {
38+ gcm_sender_id : string ;
39+ }
40+
3741export class WindowController extends ControllerInterface {
3842 private registrationToUse : ServiceWorkerRegistration | null = null ;
3943 private publicVapidKeyToUse : Uint8Array | null = null ;
@@ -73,17 +77,17 @@ export class WindowController extends ControllerInterface {
7377 * @return Returns a promise that resolves to an FCM token or null if
7478 * permission isn't granted.
7579 */
76- getToken ( ) : Promise < string | null > {
80+ async getToken ( ) : Promise < string | null > {
7781 // Check that the required API's are available
7882 if ( ! this . isSupported_ ( ) ) {
79- return Promise . reject (
80- errorFactory . create ( ERROR_CODES . UNSUPPORTED_BROWSER )
81- ) ;
83+ throw errorFactory . create ( ERROR_CODES . UNSUPPORTED_BROWSER ) ;
8284 }
8385
84- return this . manifestCheck_ ( ) . then ( ( ) => {
85- return super . getToken ( ) ;
86- } ) ;
86+ if ( ! this . manifestCheckPromise ) {
87+ this . manifestCheckPromise = this . manifestCheck_ ( ) ;
88+ }
89+ await this . manifestCheckPromise ;
90+ return super . getToken ( ) ;
8791 }
8892
8993 /**
@@ -94,41 +98,32 @@ export class WindowController extends ControllerInterface {
9498 */
9599 // Visible for testing
96100 // TODO: make private
97- manifestCheck_ ( ) : Promise < void > {
98- if ( this . manifestCheckPromise ) {
99- return this . manifestCheckPromise ;
100- }
101-
101+ async manifestCheck_ ( ) : Promise < void > {
102102 const manifestTag = document . querySelector < HTMLAnchorElement > (
103103 'link[rel="manifest"]'
104104 ) ;
105+
105106 if ( ! manifestTag ) {
106- this . manifestCheckPromise = Promise . resolve ( ) ;
107- } else {
108- this . manifestCheckPromise = fetch ( manifestTag . href )
109- . then ( response => {
110- return response . json ( ) ;
111- } )
112- . catch ( ( ) => {
113- // If the download or parsing fails allow check.
114- // We only want to error if we KNOW that the gcm_sender_id is incorrect.
115- } )
116- . then ( manifestContent => {
117- if ( ! manifestContent ) {
118- return ;
119- }
120-
121- if ( ! manifestContent [ 'gcm_sender_id' ] ) {
122- return ;
123- }
124-
125- if ( manifestContent [ 'gcm_sender_id' ] !== '103953800507' ) {
126- throw errorFactory . create ( ERROR_CODES . INCORRECT_GCM_SENDER_ID ) ;
127- }
128- } ) ;
107+ return ;
108+ }
109+
110+ let manifestContent : ManifestContent ;
111+ try {
112+ const response = await fetch ( manifestTag . href ) ;
113+ manifestContent = await response . json ( ) ;
114+ } catch ( e ) {
115+ // If the download or parsing fails allow check.
116+ // We only want to error if we KNOW that the gcm_sender_id is incorrect.
117+ return ;
129118 }
130119
131- return this . manifestCheckPromise ;
120+ if ( ! manifestContent || ! manifestContent . gcm_sender_id ) {
121+ return ;
122+ }
123+
124+ if ( manifestContent . gcm_sender_id !== '103953800507' ) {
125+ throw errorFactory . create ( ERROR_CODES . INCORRECT_GCM_SENDER_ID ) ;
126+ }
132127 }
133128
134129 /**
@@ -339,12 +334,12 @@ export class WindowController extends ControllerInterface {
339334 * This will return the default VAPID key or the uint8array version of the public VAPID key
340335 * provided by the developer.
341336 */
342- getPublicVapidKey_ ( ) : Promise < Uint8Array > {
337+ async getPublicVapidKey_ ( ) : Promise < Uint8Array > {
343338 if ( this . publicVapidKeyToUse ) {
344- return Promise . resolve ( this . publicVapidKeyToUse ) ;
339+ return this . publicVapidKeyToUse ;
345340 }
346341
347- return Promise . resolve ( DEFAULT_PUBLIC_VAPID_KEY ) ;
342+ return DEFAULT_PUBLIC_VAPID_KEY ;
348343 }
349344
350345 /**
0 commit comments