Bug 806591 - Add CommonUtils.generateUUID utility function; r=rnewman

This commit is contained in:
Gregory Szorc 2012-11-05 12:49:34 -08:00
parent d041d8d828
commit 3a8504a280
3 changed files with 29 additions and 0 deletions

View File

@ -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();
}

View File

@ -15,6 +15,7 @@ tail =
[test_utils_namedTimer.js]
[test_utils_stackTrace.js]
[test_utils_utf8.js]
[test_utils_uuid.js]
[test_aitc_server.js]
[test_async_chain.js]

View File

@ -467,6 +467,22 @@ this.CommonUtils = {
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() {