@@ -2318,7 +2318,7 @@ function adoptPopupTab(
23182318 } : { agentOwned : boolean ; background : boolean ; referrer ?: Referrer }
23192319) : WebContents {
23202320 const openerTabId = tabForContents ( opener ) ?. id
2321- const tab = openLinkTab ( url , { agentOwned, background } , popup )
2321+ const tab = openLinkTab ( url , { agentOwned, background } , { opener , options : popup } )
23222322 tab . openerTabId = openerTabId
23232323 tab . returnToOpenerOnClose = true
23242324 const contents = tab . view . webContents
@@ -2336,7 +2336,7 @@ function adoptPopupTab(
23362336function openLinkTab (
23372337 url : string ,
23382338 { agentOwned, background } : { agentOwned : boolean ; background : boolean } ,
2339- popup ?: PopupWindowOptions
2339+ popup ?: AdoptedPopup
23402340) : AgentTab {
23412341 if ( agentOwned ) return addAutomationTab ( url , popup )
23422342 if ( background ) {
@@ -2379,23 +2379,33 @@ function openTabWithUrl(
23792379 */
23802380type PopupWindowOptions = BrowserWindowConstructorOptions & WebContentsViewConstructorOptions
23812381
2382+ /** A page-opened window becoming a tab, with the page that opened it. */
2383+ interface AdoptedPopup {
2384+ opener : WebContents
2385+ options : PopupWindowOptions
2386+ }
2387+
23822388/**
23832389 * Creates a tab's view. `popup` adopts a page-opened window's WebContents, which Chromium created
23842390 * with the opener's (identical) web preferences, so the opener relationship and its session stay
2385- * intact; callers only adopt popups that are blank or whose URL belongs to the opener's session.
2391+ * intact; a deferred popup without one gets a fresh view in the opener's session. Callers only
2392+ * adopt popups that are blank or whose URL belongs to the opener's session.
23862393 */
2387- function createTabView ( url ?: string , popup ?: PopupWindowOptions ) : WebContentsView {
2394+ function createTabView ( url ?: string , popup ?: AdoptedPopup ) : WebContentsView {
23882395 let appSession : BrowserAppSession | undefined
2389- if ( popup ?. webContents ) {
2390- // A blank popup's URL names no origin, so trust the session Chromium created it in.
2391- if ( browserAppSession && popup . webContents . session === browserAppSession . session ) {
2396+ if ( popup ) {
2397+ // A popup follows its opener's session (Chromium creates it there); a blank URL names no origin.
2398+ const popupSession = popup . options . webContents ?. session ?? popup . opener . session
2399+ if ( browserAppSession && popupSession === browserAppSession . session ) {
23922400 appSession = browserAppSession
23932401 }
23942402 } else if ( url && browserAppSession && isAppOrigin ( url , browserAppSession . origin ) ) {
23952403 appSession = browserAppSession
23962404 }
23972405 const scopeId = getBrowserScopeId ( )
2398- const view = popup ?. webContents ? new WebContentsView ( popup ) : createFreshTabView ( appSession )
2406+ const view = popup ?. options . webContents
2407+ ? new WebContentsView ( popup . options )
2408+ : createFreshTabView ( appSession )
23992409 try {
24002410 /** Detached tabs must lay out before a foreground panel owns their native view. */
24012411 const [ width , height ] = getMainWindow ( ) ?. getContentSize ( ) ?? [ 1280 , 720 ]
@@ -2960,7 +2970,7 @@ interface AddTabOptions {
29602970 activate ?: boolean
29612971 notify ?: boolean
29622972 url ?: string
2963- popup ?: PopupWindowOptions
2973+ popup ?: AdoptedPopup
29642974}
29652975
29662976function addTabInternal ( {
@@ -3441,7 +3451,7 @@ export function restoreBrowserSession(): void {
34413451 }
34423452}
34433453
3444- export function addTab ( url ?: string , popup ?: PopupWindowOptions ) : AgentTab {
3454+ export function addTab ( url ?: string , popup ?: AdoptedPopup ) : AgentTab {
34453455 restoreBrowserSession ( )
34463456 currentScope . visibleTabUserSelected = true
34473457 return addTabInternal ( { url, popup } )
@@ -3452,7 +3462,7 @@ export function addTab(url?: string, popup?: PopupWindowOptions): AgentTab {
34523462 * renderer's decision: every page is a resource tab there, and it shows the
34533463 * agent's tab or badges it depending on what the user is doing.
34543464 */
3455- export function addAutomationTab ( url ?: string , popup ?: PopupWindowOptions ) : AgentTab {
3465+ export function addAutomationTab ( url ?: string , popup ?: AdoptedPopup ) : AgentTab {
34563466 restoreBrowserSession ( )
34573467 const tab = addTabInternal ( { activate : false , notify : false , url, popup } )
34583468 currentScope . automationTabId = tab . id
0 commit comments