Bug 1223589 - [webext] Implement getPlatformInfo (r=kmag)
authorBill McCloskey <billm@mozilla.com>
Tue, 10 Nov 2015 14:15:09 -0800
changeset 308651 4371f650013131bdbd0efded140e64e8fb5aa9c4
parent 308650 be72540c9e57cdc7f272daed185069eb06c2101e
child 308652 9e61ae324d135478280c09b5f493e9d375109334
push id7513
push useratolfsen@mozilla.com
push dateFri, 13 Nov 2015 14:03:43 +0000
reviewerskmag
bugs1223589
milestone45.0a1
Bug 1223589 - [webext] Implement getPlatformInfo (r=kmag)
toolkit/components/extensions/ext-runtime.js
toolkit/components/extensions/test/mochitest/mochitest.ini
toolkit/components/extensions/test/mochitest/test_ext_runtime_getPlatformInfo.html
--- a/toolkit/components/extensions/ext-runtime.js
+++ b/toolkit/components/extensions/ext-runtime.js
@@ -1,14 +1,18 @@
 var { classes: Cc, interfaces: Ci, utils: Cu } = Components;
 
+XPCOMUtils.defineLazyModuleGetter(this, "AppConstants",
+                                  "resource://gre/modules/AppConstants.jsm");
+
 Cu.import("resource://gre/modules/ExtensionUtils.jsm");
 var {
   EventManager,
   ignoreEvent,
+  runSafe,
 } = ExtensionUtils;
 
 function processRuntimeConnectParams(win, ...args) {
   let extensionId, connectInfo;
 
   // connect("...") and connect("...", { ... })
   if (typeof args[0] == "string") {
     extensionId = args.shift();
@@ -69,11 +73,29 @@ extensions.registerAPI((extension, conte
         return Cu.cloneInto(extension.manifest, context.cloneScope);
       },
 
       id: extension.id,
 
       getURL: function(url) {
         return extension.baseURI.resolve(url);
       },
+
+      getPlatformInfo: function(callback) {
+        let os = AppConstants.platform;
+        if (os == "macosx") {
+          os = "mac";
+        }
+
+        let abi = Services.appinfo.XPCOMABI;
+        let [arch, compiler] = abi.split("-");
+        if (arch == "x86") {
+          arch = "x86-32";
+        } else if (arch == "x86_64") {
+          arch = "x86-64";
+        }
+
+        let info = {os, arch};
+        runSafe(context, callback, info);
+      },
     },
   };
 });
--- a/toolkit/components/extensions/test/mochitest/mochitest.ini
+++ b/toolkit/components/extensions/test/mochitest/mochitest.ini
@@ -19,16 +19,17 @@ support-files =
 [test_ext_simple.html]
 [test_ext_geturl.html]
 [test_ext_contentscript.html]
 [test_ext_webrequest.html]
 [test_ext_generate.html]
 [test_ext_notifications.html]
 [test_ext_runtime_connect.html]
 [test_ext_runtime_disconnect.html]
+[test_ext_runtime_getPlatformInfo.html]
 [test_ext_sandbox_var.html]
 [test_ext_sendmessage_reply.html]
 [test_ext_sendmessage_doublereply.html]
 [test_ext_storage.html]
 [test_ext_background_runtime_connect_params.html]
 [test_ext_cookies.html]
 [test_ext_bookmarks.html]
 [test_ext_alarms.html]
new file mode 100644
--- /dev/null
+++ b/toolkit/components/extensions/test/mochitest/test_ext_runtime_getPlatformInfo.html
@@ -0,0 +1,40 @@
+<!DOCTYPE HTML>
+<html>
+<head>
+  <title>WebExtension test</title>
+  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
+  <script type="text/javascript" src="/tests/SimpleTest/SpawnTask.js"></script>
+  <script type="text/javascript" src="/tests/SimpleTest/ExtensionTestUtils.js"></script>
+  <script type="text/javascript" src="head.js"></script>
+  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
+</head>
+<body>
+
+<script type="application/javascript;version=1.8">
+"use strict";
+
+function backgroundScript() {
+  browser.runtime.getPlatformInfo(info => {
+    var validOSs = ["mac", "win", "android", "cros", "linux", "openbsd"];
+    var validArchs = ["arm", "x86-32", "x86-64"];
+
+    browser.test.assertTrue(validOSs.indexOf(info.os) != -1, "OS is valid");
+    browser.test.assertTrue(validArchs.indexOf(info.arch) != -1, "Architecture is valid");
+    browser.test.notifyPass("runtime.getPlatformInfo");
+  });
+}
+
+let extensionData = {
+  background: "(" + backgroundScript.toString() + ")()",
+};
+
+add_task(function* test_contentscript() {
+  let extension = ExtensionTestUtils.loadExtension(extensionData);
+  yield extension.startup();
+  yield extension.awaitFinish("runtime.getPlatformInfo");
+  yield extension.unload();
+});
+</script>
+
+</body>
+</html>