Bug 1283116 - Implement chrome.management.getSelf, r?aswan draft
authorBob Silverberg <bsilverberg@mozilla.com>
Thu, 21 Jul 2016 09:34:07 -0400
changeset 390609 3eecb3c2f4afc90f66f704439280f423380a0cc2
parent 390243 f501719a4711045cf5bcb519812d56899cd635a9
child 526033 f4ace001c67125aff2264277f11cee267ca881c5
push id23710
push userbmo:bob.silverberg@gmail.com
push dateThu, 21 Jul 2016 14:19:55 +0000
reviewersaswan
bugs1283116
milestone50.0a1
Bug 1283116 - Implement chrome.management.getSelf, r?aswan MozReview-Commit-ID: Bj9ZyF1meED
toolkit/components/extensions/ext-management.js
toolkit/components/extensions/schemas/management.json
toolkit/components/extensions/test/mochitest/test_ext_management.html
--- a/toolkit/components/extensions/ext-management.js
+++ b/toolkit/components/extensions/ext-management.js
@@ -1,9 +1,61 @@
 /* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
 /* vim: set sts=2 sw=2 et tw=80: */
 "use strict";
 
+XPCOMUtils.defineLazyModuleGetter(this, "AddonManager",
+                                  "resource://gre/modules/AddonManager.jsm");
+
+function installType(addon) {
+  if (addon.temporarilyInstalled) {
+    return "development";
+  } else if (addon.foreignInstall) {
+    return "sideload";
+  } else if (addon.isSystem) {
+    return "other";
+  }
+  return "normal";
+}
+
 extensions.registerSchemaAPI("management", (extension, context) => {
   return {
-    management: {},
+    management: {
+      getSelf: function() {
+        return new Promise((resolve, reject) => AddonManager.getAddonByID(extension.id, addon => {
+          try {
+            let m = extension.manifest;
+            let extInfo = {
+              id: extension.id,
+              name: addon.name,
+              shortName: m.short_name || "",
+              description: addon.description || "",
+              version: addon.version,
+              mayDisable: !!(addon.permissions & AddonManager.PERM_CAN_DISABLE),
+              enabled: addon.isActive,
+              optionsUrl: addon.optionsURL || "",
+              permissions: Array.from(extension.permissions).filter(perm => {
+                return !extension.whiteListedHosts.pat.includes(perm);
+              }),
+              hostPermissions: extension.whiteListedHosts.pat,
+              installType: installType(addon),
+            };
+            if (addon.homepageURL) {
+              extInfo.homepageUrl = addon.homepageURL;
+            }
+            if (addon.updateURL) {
+              extInfo.updateUrl = addon.updateURL;
+            }
+            if (m.icons) {
+              extInfo.icons = Object.keys(m.icons).map(key => {
+                return {size: Number(key), url: m.icons[key]};
+              });
+            }
+
+            resolve(extInfo);
+          } catch (e) {
+            reject(e);
+          }
+        }));
+      },
+    },
   };
 });
--- a/toolkit/components/extensions/schemas/management.json
+++ b/toolkit/components/extensions/schemas/management.json
@@ -193,17 +193,16 @@
               }
             ]
           }
         ]
       },
       {
         "name": "getSelf",
         "type": "function",
-        "unsupported": true,
         "description": "Returns information about the calling extension. Note: This function can be used without requesting the 'management' permission in the manifest.",
         "async": "callback",
         "parameters": [
           {
             "type": "function",
             "name": "callback",
             "optional": true,
             "parameters": [
--- a/toolkit/components/extensions/test/mochitest/test_ext_management.html
+++ b/toolkit/components/extensions/test/mochitest/test_ext_management.html
@@ -25,12 +25,107 @@ add_task(function* test_management_schem
     },
     background: `(${background})()`,
   });
   yield extension.startup();
   yield extension.awaitFinish("management-schema");
   yield extension.unload();
 });
 
+// Shared background function for getSelf tests
+function backgroundGetSelf() {
+  browser.management.getSelf().then(extInfo => {
+    browser.test.sendMessage("management-getSelf", extInfo);
+  }, error => {
+    browser.test.notifyFail(`getSelf rejected with error: ${String(error)}`);
+  });
+}
+
+add_task(function* test_management_get_self_complete() {
+  const id = "get_self_test_complete@tests.mozilla.com";
+  const permissions = ["management", "cookies"];
+  const hostPermissions = ["*://example.org/", "https://foo.example.org/"];
+
+  let manifest = {
+    applications: {
+      gecko: {
+        update_url: "https://updates.mozilla.com/",
+      },
+    },
+    name: "test extension name",
+    short_name: "test extension short name",
+    description: "test extension description",
+    version: "1.0",
+    homepage_url: "http://www.example.com/",
+    options_ui: {
+      "page": "get_self_options.html",
+    },
+    icons: {
+      "16": "icons/icon-16.png",
+      "48": "icons/icon-48.png",
+    },
+    permissions: permissions.concat(hostPermissions),
+  };
+
+  let extension = ExtensionTestUtils.loadExtension({
+    manifest,
+    background: `(${backgroundGetSelf})()`,
+    useAddonManager: true,
+  }, id);
+  yield extension.startup();
+  let extInfo = yield extension.awaitMessage("management-getSelf");
+
+  is(extInfo.id, id, "getSelf returned the expected id");
+  for (let prop of ["name", "description", "version"]) {
+    is(extInfo[prop], manifest[prop], `getSelf returned the expected ${prop}`);
+  }
+  is(extInfo.shortName, manifest.short_name, "getSelf returned the expected shortName");
+  is(extInfo.mayDisable, true, "getSelf returned the expected value for mayDisable");
+  is(extInfo.enabled, true, "getSelf returned the expected value for enabled");
+  is(extInfo.homepageUrl, manifest.homepage_url, "getSelf returned the expected homepageUrl");
+  is(extInfo.updateUrl, manifest.applications.gecko.update_url, "getSelf returned the expected updateUrl");
+  ok(extInfo.optionsUrl.endsWith(manifest.options_ui.page), "getSelf returned the expected optionsUrl");
+  for (let [index, size] of Object.keys(manifest.icons).sort().entries()) {
+    is(extInfo.icons[index].size, +size, "getSelf returned the expected icon size");
+    is(extInfo.icons[index].url, manifest.icons[size], "getSelf returned the expected icon url");
+  }
+  isDeeply(extInfo.permissions.sort(), permissions.sort(), "getSelf returned the expected permissions");
+  isDeeply(extInfo.hostPermissions.sort(), hostPermissions.sort(), "getSelf returned the expected hostPermissions");
+  is(extInfo.installType, "development", "getSelf returned the expected installType");
+  yield extension.unload();
+});
+
+add_task(function* test_management_get_self_minimal() {
+  const id = "get_self_test_minimal@tests.mozilla.com";
+
+  let manifest = {
+    name: "test extension name",
+    version: "1.0",
+  };
+
+  let extension = ExtensionTestUtils.loadExtension({
+    manifest,
+    background: `(${backgroundGetSelf})()`,
+    useAddonManager: true,
+  }, id);
+  yield extension.startup();
+  let extInfo = yield extension.awaitMessage("management-getSelf");
+
+  is(extInfo.id, id, "getSelf returned the expected id");
+  for (let prop of ["name", "version"]) {
+    is(extInfo[prop], manifest[prop], `getSelf returned the expected ${prop}`);
+  }
+  for (let prop of ["shortName", "description", "optionsUrl"]) {
+    is(extInfo[prop], "", `getSelf returned the expected ${prop}`);
+  }
+  for (let prop of ["homepageUrl", " updateUrl", "icons"]) {
+    is(extInfo.hasOwnProperty(prop), false, `getSelf did not return a ${prop} property`);
+  }
+  for (let prop of ["permissions", "hostPermissions"]) {
+    isDeeply(extInfo[prop], [], `getSelf returned the expected ${prop}`);
+  }
+  yield extension.unload();
+});
+
 </script>
 
 </body>
 </html>