@@ -34,7 +34,7 @@ export class CRBrowser extends BrowserBase {
34
34
readonly _connection : CRConnection ;
35
35
_session : CRSession ;
36
36
private _clientRootSessionPromise : Promise < CRSession > | null = null ;
37
- readonly _defaultContext : CRBrowserContext ;
37
+ readonly _defaultContext : CRBrowserContext | null = null ;
38
38
readonly _contexts = new Map < string , CRBrowserContext > ( ) ;
39
39
_crPages = new Map < string , CRPage > ( ) ;
40
40
_backgroundPages = new Map < string , CRPage > ( ) ;
@@ -48,7 +48,7 @@ export class CRBrowser extends BrowserBase {
48
48
49
49
static async connect ( transport : ConnectionTransport , isPersistent : boolean , slowMo ?: number ) : Promise < CRBrowser > {
50
50
const connection = new CRConnection ( SlowMoTransport . wrap ( transport , slowMo ) ) ;
51
- const browser = new CRBrowser ( connection ) ;
51
+ const browser = new CRBrowser ( connection , isPersistent ) ;
52
52
const session = connection . rootSession ;
53
53
if ( ! isPersistent ) {
54
54
await session . send ( 'Target.setAutoAttach' , { autoAttach : true , waitForDebuggerOnStart : true , flatten : true } ) ;
@@ -83,12 +83,13 @@ export class CRBrowser extends BrowserBase {
83
83
return browser ;
84
84
}
85
85
86
- constructor ( connection : CRConnection ) {
86
+ constructor ( connection : CRConnection , isPersistent : boolean ) {
87
87
super ( ) ;
88
88
this . _connection = connection ;
89
89
this . _session = this . _connection . rootSession ;
90
90
91
- this . _defaultContext = new CRBrowserContext ( this , null , validateBrowserContextOptions ( { } ) ) ;
91
+ if ( isPersistent )
92
+ this . _defaultContext = new CRBrowserContext ( this , null , validateBrowserContextOptions ( { } ) ) ;
92
93
this . _connection . on ( ConnectionEvents . Disconnected , ( ) => {
93
94
for ( const context of this . _contexts . values ( ) )
94
95
context . _browserClosed ( ) ;
@@ -113,9 +114,26 @@ export class CRBrowser extends BrowserBase {
113
114
}
114
115
115
116
_onAttachedToTarget ( { targetInfo, sessionId, waitingForDebugger} : Protocol . Target . attachedToTargetPayload ) {
117
+ if ( targetInfo . type === 'browser' )
118
+ return ;
116
119
const session = this . _connection . session ( sessionId ) ! ;
117
- const context = ( targetInfo . browserContextId && this . _contexts . has ( targetInfo . browserContextId ) ) ?
118
- this . _contexts . get ( targetInfo . browserContextId ) ! : this . _defaultContext ;
120
+ assert ( targetInfo . browserContextId , 'targetInfo: ' + JSON . stringify ( targetInfo , null , 2 ) ) ;
121
+ let context = this . _contexts . get ( targetInfo . browserContextId ) || null ;
122
+ if ( ! context ) {
123
+ // TODO: auto attach only to pages from our contexts.
124
+ // assert(this._defaultContext);
125
+ context = this . _defaultContext ;
126
+ }
127
+
128
+ if ( targetInfo . type === 'other' || ! context ) {
129
+ if ( waitingForDebugger ) {
130
+ // Ideally, detaching should resume any target, but there is a bug in the backend.
131
+ session . send ( 'Runtime.runIfWaitingForDebugger' ) . catch ( debugError ) . then ( ( ) => {
132
+ this . _session . send ( 'Target.detachFromTarget' , { sessionId } ) . catch ( debugError ) ;
133
+ } ) ;
134
+ }
135
+ return ;
136
+ }
119
137
120
138
assert ( ! this . _crPages . has ( targetInfo . targetId ) , 'Duplicate target ' + targetInfo . targetId ) ;
121
139
assert ( ! this . _backgroundPages . has ( targetInfo . targetId ) , 'Duplicate target ' + targetInfo . targetId ) ;
@@ -125,7 +143,7 @@ export class CRBrowser extends BrowserBase {
125
143
const backgroundPage = new CRPage ( session , targetInfo . targetId , context , null ) ;
126
144
this . _backgroundPages . set ( targetInfo . targetId , backgroundPage ) ;
127
145
backgroundPage . pageOrError ( ) . then ( ( ) => {
128
- context . emit ( Events . CRBrowserContext . BackgroundPage , backgroundPage . _page ) ;
146
+ context ! . emit ( Events . CRBrowserContext . BackgroundPage , backgroundPage . _page ) ;
129
147
} ) ;
130
148
return ;
131
149
}
@@ -140,7 +158,7 @@ export class CRBrowser extends BrowserBase {
140
158
}
141
159
crPage . pageOrError ( ) . then ( ( ) => {
142
160
this . _firstPageCallback ( ) ;
143
- context . emit ( CommonEvents . BrowserContext . Page , crPage . _page ) ;
161
+ context ! . emit ( CommonEvents . BrowserContext . Page , crPage . _page ) ;
144
162
if ( opener ) {
145
163
opener . pageOrError ( ) . then ( openerPage => {
146
164
if ( openerPage instanceof Page && ! openerPage . isClosed ( ) )
@@ -158,13 +176,7 @@ export class CRBrowser extends BrowserBase {
158
176
return ;
159
177
}
160
178
161
- assert ( targetInfo . type === 'browser' || targetInfo . type === 'other' ) ;
162
- if ( waitingForDebugger ) {
163
- // Ideally, detaching should resume any target, but there is a bug in the backend.
164
- session . send ( 'Runtime.runIfWaitingForDebugger' ) . catch ( debugError ) . then ( ( ) => {
165
- this . _session . send ( 'Target.detachFromTarget' , { sessionId } ) . catch ( debugError ) ;
166
- } ) ;
167
- }
179
+ assert ( false , 'Unknown target type: ' + targetInfo . type ) ;
168
180
}
169
181
170
182
_onDetachedFromTarget ( payload : Protocol . Target . detachFromTargetParameters ) {
0 commit comments