77 getWalletInformation ,
88 isErrorWithKey ,
99 moveToFront ,
10+ CURRENT_DATA_CONSENT_VERSION ,
11+ isConsentRequired ,
1012} from '@/shared/helpers' ;
1113import { KeyAutoAddService } from '@/background/services/keyAutoAdd' ;
1214import { OpenPaymentsClientError } from '@interledger/open-payments/dist/client/error' ;
@@ -124,7 +126,12 @@ export class Background {
124126 }
125127 await this . storage . populate ( ) ;
126128 await this . checkPermissions ( ) ;
129+ const { consent } = await this . storage . get ( [ 'consent' ] ) ;
130+ if ( isConsentRequired ( consent ) ) {
131+ await this . storage . setState ( { consent_required : true } ) ;
132+ }
127133 await this . scheduleResetOutOfFundsState ( ) ;
134+ await this . updateVisualIndicatorsForCurrentTab ( ) . catch ( ( ) => { } ) ;
128135 }
129136
130137 async scheduleResetOutOfFundsState ( ) {
@@ -147,12 +154,14 @@ export class Background {
147154 }
148155
149156 async getAppData ( ) : Promise < AppStore > {
150- const { connected, publicKey } = await this . storage . get ( [
157+ const { connected, publicKey, consent } = await this . storage . get ( [
151158 'connected' ,
152159 'publicKey' ,
160+ 'consent' ,
153161 ] ) ;
154162
155163 return {
164+ consent,
156165 connected,
157166 publicKey,
158167 transientState : this . storage . getPopupTransientState ( ) ,
@@ -295,6 +304,10 @@ export class Background {
295304 await this . monetizationService . pay ( message . payload ) ,
296305 ) ;
297306
307+ case 'OPEN_APP' :
308+ await this . openAppPage ( message . payload . path ) ;
309+ return success ( undefined ) ;
310+
298311 // endregion
299312
300313 // region Content
@@ -337,6 +350,13 @@ export class Background {
337350 // region App
338351 case 'GET_DATA_APP' :
339352 return success ( await this . getAppData ( ) ) ;
353+
354+ case 'PROVIDE_CONSENT' : {
355+ await this . storage . set ( { consent : CURRENT_DATA_CONSENT_VERSION } ) ;
356+ await this . storage . setState ( { consent_required : false } ) ;
357+ return success ( CURRENT_DATA_CONSENT_VERSION ) ;
358+ }
359+
340360 // endregion
341361
342362 default :
@@ -406,6 +426,9 @@ export class Background {
406426 this . events . on ( 'storage.state_update' , async ( { state, prevState } ) => {
407427 this . sendToPopup . send ( 'SET_STATE' , { state, prevState } ) ;
408428 await this . updateVisualIndicatorsForCurrentTab ( ) ;
429+ if ( state . consent_required ) {
430+ await this . openAppPage ( '/post-install/consent' ) ;
431+ }
409432 } ) ;
410433
411434 this . events . on ( 'monetization.state_update' , async ( tabId ) => {
@@ -446,6 +469,9 @@ export class Background {
446469 ) ;
447470 }
448471 }
472+ if ( isConsentRequired ( data . consent ) ) {
473+ await this . storage . setState ( { consent_required : true } ) ;
474+ }
449475 } ) ;
450476 }
451477
@@ -460,4 +486,20 @@ export class Background {
460486 this . logger . error ( error ) ;
461487 }
462488 } ;
489+
490+ async openAppPage ( path : string ) {
491+ const appUrl = this . browser . runtime . getURL ( APP_URL ) ;
492+
493+ const allTabs = await this . browser . tabs . query ( { } ) ;
494+ const appTab = allTabs . find ( ( t ) => t . url ?. startsWith ( appUrl ) ) ;
495+
496+ const url = `${ appUrl } #${ path } ` ;
497+ if ( appTab ?. id ) {
498+ await this . browser . tabs . update ( appTab . id , { url } ) ;
499+ await this . sendToPopup . send ( 'CLOSE_POPUP' , undefined ) ;
500+ return appTab ;
501+ } else {
502+ return await this . browser . tabs . create ( { url } ) ;
503+ }
504+ }
463505}
0 commit comments