mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-11-23 21:01:08 +00:00
Bug 1748551 support persistent event for commands.onCommand api r=rpl
Differential Revision: https://phabricator.services.mozilla.com/D141516
This commit is contained in:
parent
b830758f26
commit
d7acd3e148
@ -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(),
|
||||
},
|
||||
};
|
||||
|
@ -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();
|
||||
});
|
||||
|
@ -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;
|
||||
|
Loading…
Reference in New Issue
Block a user