Bug 1421811 - Part 2: Support commands.reset() to reset a command's updates r?mixedpuppy draft
authorMark Striemer <mstriemer@mozilla.com>
Wed, 31 Jan 2018 15:49:20 -0600
changeset 752334 b189a895fa3365b5a5a389686ef9f2661ccb7eed
parent 752333 8dbeec1f5b7b4b3493afb1fde17bbd0c6a8947ee
child 752335 2ac9173836cfa2fbbc14194ace358d061da7c0ae
child 752349 aa7f80873254f7e313067fea54436eb24ef27e9c
child 753177 4eb8f0b4220d5a0cb1a1b1b2e93ec8e280ba17e6
push id98231
push userbmo:mstriemer@mozilla.com
push dateWed, 07 Feb 2018 23:16:08 +0000
reviewersmixedpuppy
bugs1421811
milestone60.0a1
Bug 1421811 - Part 2: Support commands.reset() to reset a command's updates r?mixedpuppy MozReview-Commit-ID: 4hWGo1ZH6tn
browser/components/extensions/ext-commands.js
browser/components/extensions/schemas/commands.json
browser/components/extensions/test/browser/browser_ext_commands_update.js
--- 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"});