Bug 1421811 - Part 2: Support commands.reset() to reset a command's updates r?mixedpuppy
MozReview-Commit-ID: 4hWGo1ZH6tn
--- a/browser/components/extensions/ext-commands.js
+++ b/browser/components/extensions/ext-commands.js
@@ -330,16 +330,34 @@ this.commands = class extends ExtensionA
command.shortcut = shortcut;
}
await ExtensionSettingsStore.addSetting(
extension.id, "commands", name, commandUpdates);
this.registerKeys(commands);
},
+ reset: async (name) => {
+ let {extension, manifestCommands} = this;
+ let commands = await this.commands;
+ let command = commands.get(name);
+
+ if (!command) {
+ throw new ExtensionError(`Unknown command "${name}"`);
+ }
+
+ let storedCommand = ExtensionSettingsStore.getSetting(
+ "commands", name, extension.id);
+
+ if (storedCommand && storedCommand.value) {
+ commands.set(name, {...manifestCommands.get(name)});
+ ExtensionSettingsStore.removeSetting(extension.id, "commands", name);
+ this.registerKeys(commands);
+ }
+ },
onCommand: new EventManager(context, "commands.onCommand", fire => {
let listener = (eventName, commandName) => {
fire.async(commandName);
};
this.on("command", listener);
return () => {
this.off("command", listener);
};
--- a/browser/components/extensions/schemas/commands.json
+++ b/browser/components/extensions/schemas/commands.json
@@ -148,16 +148,29 @@
"$ref": "manifest.KeyName",
"optional": true
}
}
}
]
},
{
+ "name": "reset",
+ "type": "function",
+ "async": true,
+ "description": "Reset a command's details to what is specified in the manifest.",
+ "parameters": [
+ {
+ "type": "string",
+ "name": "name",
+ "description": "The name of the command."
+ }
+ ]
+ },
+ {
"name": "getAll",
"type": "function",
"async": "callback",
"description": "Returns all the registered extension commands for this extension and their shortcut (if active).",
"parameters": [
{
"type": "function",
"name": "callback",
--- a/browser/components/extensions/test/browser/browser_ext_commands_update.js
+++ b/browser/components/extensions/test/browser/browser_ext_commands_update.js
@@ -67,16 +67,19 @@ add_task(async function test_update_defi
},
},
},
background() {
browser.test.onMessage.addListener(async (msg, data) => {
if (msg == "update") {
await browser.commands.update(data);
return browser.test.sendMessage("updateDone");
+ } else if (msg == "reset") {
+ await browser.commands.reset(data);
+ return browser.test.sendMessage("resetDone");
} else if (msg != "run") {
return;
}
// Test initial manifest command.
let commands = await browser.commands.getAll();
browser.test.assertEq(1, commands.length, "There is 1 command");
let command = commands[0];
browser.test.assertEq("foo", command.name, "The name is right");
@@ -182,16 +185,22 @@ add_task(async function test_update_defi
// This command now only has a description set in storage, also update the shortcut.
extension.sendMessage("update", {name: "foo", shortcut: "Alt+Shift+P"});
await extension.awaitMessage("updateDone");
let storedCommand = await ExtensionSettingsStore.getSetting(
"commands", "foo", extension.id);
is(storedCommand.value.shortcut, "Alt+Shift+P", "The shortcut is saved correctly");
is(storedCommand.value.description, "description only", "The description is saved correctly");
+ // Calling browser.commands.reset("foo") should reset to manifest version.
+ extension.sendMessage("reset", "foo");
+ await extension.awaitMessage("resetDone");
+
+ checkKey(extension.id, "I", "accel shift");
+
// Check that enable/disable removes the keyset and reloads the saved command.
let addon = await AddonManager.getAddonByID(extension.id);
await disableAddon(addon);
let keyset = extensionKeyset(extension.id);
is(keyset, null, "The extension keyset is removed when disabled");
// Add some commands to storage, only "foo" should get loaded.
await ExtensionSettingsStore.addSetting(
extension.id, "commands", "foo", {shortcut: "Alt+Shift+P"});