From aa31799d78381d32070876dfaa3479eb1118eeaf Mon Sep 17 00:00:00 2001 From: Daisuke Akatsuka Date: Fri, 11 Dec 2020 23:52:20 +0000 Subject: [PATCH] Bug 1681217: Avoid sending keyup event to content document during cycling tabs. r=dao Differential Revision: https://phabricator.services.mozilla.com/D98993 --- browser/base/content/browser-ctrlTab.js | 23 ++++++++++++----- .../content/test/general/browser_ctrlTab.js | 25 +++++++++++++++++++ 2 files changed, 42 insertions(+), 6 deletions(-) diff --git a/browser/base/content/browser-ctrlTab.js b/browser/base/content/browser-ctrlTab.js index de15801ab282..c05a474fd5ef 100644 --- a/browser/base/content/browser-ctrlTab.js +++ b/browser/base/content/browser-ctrlTab.js @@ -383,8 +383,6 @@ var ctrlTab = { return; } - document.addEventListener("keyup", this, true); - this.canvasWidth = Math.ceil( (screen.availWidth * 0.85) / this.maxTabPreviews ); @@ -454,8 +452,6 @@ var ctrlTab = { }, suspendGUI: function ctrlTab_suspendGUI() { - document.removeEventListener("keyup", this, true); - for (let preview of this.previews) { this.updatePreview(preview, null); } @@ -480,6 +476,8 @@ var ctrlTab = { return; } + Services.els.addSystemEventListener(document, "keyup", this, false); + let tabs = gBrowser.visibleTabs; if (tabs.length > 2) { this.open(); @@ -584,8 +582,21 @@ var ctrlTab = { this.onKeyPress(event); break; case "keyup": - if (event.keyCode == event.DOM_VK_CONTROL) { - this.pick(); + // During cycling tabs, we avoid sending keyup event to content document. + event.preventDefault(); + event.stopPropagation(); + + if (event.keyCode === event.DOM_VK_CONTROL) { + Services.els.removeSystemEventListener( + document, + "keyup", + this, + false + ); + + if (this.isOpen) { + this.pick(); + } } break; case "popupshowing": diff --git a/browser/base/content/test/general/browser_ctrlTab.js b/browser/base/content/test/general/browser_ctrlTab.js index b3c495ab7828..b31b971315d9 100644 --- a/browser/base/content/test/general/browser_ctrlTab.js +++ b/browser/base/content/test/general/browser_ctrlTab.js @@ -211,6 +211,17 @@ add_task(async function() { ? "back to the previously selected tab" : normalized + " tabs back in most-recently-selected order"; + // Add keyup listener to all content documents. + await Promise.all( + gBrowser.tabs.map(tab => + SpecialPowers.spawn(tab.linkedBrowser, [], () => { + content.window.addEventListener("keyup", () => { + content.window._ctrlTabTestKeyupHappend = true; + }); + }) + ) + ); + for (let i = 0; i < tabTimes; i++) { await pressCtrlTab(); @@ -253,5 +264,19 @@ add_task(async function() { " goes " + where ); + + const keyupEvents = await Promise.all( + gBrowser.tabs.map(tab => + SpecialPowers.spawn( + tab.linkedBrowser, + [], + () => !!content.window._ctrlTabTestKeyupHappend + ) + ) + ); + ok( + keyupEvents.every(isKeyupHappned => !isKeyupHappned), + "Content document doesn't capture Keyup event during cycling tabs" + ); } });