@@ -57,7 +57,7 @@ class Background {
5757 this . survey = new Survey ( ) ;
5858 this . exemptTabStatus = new Map ( ) ;
5959 this . fxaEndpoints = new Map ( ) ;
60- this . proxyState = PROXY_STATE_UNAUTHENTICATED ;
60+ this . proxyState = PROXY_STATE_LOADING ;
6161 this . webSocketConnectionIsolationCounter = 0 ;
6262 this . nextExpireTime = 0 ;
6363
@@ -123,10 +123,6 @@ class Background {
123123
124124 // Handle header errors before we render the response
125125 browser . webRequest . onHeadersReceived . addListener ( details => {
126- if ( this . proxyState === PROXY_STATE_OFFLINE ) {
127- return { } ;
128- }
129-
130126 let hasWarpError = ! ! details . responseHeaders . find ( ( header ) => {
131127 return header . name === "cf-warp-error" && header . value === "1" ;
132128 } ) ;
@@ -160,7 +156,8 @@ class Background {
160156 } , { urls : [ "http://*/*" ] } , [ "responseHeaders" , "blocking" ] ) ;
161157
162158 browser . webRequest . onHeadersReceived . addListener ( details => {
163- if ( this . proxyState !== PROXY_STATE_CONNECTING ) {
159+ if ( this . proxyState !== PROXY_STATE_CONNECTING &&
160+ this . proxyState !== PROXY_STATE_OFFLINE ) {
164161 return ;
165162 }
166163
@@ -218,25 +215,32 @@ class Background {
218215 // go back online. It fetches all the required resources and it computes the
219216 // proxy state.
220217 async run ( ) {
218+ log ( "run!" ) ;
219+
221220 clearTimeout ( this . runTimeoutId ) ;
222221
223222 if ( this . fxaEndpoints . size === 0 ) {
224- this . proxyState = PROXY_STATE_LOADING ;
223+ const previousProxyState = this . proxyState ;
224+
225225 // Let's fetch the well-known data.
226226 let wellKnownData = await this . fetchWellKnownData ( ) ;
227227 if ( ! wellKnownData ) {
228- this . proxyState = PROXY_STATE_OFFLINE ;
229- this . updateUI ( ) ;
228+ log ( "failed to fetch well-known resources" ) ;
229+
230+ // We are offline. Let's show the 'offline' view, and let's try to
231+ // fetch the well-known data again later.
232+ this . setOfflineAndStartRecoveringTimer ( ) ;
233+
234+ if ( previousProxyState !== PROXY_STATE_OFFLINE ) {
235+ this . updateUI ( ) ;
236+ }
230237
231- this . runTimeoutId = setTimeout ( _ => this . run ( ) , RUN_TIMEOUT ) ;
232238 return ;
233239 }
234- }
235240
236- // Better to be in this state to compute the new one, but we don't want to
237- // update the UI, right now, because maybe the user is already
238- // authenticated.
239- this . proxyState = PROXY_STATE_UNAUTHENTICATED ;
241+ // Better to be in this state to compute the new one.
242+ this . proxyState = PROXY_STATE_LOADING ;
243+ }
240244
241245 // Here we generate the current proxy state.
242246 await this . computeProxyState ( ) ;
@@ -245,6 +249,15 @@ class Background {
245249 this . updateUI ( ) ;
246250 }
247251
252+ setOfflineAndStartRecoveringTimer ( ) {
253+ log ( "set offline state and start the timer" ) ;
254+
255+ this . proxyState = PROXY_STATE_OFFLINE ;
256+
257+ clearTimeout ( this . runTimeoutId ) ;
258+ this . runTimeoutId = setTimeout ( _ => this . run ( ) , RUN_TIMEOUT ) ;
259+ }
260+
248261 getTranslation ( stringName , ...args ) {
249262 if ( args . length > 0 ) {
250263 return browser . i18n . getMessage ( stringName , ...args ) ;
@@ -281,16 +294,17 @@ class Background {
281294 return ;
282295 }
283296
284- if ( this . proxyState === PROXY_STATE_CONNECTING &&
297+ if ( ( this . proxyState === PROXY_STATE_CONNECTING ||
298+ this . proxyState === PROXY_STATE_OFFLINE ) &&
285299 url === CONNECTING_HTTP_REQUEST &&
286300 ( errorStatus === "NS_ERROR_UNKNOWN_PROXY_HOST" ||
287301 errorStatus === "NS_ERROR_ABORT" ) ) {
288- this . proxyState = PROXY_STATE_OFFLINE ;
302+ this . setOfflineAndStartRecoveringTimer ( ) ;
289303 this . updateUI ( ) ;
290304 }
291305 }
292306
293- showStatusPrompt ( ) {
307+ async showStatusPrompt ( ) {
294308 // No need to show the toast if the panel is visible.
295309 if ( this . currentPort ) {
296310 return ;
@@ -321,7 +335,7 @@ class Background {
321335 break ;
322336 }
323337
324- if ( this . isCurrentTabExempt ( ) ) {
338+ if ( await this . isCurrentTabExempt ( ) ) {
325339 promptNotice = "toastWarning" ;
326340 isWarning = true ;
327341 }
@@ -341,17 +355,16 @@ class Background {
341355 this . tokenGenerationTimeout = 0 ;
342356 }
343357
344- // We are offline.
345- if ( ! navigator . onLine || this . fxaEndpoints . size === 0 ) {
346- this . proxyState = PROXY_STATE_OFFLINE ;
358+ // The run() failed to fetch the well-known resources. We are offline.
359+ if ( this . fxaEndpoints . size === 0 ) {
360+ this . setOfflineAndStartRecoveringTimer ( ) ;
347361 }
348362
349363 // We want to keep these states.
350364 let currentState = this . proxyState ;
351365 if ( currentState !== PROXY_STATE_AUTHFAILURE &&
352366 currentState !== PROXY_STATE_PROXYERROR &&
353- currentState !== PROXY_STATE_PROXYAUTHFAILED &&
354- currentState !== PROXY_STATE_OFFLINE ) {
367+ currentState !== PROXY_STATE_PROXYAUTHFAILED ) {
355368 this . proxyState = PROXY_STATE_UNAUTHENTICATED ;
356369 }
357370
@@ -546,8 +559,10 @@ class Background {
546559 return false ;
547560 }
548561
549- // If we are 'connecting', we want to allow just the CONNECTING_HTTP_REQUEST.
550- if ( this . proxyState === PROXY_STATE_CONNECTING ) {
562+ // If we are 'connecting' or 'offline' state, we want to allow just the
563+ // CONNECTING_HTTP_REQUEST.
564+ if ( this . proxyState === PROXY_STATE_CONNECTING ||
565+ this . proxyState === PROXY_STATE_OFFLINE ) {
551566 return requestInfo . url === CONNECTING_HTTP_REQUEST ;
552567 }
553568
@@ -649,7 +664,6 @@ class Background {
649664 } ) ;
650665
651666 let refreshTokenData ;
652-
653667 // This will trigger the authentication form.
654668 try {
655669 refreshTokenData = await fxaKeysUtil . launchWebExtensionFlow ( FXA_CLIENT_ID , {
@@ -960,19 +974,6 @@ class Background {
960974 async onConnectivityChanged ( connectivity ) {
961975 log ( "connectivity changed!" ) ;
962976
963- // (!inactive) -> offline.
964- if ( ( this . proxyState === PROXY_STATE_LOADING ||
965- this . proxyState === PROXY_STATE_UNAUTHENTICATED ||
966- this . proxyState === PROXY_STATE_ACTIVE ||
967- this . proxyState === PROXY_STATE_CONNECTING ||
968- this . proxyState === PROXY_STATE_PROXYERROR ||
969- this . proxyState === PROXY_STATE_PROXYAUTHFAILED ||
970- this . proxyState === PROXY_STATE_AUTHFAILURE ) && ! connectivity ) {
971- this . proxyState = PROXY_STATE_OFFLINE ;
972- this . updateUI ( ) ;
973- return ;
974- }
975-
976977 // Offline -> online.
977978 if ( ( this . proxyState === PROXY_STATE_OFFLINE ) && connectivity ) {
978979 await this . run ( ) ;
0 commit comments