提交记录修改internal/cage/browser-clipboard.mjs+5 −1看文件
修改scripts/guest-os/tests/windows-browser.test.mjs+33 −0看文件
修改scripts/guest-os/windows-browser.mjs+35 −7看文件
| @@ -579,7 +579,7 @@ export class BrowserClipboard { | ||
| 579 | 579 | ) { |
| 580 | 580 | void this.attach(p.sessionId, state.tabId).catch(() => {}); |
| 581 | 581 | } else if (event.method === "Target.detachedFromTarget") { |
| 582 | 删除: this.sessions.delete(p.sessionId); | |
| 582 | 新增: this.detachSession(p.sessionId); | |
| 583 | 583 | } else if (event.method === "Runtime.executionContextCreated") { |
| 584 | 584 | if (p.context?.auxData?.isDefault) |
| 585 | 585 | state.contexts.set(p.context.id, p.context.auxData.frameId); |
| @@ -625,6 +625,10 @@ export class BrowserClipboard { | ||
| 625 | 625 | } |
| 626 | 626 | } |
| 627 | 627 | |
| 628 | 新增: detachSession(id) { | |
| 629 | 新增: this.sessions.delete(id); | |
| 630 | 新增: } | |
| 631 | 新增: | |
| 628 | 632 | detachTab(id) { |
| 629 | 633 | this.copyContexts.delete(id); |
| 630 | 634 | for (const [session, state] of this.sessions) |
| @@ -111,6 +111,39 @@ integration("interactive browser identity is native in normal and private docume | ||
| 111 | 111 | assert.equal(f.launchOptions().chromiumSandbox, true); |
| 112 | 112 | }); |
| 113 | 113 | |
| 114 | 新增:integration("closed browser tabs release their controller sessions", async t => { | |
| 115 | 新增: const f = await fixture(t); | |
| 116 | 新增: const original = (await f.call("health", {}, "GET")).resources; | |
| 117 | 新增: for (let index = 0; index < 12; index++) { | |
| 118 | 新增: const { id, page } = await f.open("/lifecycle-" + index); | |
| 119 | 新增: await f.call("close", { id }); | |
| 120 | 新增: assert.equal(page.isClosed(), true); | |
| 121 | 新增: assert.deepEqual((await f.call("health", {}, "GET")).resources, original, "closed tabs must release their sessions"); | |
| 122 | 新增: } | |
| 123 | 新增:}); | |
| 124 | 新增: | |
| 125 | 新增:integration("removed browser frames release clipboard and controller sessions", async t => { | |
| 126 | 新增: const f = await fixture(t); | |
| 127 | 新增: const { id, page } = await f.open("/frame-lifecycle"); | |
| 128 | 新增: const original = (await f.call("health", {}, "GET")).resources; | |
| 129 | 新增: for (let index = 0; index < 5; index++) { | |
| 130 | 新增: await page.evaluate(async url => { | |
| 131 | 新增: const frame = document.createElement("iframe"); | |
| 132 | 新增: frame.src = url; | |
| 133 | 新增: const loaded = new Promise(resolve => { frame.onload = resolve; }); | |
| 134 | 新增: document.body.append(frame); | |
| 135 | 新增: await loaded; | |
| 136 | 新增: }, f.url.replace("127.0.0.1", "localhost") + "/child-" + index); | |
| 137 | 新增: await page.waitForTimeout(100); | |
| 138 | 新增: const attached = (await f.call("health", {}, "GET")).resources; | |
| 139 | 新增: assert.ok(attached.sessions > original.sessions); | |
| 140 | 新增: await page.evaluate(() => document.querySelector("iframe").remove()); | |
| 141 | 新增: await page.waitForTimeout(100); | |
| 142 | 新增: assert.deepEqual((await f.call("health", {}, "GET")).resources, original, "detached frames must release clipboard state"); | |
| 143 | 新增: await f.call("copy-context", { id, context: "a".repeat(32) }); | |
| 144 | 新增: } | |
| 145 | 新增:}); | |
| 146 | 新增: | |
| 114 | 147 | integration("actual Chromium tabs restore and project switching retains unsaved editor buffers", async t => { |
| 115 | 148 | const f = await fixture(t); |
| 116 | 149 | const one = await f.open("/one"), two = await f.open("/two"); |
| @@ -360,25 +360,47 @@ export function createNativeBrowserService(options = {}) { | ||
| 360 | 360 | if (method === "Target.setAutoAttach") return {}; |
| 361 | 361 | return session.send(method, params); |
| 362 | 362 | } |
| 363 | 新增: function detachFrame(tab, frame, expected) { | |
| 364 | 新增: const id = tab.frameSessions.get(frame); | |
| 365 | 新增: if (!id || expected && expected !== id) return; | |
| 366 | 新增: tab.frameSessions.delete(frame); | |
| 367 | 新增: frameSessions.delete(frame); | |
| 368 | 新增: const connection = sessions.get(id); | |
| 369 | 新增: sessions.delete(id); | |
| 370 | 新增: clipboard.detachSession(id); | |
| 371 | 新增: // Explicit detach also releases Playwright's transport and event listeners. | |
| 372 | 新增: void connection?.detach().catch(() => {}); | |
| 373 | 新增: } | |
| 374 | 新增: | |
| 363 | 375 | async function attachFrame(tab, frame) { |
| 364 | 376 | if (frameSessions.has(frame) || frame.isDetached()) return; |
| 377 | 新增: const id = token(); | |
| 378 | 新增: // Navigation and frameattached can race while newCDPSession is pending. | |
| 379 | 新增: frameSessions.set(frame, id); | |
| 380 | 新增: tab.frameSessions.set(frame, id); | |
| 365 | 381 | try { |
| 366 | 删除: const connection = await tab.page.context().newCDPSession(frame), id = token(); | |
| 367 | 删除: frameSessions.set(frame, id); sessions.set(id, connection); | |
| 382 | 新增: const connection = await tab.page.context().newCDPSession(frame); | |
| 383 | 新增: if (tab.page.isClosed() || frame.isDetached() || frameSessions.get(frame) !== id) { | |
| 384 | 新增: detachFrame(tab, frame, id); | |
| 385 | 新增: await connection.detach().catch(() => {}); | |
| 386 | 新增: return; | |
| 387 | 新增: } | |
| 388 | 新增: sessions.set(id, connection); | |
| 368 | 389 | for (const method of ["Runtime.executionContextCreated", "Runtime.executionContextDestroyed", "Runtime.executionContextsCleared", "Runtime.bindingCalled", "Page.frameDetached", "Page.frameNavigated"]) { |
| 369 | 390 | connection.on(method, params => clipboard.event({ sessionId: id, method, params })); |
| 370 | 391 | } |
| 371 | 392 | await clipboard.attach(id, tab.id); |
| 372 | 393 | if (tab.copyContext) await clipboard.setContext(tab.id, tab.copyContext); |
| 373 | 删除: } catch { /* A navigation can destroy a frame while its session attaches. */ } | |
| 394 | 新增: } catch { detachFrame(tab, frame, id); } | |
| 374 | 395 | } |
| 375 | 396 | async function addPage(page, { id = token(), incognito = false, login = false, kind = "web", workspace, blocked = false } = {}) { |
| 376 | 397 | if (byPage.has(page)) { const existing = byPage.get(page); await existing.ready; return existing; } |
| 377 | 398 | if (tabs.size >= 100) { await page.close(); throw failure("tab_limit", "打开的标签页过多,请先关闭部分标签页"); } |
| 378 | 删除: const tab = { id, page, incognito, login, kind, workspace, blocked, document: token(), geometry: token(), title: "", copyContext: "" }; | |
| 399 | 新增: const tab = { id, page, incognito, login, kind, workspace, blocked, document: token(), geometry: token(), title: "", copyContext: "", frameSessions: new Map() }; | |
| 379 | 400 | tabs.set(id, tab); byPage.set(page, tab); |
| 380 | 401 | page.on("close", () => { |
| 381 | 402 | void stopCapture(page); |
| 403 | 新增: for (const frame of tab.frameSessions.keys()) detachFrame(tab, frame); | |
| 382 | 404 | tabs.delete(id); clipboard?.detachTab(id); uploads?.cancelTab(id); |
| 383 | 405 | if (active === id) invalidateCopies(); |
| 384 | 406 | if (vault) for (const [sid, entry] of vault.sessions) if (entry.tab === id) vault.sessions.delete(sid); |
| @@ -411,17 +433,19 @@ export function createNativeBrowserService(options = {}) { | ||
| 411 | 433 | }); |
| 412 | 434 | page.on("frameattached", frame => { void attachFrame(tab, frame); }); |
| 413 | 435 | page.on("domcontentloaded", changed); |
| 414 | 删除: page.on("framedetached", frame => { const sid = frameSessions.get(frame); sessions.delete(sid); }); | |
| 436 | 新增: page.on("framedetached", frame => detachFrame(tab, frame)); | |
| 415 | 437 | tab.ready = (async () => { |
| 416 | 438 | // A persistent context keeps its launch viewport for newly opened pages. |
| 417 | 439 | // Each tab must join the workbench's current viewport before capture starts. |
| 418 | 440 | await page.setViewportSize(viewport); |
| 419 | 441 | const connection = await page.context().newCDPSession(page), sid = token(); |
| 442 | 新增: if (page.isClosed()) { await connection.detach().catch(() => {}); return; } | |
| 443 | 新增: tab.session = sid; sessions.set(sid, connection); frameSessions.set(page.mainFrame(), sid); | |
| 444 | 新增: tab.frameSessions.set(page.mainFrame(), sid); | |
| 420 | 445 | // Playwright enables focus emulation for every page. Native input proofs |
| 421 | 446 | // must observe the real selected document and native Chrome UI focus. |
| 422 | 447 | if (!mac && !(options.testing === true && options.headless !== false)) await connection.send("Emulation.setFocusEmulationEnabled", { enabled: false }); |
| 423 | 448 | tab.browserContextId = (await connection.send("Target.getTargetInfo")).targetInfo.browserContextId; |
| 424 | 删除: tab.session = sid; sessions.set(sid, connection); frameSessions.set(page.mainFrame(), sid); | |
| 425 | 449 | tab.nativeTarget = (await connection.send("Target.getTargetInfo")).targetInfo.targetId; |
| 426 | 450 | for (const method of ["Runtime.executionContextCreated", "Runtime.executionContextDestroyed", "Runtime.executionContextsCleared", "Runtime.bindingCalled", "Page.frameDetached", "Page.frameNavigated"]) { |
| 427 | 451 | connection.on(method, params => { clipboard.event({ sessionId: sid, method, params }); vault?.event({ sessionId: sid, method, params }); }); |
| @@ -1018,7 +1042,11 @@ export function createNativeBrowserService(options = {}) { | ||
| 1018 | 1042 | if (method === "GET") { |
| 1019 | 1043 | if (action === "frame-proof") return nativeFrameProof(); |
| 1020 | 1044 | if (action === "focus-proof") return nativeFocusProof(); |
| 1021 | 删除: if (action === "health") return { ready: true, native: !options.testing, capabilities: (await snapshot()).capabilities, ...(!mac ? { capture: { ...captureMetrics } } : {}) }; | |
| 1045 | 新增: if (action === "health") return { | |
| 1046 | 新增: ready: true, native: !options.testing, capabilities: (await snapshot()).capabilities, | |
| 1047 | 新增: resources: { tabs: tabs.size, sessions: sessions.size, clipboard_sessions: clipboard.sessions.size, captures: captures.size }, | |
| 1048 | 新增: ...(!mac ? { capture: { ...captureMetrics } } : {}), | |
| 1049 | 新增: }; | |
| 1022 | 1050 | if (action === "tabs") return snapshot(); |
| 1023 | 1051 | if (action === "events") { |
| 1024 | 1052 | const after = Number.isSafeInteger(input.after) && input.after >= 0 ? input.after : 0; |

