Skip to content
Closed
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
7 changes: 7 additions & 0 deletions src/main/browser/cdp-bridge-integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,13 @@ describe('Browser automation pipeline (integration)', () => {
name: 'More information...'
})
expect(activeGuest.debugger.attach).toHaveBeenCalledTimes(1)
expect(
activeGuestHarness.sendCommandMock.mock.calls.some(
([method, params]) =>
method === 'Emulation.setFocusEmulationEnabled' &&
JSON.stringify(params) === JSON.stringify({ enabled: true })
)
).toBe(true)
})

it('preserves debugger listeners owned by other browser streams', async () => {
Expand Down
9 changes: 9 additions & 0 deletions src/main/browser/cdp-bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import {
import { insertTextThroughCdp } from './browser-text-insertion'
import type { BrowserManager } from './browser-manager'
import { ANTI_DETECTION_SCRIPT } from './anti-detection'
import { enableCdpFocusEmulation } from './cdp-focus-emulation'

const CAPTURE_LOG_LIMIT = 1000

Expand Down Expand Up @@ -1162,6 +1163,14 @@ export class CdpBridge {
source: ANTI_DETECTION_SCRIPT
})

// Why: keep document.hasFocus() true for guest views that are not the OS
// first responder so rich-editor insertText/fill paths commit (#10375).
try {
await enableCdpFocusEmulation(sender)
} catch {
/* best-effort */
}

// Why: only remove this bridge's listeners; screencast/proxy sessions share the debugger and own their teardown.
this.removeDebuggerListeners(guest, state)

Expand Down
28 changes: 28 additions & 0 deletions src/main/browser/cdp-focus-emulation.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { describe, expect, it, vi } from 'vitest'
import { CDP_FOCUS_EMULATION_METHOD, enableCdpFocusEmulation } from './cdp-focus-emulation'

describe('enableCdpFocusEmulation', () => {
it('enables Emulation.setFocusEmulationEnabled over CDP', async () => {
const send = vi.fn(async () => ({}))

await enableCdpFocusEmulation(send)

expect(send).toHaveBeenCalledWith(CDP_FOCUS_EMULATION_METHOD, { enabled: true })
})

it('bounds a stalled debugger command', async () => {
vi.useFakeTimers()
try {
const send = vi.fn(() => new Promise<unknown>(() => {}))
const result = enableCdpFocusEmulation(send)
const expectation = expect(result).rejects.toThrow('CDP focus emulation timed out')

await vi.advanceTimersByTimeAsync(5_000)

await expectation
expect(vi.getTimerCount()).toBe(0)
} finally {
vi.useRealTimers()
}
})
})
33 changes: 33 additions & 0 deletions src/main/browser/cdp-focus-emulation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* CDP focus emulation so `document.hasFocus()` reports true even when the
* guest WebContents is not the OS first responder (Electron 43+ / multi-view).
*
* Required for rich editors (Draft.js, etc.) that gate Input.insertText and
* execCommand on frame focus — see #10375 / regression of #7035.
*/
export const CDP_FOCUS_EMULATION_METHOD = 'Emulation.setFocusEmulationEnabled' as const
const CDP_FOCUS_EMULATION_TIMEOUT_MS = 5_000

export type CdpFocusEmulationSender = (
method: string,
params?: Record<string, unknown>
) => Promise<unknown>

export async function enableCdpFocusEmulation(send: CdpFocusEmulationSender): Promise<void> {
let timer: ReturnType<typeof setTimeout> | null = null
try {
await Promise.race([
send(CDP_FOCUS_EMULATION_METHOD, { enabled: true }),
new Promise<never>((_, reject) => {
timer = setTimeout(
() => reject(new Error(`CDP focus emulation timed out: ${CDP_FOCUS_EMULATION_METHOD}`)),
CDP_FOCUS_EMULATION_TIMEOUT_MS
)
})
])
} finally {
if (timer) {
clearTimeout(timer)
}
}
}
9 changes: 9 additions & 0 deletions src/main/browser/cdp-ws-proxy-focus-replay.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ describe('CdpWsProxy DOM.focus replay', () => {
expect(getSendCommandCalls(mock)).toEqual([
['Page.enable', {}],
['Page.addScriptToEvaluateOnNewDocument', expect.any(Object)],
['Emulation.setFocusEmulationEnabled', { enabled: true }],
['DOM.focus', { backendNodeId: 99 }],
['DOM.focus', { backendNodeId: 99 }],
['Input.insertText', { text: 'hello' }]
Expand Down Expand Up @@ -81,6 +82,7 @@ describe('CdpWsProxy DOM.focus replay', () => {
expect(getSendCommandCalls(mock)).toEqual([
['Page.enable', {}],
['Page.addScriptToEvaluateOnNewDocument', expect.any(Object)],
['Emulation.setFocusEmulationEnabled', { enabled: true }],
['DOM.focus', { backendNodeId: 123 }, 'oopif-session-123'],
['DOM.focus', { backendNodeId: 123 }, 'oopif-session-123'],
['Input.insertText', { text: 'frame text' }, 'oopif-session-123']
Expand Down Expand Up @@ -113,6 +115,7 @@ describe('CdpWsProxy DOM.focus replay', () => {
expect(getSendCommandCalls(mock)).toEqual([
['Page.enable', {}],
['Page.addScriptToEvaluateOnNewDocument', expect.any(Object)],
['Emulation.setFocusEmulationEnabled', { enabled: true }],
['DOM.focus', { backendNodeId: 44 }],
['Runtime.callFunctionOn', { functionDeclaration: '() => document.activeElement?.id' }],
['Input.insertText', { text: 'after eval' }]
Expand Down Expand Up @@ -156,6 +159,7 @@ describe('CdpWsProxy DOM.focus replay', () => {
expect(getSendCommandCalls(mock)).toEqual([
['Page.enable', {}],
['Page.addScriptToEvaluateOnNewDocument', expect.any(Object)],
['Emulation.setFocusEmulationEnabled', { enabled: true }],
['DOM.focus', { backendNodeId: 55 }],
['Input.insertText', { text: 'fallback' }]
])
Expand Down Expand Up @@ -198,6 +202,7 @@ describe('CdpWsProxy DOM.focus replay', () => {
expect(getSendCommandCalls(mock)).toEqual([
['Page.enable', {}],
['Page.addScriptToEvaluateOnNewDocument', expect.any(Object)],
['Emulation.setFocusEmulationEnabled', { enabled: true }],
['DOM.focus', { backendNodeId: 77 }],
['DOM.focus', { backendNodeId: 77 }]
])
Expand Down Expand Up @@ -243,6 +248,7 @@ describe('CdpWsProxy DOM.focus replay', () => {
expect(getSendCommandMethods(mock)).toEqual([
'Page.enable',
'Page.addScriptToEvaluateOnNewDocument',
'Emulation.setFocusEmulationEnabled',
'DOM.focus',
'DOM.focus',
'Input.insertText'
Expand Down Expand Up @@ -273,6 +279,7 @@ describe('CdpWsProxy DOM.focus replay', () => {
expect(getSendCommandMethods(mock)).toEqual([
'Page.enable',
'Page.addScriptToEvaluateOnNewDocument',
'Emulation.setFocusEmulationEnabled',
'DOM.focus',
'Input.insertText'
])
Expand All @@ -299,6 +306,7 @@ describe('CdpWsProxy DOM.focus replay', () => {
expect(getSendCommandMethods(mock)).toEqual([
'Page.enable',
'Page.addScriptToEvaluateOnNewDocument',
'Emulation.setFocusEmulationEnabled',
'DOM.focus',
'Page.captureScreenshot',
'Input.insertText'
Expand All @@ -325,6 +333,7 @@ describe('CdpWsProxy DOM.focus replay', () => {
expect(getSendCommandMethods(mock)).toEqual([
'Page.enable',
'Page.addScriptToEvaluateOnNewDocument',
'Emulation.setFocusEmulationEnabled',
'DOM.focus',
'Input.insertText'
])
Expand Down
15 changes: 13 additions & 2 deletions src/main/browser/cdp-ws-proxy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ describe('CdpWsProxy', () => {
expect(mock.webContents.debugger.attach).toHaveBeenCalledWith('1.3')
})

it('enables CDP focus emulation on attach so document.hasFocus() can be true (#10375)', () => {
expect(mock.webContents.debugger.sendCommand).toHaveBeenCalledWith(
'Emulation.setFocusEmulationEnabled',
{ enabled: true }
)
})

// ── CDP message ID correlation ──

it('correlates CDP request/response IDs', async () => {
Expand Down Expand Up @@ -403,6 +410,7 @@ describe('CdpWsProxy', () => {
expect(getSendCommandMethods(mock)).toEqual([
'Page.enable',
'Page.addScriptToEvaluateOnNewDocument',
'Emulation.setFocusEmulationEnabled',
'Input.insertText'
])
client.close()
Expand All @@ -422,6 +430,7 @@ describe('CdpWsProxy', () => {
expect(getSendCommandMethods(mock)).toEqual([
'Page.enable',
'Page.addScriptToEvaluateOnNewDocument',
'Emulation.setFocusEmulationEnabled',
'Network.enable',
'Page.enable',
'Page.setLifecycleEventsEnabled',
Expand All @@ -443,6 +452,7 @@ describe('CdpWsProxy', () => {
expect(getSendCommandMethods(mock)).toEqual([
'Page.enable',
'Page.addScriptToEvaluateOnNewDocument',
'Emulation.setFocusEmulationEnabled',
'Network.enable',
'Page.enable',
'Page.setLifecycleEventsEnabled'
Expand All @@ -462,7 +472,7 @@ describe('CdpWsProxy', () => {
sessionId: 'iframe-session-123'
})

expect(getSendCommandCalls(mock).slice(2)).toEqual([
expect(getSendCommandCalls(mock).slice(3)).toEqual([
['Network.enable', {}, 'iframe-session-123'],
['Page.enable', {}, 'iframe-session-123'],
['Page.setLifecycleEventsEnabled', { enabled: true }, 'iframe-session-123'],
Expand All @@ -481,7 +491,7 @@ describe('CdpWsProxy', () => {
sessionId: 'iframe-session-123'
})

expect(getSendCommandCalls(mock).slice(2)).toEqual([
expect(getSendCommandCalls(mock).slice(3)).toEqual([
['Network.enable', {}, 'iframe-session-123'],
['Page.enable', {}, 'iframe-session-123'],
['Page.setLifecycleEventsEnabled', { enabled: true }, 'iframe-session-123'],
Expand Down Expand Up @@ -565,6 +575,7 @@ describe('CdpWsProxy', () => {
expect(getSendCommandMethods(mock)).toEqual([
'Page.enable',
'Page.addScriptToEvaluateOnNewDocument',
'Emulation.setFocusEmulationEnabled',
'Runtime.evaluate'
])
client.close()
Expand Down
12 changes: 12 additions & 0 deletions src/main/browser/cdp-ws-proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { captureScreenshot } from './cdp-screenshot'
import { buildPrintToPdfOptions, CdpPdfStreamStore } from './cdp-print-to-pdf'
import { ANTI_DETECTION_SCRIPT } from './anti-detection'
import { acquireElectronDebugger, type ElectronDebuggerLease } from './electron-debugger-lease'
import { enableCdpFocusEmulation } from './cdp-focus-emulation'

const LIFECYCLE_PRIMING_TIMEOUT_MS = 1_000

Expand Down Expand Up @@ -233,6 +234,17 @@ export class CdpWsProxy {
/* best-effort — page domain may not be ready yet */
}

// Why: Electron 43+ tracks WebContentsView focus correctly, so embedded
// tabs can report document.hasFocus()===false even when Orca is frontmost.
// Rich editors (Draft.js) then drop Input.insertText / execCommand (#10375).
try {
await enableCdpFocusEmulation((method, params) =>
this.webContents.debugger.sendCommand(method, params ?? {})
)
} catch {
/* best-effort — older CDP stacks may lack the method */
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

this.debuggerMessageHandler = (_event: unknown, ...rest: unknown[]) => {
const [method, params, sessionId] = rest as [
string,
Expand Down
Loading