Bug 806591 - Add CommonUtils.generateUUID utility function; r=rnewman
authorGregory Szorc <gps@mozilla.com>
Mon, 05 Nov 2012 12:49:34 -0800
changeset 113817 86b0762e70edb17d863eae09e7abddbcea3b351d
parent 113816 cc266c0b96aaad6a23caa0ec1ca59cb658d86906
child 113818 77027b565e07ec6d89c081adfc92360892b31b23
push id23890
push userryanvm@gmail.com
push dateWed, 21 Nov 2012 02:43:32 +0000
treeherdermozilla-central@4f19e7fd8bea [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersrnewman
bugs806591
milestone20.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 806591 - Add CommonUtils.generateUUID utility function; r=rnewman
services/common/tests/unit/test_utils_uuid.js
services/common/tests/unit/xpcshell.ini
services/common/utils.js
new file mode 100644
--- /dev/null
+++ b/services/common/tests/unit/test_utils_uuid.js
@@ -0,0 +1,12 @@
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/ */
+
+"use strict";
+
+function run_test() {
+  let uuid = CommonUtils.generateUUID();
+  do_check_eq(uuid.length, 36);
+  do_check_eq(uuid[8], "-");
+
+  run_next_test();
+}
--- a/services/common/tests/unit/xpcshell.ini
+++ b/services/common/tests/unit/xpcshell.ini
@@ -10,16 +10,17 @@ tail =
 [test_utils_encodeBase32.js]
 [test_utils_encodeBase64URL.js]
 [test_utils_ensureMillisecondsTimestamp.js]
 [test_utils_json.js]
 [test_utils_makeURI.js]
 [test_utils_namedTimer.js]
 [test_utils_stackTrace.js]
 [test_utils_utf8.js]
+[test_utils_uuid.js]
 
 [test_aitc_server.js]
 [test_async_chain.js]
 [test_async_querySpinningly.js]
 [test_log4moz.js]
 [test_observers.js]
 [test_preferences.js]
 [test_restrequest.js]
--- a/services/common/utils.js
+++ b/services/common/utils.js
@@ -462,16 +462,32 @@ this.CommonUtils = {
         "nsIBinaryInputStream",
         "setInputStream");
     if (!count) {
       count = stream.available();
     }
 
     return new BinaryInputStream(stream).readBytes(count);
   },
+
+  /**
+   * Generate a new UUID using nsIUUIDGenerator.
+   *
+   * Example value: "1e00a2e2-1570-443e-bf5e-000354124234"
+   *
+   * @return string A hex-formatted UUID string.
+   */
+  generateUUID: function generateUUID() {
+    let uuid = Cc["@mozilla.org/uuid-generator;1"]
+                 .getService(Ci.nsIUUIDGenerator)
+                 .generateUUID()
+                 .toString();
+
+    return uuid.substring(1, uuid.length - 1);
+  },
 };
 
 XPCOMUtils.defineLazyGetter(CommonUtils, "_utf8Converter", function() {
   let converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"]
                     .createInstance(Ci.nsIScriptableUnicodeConverter);
   converter.charset = "UTF-8";
   return converter;
 });