2012-05-21 11:12:37 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-12-11 05:38:53 +00:00
|
|
|
|
2012-10-31 16:13:28 +00:00
|
|
|
this.EXPORTED_SYMBOLS = ["XPCOMUtils", "Services", "NetUtil", "PlacesUtils",
|
|
|
|
"FileUtils", "Utils", "Async", "Svc", "Str"];
|
2007-12-11 05:38:53 +00:00
|
|
|
|
2012-04-06 06:26:06 +00:00
|
|
|
const {classes: Cc, interfaces: Ci, results: Cr, utils: Cu} = Components;
|
2007-12-11 05:38:53 +00:00
|
|
|
|
2013-08-26 18:55:58 +00:00
|
|
|
Cu.import("resource://gre/modules/Log.jsm");
|
2012-04-16 19:19:01 +00:00
|
|
|
Cu.import("resource://services-common/observers.js");
|
2012-04-06 06:26:06 +00:00
|
|
|
Cu.import("resource://services-common/stringbundle.js");
|
|
|
|
Cu.import("resource://services-common/utils.js");
|
2012-10-31 16:13:28 +00:00
|
|
|
Cu.import("resource://services-common/async.js", this);
|
2012-04-16 19:19:01 +00:00
|
|
|
Cu.import("resource://services-crypto/utils.js");
|
2010-06-16 21:30:08 +00:00
|
|
|
Cu.import("resource://services-sync/constants.js");
|
2012-10-31 16:13:28 +00:00
|
|
|
Cu.import("resource://gre/modules/FileUtils.jsm", this);
|
2013-04-15 19:45:37 +00:00
|
|
|
Cu.import("resource://gre/modules/NetUtil.jsm", this);
|
|
|
|
Cu.import("resource://gre/modules/PlacesUtils.jsm", this);
|
|
|
|
Cu.import("resource://gre/modules/Preferences.jsm");
|
|
|
|
Cu.import("resource://gre/modules/Services.jsm", this);
|
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm", this);
|
2011-01-27 05:43:33 +00:00
|
|
|
|
2007-12-11 05:38:53 +00:00
|
|
|
/*
|
|
|
|
* Utility functions
|
|
|
|
*/
|
|
|
|
|
2012-10-31 16:13:28 +00:00
|
|
|
this.Utils = {
|
2012-04-06 06:26:06 +00:00
|
|
|
// Alias in functions from CommonUtils. These previously were defined here.
|
|
|
|
// In the ideal world, references to these would be removed.
|
|
|
|
nextTick: CommonUtils.nextTick,
|
|
|
|
namedTimer: CommonUtils.namedTimer,
|
|
|
|
exceptionStr: CommonUtils.exceptionStr,
|
|
|
|
stackTrace: CommonUtils.stackTrace,
|
|
|
|
makeURI: CommonUtils.makeURI,
|
2012-04-09 22:39:59 +00:00
|
|
|
encodeUTF8: CommonUtils.encodeUTF8,
|
|
|
|
decodeUTF8: CommonUtils.decodeUTF8,
|
|
|
|
safeAtoB: CommonUtils.safeAtoB,
|
2012-04-16 19:19:01 +00:00
|
|
|
byteArrayToString: CommonUtils.byteArrayToString,
|
|
|
|
bytesAsHex: CommonUtils.bytesAsHex,
|
2013-12-17 04:45:03 +00:00
|
|
|
hexToBytes: CommonUtils.hexToBytes,
|
2012-04-16 19:19:01 +00:00
|
|
|
encodeBase32: CommonUtils.encodeBase32,
|
|
|
|
decodeBase32: CommonUtils.decodeBase32,
|
|
|
|
|
|
|
|
// Aliases from CryptoUtils.
|
|
|
|
generateRandomBytes: CryptoUtils.generateRandomBytes,
|
|
|
|
computeHTTPMACSHA1: CryptoUtils.computeHTTPMACSHA1,
|
|
|
|
digestUTF8: CryptoUtils.digestUTF8,
|
|
|
|
digestBytes: CryptoUtils.digestBytes,
|
|
|
|
sha1: CryptoUtils.sha1,
|
|
|
|
sha1Base32: CryptoUtils.sha1Base32,
|
|
|
|
makeHMACKey: CryptoUtils.makeHMACKey,
|
|
|
|
makeHMACHasher: CryptoUtils.makeHMACHasher,
|
|
|
|
hkdfExpand: CryptoUtils.hkdfExpand,
|
|
|
|
pbkdf2Generate: CryptoUtils.pbkdf2Generate,
|
|
|
|
deriveKeyFromPassphrase: CryptoUtils.deriveKeyFromPassphrase,
|
|
|
|
getHTTPMACSHA1Header: CryptoUtils.getHTTPMACSHA1Header,
|
2012-04-06 06:26:06 +00:00
|
|
|
|
2009-06-06 05:21:14 +00:00
|
|
|
/**
|
|
|
|
* Wrap a function to catch all exceptions and log them
|
|
|
|
*
|
|
|
|
* @usage MyObj._catch = Utils.catch;
|
|
|
|
* MyObj.foo = function() { this._catch(func)(); }
|
2013-12-17 04:45:03 +00:00
|
|
|
*
|
2011-01-11 05:51:29 +00:00
|
|
|
* Optionally pass a function which will be called if an
|
|
|
|
* exception occurs.
|
2009-06-06 05:21:14 +00:00
|
|
|
*/
|
2011-01-11 05:51:29 +00:00
|
|
|
catch: function Utils_catch(func, exceptionCallback) {
|
2009-06-06 05:21:14 +00:00
|
|
|
let thisArg = this;
|
|
|
|
return function WrappedCatch() {
|
|
|
|
try {
|
|
|
|
return func.call(thisArg);
|
|
|
|
}
|
|
|
|
catch(ex) {
|
2009-07-24 22:28:42 +00:00
|
|
|
thisArg._log.debug("Exception: " + Utils.exceptionStr(ex));
|
2011-01-11 05:51:29 +00:00
|
|
|
if (exceptionCallback) {
|
|
|
|
return exceptionCallback.call(thisArg, ex);
|
|
|
|
}
|
|
|
|
return null;
|
2009-06-06 05:21:14 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2009-06-06 18:52:49 +00:00
|
|
|
/**
|
|
|
|
* Wrap a function to call lock before calling the function then unlock.
|
|
|
|
*
|
|
|
|
* @usage MyObj._lock = Utils.lock;
|
|
|
|
* MyObj.foo = function() { this._lock(func)(); }
|
|
|
|
*/
|
2010-11-30 00:41:17 +00:00
|
|
|
lock: function lock(label, func) {
|
2009-06-06 18:52:49 +00:00
|
|
|
let thisArg = this;
|
|
|
|
return function WrappedLock() {
|
2010-11-30 00:41:17 +00:00
|
|
|
if (!thisArg.lock()) {
|
|
|
|
throw "Could not acquire lock. Label: \"" + label + "\".";
|
|
|
|
}
|
2009-06-06 18:52:49 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
return func.call(thisArg);
|
|
|
|
}
|
|
|
|
finally {
|
|
|
|
thisArg.unlock();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
},
|
2013-12-17 04:45:03 +00:00
|
|
|
|
2010-12-16 20:00:57 +00:00
|
|
|
isLockException: function isLockException(ex) {
|
2011-01-11 05:51:29 +00:00
|
|
|
return ex && ex.indexOf && ex.indexOf("Could not acquire lock.") == 0;
|
2010-12-16 20:00:57 +00:00
|
|
|
},
|
2009-06-06 18:52:49 +00:00
|
|
|
|
2009-06-05 22:34:23 +00:00
|
|
|
/**
|
2011-07-30 01:47:39 +00:00
|
|
|
* Wrap functions to notify when it starts and finishes executing or if it
|
|
|
|
* threw an error.
|
2013-12-17 04:45:03 +00:00
|
|
|
*
|
2011-07-30 01:47:39 +00:00
|
|
|
* The message is a combination of a provided prefix, the local name, and
|
|
|
|
* the event. Possible events are: "start", "finish", "error". The subject
|
|
|
|
* is the function's return value on "finish" or the caught exception on
|
|
|
|
* "error". The data argument is the predefined data value.
|
2013-12-17 04:45:03 +00:00
|
|
|
*
|
2011-07-30 01:47:39 +00:00
|
|
|
* Example:
|
2013-12-17 04:45:03 +00:00
|
|
|
*
|
2011-07-30 01:47:39 +00:00
|
|
|
* @usage function MyObj(name) {
|
|
|
|
* this.name = name;
|
|
|
|
* this._notify = Utils.notify("obj:");
|
|
|
|
* }
|
|
|
|
* MyObj.prototype = {
|
|
|
|
* foo: function() this._notify("func", "data-arg", function () {
|
|
|
|
* //...
|
|
|
|
* }(),
|
|
|
|
* };
|
2009-06-05 22:34:23 +00:00
|
|
|
*/
|
|
|
|
notify: function Utils_notify(prefix) {
|
2011-07-30 01:47:39 +00:00
|
|
|
return function NotifyMaker(name, data, func) {
|
2009-06-05 22:34:23 +00:00
|
|
|
let thisArg = this;
|
2011-07-30 01:47:39 +00:00
|
|
|
let notify = function(state, subject) {
|
2009-06-05 22:34:23 +00:00
|
|
|
let mesg = prefix + name + ":" + state;
|
2009-12-18 02:51:55 +00:00
|
|
|
thisArg._log.trace("Event: " + mesg);
|
2011-07-30 01:47:39 +00:00
|
|
|
Observers.notify(mesg, subject, data);
|
2009-06-05 22:34:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return function WrappedNotify() {
|
|
|
|
try {
|
2011-07-30 01:47:39 +00:00
|
|
|
notify("start", null);
|
2009-06-05 22:34:23 +00:00
|
|
|
let ret = func.call(thisArg);
|
2011-07-30 01:47:39 +00:00
|
|
|
notify("finish", ret);
|
2009-06-05 22:34:23 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
catch(ex) {
|
2011-07-30 01:47:39 +00:00
|
|
|
notify("error", ex);
|
2009-06-05 22:34:23 +00:00
|
|
|
throw ex;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
},
|
2009-08-31 23:28:00 +00:00
|
|
|
|
2011-02-03 18:23:38 +00:00
|
|
|
runInTransaction: function(db, callback, thisObj) {
|
|
|
|
let hasTransaction = false;
|
|
|
|
try {
|
|
|
|
db.beginTransaction();
|
|
|
|
hasTransaction = true;
|
|
|
|
} catch(e) { /* om nom nom exceptions */ }
|
|
|
|
|
|
|
|
try {
|
|
|
|
return callback.call(thisObj);
|
|
|
|
} finally {
|
|
|
|
if (hasTransaction) {
|
|
|
|
db.commitTransaction();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2010-11-30 18:26:13 +00:00
|
|
|
/**
|
|
|
|
* GUIDs are 9 random bytes encoded with base64url (RFC 4648).
|
|
|
|
* That makes them 12 characters long with 72 bits of entropy.
|
|
|
|
*/
|
|
|
|
makeGUID: function makeGUID() {
|
2012-04-17 22:42:33 +00:00
|
|
|
return CommonUtils.encodeBase64URL(Utils.generateRandomBytes(9));
|
2008-06-20 19:34:29 +00:00
|
|
|
},
|
|
|
|
|
2011-01-28 17:39:28 +00:00
|
|
|
_base64url_regex: /^[-abcdefghijklmnopqrstuvwxyz0123456789_]{12}$/i,
|
|
|
|
checkGUID: function checkGUID(guid) {
|
|
|
|
return !!guid && this._base64url_regex.test(guid);
|
|
|
|
},
|
|
|
|
|
2009-04-13 21:39:29 +00:00
|
|
|
/**
|
|
|
|
* Add a simple getter/setter to an object that defers access of a property
|
|
|
|
* to an inner property.
|
|
|
|
*
|
|
|
|
* @param obj
|
|
|
|
* Object to add properties to defer in its prototype
|
|
|
|
* @param defer
|
2011-02-25 22:43:10 +00:00
|
|
|
* Property of obj to defer to
|
2009-04-13 21:39:29 +00:00
|
|
|
* @param prop
|
|
|
|
* Property name to defer (or an array of property names)
|
|
|
|
*/
|
|
|
|
deferGetSet: function Utils_deferGetSet(obj, defer, prop) {
|
2011-08-26 17:26:11 +00:00
|
|
|
if (Array.isArray(prop))
|
2009-04-13 21:39:29 +00:00
|
|
|
return prop.map(function(prop) Utils.deferGetSet(obj, defer, prop));
|
|
|
|
|
|
|
|
let prot = obj.prototype;
|
|
|
|
|
|
|
|
// Create a getter if it doesn't exist yet
|
2010-11-30 00:41:17 +00:00
|
|
|
if (!prot.__lookupGetter__(prop)) {
|
2011-02-25 22:43:10 +00:00
|
|
|
prot.__defineGetter__(prop, function () {
|
|
|
|
return this[defer][prop];
|
|
|
|
});
|
2010-11-30 00:41:17 +00:00
|
|
|
}
|
2009-04-13 21:39:29 +00:00
|
|
|
|
|
|
|
// Create a setter if it doesn't exist yet
|
2011-02-25 22:43:10 +00:00
|
|
|
if (!prot.__lookupSetter__(prop)) {
|
|
|
|
prot.__defineSetter__(prop, function (val) {
|
|
|
|
this[defer][prop] = val;
|
|
|
|
});
|
|
|
|
}
|
2009-04-13 21:39:29 +00:00
|
|
|
},
|
|
|
|
|
2009-08-19 03:03:11 +00:00
|
|
|
lazyStrings: function Weave_lazyStrings(name) {
|
2010-06-10 00:03:31 +00:00
|
|
|
let bundle = "chrome://weave/locale/services/" + name + ".properties";
|
2009-08-19 03:03:11 +00:00
|
|
|
return function() new StringBundle(bundle);
|
|
|
|
},
|
|
|
|
|
2009-05-19 15:53:30 +00:00
|
|
|
deepEquals: function eq(a, b) {
|
|
|
|
// If they're triple equals, then it must be equals!
|
|
|
|
if (a === b)
|
2008-02-13 22:30:44 +00:00
|
|
|
return true;
|
2008-03-05 08:00:56 +00:00
|
|
|
|
2009-07-23 06:49:15 +00:00
|
|
|
// If they weren't equal, they must be objects to be different
|
|
|
|
if (typeof a != "object" || typeof b != "object")
|
2008-02-13 22:30:44 +00:00
|
|
|
return false;
|
|
|
|
|
2009-07-23 06:49:15 +00:00
|
|
|
// But null objects won't have properties to compare
|
|
|
|
if (a === null || b === null)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Make sure all of a's keys have a matching value in b
|
|
|
|
for (let k in a)
|
|
|
|
if (!eq(a[k], b[k]))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Do the same for b's keys but skip those that we already checked
|
|
|
|
for (let k in b)
|
|
|
|
if (!(k in a) && !eq(a[k], b[k]))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
2008-02-13 22:30:44 +00:00
|
|
|
},
|
2007-12-11 05:38:53 +00:00
|
|
|
|
2010-12-09 18:32:03 +00:00
|
|
|
// Generator and discriminator for HMAC exceptions.
|
2012-04-06 06:26:06 +00:00
|
|
|
// Split these out in case we want to make them richer in future, and to
|
2010-12-09 18:32:03 +00:00
|
|
|
// avoid inevitable confusion if the message changes.
|
|
|
|
throwHMACMismatch: function throwHMACMismatch(shouldBe, is) {
|
|
|
|
throw "Record SHA256 HMAC mismatch: should be " + shouldBe + ", is " + is;
|
|
|
|
},
|
2012-04-06 06:26:06 +00:00
|
|
|
|
2010-12-09 18:32:03 +00:00
|
|
|
isHMACMismatch: function isHMACMismatch(ex) {
|
|
|
|
const hmacFail = "Record SHA256 HMAC mismatch: ";
|
|
|
|
return ex && ex.indexOf && (ex.indexOf(hmacFail) == 0);
|
|
|
|
},
|
2012-04-06 06:26:06 +00:00
|
|
|
|
2010-11-30 00:41:17 +00:00
|
|
|
/**
|
|
|
|
* Turn RFC 4648 base32 into our own user-friendly version.
|
|
|
|
* ABCDEFGHIJKLMNOPQRSTUVWXYZ234567
|
|
|
|
* becomes
|
|
|
|
* abcdefghijk8mn9pqrstuvwxyz234567
|
|
|
|
*/
|
|
|
|
base32ToFriendly: function base32ToFriendly(input) {
|
|
|
|
return input.toLowerCase()
|
|
|
|
.replace("l", '8', "g")
|
|
|
|
.replace("o", '9', "g");
|
|
|
|
},
|
|
|
|
|
|
|
|
base32FromFriendly: function base32FromFriendly(input) {
|
|
|
|
return input.toUpperCase()
|
|
|
|
.replace("8", 'L', "g")
|
|
|
|
.replace("9", 'O', "g");
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Key manipulation.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Return an octet string in friendly base32 *with no trailing =*.
|
|
|
|
encodeKeyBase32: function encodeKeyBase32(keyData) {
|
|
|
|
return Utils.base32ToFriendly(
|
|
|
|
Utils.encodeBase32(keyData))
|
|
|
|
.slice(0, SYNC_KEY_ENCODED_LENGTH);
|
|
|
|
},
|
|
|
|
|
|
|
|
decodeKeyBase32: function decodeKeyBase32(encoded) {
|
|
|
|
return Utils.decodeBase32(
|
|
|
|
Utils.base32FromFriendly(
|
|
|
|
Utils.normalizePassphrase(encoded)))
|
|
|
|
.slice(0, SYNC_KEY_DECODED_LENGTH);
|
|
|
|
},
|
|
|
|
|
|
|
|
base64Key: function base64Key(keyData) {
|
|
|
|
return btoa(keyData);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* N.B., salt should be base64 encoded, even though we have to decode
|
|
|
|
* it later!
|
|
|
|
*/
|
|
|
|
derivePresentableKeyFromPassphrase : function derivePresentableKeyFromPassphrase(passphrase, salt, keyLength, forceJS) {
|
2012-04-16 19:19:01 +00:00
|
|
|
let k = CryptoUtils.deriveKeyFromPassphrase(passphrase, salt, keyLength,
|
|
|
|
forceJS);
|
2010-11-30 00:41:17 +00:00
|
|
|
return Utils.encodeKeyBase32(k);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* N.B., salt should be base64 encoded, even though we have to decode
|
|
|
|
* it later!
|
|
|
|
*/
|
|
|
|
deriveEncodedKeyFromPassphrase : function deriveEncodedKeyFromPassphrase(passphrase, salt, keyLength, forceJS) {
|
2012-04-16 19:19:01 +00:00
|
|
|
let k = CryptoUtils.deriveKeyFromPassphrase(passphrase, salt, keyLength,
|
|
|
|
forceJS);
|
2010-11-30 00:41:17 +00:00
|
|
|
return Utils.base64Key(k);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Take a base64-encoded 128-bit AES key, returning it as five groups of five
|
|
|
|
* uppercase alphanumeric characters, separated by hyphens.
|
|
|
|
* A.K.A. base64-to-base32 encoding.
|
|
|
|
*/
|
|
|
|
presentEncodedKeyAsSyncKey : function presentEncodedKeyAsSyncKey(encodedKey) {
|
|
|
|
return Utils.encodeKeyBase32(atob(encodedKey));
|
|
|
|
},
|
|
|
|
|
2013-02-03 08:51:26 +00:00
|
|
|
/**
|
|
|
|
* Load a JSON file from disk in the profile directory.
|
|
|
|
*
|
|
|
|
* @param filePath
|
|
|
|
* JSON file path load from profile. Loaded file will be
|
|
|
|
* <profile>/<filePath>.json. i.e. Do not specify the ".json"
|
|
|
|
* extension.
|
|
|
|
* @param that
|
|
|
|
* Object to use for logging and "this" for callback.
|
|
|
|
* @param callback
|
|
|
|
* Function to process json object as its first argument. If the file
|
|
|
|
* could not be loaded, the first argument will be undefined.
|
|
|
|
*/
|
|
|
|
jsonLoad: function jsonLoad(filePath, that, callback) {
|
|
|
|
let path = "weave/" + filePath + ".json";
|
|
|
|
|
|
|
|
if (that._log) {
|
|
|
|
that._log.trace("Loading json from disk: " + filePath);
|
|
|
|
}
|
|
|
|
|
|
|
|
let file = FileUtils.getFile("ProfD", path.split("/"), true);
|
|
|
|
if (!file.exists()) {
|
|
|
|
callback.call(that);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let channel = NetUtil.newChannel(file);
|
|
|
|
channel.contentType = "application/json";
|
|
|
|
|
|
|
|
NetUtil.asyncFetch(channel, function (is, result) {
|
|
|
|
if (!Components.isSuccessCode(result)) {
|
|
|
|
callback.call(that);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let string = NetUtil.readInputStreamToString(is, is.available());
|
|
|
|
is.close();
|
|
|
|
let json;
|
|
|
|
try {
|
|
|
|
json = JSON.parse(string);
|
|
|
|
} catch (ex) {
|
|
|
|
if (that._log) {
|
|
|
|
that._log.debug("Failed to load json: " +
|
|
|
|
CommonUtils.exceptionStr(ex));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
callback.call(that, json);
|
|
|
|
});
|
2009-04-01 00:52:16 +00:00
|
|
|
},
|
|
|
|
|
2013-02-03 08:51:26 +00:00
|
|
|
/**
|
|
|
|
* Save a json-able object to disk in the profile directory.
|
|
|
|
*
|
|
|
|
* @param filePath
|
|
|
|
* JSON file path save to <filePath>.json
|
|
|
|
* @param that
|
|
|
|
* Object to use for logging and "this" for callback
|
|
|
|
* @param obj
|
|
|
|
* Function to provide json-able object to save. If this isn't a
|
|
|
|
* function, it'll be used as the object to make a json string.
|
|
|
|
* @param callback
|
|
|
|
* Function called when the write has been performed. Optional.
|
|
|
|
* The first argument will be a Components.results error
|
|
|
|
* constant on error or null if no error was encountered (and
|
|
|
|
* the file saved successfully).
|
|
|
|
*/
|
|
|
|
jsonSave: function jsonSave(filePath, that, obj, callback) {
|
|
|
|
let path = "weave/" + filePath + ".json";
|
|
|
|
if (that._log) {
|
|
|
|
that._log.trace("Saving json to disk: " + path);
|
|
|
|
}
|
|
|
|
|
|
|
|
let file = FileUtils.getFile("ProfD", path.split("/"), true);
|
|
|
|
let json = typeof obj == "function" ? obj.call(that) : obj;
|
|
|
|
let out = JSON.stringify(json);
|
|
|
|
|
|
|
|
let fos = FileUtils.openSafeFileOutputStream(file);
|
|
|
|
let is = this._utf8Converter.convertToInputStream(out);
|
|
|
|
NetUtil.asyncCopy(is, fos, function (result) {
|
|
|
|
if (typeof callback == "function") {
|
|
|
|
let error = (result == Cr.NS_OK) ? null : result;
|
|
|
|
callback.call(that, error);
|
|
|
|
}
|
|
|
|
});
|
2009-04-01 00:52:16 +00:00
|
|
|
},
|
|
|
|
|
2009-11-26 00:59:02 +00:00
|
|
|
getIcon: function(iconUri, defaultIcon) {
|
|
|
|
try {
|
|
|
|
let iconURI = Utils.makeURI(iconUri);
|
2011-05-20 01:08:22 +00:00
|
|
|
return PlacesUtils.favicons.getFaviconLinkForIcon(iconURI).spec;
|
2009-11-26 00:59:02 +00:00
|
|
|
}
|
|
|
|
catch(ex) {}
|
|
|
|
|
|
|
|
// Just give the provided default icon or the system's default
|
2011-05-20 01:08:22 +00:00
|
|
|
return defaultIcon || PlacesUtils.favicons.defaultFavicon.spec;
|
2009-11-26 00:59:02 +00:00
|
|
|
},
|
|
|
|
|
2009-08-25 04:47:35 +00:00
|
|
|
getErrorString: function Utils_getErrorString(error, args) {
|
2009-09-16 01:38:52 +00:00
|
|
|
try {
|
2009-09-16 23:52:43 +00:00
|
|
|
return Str.errors.get(error, args || null);
|
2009-09-16 01:38:52 +00:00
|
|
|
} catch (e) {}
|
2009-11-20 22:34:20 +00:00
|
|
|
|
2009-09-16 01:38:52 +00:00
|
|
|
// basically returns "Unknown Error"
|
2009-09-16 23:52:43 +00:00
|
|
|
return Str.errors.get("error.reason.unknown");
|
2009-08-25 04:47:35 +00:00
|
|
|
},
|
|
|
|
|
2010-09-17 15:48:06 +00:00
|
|
|
/**
|
2010-11-30 00:41:17 +00:00
|
|
|
* Generate 26 characters.
|
2010-09-17 15:48:06 +00:00
|
|
|
*/
|
2010-11-30 00:41:17 +00:00
|
|
|
generatePassphrase: function generatePassphrase() {
|
|
|
|
// Note that this is a different base32 alphabet to the one we use for
|
|
|
|
// other tasks. It's lowercase, uses different letters, and needs to be
|
|
|
|
// decoded with decodeKeyBase32, not just decodeBase32.
|
2012-04-16 19:19:01 +00:00
|
|
|
return Utils.encodeKeyBase32(CryptoUtils.generateRandomBytes(16));
|
2010-09-17 15:48:06 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2010-11-30 00:41:17 +00:00
|
|
|
* The following are the methods supported for UI use:
|
|
|
|
*
|
|
|
|
* * isPassphrase:
|
|
|
|
* determines whether a string is either a normalized or presentable
|
|
|
|
* passphrase.
|
|
|
|
* * hyphenatePassphrase:
|
|
|
|
* present a normalized passphrase for display. This might actually
|
|
|
|
* perform work beyond just hyphenation; sorry.
|
|
|
|
* * hyphenatePartialPassphrase:
|
|
|
|
* present a fragment of a normalized passphrase for display.
|
|
|
|
* * normalizePassphrase:
|
|
|
|
* take a presentable passphrase and reduce it to a normalized
|
|
|
|
* representation for storage. normalizePassphrase can safely be called
|
|
|
|
* on normalized input.
|
2010-12-21 23:32:58 +00:00
|
|
|
* * normalizeAccount:
|
|
|
|
* take user input for account/username, cleaning up appropriately.
|
2010-09-17 15:48:06 +00:00
|
|
|
*/
|
2010-11-30 00:41:17 +00:00
|
|
|
|
|
|
|
isPassphrase: function(s) {
|
|
|
|
if (s) {
|
|
|
|
return /^[abcdefghijkmnpqrstuvwxyz23456789]{26}$/.test(Utils.normalizePassphrase(s));
|
|
|
|
}
|
|
|
|
return false;
|
2010-09-17 15:48:06 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2010-11-30 00:41:17 +00:00
|
|
|
* Hyphenate a passphrase (26 characters) into groups.
|
|
|
|
* abbbbccccddddeeeeffffggggh
|
|
|
|
* =>
|
|
|
|
* a-bbbbc-cccdd-ddeee-effff-ggggh
|
2010-09-17 15:48:06 +00:00
|
|
|
*/
|
2010-11-30 00:41:17 +00:00
|
|
|
hyphenatePassphrase: function hyphenatePassphrase(passphrase) {
|
|
|
|
// For now, these are the same.
|
|
|
|
return Utils.hyphenatePartialPassphrase(passphrase, true);
|
|
|
|
},
|
|
|
|
|
|
|
|
hyphenatePartialPassphrase: function hyphenatePartialPassphrase(passphrase, omitTrailingDash) {
|
|
|
|
if (!passphrase)
|
|
|
|
return null;
|
|
|
|
|
|
|
|
// Get the raw data input. Just base32.
|
|
|
|
let data = passphrase.toLowerCase().replace(/[^abcdefghijkmnpqrstuvwxyz23456789]/g, "");
|
|
|
|
|
|
|
|
// This is the neatest way to do this.
|
|
|
|
if ((data.length == 1) && !omitTrailingDash)
|
|
|
|
return data + "-";
|
|
|
|
|
|
|
|
// Hyphenate it.
|
|
|
|
let y = data.substr(0,1);
|
|
|
|
let z = data.substr(1).replace(/(.{1,5})/g, "-$1");
|
|
|
|
|
|
|
|
// Correct length? We're done.
|
|
|
|
if ((z.length == 30) || omitTrailingDash)
|
|
|
|
return y + z;
|
|
|
|
|
|
|
|
// Add a trailing dash if appropriate.
|
|
|
|
return (y + z.replace(/([^-]{5})$/, "$1-")).substr(0, SYNC_KEY_HYPHENATED_LENGTH);
|
|
|
|
},
|
|
|
|
|
|
|
|
normalizePassphrase: function normalizePassphrase(pp) {
|
|
|
|
// Short var name... have you seen the lines below?!
|
2010-12-20 22:46:16 +00:00
|
|
|
// Allow leading and trailing whitespace.
|
2010-12-28 18:33:23 +00:00
|
|
|
pp = pp.trim().toLowerCase();
|
2010-12-20 18:10:37 +00:00
|
|
|
|
|
|
|
// 20-char sync key.
|
|
|
|
if (pp.length == 23 &&
|
|
|
|
[5, 11, 17].every(function(i) pp[i] == '-')) {
|
|
|
|
|
|
|
|
return pp.slice(0, 5) + pp.slice(6, 11)
|
|
|
|
+ pp.slice(12, 17) + pp.slice(18, 23);
|
|
|
|
}
|
|
|
|
|
|
|
|
// "Modern" 26-char key.
|
|
|
|
if (pp.length == 31 &&
|
|
|
|
[1, 7, 13, 19, 25].every(function(i) pp[i] == '-')) {
|
|
|
|
|
2010-11-30 00:41:17 +00:00
|
|
|
return pp.slice(0, 1) + pp.slice(2, 7)
|
|
|
|
+ pp.slice(8, 13) + pp.slice(14, 19)
|
|
|
|
+ pp.slice(20, 25) + pp.slice(26, 31);
|
2010-12-20 18:10:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Something else -- just return.
|
2010-09-17 15:48:06 +00:00
|
|
|
return pp;
|
|
|
|
},
|
2013-12-17 04:45:03 +00:00
|
|
|
|
2010-12-21 23:32:58 +00:00
|
|
|
normalizeAccount: function normalizeAccount(acc) {
|
2010-12-28 18:33:23 +00:00
|
|
|
return acc.trim();
|
2010-12-21 23:32:58 +00:00
|
|
|
},
|
2010-09-17 15:48:06 +00:00
|
|
|
|
2009-09-11 06:11:33 +00:00
|
|
|
/**
|
2011-05-30 22:57:33 +00:00
|
|
|
* Create an array like the first but without elements of the second. Reuse
|
|
|
|
* arrays if possible.
|
2009-09-11 06:11:33 +00:00
|
|
|
*/
|
|
|
|
arraySub: function arraySub(minuend, subtrahend) {
|
2011-05-30 22:57:33 +00:00
|
|
|
if (!minuend.length || !subtrahend.length)
|
|
|
|
return minuend;
|
2009-09-11 06:11:33 +00:00
|
|
|
return minuend.filter(function(i) subtrahend.indexOf(i) == -1);
|
|
|
|
},
|
|
|
|
|
2011-01-28 17:39:12 +00:00
|
|
|
/**
|
2011-05-30 22:57:33 +00:00
|
|
|
* Build the union of two arrays. Reuse arrays if possible.
|
2011-01-28 17:39:12 +00:00
|
|
|
*/
|
|
|
|
arrayUnion: function arrayUnion(foo, bar) {
|
2011-05-30 22:57:33 +00:00
|
|
|
if (!foo.length)
|
|
|
|
return bar;
|
|
|
|
if (!bar.length)
|
|
|
|
return foo;
|
2011-01-28 17:39:12 +00:00
|
|
|
return foo.concat(Utils.arraySub(bar, foo));
|
|
|
|
},
|
|
|
|
|
2008-03-07 09:56:36 +00:00
|
|
|
bind2: function Async_bind2(object, method) {
|
2008-05-23 18:05:42 +00:00
|
|
|
return function innerBind() { return method.apply(object, arguments); };
|
2008-03-07 09:56:36 +00:00
|
|
|
},
|
|
|
|
|
2010-04-08 21:03:10 +00:00
|
|
|
mpLocked: function mpLocked() {
|
2011-02-23 00:09:43 +00:00
|
|
|
let modules = Cc["@mozilla.org/security/pkcs11moduledb;1"]
|
|
|
|
.getService(Ci.nsIPKCS11ModuleDB);
|
2010-04-08 21:03:10 +00:00
|
|
|
let sdrSlot = modules.findSlotByName("");
|
|
|
|
let status = sdrSlot.status;
|
|
|
|
let slots = Ci.nsIPKCS11Slot;
|
|
|
|
|
2010-08-31 15:58:35 +00:00
|
|
|
if (status == slots.SLOT_READY || status == slots.SLOT_LOGGED_IN
|
|
|
|
|| status == slots.SLOT_UNINITIALIZED)
|
2010-04-08 21:03:10 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if (status == slots.SLOT_NOT_LOGGED_IN)
|
|
|
|
return true;
|
2013-12-17 04:45:03 +00:00
|
|
|
|
2010-04-08 21:03:10 +00:00
|
|
|
// something wacky happened, pretend MP is locked
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
2011-01-14 00:56:10 +00:00
|
|
|
// If Master Password is enabled and locked, present a dialog to unlock it.
|
|
|
|
// Return whether the system is unlocked.
|
|
|
|
ensureMPUnlocked: function ensureMPUnlocked() {
|
2011-10-26 20:46:40 +00:00
|
|
|
if (!Utils.mpLocked()) {
|
|
|
|
return true;
|
|
|
|
}
|
2011-02-23 00:09:43 +00:00
|
|
|
let sdr = Cc["@mozilla.org/security/sdr;1"]
|
|
|
|
.getService(Ci.nsISecretDecoderRing);
|
2011-01-14 00:56:10 +00:00
|
|
|
try {
|
|
|
|
sdr.encryptString("bacon");
|
2011-02-23 00:09:43 +00:00
|
|
|
return true;
|
2011-01-14 00:56:10 +00:00
|
|
|
} catch(e) {}
|
2011-02-23 00:09:43 +00:00
|
|
|
return false;
|
2011-01-14 00:56:10 +00:00
|
|
|
},
|
2013-12-17 04:45:03 +00:00
|
|
|
|
2011-06-27 13:22:49 +00:00
|
|
|
/**
|
|
|
|
* Return a value for a backoff interval. Maximum is eight hours, unless
|
|
|
|
* Status.backoffInterval is higher.
|
|
|
|
*
|
|
|
|
*/
|
2012-03-22 22:49:50 +00:00
|
|
|
calculateBackoff: function calculateBackoff(attempts, baseInterval,
|
|
|
|
statusInterval) {
|
2011-06-27 13:22:49 +00:00
|
|
|
let backoffInterval = attempts *
|
2012-03-22 22:49:50 +00:00
|
|
|
(Math.floor(Math.random() * baseInterval) +
|
|
|
|
baseInterval);
|
|
|
|
return Math.max(Math.min(backoffInterval, MAXIMUM_BACKOFF_INTERVAL),
|
|
|
|
statusInterval);
|
|
|
|
},
|
2009-01-06 21:54:18 +00:00
|
|
|
};
|
|
|
|
|
2011-05-20 01:08:35 +00:00
|
|
|
XPCOMUtils.defineLazyGetter(Utils, "_utf8Converter", function() {
|
2011-03-01 22:29:41 +00:00
|
|
|
let converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"]
|
|
|
|
.createInstance(Ci.nsIScriptableUnicodeConverter);
|
|
|
|
converter.charset = "UTF-8";
|
|
|
|
return converter;
|
|
|
|
});
|
2010-03-02 19:07:30 +00:00
|
|
|
|
2009-01-06 21:54:18 +00:00
|
|
|
/*
|
|
|
|
* Commonly-used services
|
|
|
|
*/
|
2012-10-31 16:13:28 +00:00
|
|
|
this.Svc = {};
|
2009-02-10 08:52:05 +00:00
|
|
|
Svc.Prefs = new Preferences(PREFS_BRANCH);
|
2010-06-16 22:11:40 +00:00
|
|
|
Svc.DefaultPrefs = new Preferences({branch: PREFS_BRANCH, defaultBranch: true});
|
2009-12-15 22:21:12 +00:00
|
|
|
Svc.Obs = Observers;
|
2010-08-17 13:44:29 +00:00
|
|
|
|
2011-05-20 01:08:35 +00:00
|
|
|
let _sessionCID = Services.appinfo.ID == SEAMONKEY_ID ?
|
|
|
|
"@mozilla.org/suite/sessionstore;1" :
|
|
|
|
"@mozilla.org/browser/sessionstore;1";
|
|
|
|
|
2013-06-20 16:56:49 +00:00
|
|
|
[
|
2009-07-17 23:55:15 +00:00
|
|
|
["Idle", "@mozilla.org/widget/idleservice;1", "nsIIdleService"],
|
2011-05-20 01:08:35 +00:00
|
|
|
["Session", _sessionCID, "nsISessionStore"]
|
|
|
|
].forEach(function([name, contract, iface]) {
|
|
|
|
XPCOMUtils.defineLazyServiceGetter(Svc, name, contract, iface);
|
|
|
|
});
|
2009-08-19 03:03:11 +00:00
|
|
|
|
2013-06-20 16:56:49 +00:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(Svc, "FormHistory", "resource://gre/modules/FormHistory.jsm");
|
|
|
|
|
2010-10-14 19:17:35 +00:00
|
|
|
Svc.__defineGetter__("Crypto", function() {
|
|
|
|
let cryptoSvc;
|
2011-04-07 22:31:11 +00:00
|
|
|
let ns = {};
|
|
|
|
Cu.import("resource://services-crypto/WeaveCrypto.js", ns);
|
|
|
|
cryptoSvc = new ns.WeaveCrypto();
|
2010-10-14 19:17:35 +00:00
|
|
|
delete Svc.Crypto;
|
|
|
|
return Svc.Crypto = cryptoSvc;
|
|
|
|
});
|
|
|
|
|
2012-10-31 16:13:28 +00:00
|
|
|
this.Str = {};
|
2011-05-20 01:08:35 +00:00
|
|
|
["errors", "sync"].forEach(function(lazy) {
|
|
|
|
XPCOMUtils.defineLazyGetter(Str, lazy, Utils.lazyStrings(lazy));
|
|
|
|
});
|
2010-08-10 23:54:46 +00:00
|
|
|
|
|
|
|
Svc.Obs.add("xpcom-shutdown", function () {
|
|
|
|
for (let name in Svc)
|
|
|
|
delete Svc[name];
|
|
|
|
});
|