@@ -85,7 +85,7 @@ export abstract class BrowserContext extends SdkObject {
85
85
readonly fetchRequest : BrowserContextAPIRequestContext ;
86
86
private _customCloseHandler ?: ( ) => Promise < any > ;
87
87
readonly _tempDirs : string [ ] = [ ] ;
88
- private _settingStorageState = false ;
88
+ private _creatingStorageStatePage = false ;
89
89
bindingsInitScript ?: InitScript ;
90
90
initScripts : InitScript [ ] = [ ] ;
91
91
private _routesInFlight = new Set < network . Route > ( ) ;
@@ -209,15 +209,11 @@ export abstract class BrowserContext extends SdkObject {
209
209
210
210
// Note: we only need to reset properties from the "paramsThatAllowContextReuse" list.
211
211
// All other properties force a new context.
212
- await this . _resetStorage ( progress ) ;
213
212
await this . clock . uninstall ( progress ) ;
214
213
await progress . race ( this . setUserAgent ( this . _options . userAgent ) ) ;
215
- await progress . race ( this . clearCache ( ) ) ;
216
- await progress . race ( this . doClearCookies ( ) ) ;
217
214
await progress . race ( this . doUpdateDefaultEmulatedMedia ( ) ) ;
218
215
await progress . race ( this . doUpdateDefaultViewport ( ) ) ;
219
- if ( this . _options . storageState ?. cookies )
220
- await progress . race ( this . addCookies ( this . _options . storageState ?. cookies ) ) ;
216
+ await this . setStorageState ( progress , this . _options . storageState , 'reset' ) ;
221
217
222
218
await page ?. resetForReuse ( progress ) ;
223
219
}
@@ -612,68 +608,62 @@ export abstract class BrowserContext extends SdkObject {
612
608
return result ;
613
609
}
614
610
615
- async _resetStorage ( progress : Progress ) {
616
- const oldOrigins = this . _origins ;
617
- const newOrigins = new Map ( this . _options . storageState ?. origins ?. map ( p => [ p . origin , p ] ) || [ ] ) ;
618
- if ( ! oldOrigins . size && ! newOrigins . size )
619
- return ;
620
- let page = this . pages ( ) [ 0 ] ;
621
-
622
- // Do not mark this page as internal, because we will leave it for later reuse
623
- // as a user-visible page.
624
- page = page || await this . newPage ( progress , false ) ;
625
- const interceptor = ( route : network . Route ) => {
626
- route . fulfill ( { body : '<html></html>' } ) . catch ( ( ) => { } ) ;
627
- } ;
628
- await page . addRequestInterceptor ( progress , interceptor , 'prepend' ) ;
611
+ isCreatingStorageStatePage ( ) : boolean {
612
+ return this . _creatingStorageStatePage ;
613
+ }
629
614
615
+ async setStorageState ( progress : Progress , state : channels . BrowserNewContextParams [ 'storageState' ] , mode : 'initial' | 'reset' ) {
616
+ let page : Page | undefined ;
617
+ let interceptor : network . RouteHandler | undefined ;
630
618
try {
631
- for ( const origin of new Set ( [ ...oldOrigins , ...newOrigins . keys ( ) ] ) ) {
632
- const frame = page . mainFrame ( ) ;
633
- await frame . gotoImpl ( progress , origin , { } ) ;
634
- await progress . race ( frame . resetStorageForCurrentOriginBestEffort ( newOrigins . get ( origin ) ) ) ;
619
+ if ( mode === 'reset' ) {
620
+ await progress . race ( this . clearCache ( ) ) ;
621
+ await progress . race ( this . doClearCookies ( ) ) ;
635
622
}
636
623
637
- this . _origins = new Set ( [ ...newOrigins . keys ( ) ] ) ;
638
- // It is safe to not restore the URL to about:blank since we are doing it in Page::resetForReuse.
639
- } finally {
640
- await page . removeRequestInterceptor ( interceptor ) ;
641
- }
642
- }
624
+ if ( state ?. cookies )
625
+ await progress . race ( this . addCookies ( state . cookies ) ) ;
643
626
644
- isSettingStorageState ( ) : boolean {
645
- return this . _settingStorageState ;
646
- }
627
+ const newOrigins = new Map ( state ?. origins ?. map ( p => [ p . origin , p ] ) || [ ] ) ;
628
+ const allOrigins = new Set ( [ ...this . _origins , ...newOrigins . keys ( ) ] ) ;
629
+ if ( allOrigins . size ) {
630
+ if ( mode === 'reset' )
631
+ page = this . pages ( ) [ 0 ] ;
632
+ if ( ! page ) {
633
+ try {
634
+ this . _creatingStorageStatePage = mode === 'initial' ;
635
+ page = await this . newPage ( progress , this . _creatingStorageStatePage ) ;
636
+ } finally {
637
+ this . _creatingStorageStatePage = false ;
638
+ }
639
+ }
647
640
648
- async setStorageState ( progress : Progress , state : NonNullable < channels . BrowserNewContextParams [ 'storageState' ] > ) {
649
- let page : Page | undefined ;
650
- this . _settingStorageState = true ;
651
- try {
652
- if ( state . cookies )
653
- await progress . race ( this . addCookies ( state . cookies ) ) ;
654
- if ( state . origins && state . origins . length ) {
655
- page = await this . newPage ( progress , true ) ;
656
- await page . addRequestInterceptor ( progress , route => {
641
+ interceptor = ( route : network . Route ) => {
657
642
route . fulfill ( { body : '<html></html>' } ) . catch ( ( ) => { } ) ;
658
- } , 'prepend' ) ;
659
- for ( const originState of state . origins ) {
643
+ } ;
644
+ await page . addRequestInterceptor ( progress , interceptor , 'prepend' ) ;
645
+
646
+ for ( const origin of allOrigins ) {
660
647
const frame = page . mainFrame ( ) ;
661
- await frame . gotoImpl ( progress , originState . origin , { } ) ;
648
+ await frame . gotoImpl ( progress , origin , { } ) ;
662
649
const restoreScript = `(() => {
663
650
const module = {};
664
651
${ rawStorageSource . source }
665
652
const script = new (module.exports.StorageScript())(${ this . _browser . options . name === 'firefox' } );
666
- return script.restore(${ JSON . stringify ( originState ) } );
653
+ return script.restore(${ JSON . stringify ( newOrigins . get ( origin ) ) } );
667
654
})()` ;
668
655
await progress . race ( frame . evaluateExpression ( restoreScript , { world : 'utility' } ) ) ;
669
656
}
670
657
}
658
+ this . _origins = new Set ( [ ...newOrigins . keys ( ) ] ) ;
671
659
} catch ( error ) {
672
660
rewriteErrorMessage ( error , `Error setting storage state:\n` + error . message ) ;
673
661
throw error ;
674
662
} finally {
675
- await page ?. close ( ) ;
676
- this . _settingStorageState = false ;
663
+ if ( mode === 'initial' )
664
+ await page ?. close ( ) ;
665
+ else if ( interceptor )
666
+ await page ?. removeRequestInterceptor ( interceptor ) ;
677
667
}
678
668
}
679
669
0 commit comments