Bug 1893831 - Fix issue with browser.menus.onClicked not working when the background script has been stopped. r=standard8

Differential Revision: https://phabricator.services.mozilla.com/D209542

--HG--
extra : amend_source : 52cd0c9a04ee355e5b64af65de31e8e1bf46879e
This commit is contained in:
Mark Banner 2024-05-10 13:10:50 +03:00
Родитель 90ae0661e2
Коммит e2c5343bcf
3 изменённых файлов: 45 добавлений и 15 удалений

Просмотреть файл

@ -1446,22 +1446,26 @@ this.menus = class extends ExtensionAPIPersistent {
onClicked({ context, fire }) {
const { extension } = this;
const listener = async (event, info, nativeTab) => {
const { linkedBrowser } = nativeTab || tabTracker.activeTab;
const tab = nativeTab && extension.tabManager.convert(nativeTab);
if (fire.wakeup) {
// force the wakeup, thus the call to convert to get the context.
await fire.wakeup();
// If while waiting the tab disappeared we bail out.
if (
!linkedBrowser.ownerGlobal.gBrowser.getTabForBrowser(linkedBrowser)
) {
if (!tabTracker.getTab(tab.id, /* do not throw, but return */ null)) {
console.error(
`menus.onClicked: target tab closed during background startup.`
);
return;
}
}
context.withPendingBrowser(linkedBrowser, () => fire.sync(info, tab));
// The pending browser concept is a hack to be able to access the browser
// without having to explicitly pass it around. This basically sets
// context.pendingEventBrowser before calling the provided callback.
// The linked browser being null (for example if no message is selected)
// does not have negative consequences here.
context.withPendingBrowser(nativeTab.linkedBrowser, () =>
fire.sync(info, tab)
);
};
extension.on("webext-menu-menuitem-click", listener);

Просмотреть файл

@ -42,7 +42,8 @@ async function subtest_action_menu(
target,
expectedInfo,
expectedTab,
manifest
manifest,
terminateBackground
) {
function checkVisibility(menu, visible) {
const removeExtension = menu.querySelector(
@ -151,6 +152,16 @@ async function subtest_action_menu(
expectedTab
);
if (terminateBackground) {
await extension.terminateBackground({
disableResetIdleForTest: true,
});
await extension.awaitMessage("suspended-test_menu_onclick");
assertPersistentListeners(extension, "menus", "onClicked", {
primed: true,
});
}
const clickedPromise = checkClickedEvent(
extension,
expectedInfo,
@ -161,6 +172,12 @@ async function subtest_action_menu(
);
await clickedPromise;
if (terminateBackground) {
// The extension will re-add menu entries on wakeup (ignored if they already
// exist).
await extension.awaitMessage("menus-created");
}
// Test the non actionButton element for visibility of the management menu entries.
if (target.nonActionButtonSelector) {
const nonActionButtonElement = testWindow.document.querySelector(
@ -318,7 +335,8 @@ add_task(async function test_browser_action_menu_mv3() {
action: {
default_title: "This is a test",
},
}
},
true
);
});
add_task(async function test_message_display_action_menu_pane_mv3() {

Просмотреть файл

@ -181,6 +181,22 @@ async function getMenuExtension(manifest) {
const details = {
files: {
"background.js": async () => {
// Register listeners before the first await, so they get registered as
// persistent listeners.
browser.menus.onClicked.addListener((...args) => {
browser.test.sendMessage("onClicked", args);
});
browser.menus.onShown.addListener((...args) => {
browser.test.sendMessage("onShown", args);
});
if (browser.runtime.getManifest().manifest_version > 2) {
browser.runtime.onSuspend.addListener(() => {
browser.test.sendMessage("suspended-test_menu_onclick");
});
}
const contexts = [
"audio",
"compose_action",
@ -207,7 +223,6 @@ async function getMenuExtension(manifest) {
} else {
contexts.push("browser_action", "browser_action_menu");
}
for (const context of contexts) {
await new Promise(resolve =>
browser.menus.create(
@ -221,13 +236,6 @@ async function getMenuExtension(manifest) {
);
}
browser.menus.onShown.addListener((...args) => {
browser.test.sendMessage("onShown", args);
});
browser.menus.onClicked.addListener((...args) => {
browser.test.sendMessage("onClicked", args);
});
browser.test.sendMessage("menus-created");
},
},