From c054a5ad9d9c4b5ccdf371ec63c57322bfdb0bde Mon Sep 17 00:00:00 2001 From: Gregory Szorc Date: Mon, 5 Nov 2012 12:49:34 -0800 Subject: [PATCH] Bug 806591 - Add CommonUtils.generateUUID utility function; r=rnewman --- services/common/tests/unit/test_utils_uuid.js | 12 ++++++++++++ services/common/tests/unit/xpcshell.ini | 1 + services/common/utils.js | 16 ++++++++++++++++ 3 files changed, 29 insertions(+) create mode 100644 services/common/tests/unit/test_utils_uuid.js diff --git a/services/common/tests/unit/test_utils_uuid.js b/services/common/tests/unit/test_utils_uuid.js new file mode 100644 index 000000000000..f1eabf50e485 --- /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(); +} diff --git a/services/common/tests/unit/xpcshell.ini b/services/common/tests/unit/xpcshell.ini index ceca3805bdca..b8f4a7013100 100644 --- a/services/common/tests/unit/xpcshell.ini +++ b/services/common/tests/unit/xpcshell.ini @@ -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] diff --git a/services/common/utils.js b/services/common/utils.js index abb64318598e..c21c593df461 100644 --- a/services/common/utils.js +++ b/services/common/utils.js @@ -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() {