Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(browser): 弹出页签窗口里的 popup 与快捷键转发留在原窗口 renderer

Dash4478a5d580e5
diff --git a/apps/desktop/src/main/__tests__/webview-security.test.ts b/apps/desktop/src/main/__tests__/webview-security.test.ts
index 8c01e3d91d..b284cb2508 100644
--- a/apps/desktop/src/main/__tests__/webview-security.test.ts
+++ b/apps/desktop/src/main/__tests__/webview-security.test.ts
@@ -1088,6 +1088,40 @@ describe('installDeferredPopupRouter', () => {
expect(hostContents.send).not.toHaveBeenCalled();
});
+ // 测试价值
+ // - 生产入口与可观察结果:installDeferredPopupRouter 经 host resolver 发送 popup;可观察的是 resolver
+ // 收到捕获时的 hostContents,并可据此让「弹出到独立窗口的页签」的 popup 留在那扇窗口 renderer。
+ // - Bug / 规则依据:review「弹出的浏览器页签的 window.open 被改投整栏侧栏,popup 页签无法显示、
+ // 原生 popup surface owner 校验失败」。
+ // - 现有覆盖缺口:现有用例的 resolver 不看捕获的 host。
+ // - 最小失败输入:resolver 对捕获的页签窗口 host 返回其自身,对其它 host 返回侧栏宿主。
+ it('把捕获的 hostContents 交给 host resolver,页签窗口的 popup 留在原 renderer', () => {
+ const { childContents, hostContents, popupWindow } = makePopupHarness();
+ const sidebarHost = {
+ isDestroyed: vi.fn(() => false),
+ send: vi.fn(),
+ } as unknown as WebContents & { send: ReturnType<typeof vi.fn> };
+ const seen: unknown[] = [];
+ setRsbPopupHostResolver((captured) => {
+ seen.push(captured);
+ return captured === hostContents ? captured : sidebarHost;
+ });
+
+ installDeferredPopupRouter(
+ hostContents,
+ popupWindow as unknown as BrowserWindow,
+ 'foreground-tab',
+ );
+ childContents.emit('will-navigate', {}, 'https://accounts.example.com/oauth');
+
+ expect(seen).toEqual([hostContents]);
+ expect(hostContents.send).toHaveBeenCalledWith(RSB_BROWSER_POPUP_CHANNEL, {
+ url: 'https://accounts.example.com/oauth',
+ disposition: 'foreground-tab',
+ });
+ expect(sidebarHost.send).not.toHaveBeenCalled();
+ });
+
it('host resolver 解析失败或返回已销毁 host 时回落捕获的 hostContents', () => {
const { childContents, hostContents, popupWindow } = makePopupHarness();
setRsbPopupHostResolver(() => null);
diff --git a/apps/desktop/src/main/bootstrap-electron.ts b/apps/desktop/src/main/bootstrap-electron.ts
index 1e6fe04620..1e6de79d3d 100644
--- a/apps/desktop/src/main/bootstrap-electron.ts
+++ b/apps/desktop/src/main/bootstrap-electron.ts
@@ -2414,7 +2414,13 @@ setRsbPopupOpenerReportSubscriber((listener) =>
// popup 路由目标 host 动态解析:异步等待(归属反查 / deferred URL 捕获)期间用户
// detach 侧边栏或切视图后,捕获时的 hostContents 已过时 —— 发送时刻按当前宿主
// 形态解析(与 tab-op bridge 同一来源),消息才能落到活着的订阅者。
-setRsbPopupHostResolver(() => rsbWindowController.getHostWebContents());
+// 弹出到独立页签窗口的浏览器页签,guest 挂在那扇窗口的 renderer 上:popup 与快捷键转发
+// 回到它(捕获时的 host 仍是页签窗口就保持),不被改投给整栏侧栏宿主。
+setRsbPopupHostResolver((capturedHost) =>
+ sidebarTabWindowsController.slotForSender(capturedHost)
+ ? capturedHost
+ : rsbWindowController.getHostWebContents(),
+);
// Tab-op result handler for the main → renderer request/response bridge —
// RsbWebviewBackend (Phase 3) uses this to drive `open` / `focus` / `close`
// against the renderer's RSB store.
diff --git a/apps/desktop/src/main/webview-security.ts b/apps/desktop/src/main/webview-security.ts
index 3ad2a24c39..acbde7d234 100644
--- a/apps/desktop/src/main/webview-security.ts
+++ b/apps/desktop/src/main/webview-security.ts
@@ -664,9 +664,15 @@ function isRoutablePopupUrl(url: string): boolean {
* hostContents 过时 —— 旧 renderer 的 Shell 已退订,fanOut 不缓冲,消息发过去
* 就是丢弃。发送时刻动态解析当前 host 才能落到活着的订阅者。
*/
-let popupHostResolver: (() => WebContents | null) | null = null;
+let popupHostResolver: ((capturedHost: WebContents) => WebContents | null) | null = null;
-export function setRsbPopupHostResolver(resolver: (() => WebContents | null) | null): void {
+/**
+ * 解析函数收到捕获时的 hostContents:弹出到独立窗口的页签,其 guest 就挂在那扇窗口的 renderer
+ * 上,popup 必须回到同一个 renderer(原生 popup surface 的 owner 校验也认它),解析函数据此放行。
+ */
+export function setRsbPopupHostResolver(
+ resolver: ((capturedHost: WebContents) => WebContents | null) | null,
+): void {
popupHostResolver = resolver;
}
@@ -679,7 +685,7 @@ function sendBrowserPopup(
let current: WebContents | null = null;
if (popupHostResolver) {
try {
- current = popupHostResolver();
+ current = popupHostResolver(hostContents);
} catch {
current = null;
}
@@ -821,7 +827,7 @@ export function installBrowserGuestHandlers(
event.preventDefault();
let target: WebContents | null = null;
try {
- target = popupHostResolver?.() ?? null;
+ target = popupHostResolver?.(hostContents) ?? null;
} catch {
target = null;
}