From f511c55b0e65e9ff7ace9c7cdf2b9cff62206b3f Mon Sep 17 00:00:00 2001 From: William Durand Date: Fri, 3 Apr 2020 12:36:01 +0000 Subject: [PATCH] Bug 1613796 - Return false when browser.permissions.request isn't called on the active tab. r=robwu Differential Revision: https://phabricator.services.mozilla.com/D69226 --HG-- extra : moz-landing-system : lando --- .../extensions/test/browser/browser.ini | 1 + .../browser_ext_request_permissions.js | 38 +++++++++++++++++++ browser/modules/ExtensionsUI.jsm | 5 +++ 3 files changed, 44 insertions(+) create mode 100644 browser/components/extensions/test/browser/browser_ext_request_permissions.js diff --git a/browser/components/extensions/test/browser/browser.ini b/browser/components/extensions/test/browser/browser.ini index 720abe5cc5a2..389f9203ef9a 100644 --- a/browser/components/extensions/test/browser/browser.ini +++ b/browser/components/extensions/test/browser/browser.ini @@ -184,6 +184,7 @@ skip-if = debug || os != 'win' # FIXME: re-enable on debug build (bug 1442822) [browser_ext_port_disconnect_on_crash.js] skip-if = !e10s || !crashreporter # the tab's process is killed during the test. Without e10s the parent process would die too [browser_ext_port_disconnect_on_window_close.js] +[browser_ext_request_permissions.js] [browser_ext_runtime_openOptionsPage.js] [browser_ext_runtime_openOptionsPage_uninstall.js] [browser_ext_search.js] diff --git a/browser/components/extensions/test/browser/browser_ext_request_permissions.js b/browser/components/extensions/test/browser/browser_ext_request_permissions.js new file mode 100644 index 000000000000..5a79bd246c04 --- /dev/null +++ b/browser/components/extensions/test/browser/browser_ext_request_permissions.js @@ -0,0 +1,38 @@ +"use strict"; + +add_task(async function test_permissions_prompt() { + let extension = ExtensionTestUtils.loadExtension({ + manifest: { + optional_permissions: ["history"], + }, + background: () => { + browser.tabs.create({ + url: browser.runtime.getURL("test.html"), + active: false, + }); + }, + files: { + "test.html": ``, + "test.js": async () => { + const result = await new Promise(resolve => { + browser.test.withHandlingUserInput(() => { + resolve(browser.permissions.request({ permissions: ["history"] })); + }); + }); + + browser.test.assertFalse( + result, + "permissions.request() from a hidden tab should be ignored" + ); + + browser.test.sendMessage("done"); + }, + }, + }); + await extension.startup(); + + await extension.awaitMessage("done"); + + // The extension tab is automatically closed upon unload. + await extension.unload(); +}); diff --git a/browser/modules/ExtensionsUI.jsm b/browser/modules/ExtensionsUI.jsm index 325abe4cca3a..8b79d297d62f 100644 --- a/browser/modules/ExtensionsUI.jsm +++ b/browser/modules/ExtensionsUI.jsm @@ -372,6 +372,11 @@ var ExtensionsUI = { await pending; } + // Make sure the tab is the active tab in the window. + if (window.gBrowser.selectedBrowser !== browser) { + return false; + } + let promise = new Promise(resolve => { function eventCallback(topic) { let doc = this.browser.ownerDocument;