Bug 1748551 support persistent event for commands.onCommand api r=rpl

Differential Revision: https://phabricator.services.mozilla.com/D141516
This commit is contained in:
Shane Caraveo 2022-03-19 01:42:51 +00:00
parent b830758f26
commit d7acd3e148
3 changed files with 99 additions and 25 deletions

View File

@ -12,7 +12,22 @@ ChromeUtils.defineModuleGetter(
"resource://gre/modules/ExtensionShortcuts.jsm"
);
this.commands = class extends ExtensionAPI {
this.commands = class extends ExtensionAPIPersistent {
PERSISTENT_EVENTS = {
onCommand({ fire }) {
let listener = (eventName, commandName) => {
fire.async(commandName);
};
this.on("command", listener);
return {
unregister: () => this.off("command", listener),
convert(_fire) {
fire = _fire;
},
};
},
};
static onUninstall(extensionId) {
return ExtensionShortcuts.removeCommandsFromStorage(extensionId);
}
@ -39,17 +54,10 @@ this.commands = class extends ExtensionAPI {
reset: name => this.extension.shortcuts.resetCommand(name),
onCommand: new EventManager({
context,
name: "commands.onCommand",
module: "commands",
event: "onCommand",
inputHandling: true,
register: fire => {
let listener = (eventName, commandName) => {
fire.async(commandName);
};
this.on("command", listener);
return () => {
this.off("command", listener);
};
},
extensionApi: this,
}).api(),
},
};

View File

@ -378,3 +378,55 @@ add_task(async function test_user_defined_commands() {
SimpleTest.endMonitorConsole();
await waitForConsole;
});
add_task(async function test_commands_event_page() {
await SpecialPowers.pushPrefEnv({
set: [["extensions.eventPages.enabled", true]],
});
let extension = ExtensionTestUtils.loadExtension({
useAddonManager: "permanent",
manifest: {
applications: { gecko: { id: "eventpage@commands" } },
background: { persistent: false },
commands: {
"toggle-feature": {
suggested_key: {
default: "Alt+Shift+J",
},
},
},
},
background() {
browser.commands.onCommand.addListener(name => {
browser.test.assertEq(name, "toggle-feature", "command received");
browser.test.sendMessage("onCommand");
});
browser.test.sendMessage("ready");
},
});
await extension.startup();
await extension.awaitMessage("ready");
assertPersistentListeners(extension, "commands", "onCommand", {
primed: false,
});
// test events waken background
await extension.terminateBackground();
assertPersistentListeners(extension, "commands", "onCommand", {
primed: true,
});
EventUtils.synthesizeKey("j", { altKey: true, shiftKey: true });
await extension.awaitMessage("ready");
await extension.awaitMessage("onCommand");
ok(true, "persistent event woke background");
assertPersistentListeners(extension, "commands", "onCommand", {
primed: false,
});
await extension.unload();
await SpecialPowers.popPrefEnv();
});

View File

@ -6,9 +6,6 @@
/* exported ExtensionShortcuts */
const EXPORTED_SYMBOLS = ["ExtensionShortcuts", "ExtensionShortcutKeyMap"];
const { XPCOMUtils } = ChromeUtils.import(
"resource://gre/modules/XPCOMUtils.jsm"
);
const { ExtensionCommon } = ChromeUtils.import(
"resource://gre/modules/ExtensionCommon.jsm"
);
@ -35,17 +32,34 @@ ChromeUtils.defineModuleGetter(
"resource://gre/modules/PrivateBrowsingUtils.jsm"
);
XPCOMUtils.defineLazyGetter(this, "windowTracker", () => {
return ExtensionParent.apiManager.global.windowTracker;
});
XPCOMUtils.defineLazyGetter(this, "browserActionFor", () => {
return ExtensionParent.apiManager.global.browserActionFor;
});
XPCOMUtils.defineLazyGetter(this, "pageActionFor", () => {
return ExtensionParent.apiManager.global.pageActionFor;
});
XPCOMUtils.defineLazyGetter(this, "sidebarActionFor", () => {
return ExtensionParent.apiManager.global.sidebarActionFor;
/**
* These properties cannot be lazy getters otherwise they
* get defined on first use, at a time when some modules
* may not have been loaded. In that case, the getter would
* become undefined until next app restart.
*/
/* globals windowTracker, browserActionFor, pageActionFor, sidebarActionFor */
Object.defineProperties(this, {
windowTracker: {
get() {
return ExtensionParent.apiManager.global.windowTracker;
},
},
browserActionFor: {
get() {
return ExtensionParent.apiManager.global.browserActionFor;
},
},
pageActionFor: {
get() {
return ExtensionParent.apiManager.global.pageActionFor;
},
},
sidebarActionFor: {
get() {
return ExtensionParent.apiManager.global.sidebarActionFor;
},
},
});
const { ExtensionError, DefaultMap } = ExtensionUtils;