From cd4feddfe0e98e9ada19f268084e6b7fe219aae7 Mon Sep 17 00:00:00 2001 From: Anant Narayanan Date: Tue, 17 Apr 2012 15:42:33 -0700 Subject: [PATCH] Bug 745425 - Move Utils.encodeBase64url to CommonUtils; r=gps --- services/common/utils.js | 7 +++++++ services/sync/modules/engines/prefs.js | 3 ++- services/sync/modules/util.js | 9 +-------- services/sync/tests/unit/test_prefs_store.js | 3 ++- services/sync/tests/unit/test_prefs_tracker.js | 3 ++- 5 files changed, 14 insertions(+), 11 deletions(-) diff --git a/services/common/utils.js b/services/common/utils.js index 91341d9e735..817fbe5bd9d 100644 --- a/services/common/utils.js +++ b/services/common/utils.js @@ -55,6 +55,13 @@ let CommonUtils = { return "No traceback available"; }, + /** + * Encode byte string as base64URL (RFC 4648). + */ + encodeBase64URL: function encodeBase64URL(bytes) { + return btoa(bytes).replace("+", "-", "g").replace("/", "_", "g"); + }, + /** * Create a nsIURI instance from a string. */ diff --git a/services/sync/modules/engines/prefs.js b/services/sync/modules/engines/prefs.js index 0b4c6e72439..952e1ef3a02 100644 --- a/services/sync/modules/engines/prefs.js +++ b/services/sync/modules/engines/prefs.js @@ -47,10 +47,11 @@ Cu.import("resource://services-sync/engines.js"); Cu.import("resource://services-sync/record.js"); Cu.import("resource://services-sync/util.js"); Cu.import("resource://services-sync/constants.js"); +Cu.import("resource://services-common/utils.js"); Cu.import("resource://services-common/preferences.js"); Cu.import("resource://gre/modules/LightweightThemeManager.jsm"); -const PREFS_GUID = Utils.encodeBase64url(Services.appinfo.ID); +const PREFS_GUID = CommonUtils.encodeBase64URL(Services.appinfo.ID); function PrefRec(collection, id) { CryptoWrapper.call(this, collection, id); diff --git a/services/sync/modules/util.js b/services/sync/modules/util.js index 7c3d248b245..d55020242db 100644 --- a/services/sync/modules/util.js +++ b/services/sync/modules/util.js @@ -200,19 +200,12 @@ let Utils = { } }, - /** - * Encode byte string as base64url (RFC 4648). - */ - encodeBase64url: function encodeBase64url(bytes) { - return btoa(bytes).replace('+', '-', 'g').replace('/', '_', 'g'); - }, - /** * GUIDs are 9 random bytes encoded with base64url (RFC 4648). * That makes them 12 characters long with 72 bits of entropy. */ makeGUID: function makeGUID() { - return Utils.encodeBase64url(Utils.generateRandomBytes(9)); + return CommonUtils.encodeBase64URL(Utils.generateRandomBytes(9)); }, _base64url_regex: /^[-abcdefghijklmnopqrstuvwxyz0123456789_]{12}$/i, diff --git a/services/sync/tests/unit/test_prefs_store.js b/services/sync/tests/unit/test_prefs_store.js index 25360992667..545536e445f 100644 --- a/services/sync/tests/unit/test_prefs_store.js +++ b/services/sync/tests/unit/test_prefs_store.js @@ -1,10 +1,11 @@ Cu.import("resource://services-sync/engines/prefs.js"); Cu.import("resource://services-sync/util.js"); +Cu.import("resource://services-common/utils.js"); Cu.import("resource://services-common/preferences.js"); Cu.import("resource://gre/modules/LightweightThemeManager.jsm"); Cu.import("resource://gre/modules/Services.jsm"); -const PREFS_GUID = Utils.encodeBase64url(Services.appinfo.ID); +const PREFS_GUID = CommonUtils.encodeBase64URL(Services.appinfo.ID); function makePersona(id) { return { diff --git a/services/sync/tests/unit/test_prefs_tracker.js b/services/sync/tests/unit/test_prefs_tracker.js index ea4ee28d99d..bfc1b20f956 100644 --- a/services/sync/tests/unit/test_prefs_tracker.js +++ b/services/sync/tests/unit/test_prefs_tracker.js @@ -1,6 +1,7 @@ Cu.import("resource://services-sync/engines/prefs.js"); Cu.import("resource://services-sync/util.js"); Cu.import("resource://services-sync/constants.js"); +Cu.import("resource://services-common/util.js"); Cu.import("resource://services-common/preferences.js"); function run_test() { @@ -22,7 +23,7 @@ function run_test() { let changedIDs = engine.getChangedIDs(); let ids = Object.keys(changedIDs); do_check_eq(ids.length, 1); - do_check_eq(ids[0], Utils.encodeBase64url(Services.appinfo.ID)); + do_check_eq(ids[0], CommonUtils.encodeBase64URL(Services.appinfo.ID)); Svc.Prefs.set("engine.prefs.modified", false); do_check_false(tracker.modified);