Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/main/browser/browser-client-host-command-page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ export function selectCommandPage(
}
}
if (command.pageHostGeneration < page.generation) {
const previous = page.records.get(command.commandSequence)
if (
command.command.type === 'switchProfile' &&
previous?.result?.status === 'completed' &&
previous.event.pageHostGeneration === command.pageHostGeneration &&
previous.event.commandId === command.commandId &&
commandsMatch(previous.event, command)
) {
return { page, commit: () => {} }
}
throw new Error('browser_host_page_generation_stale')
}
if (command.pageHostGeneration === page.generation) {
Expand Down Expand Up @@ -88,6 +98,12 @@ function commandsMatch(
if (first.command.type !== second.command.type) {
return false
}
if (first.command.type === 'switchProfile' && second.command.type === 'switchProfile') {
return (
first.command.nextPageHostGeneration === second.command.nextPageHostGeneration &&
JSON.stringify(first.command.profile) === JSON.stringify(second.command.profile)
)
}
if (first.command.type === 'navigate' && second.command.type === 'navigate') {
return first.command.url === second.command.url
}
Expand Down Expand Up @@ -121,6 +137,13 @@ export function assertNewPageCommand(
page: PageState,
command: BrowserClientHostCommandEvent
): void {
const active = [...page.records.values()].filter((record) => record.status !== 'settled')
if (
active.some((record) => record.event.command.type === 'switchProfile') ||
(command.command.type === 'switchProfile' && active.length > 0)
) {
throw new Error('browser_host_profile_switch_pending')
}
if (page.nextSequence === 1 && command.command.type === 'navigate') {
throw new Error('browser_host_command_create_required')
}
Expand Down Expand Up @@ -169,6 +192,9 @@ export function recordBrowserClientPageCommandResult(
command: BrowserClientHostCommandEvent['command'],
result: BrowserClientHostCommandResult
): void {
if (command.type === 'switchProfile' && result.status === 'completed') {
page.generation = command.nextPageHostGeneration
}
if (isBrowserClientPageBootstrapCommand(command)) {
page.created = result.status === 'completed'
page.createFailed = !page.created
Expand Down
3 changes: 3 additions & 0 deletions src/main/browser/browser-client-host-command-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ export function createPageState(browserPageId: string, generation: number): Page
function snapshotPageCommand(
command: BrowserClientHostCommandEvent['command']
): BrowserClientHostCommandEvent['command'] {
if (command.type === 'switchProfile') {
return Object.freeze({ ...command, profile: Object.freeze({ ...command.profile }) })
}
if (command.type === 'reclaimPage') {
return Object.freeze({
...command,
Expand Down
6 changes: 2 additions & 4 deletions src/main/browser/browser-client-network-route-registry.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import type {
BrowserHostLeaseAuthority,
BrowserNetworkExecutionHost
} from '../../shared/browser-client-host-protocol'
import type { BrowserHostLeaseAuthority } from '../../shared/browser-client-host-protocol'
import type { BrowserNetworkExecutionHost } from '../../shared/browser-network-execution-host'
import type { BrowserClientPageNetworkRoute } from './browser-client-page-cleanup'
import {
assertBrowserClientNetworkRouteAddress,
Expand Down
16 changes: 16 additions & 0 deletions src/main/browser/browser-client-page-automation-runtime.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,22 @@ describe('BrowserClientPageAutomationRuntime', () => {
expect(fixture.unregisterGuest).toHaveBeenCalledWith('page-a')
})

it('retires an old automation session without stripping the guest needed by rollback', async () => {
const fixture = createFixture()
await fixture.runtime.execute(input(), new AbortController().signal)
await fixture.runtime.retire({
browserPageId: 'page-a',
pageHostGeneration: 9,
registration,
keepGuestBinding: true
})
expect(fixture.onTabClosed).toHaveBeenCalledWith(42)
expect(fixture.unregisterGuest).not.toHaveBeenCalled()
await expect(fixture.runtime.execute(input(), new AbortController().signal)).resolves.toEqual({
clicked: true
})
})

it('rejects aborts and registration failures before RPC dispatch', async () => {
const fixture = createFixture()
const controller = new AbortController()
Expand Down
5 changes: 3 additions & 2 deletions src/main/browser/browser-client-page-automation-runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type AutomationPageInput = {
type AutomationPageRetirement = Pick<
AutomationPageInput,
'browserPageId' | 'pageHostGeneration' | 'registration'
>
> & { keepGuestBinding?: boolean }

type AutomationRuntimeDependencies = {
browserManager: Pick<
Expand Down Expand Up @@ -68,8 +68,9 @@ export class BrowserClientPageAutomationRuntime {
await bridge.onTabClosed(current.webContentsId)
}
if (
!input.keepGuestBinding &&
this.dependencies.browserManager.getGuestWebContentsId(input.browserPageId) ===
current.webContentsId
current.webContentsId
) {
this.dependencies.browserManager.unregisterGuest(input.browserPageId)
}
Expand Down
21 changes: 21 additions & 0 deletions src/main/browser/browser-client-page-availability.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { BrowserClientPageCommandExecutorDependencies } from './browser-client-page-command-executor-dependencies'
import type { BrowserRoutePageGuestIdentity } from './browser-route-page-authority'
import type { BrowserClientRetainedPage } from './browser-client-page-retained-state'

Expand All @@ -22,3 +23,23 @@ export function markBrowserClientPageUnavailable(
onUnavailable(registration.browserPageId, page.generation)
return true
}

export function publishBrowserClientRetainedPage(
dependencies: BrowserClientPageCommandExecutorDependencies,
pages: Map<string, BrowserClientRetainedPage>,
page: BrowserClientRetainedPage
): void {
const previous = pages.get(page.inventory.browserPageId)
previous?.releaseAvailabilityWatch?.()
if (previous) {
previous.releaseAvailabilityWatch = undefined
}
page.releaseAvailabilityWatch = dependencies.routeWebContents.watchPageAvailability?.(
page.inventory.browserPageId,
(registration) =>
markBrowserClientPageUnavailable(pages, registration, (browserPageId, generation) =>
dependencies.onPageUnavailable?.(browserPageId, generation)
)
)
pages.set(page.inventory.browserPageId, page)
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export type BrowserClientPageCommandExecutorDependencies = BrowserClientPageAuth
signal: AbortSignal
): Promise<unknown>
retireAutomation(input: {
keepGuestBinding?: boolean
browserPageId: string
pageHostGeneration: number
registration: BrowserClientRetainedPage['registration']
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { BrowserRoutePreparePageInput } from './browser-route-session-state'
import type { BrowserClientHostCommandEvent } from '../../shared/browser-client-host-protocol'
import { vi } from 'vitest'
import { BrowserClientPageCommandExecutor } from './browser-client-page-command-executor'
Expand Down Expand Up @@ -89,7 +90,7 @@ export function createHarness(options: { maxPages?: number } = {}) {
}),
selectRenderer: vi.fn(() => renderer),
routeSessions: {
preparePage: vi.fn(async () => {
preparePage: vi.fn(async (_input: BrowserRoutePreparePageInput) => {
order.push('prepare-page')
return routeSession
})
Expand All @@ -100,7 +101,7 @@ export function createHarness(options: { maxPages?: number } = {}) {
bind: vi.fn(() => {
order.push('bind-guest')
}),
release: vi.fn(() => {
release: vi.fn((_registration: BrowserRoutePageGuestIdentity) => {
order.push('release-guest')
})
},
Expand Down
Loading
Loading