@@ -32,8 +32,9 @@ import { Events } from './events';
32
32
import { Protocol } from './protocol' ;
33
33
34
34
export class CRBrowser extends platform . EventEmitter implements Browser {
35
- _connection : CRConnection ;
36
- _client : CRSession ;
35
+ readonly _connection : CRConnection ;
36
+ _session : CRSession ;
37
+ private _clientRootSessionPromise : Promise < CRSession > | null = null ;
37
38
readonly _defaultContext : CRBrowserContext ;
38
39
readonly _contexts = new Map < string , CRBrowserContext > ( ) ;
39
40
_targets = new Map < string , CRTarget > ( ) ;
@@ -70,23 +71,23 @@ export class CRBrowser extends platform.EventEmitter implements Browser {
70
71
constructor ( connection : CRConnection ) {
71
72
super ( ) ;
72
73
this . _connection = connection ;
73
- this . _client = this . _connection . rootSession ;
74
+ this . _session = this . _connection . rootSession ;
74
75
75
76
this . _defaultContext = new CRBrowserContext ( this , null , validateBrowserContextOptions ( { } ) ) ;
76
77
this . _connection . on ( ConnectionEvents . Disconnected , ( ) => {
77
78
for ( const context of this . _contexts . values ( ) )
78
79
context . _browserClosed ( ) ;
79
80
this . emit ( CommonEvents . Browser . Disconnected ) ;
80
81
} ) ;
81
- this . _client . on ( 'Target.targetCreated' , this . _targetCreated . bind ( this ) ) ;
82
- this . _client . on ( 'Target.targetDestroyed' , this . _targetDestroyed . bind ( this ) ) ;
83
- this . _client . on ( 'Target.targetInfoChanged' , this . _targetInfoChanged . bind ( this ) ) ;
84
- this . _client . on ( 'Target.attachedToTarget' , this . _onAttachedToTarget . bind ( this ) ) ;
82
+ this . _session . on ( 'Target.targetCreated' , this . _targetCreated . bind ( this ) ) ;
83
+ this . _session . on ( 'Target.targetDestroyed' , this . _targetDestroyed . bind ( this ) ) ;
84
+ this . _session . on ( 'Target.targetInfoChanged' , this . _targetInfoChanged . bind ( this ) ) ;
85
+ this . _session . on ( 'Target.attachedToTarget' , this . _onAttachedToTarget . bind ( this ) ) ;
85
86
}
86
87
87
88
async newContext ( options : BrowserContextOptions = { } ) : Promise < BrowserContext > {
88
89
options = validateBrowserContextOptions ( options ) ;
89
- const { browserContextId } = await this . _client . send ( 'Target.createBrowserContext' , { disposeOnDetach : true } ) ;
90
+ const { browserContextId } = await this . _session . send ( 'Target.createBrowserContext' , { disposeOnDetach : true } ) ;
90
91
const context = new CRBrowserContext ( this , browserContextId , options ) ;
91
92
await context . _initialize ( ) ;
92
93
this . _contexts . set ( browserContextId , context ) ;
@@ -159,7 +160,7 @@ export class CRBrowser extends platform.EventEmitter implements Browser {
159
160
}
160
161
161
162
async _closePage ( page : Page ) {
162
- await this . _client . send ( 'Target.closeTarget' , { targetId : CRTarget . fromPage ( page ) . _targetId } ) ;
163
+ await this . _session . send ( 'Target.closeTarget' , { targetId : CRTarget . fromPage ( page ) . _targetId } ) ;
163
164
}
164
165
165
166
_allTargets ( ) : CRTarget [ ] {
@@ -179,7 +180,7 @@ export class CRBrowser extends platform.EventEmitter implements Browser {
179
180
180
181
async startTracing ( page ?: Page , options : { path ?: string ; screenshots ?: boolean ; categories ?: string [ ] ; } = { } ) {
181
182
assert ( ! this . _tracingRecording , 'Cannot start recording trace while already recording trace.' ) ;
182
- this . _tracingClient = page ? ( page . _delegate as CRPage ) . _client : this . _client ;
183
+ this . _tracingClient = page ? ( page . _delegate as CRPage ) . _client : this . _session ;
183
184
184
185
const defaultCategories = [
185
186
'-*' , 'devtools.timeline' , 'v8.execute' , 'disabled-by-default-devtools.timeline' ,
@@ -220,6 +221,12 @@ export class CRBrowser extends platform.EventEmitter implements Browser {
220
221
return ! this . _connection . _closed ;
221
222
}
222
223
224
+ async _clientRootSession ( ) : Promise < CRSession > {
225
+ if ( ! this . _clientRootSessionPromise )
226
+ this . _clientRootSessionPromise = this . _connection . createBrowserSession ( ) ;
227
+ return this . _clientRootSessionPromise ;
228
+ }
229
+
223
230
_setDebugFunction ( debugFunction : ( message : string ) => void ) {
224
231
this . _connection . _debugProtocol = debugFunction ;
225
232
}
@@ -265,7 +272,7 @@ export class CRBrowserContext extends BrowserContextBase {
265
272
266
273
async newPage ( ) : Promise < Page > {
267
274
assertBrowserContextIsNotOwned ( this ) ;
268
- const { targetId } = await this . _browser . _client . send ( 'Target.createTarget' , { url : 'about:blank' , browserContextId : this . _browserContextId || undefined } ) ;
275
+ const { targetId } = await this . _browser . _session . send ( 'Target.createTarget' , { url : 'about:blank' , browserContextId : this . _browserContextId || undefined } ) ;
269
276
const target = this . _browser . _targets . get ( targetId ) ! ;
270
277
const result = await target . pageOrError ( ) ;
271
278
if ( result instanceof Page ) {
@@ -277,7 +284,7 @@ export class CRBrowserContext extends BrowserContextBase {
277
284
}
278
285
279
286
async cookies ( urls ?: string | string [ ] ) : Promise < network . NetworkCookie [ ] > {
280
- const { cookies } = await this . _browser . _client . send ( 'Storage.getCookies' , { browserContextId : this . _browserContextId || undefined } ) ;
287
+ const { cookies } = await this . _browser . _session . send ( 'Storage.getCookies' , { browserContextId : this . _browserContextId || undefined } ) ;
281
288
return network . filterCookies ( cookies . map ( c => {
282
289
const copy : any = { sameSite : 'None' , ...c } ;
283
290
delete copy . size ;
@@ -287,11 +294,11 @@ export class CRBrowserContext extends BrowserContextBase {
287
294
}
288
295
289
296
async setCookies ( cookies : network . SetNetworkCookieParam [ ] ) {
290
- await this . _browser . _client . send ( 'Storage.setCookies' , { cookies : network . rewriteCookies ( cookies ) , browserContextId : this . _browserContextId || undefined } ) ;
297
+ await this . _browser . _session . send ( 'Storage.setCookies' , { cookies : network . rewriteCookies ( cookies ) , browserContextId : this . _browserContextId || undefined } ) ;
291
298
}
292
299
293
300
async clearCookies ( ) {
294
- await this . _browser . _client . send ( 'Storage.clearCookies' , { browserContextId : this . _browserContextId || undefined } ) ;
301
+ await this . _browser . _session . send ( 'Storage.clearCookies' , { browserContextId : this . _browserContextId || undefined } ) ;
295
302
}
296
303
297
304
async setPermissions ( origin : string , permissions : string [ ] ) : Promise < void > {
@@ -319,11 +326,11 @@ export class CRBrowserContext extends BrowserContextBase {
319
326
throw new Error ( 'Unknown permission: ' + permission ) ;
320
327
return protocolPermission ;
321
328
} ) ;
322
- await this . _browser . _client . send ( 'Browser.grantPermissions' , { origin, browserContextId : this . _browserContextId || undefined , permissions : filtered } ) ;
329
+ await this . _browser . _session . send ( 'Browser.grantPermissions' , { origin, browserContextId : this . _browserContextId || undefined , permissions : filtered } ) ;
323
330
}
324
331
325
332
async clearPermissions ( ) {
326
- await this . _browser . _client . send ( 'Browser.resetPermissions' , { browserContextId : this . _browserContextId || undefined } ) ;
333
+ await this . _browser . _session . send ( 'Browser.resetPermissions' , { browserContextId : this . _browserContextId || undefined } ) ;
327
334
}
328
335
329
336
async setGeolocation ( geolocation : types . Geolocation | null ) : Promise < void > {
@@ -376,7 +383,7 @@ export class CRBrowserContext extends BrowserContextBase {
376
383
if ( this . _closed )
377
384
return ;
378
385
assert ( this . _browserContextId , 'Non-incognito profiles cannot be closed!' ) ;
379
- await this . _browser . _client . send ( 'Target.disposeBrowserContext' , { browserContextId : this . _browserContextId } ) ;
386
+ await this . _browser . _session . send ( 'Target.disposeBrowserContext' , { browserContextId : this . _browserContextId } ) ;
380
387
this . _browser . _contexts . delete ( this . _browserContextId ) ;
381
388
this . _didCloseInternal ( ) ;
382
389
}
@@ -388,6 +395,9 @@ export class CRBrowserContext extends BrowserContextBase {
388
395
}
389
396
390
397
async createSession ( page : Page ) : Promise < CRSession > {
391
- return CRTarget . fromPage ( page ) . sessionFactory ( ) ;
398
+ const targetId = CRTarget . fromPage ( page ) . _targetId ;
399
+ const rootSession = await this . _browser . _clientRootSession ( ) ;
400
+ const { sessionId } = await rootSession . send ( 'Target.attachToTarget' , { targetId, flatten : true } ) ;
401
+ return this . _browser . _connection . session ( sessionId ) ! ;
392
402
}
393
403
}
0 commit comments