mirror of
https://github.com/stablyai/orca.git
synced 2026-11-19 04:23:57 +00:00
fix(browser): open links an agent clicks behind the user's current tab (#41349)
This commit is contained in:
@@ -129,7 +129,8 @@ export abstract class BrowserManagerState extends BrowserManagerViewportScrollSt
|
||||
protected abstract openLinkInOrcaTab(
|
||||
browserTabId: string,
|
||||
rawUrl: string,
|
||||
activate?: boolean
|
||||
activate?: boolean,
|
||||
agentOpened?: boolean
|
||||
): boolean
|
||||
|
||||
protected settingsResolver: (() => BrowserManagerSettings) | null = null
|
||||
|
||||
@@ -61,12 +61,21 @@ export abstract class BrowserManagerVisibility extends BrowserManagerState {
|
||||
}
|
||||
const ownerTabId = this.resolveBrowserTabIdForGuestWebContentsId(guestWebContentsId)
|
||||
if (ownerTabId) {
|
||||
const pageId =
|
||||
this.tabIdByWebContentsId.get(guestWebContentsId) ?? browserPopupPageId(guestWebContentsId)
|
||||
this.pageInputCredit.creditInput(ownerTabId, pageId)
|
||||
this.pageInputCredit.creditInput(ownerTabId, this.pageIdForGuest(guestWebContentsId))
|
||||
}
|
||||
}
|
||||
|
||||
/** An agent is driving the page this guest shows (a tab or a popup). */
|
||||
isAgentUsingGuest(guestWebContentsId: number): boolean {
|
||||
return this.isAgentUsingPage(this.pageIdForGuest(guestWebContentsId))
|
||||
}
|
||||
|
||||
private pageIdForGuest(guestWebContentsId: number): string {
|
||||
return (
|
||||
this.tabIdByWebContentsId.get(guestWebContentsId) ?? browserPopupPageId(guestWebContentsId)
|
||||
)
|
||||
}
|
||||
|
||||
handOverAgentActivity(fromPageId: string, toPageId: string): void {
|
||||
this.agentActivity.handOver(fromPageId, toPageId)
|
||||
}
|
||||
|
||||
@@ -9,7 +9,13 @@ import { BrowserManagerEventForwarding } from './browser-manager-event-forwardin
|
||||
type OpenLinkInTab = (url: string, activate: boolean) => void
|
||||
|
||||
export class BrowserManager extends BrowserManagerEventForwarding {
|
||||
protected openLinkInOrcaTab(browserTabId: string, rawUrl: string, activate?: boolean): boolean {
|
||||
protected openLinkInOrcaTab(
|
||||
browserTabId: string,
|
||||
rawUrl: string,
|
||||
activate?: boolean,
|
||||
// Opened while an agent drives the page, not by the desktop user's own click.
|
||||
agentOpened = false
|
||||
): boolean {
|
||||
const renderer = this.resolveRendererForBrowserTab(browserTabId)
|
||||
const open: OpenLinkInTab | null = renderer
|
||||
? // The renderer owns Orca's tab model; forward the validated URL for materialization.
|
||||
@@ -17,7 +23,8 @@ export class BrowserManager extends BrowserManagerEventForwarding {
|
||||
renderer.send('browser:open-link-in-orca-tab', {
|
||||
browserPageId: browserTabId,
|
||||
url,
|
||||
...(activateTab ? {} : { activate: false })
|
||||
// An agent's link must not move the desktop user's view.
|
||||
...(activateTab && !agentOpened ? {} : { activate: false })
|
||||
})
|
||||
: this.resolveOffscreenTabOpener(browserTabId)
|
||||
if (!open) {
|
||||
|
||||
+7
-8
@@ -40,6 +40,8 @@ export abstract class BrowserManagerGuestPopupPolicy extends BrowserManagerNavig
|
||||
const clicked = observed !== null
|
||||
// Only the desktop user's own click may bring a popup forward; agents and late opens wait.
|
||||
const showWhenAppActive = observed?.byAgent !== false
|
||||
const activateTab = disposition !== 'background-tab'
|
||||
const agentOpened = observed?.byAgent !== false && this.isAgentUsingGuest(guest.id)
|
||||
const ownerContext = this.resolvePopupOwnerContext(guest.id)
|
||||
const browserTabId = ownerContext?.browserTabId ?? null
|
||||
const externalUrl = normalizeExternalBrowserUrl(url)
|
||||
@@ -64,11 +66,7 @@ export abstract class BrowserManagerGuestPopupPolicy extends BrowserManagerNavig
|
||||
return { action: 'deny' }
|
||||
}
|
||||
if (
|
||||
this.openLinkInOrcaTab(
|
||||
ownerContext.browserTabId,
|
||||
newTabUrl,
|
||||
disposition !== 'background-tab'
|
||||
)
|
||||
this.openLinkInOrcaTab(ownerContext.browserTabId, newTabUrl, activateTab, agentOpened)
|
||||
) {
|
||||
this.forwardOrQueuePopupEvent(guest.id, {
|
||||
origin: safeOrigin(newTabUrl),
|
||||
@@ -93,7 +91,8 @@ export abstract class BrowserManagerGuestPopupPolicy extends BrowserManagerNavig
|
||||
tabUrl && !(options.nativeWindow?.webContents ?? options.webContents)?.opener
|
||||
? this.openPopupAsBrowserTab(guest, tabUrl, options, {
|
||||
clicked,
|
||||
activate: disposition !== 'background-tab'
|
||||
activate: activateTab,
|
||||
agentOpened
|
||||
})
|
||||
: null
|
||||
if (isNativeBrowserPopupRequest({ disposition, features, frameName })) {
|
||||
@@ -168,13 +167,13 @@ export abstract class BrowserManagerGuestPopupPolicy extends BrowserManagerNavig
|
||||
opener: Electron.WebContents,
|
||||
url: string,
|
||||
options: PopupChildWindowOptions,
|
||||
tab: { clicked: boolean; activate: boolean }
|
||||
tab: { clicked: boolean; activate: boolean; agentOpened: boolean }
|
||||
): Electron.WebContents {
|
||||
const owner = this.resolvePopupOwnerContext(opener.id)
|
||||
const opened =
|
||||
owner !== null &&
|
||||
(tab.clicked || this.tryConsumePageInitiatedTab(owner.rootGuestWebContentsId)) &&
|
||||
this.openLinkInOrcaTab(owner.browserTabId, url, tab.activate)
|
||||
this.openLinkInOrcaTab(owner.browserTabId, url, tab.activate, tab.agentOpened)
|
||||
this.forwardOrQueuePopupEvent(opener.id, {
|
||||
origin: safeOrigin(url),
|
||||
action: opened ? 'opened-in-orca' : 'blocked'
|
||||
|
||||
@@ -42,6 +42,8 @@ vi.mock('../../popups/presentation/popup-origin-bar-window', () => ({
|
||||
}))
|
||||
|
||||
import { browserManager } from '../browser-manager'
|
||||
import { withAgentInputInjection } from '../../../cdp/input/agent-input-injection'
|
||||
import { runWithActiveRpcCaller } from '../../../../runtime/rpc/rpc-active-caller'
|
||||
import {
|
||||
MAX_PAGE_INITIATED_TABS_PER_WINDOW,
|
||||
PAGE_INITIATED_TAB_WINDOW_MS
|
||||
@@ -165,6 +167,73 @@ describe('browserManager', () => {
|
||||
})
|
||||
})
|
||||
|
||||
it("opens an agent's link behind the user's tab, and the user's own click in front", async () => {
|
||||
const rendererSendMock = vi.fn()
|
||||
const guest = {
|
||||
...guestNavigationMethods(),
|
||||
id: 145,
|
||||
isDestroyed: vi.fn(() => false),
|
||||
isCurrentlyAudible: vi.fn(() => false),
|
||||
getType: vi.fn(() => 'webview'),
|
||||
setBackgroundThrottling: guestSetBackgroundThrottlingMock,
|
||||
setWindowOpenHandler: guestSetWindowOpenHandlerMock,
|
||||
setIgnoreMenuShortcuts: vi.fn(),
|
||||
on: guestOnMock,
|
||||
prependListener: vi.fn(),
|
||||
off: guestOffMock,
|
||||
openDevTools: guestOpenDevToolsMock
|
||||
}
|
||||
webContentsFromIdMock.mockImplementation((id: number) => {
|
||||
if (id === guest.id) {
|
||||
return guest
|
||||
}
|
||||
if (id === rendererWebContentsId) {
|
||||
return { id, isDestroyed: vi.fn(() => false), send: rendererSendMock }
|
||||
}
|
||||
return null
|
||||
})
|
||||
browserManager.attachGuestPolicies(guest as never)
|
||||
browserManager.registerGuest({
|
||||
browserPageId: 'browser-1',
|
||||
webContentsId: guest.id,
|
||||
rendererWebContentsId
|
||||
})
|
||||
const handler: (details: {
|
||||
url: string
|
||||
frameName: string
|
||||
features: string
|
||||
disposition: string
|
||||
}) => unknown = guestSetWindowOpenHandlerMock.mock.calls[0][0]
|
||||
const inputEventHandler = guestOnMock.mock.calls.find(([event]) => event === 'input-event')?.[1]
|
||||
const openLink = (url: string): void => {
|
||||
handler({ url, frameName: '', features: '', disposition: 'foreground-tab' })
|
||||
}
|
||||
const command = runWithActiveRpcCaller({ kind: 'local-cli' }, () =>
|
||||
browserManager.beginAgentCommand('browser-1')
|
||||
)
|
||||
|
||||
await withAgentInputInjection(guest as never, async () => {
|
||||
inputEventHandler({}, { type: 'mouseUp' })
|
||||
})
|
||||
openLink('https://docs.example.com/agent')
|
||||
inputEventHandler({}, { type: 'mouseUp' })
|
||||
openLink('https://docs.example.com/user')
|
||||
command.end()
|
||||
|
||||
expect(
|
||||
rendererSendMock.mock.calls.filter(([channel]) => channel === 'browser:open-link-in-orca-tab')
|
||||
).toEqual([
|
||||
[
|
||||
'browser:open-link-in-orca-tab',
|
||||
{ browserPageId: 'browser-1', url: 'https://docs.example.com/agent', activate: false }
|
||||
],
|
||||
[
|
||||
'browser:open-link-in-orca-tab',
|
||||
{ browserPageId: 'browser-1', url: 'https://docs.example.com/user' }
|
||||
]
|
||||
])
|
||||
})
|
||||
|
||||
it('shares the page-initiated tab budget across the whole opener popup tree', () => {
|
||||
const rendererSendMock = vi.fn()
|
||||
const guest = {
|
||||
|
||||
+7
-2
@@ -16,10 +16,15 @@ export class BrowserPopupCompatibilityManager extends BrowserManager {
|
||||
security: ReturnType<typeof contentSecurity>
|
||||
permission?: string
|
||||
}[] = []
|
||||
protected override openLinkInOrcaTab(pageId: string, url: string, activate?: boolean): boolean {
|
||||
protected override openLinkInOrcaTab(
|
||||
pageId: string,
|
||||
url: string,
|
||||
activate?: boolean,
|
||||
agentOpened?: boolean
|
||||
): boolean {
|
||||
this.tabOpens.push({ url, activate })
|
||||
this.writeStatus()
|
||||
return super.openLinkInOrcaTab(pageId, url, activate)
|
||||
return super.openLinkInOrcaTab(pageId, url, activate, agentOpened)
|
||||
}
|
||||
protected override createPopupChildWindowWithOriginBar(
|
||||
opener: WebContents,
|
||||
|
||||
Reference in New Issue
Block a user