From b265358b2cd063865f93f938ec02066f5a7f20f5 Mon Sep 17 00:00:00 2001 From: Alessio Placitelli Date: Thu, 11 Feb 2021 09:26:29 +0000 Subject: [PATCH] Bug 1691144 - Trigger windows.onFocusChanged with no private window permission. r=zombie Credits to Jonathan Mayer for submitting the original patch. The patch changes the onFocusChanged extension event handler so that it no longer bails out if focus is moving to a private window, ensuring that the onFocusChanged event gets generated with the proper WINDOW_ID_NONE identifier. Differential Revision: https://phabricator.services.mozilla.com/D104732 --- .../extensions/parent/ext-windows.js | 5 +---- .../browser/browser_ext_windows_incognito.js | 21 ++++++++++++++++++- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/browser/components/extensions/parent/ext-windows.js b/browser/components/extensions/parent/ext-windows.js index 5522b1c53316..dde88dc88010 100644 --- a/browser/components/extensions/parent/ext-windows.js +++ b/browser/components/extensions/parent/ext-windows.js @@ -92,10 +92,7 @@ this.windows = class extends ExtensionAPI { Promise.resolve().then(() => { let windowId = Window.WINDOW_ID_NONE; let window = Services.focus.activeWindow; - if (window) { - if (!context.canAccessWindow(window)) { - return; - } + if (window && context.canAccessWindow(window)) { windowId = windowTracker.getId(window); } if (windowId !== lastOnFocusChangedWindowId) { diff --git a/browser/components/extensions/test/browser/browser_ext_windows_incognito.js b/browser/components/extensions/test/browser/browser_ext_windows_incognito.js index 08e07ad7f72e..e771df719632 100644 --- a/browser/components/extensions/test/browser/browser_ext_windows_incognito.js +++ b/browser/components/extensions/test/browser/browser_ext_windows_incognito.js @@ -15,7 +15,22 @@ add_task(async function test_window_incognito() { permissions: ["http://mochi.test/"], }, background() { + let lastFocusedWindowId = null; + // Catch focus change events to power the test below. + browser.windows.onFocusChanged.addListener(function listener( + eventWindowId + ) { + lastFocusedWindowId = eventWindowId; + browser.windows.onFocusChanged.removeListener(listener); + }); + browser.test.onMessage.addListener(async pbw => { + browser.test.assertEq( + browser.windows.WINDOW_ID_NONE, + lastFocusedWindowId, + "Focus on private window sends the event, but doesn't reveal windowId (without permissions)" + ); + await browser.test.assertRejects( browser.windows.get(pbw.windowId), /Invalid window ID/, @@ -59,9 +74,13 @@ add_task(async function test_window_incognito() { }, }); + await extension.startup(); + + // The tests expect the incognito window to be + // created after the extension is started, so think + // carefully when moving this line. let winData = await getIncognitoWindow(url); - await extension.startup(); extension.sendMessage(winData.details); await extension.awaitFinish("pass"); await BrowserTestUtils.closeWindow(winData.win);