Bug 806591 - Add CommonUtils.generateUUID utility function. r=rnewman, a=akeybl
authorGregory Szorc <gps@mozilla.com>
Mon, 05 Nov 2012 12:49:34 -0800
changeset 117059 a05896ac7000443c147534844983bb883d47b296
parent 117058 92f99fc77dbcb6d91688ee4bef3724bdf67fe76a
child 117060 99437b69c0fce9cf877d037293288a3e73565411
push id1732
push userryanvm@gmail.com
push dateWed, 21 Nov 2012 23:58:59 +0000
treeherdermozilla-beta@3ec692d51952 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersrnewman, akeybl
bugs806591
milestone18.0
Bug 806591 - Add CommonUtils.generateUUID utility function. r=rnewman, a=akeybl
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
@@ -9,16 +9,17 @@ tail =
 [test_utils_deepCopy.js]
 [test_utils_encodeBase32.js]
 [test_utils_encodeBase64URL.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
@@ -460,16 +460,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;
 });