Bug 1421811 - Part 2: Support commands.reset() to reset a command's updates r=mixedpuppy
authorMark Striemer <mstriemer@mozilla.com>
Wed, 31 Jan 2018 15:49:20 -0600
changeset 403233 c8f509a2408956043de260713c0b81961142fa2e
parent 403232 4f515f0fb671cec9452b6ebb6c65547b77ccff43
child 403234 827264a477908d2f023fd95fe17ce15db78231e3
push id99755
push userbtara@mozilla.com
push dateSat, 10 Feb 2018 10:02:59 +0000
treeherdermozilla-inbound@0417e1acfc10 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmixedpuppy
bugs1421811
milestone60.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
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"});