Bug 648364 - Replace custom helpers with XPCOMUtils.jsm. r=rnewman

This commit is contained in:
Philipp von Weitershausen 2011-05-19 18:08:35 -07:00
Родитель 71e50e3e1f
Коммит a3ec746039
11 изменённых файлов: 43 добавлений и 191 удалений

Просмотреть файл

@ -193,7 +193,7 @@ function Store(name) {
let level = Svc.Prefs.get("log.logger.engine." + this.name, "Debug");
this._log.level = Log4Moz.Level[level];
Utils.lazy2(this, "_timer", function() {
XPCOMUtils.defineLazyGetter(this, "_timer", function() {
return Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
});
}
@ -267,7 +267,9 @@ Store.prototype = {
// Singleton service, holds registered engines
Utils.lazy(this, 'Engines', EngineManagerSvc);
XPCOMUtils.defineLazyGetter(this, "Engines", function() {
return new EngineManagerSvc();
});
function EngineManagerSvc() {
this._engines = {};

Просмотреть файл

@ -63,7 +63,9 @@ ClientsRec.prototype = {
Utils.deferGetSet(ClientsRec, "cleartext", ["name", "type", "commands"]);
Utils.lazy(this, "Clients", ClientEngine);
XPCOMUtils.defineLazyGetter(this, "Clients", function () {
return new ClientEngine();
});
function ClientEngine() {
SyncEngine.call(this, "Clients");
@ -151,12 +153,15 @@ ClientEngine.prototype = {
return localName;
// Generate a client name if we don't have a useful one yet
let user = Svc.Env.get("USER") || Svc.Env.get("USERNAME") ||
let env = Cc["@mozilla.org/process/environment;1"]
.getService(Ci.nsIEnvironment);
let user = env.get("USER") || env.get("USERNAME") ||
Svc.Prefs.get("account") || Svc.Prefs.get("username");
let brand = new StringBundle("chrome://branding/locale/brand.properties");
let app = brand.get("brandShortName");
let system = Svc.SysInfo.get("device") ||
let system = Cc["@mozilla.org/system-info;1"]
.getService(Ci.nsIPropertyBag2).get("device") ||
Cc["@mozilla.org/network/protocol;1?name=http"]
.getService(Ci.nsIHttpProtocolHandler).oscpu;

Просмотреть файл

@ -115,7 +115,7 @@ function PasswordStore(name) {
this._nsLoginInfo = new Components.Constructor(
"@mozilla.org/login-manager/loginInfo;1", Ci.nsILoginInfo, "init");
Utils.lazy2(this, "DBConnection", function() {
XPCOMUtils.defineLazyGetter(this, "DBConnection", function() {
return Services.logins.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.mozIStorageConnection);
});

Просмотреть файл

@ -52,7 +52,9 @@ __defineGetter__("Service", function() {
return this.Service;
});
Utils.lazy(this, 'ID', IDManager);
XPCOMUtils.defineLazyGetter(this, "ID", function () {
return new IDManager();
});
// For storing identities we'll use throughout Weave
function IDManager() {

Просмотреть файл

@ -123,7 +123,9 @@ WBORecord.prototype = {
Utils.deferGetSet(WBORecord, "data", ["id", "modified", "sortindex", "payload"]);
Utils.lazy(this, 'Records', RecordManager);
XPCOMUtils.defineLazyGetter(this, "Records", function () {
return new RecordManager();
});
function RecordManager() {
this._log = Log4Moz.repository.getLogger(this._logName);
@ -279,7 +281,9 @@ CryptoWrapper.prototype = {
Utils.deferGetSet(CryptoWrapper, "payload", ["ciphertext", "IV", "hmac"]);
Utils.deferGetSet(CryptoWrapper, "cleartext", "deleted");
Utils.lazy(this, "CollectionKeys", CollectionKeyManager);
XPCOMUtils.defineLazyGetter(this, "CollectionKeys", function () {
return new CollectionKeyManager();
});
/**

Просмотреть файл

@ -52,7 +52,9 @@ Cu.import("resource://services-sync/ext/Preferences.js");
Cu.import("resource://services-sync/log4moz.js");
Cu.import("resource://services-sync/util.js");
Utils.lazy(this, 'Auth', AuthMgr);
XPCOMUtils.defineLazyGetter(this, "Auth", function () {
return new AuthMgr();
});
// XXX: the authenticator api will probably need to be changed to support
// other methods (digest, oauth, etc)
@ -379,7 +381,7 @@ AsyncResource.prototype = {
// Make a lazy getter to convert the json response into an object.
// Note that this can cause a parse error to be thrown far away from the
// actual fetch, so be warned!
Utils.lazy2(ret, "obj", function() JSON.parse(ret));
XPCOMUtils.defineLazyGetter(ret, "obj", function() JSON.parse(ret));
// Notify if we get a 401 to maybe try again with a new URI.
// TODO: more retry logic.

Просмотреть файл

@ -72,8 +72,6 @@ Cu.import("resource://services-sync/status.js");
Cu.import("resource://services-sync/util.js");
Cu.import("resource://services-sync/main.js");
Utils.lazy(this, 'Service', WeaveSvc);
/*
* Service singleton
* Main entry point into Weave's sync framework
@ -2341,4 +2339,5 @@ WeaveSvc.prototype = {
};
// Load Weave on the first time this file is loaded
let Service = new WeaveSvc();
Service.onStartup();

Просмотреть файл

@ -35,7 +35,8 @@
*
* ***** END LICENSE BLOCK ***** */
const EXPORTED_SYMBOLS = ['Utils', 'Svc', 'Services', 'PlacesUtils', 'Str'];
const EXPORTED_SYMBOLS = ["XPCOMUtils", "Services", "NetUtil", "PlacesUtils",
"Utils", "Svc", "Str"];
const Cc = Components.classes;
const Ci = Components.interfaces;
@ -47,6 +48,7 @@ Cu.import("resource://services-sync/ext/Observers.js");
Cu.import("resource://services-sync/ext/Preferences.js");
Cu.import("resource://services-sync/ext/StringBundle.js");
Cu.import("resource://services-sync/log4moz.js");
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/PlacesUtils.jsm");
Cu.import("resource://gre/modules/NetUtil.jsm");
@ -364,55 +366,6 @@ let Utils = {
isArray: function Utils_isArray(val) val != null && typeof val == "object" &&
val.constructor.name == "Array",
// lazy load objects from a constructor on first access. It will
// work with the global object ('this' in the global context).
lazy: function Weave_lazy(dest, prop, ctr) {
delete dest[prop];
dest.__defineGetter__(prop, Utils.lazyCb(dest, prop, ctr));
},
lazyCb: function Weave_lazyCb(dest, prop, ctr) {
return function() {
delete dest[prop];
dest[prop] = new ctr();
return dest[prop];
};
},
// like lazy, but rather than new'ing the 3rd arg we use its return value
lazy2: function Weave_lazy2(dest, prop, fn) {
delete dest[prop];
dest.__defineGetter__(prop, Utils.lazyCb2(dest, prop, fn));
},
lazyCb2: function Weave_lazyCb2(dest, prop, fn) {
return function() {
delete dest[prop];
return dest[prop] = fn();
};
},
lazySvc: function Weave_lazySvc(dest, prop, cid, iface) {
let getter = function() {
delete dest[prop];
let svc = null;
// Use the platform's service if it exists
if (cid in Cc && iface in Ci)
svc = Cc[cid].getService(Ci[iface]);
else {
svc = FakeSvc[cid];
let log = Log4Moz.repository.getLogger("Service.Util");
if (svc == null)
log.warn("Component " + cid + " doesn't exist on this platform.");
else
log.debug("Using a fake svc object for " + cid);
}
return dest[prop] = svc;
};
dest.__defineGetter__(prop, getter);
},
lazyStrings: function Weave_lazyStrings(name) {
let bundle = "chrome://weave/locale/services/" + name + ".properties";
return function() new StringBundle(bundle);
@ -1506,7 +1459,7 @@ let FakeSvc = {
isFake: true
}
};
Utils.lazy2(Utils, "_utf8Converter", function() {
XPCOMUtils.defineLazyGetter(Utils, "_utf8Converter", function() {
let converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"]
.createInstance(Ci.nsIScriptableUnicodeConverter);
converter.charset = "UTF-8";
@ -1521,22 +1474,18 @@ Svc.Prefs = new Preferences(PREFS_BRANCH);
Svc.DefaultPrefs = new Preferences({branch: PREFS_BRANCH, defaultBranch: true});
Svc.Obs = Observers;
this.__defineGetter__("_sessionCID", function() {
let appinfo_id = Services.appinfo.ID;
return appinfo_id == SEAMONKEY_ID ? "@mozilla.org/suite/sessionstore;1"
: "@mozilla.org/browser/sessionstore;1";
});
[["Env", "@mozilla.org/process/environment;1", "nsIEnvironment"],
["Form", "@mozilla.org/satchel/form-history;1", "nsIFormHistory2"],
let _sessionCID = Services.appinfo.ID == SEAMONKEY_ID ?
"@mozilla.org/suite/sessionstore;1" :
"@mozilla.org/browser/sessionstore;1";
[["Form", "@mozilla.org/satchel/form-history;1", "nsIFormHistory2"],
["Idle", "@mozilla.org/widget/idleservice;1", "nsIIdleService"],
["KeyFactory", "@mozilla.org/security/keyobjectfactory;1", "nsIKeyObjectFactory"],
["Memory", "@mozilla.org/xpcom/memory-service;1", "nsIMemory"],
["Private", "@mozilla.org/privatebrowsing;1", "nsIPrivateBrowsingService"],
["Profiles", "@mozilla.org/toolkit/profile-service;1", "nsIToolkitProfileService"],
["SysInfo", "@mozilla.org/system-info;1", "nsIPropertyBag2"],
["Version", "@mozilla.org/xpcom/version-comparator;1", "nsIVersionComparator"],
["Session", this._sessionCID, "nsISessionStore"],
].forEach(function(lazy) Utils.lazySvc(Svc, lazy[0], lazy[1], lazy[2]));
["Session", _sessionCID, "nsISessionStore"]
].forEach(function([name, contract, iface]) {
XPCOMUtils.defineLazyServiceGetter(Svc, name, contract, iface);
});
Svc.__defineGetter__("Crypto", function() {
let cryptoSvc;
@ -1548,8 +1497,9 @@ Svc.__defineGetter__("Crypto", function() {
});
let Str = {};
["errors", "sync"]
.forEach(function(lazy) Utils.lazy2(Str, lazy, Utils.lazyStrings(lazy)));
["errors", "sync"].forEach(function(lazy) {
XPCOMUtils.defineLazyGetter(Str, lazy, Utils.lazyStrings(lazy));
});
Svc.Obs.add("xpcom-shutdown", function () {
for (let name in Svc)

Просмотреть файл

@ -1,40 +0,0 @@
_("Make sure lazy constructor calling/assignment works");
Cu.import("resource://services-sync/util.js");
function run_test() {
let count = 0;
let Foo = function() {
this.num = ++count;
}
_("Make a thing instance of Foo but make sure it isn't initialized yet");
let obj = {};
Utils.lazy(obj, "thing", Foo);
do_check_eq(count, 0);
_("Access the property to make it construct");
do_check_eq(typeof obj.thing, "object");
do_check_eq(obj.thing.constructor, Foo);
do_check_eq(count, 1);
do_check_eq(obj.thing.num, 1);
_("Additional accesses don't construct again (nothing should change");
do_check_eq(typeof obj.thing, "object");
do_check_eq(obj.thing.constructor, Foo);
do_check_eq(count, 1);
do_check_eq(obj.thing.num, 1);
_("More lazy properties will constrct more");
do_check_eq(typeof obj.other, "undefined");
Utils.lazy(obj, "other", Foo);
do_check_eq(typeof obj.other, "object");
do_check_eq(obj.other.constructor, Foo);
do_check_eq(count, 2);
do_check_eq(obj.other.num, 2);
_("Sanity check that the original property didn't change");
do_check_eq(typeof obj.thing, "object");
do_check_eq(obj.thing.constructor, Foo);
do_check_eq(count, 2);
do_check_eq(obj.thing.num, 1);
}

Просмотреть файл

@ -1,36 +0,0 @@
_("Make sure lazy2 function calling/assignment works");
Cu.import("resource://services-sync/util.js");
function run_test() {
let count = 0;
let Foo = function() {
return ++count;
}
_("Make a thing instance of Foo but make sure it isn't initialized yet");
let obj = {};
Utils.lazy2(obj, "thing", Foo);
do_check_eq(count, 0);
_("Access the property to make it evaluates");
do_check_eq(typeof obj.thing, "number");
do_check_eq(count, 1);
do_check_eq(obj.thing, 1);
_("Additional accesses don't evaluate again (nothing should change");
do_check_eq(typeof obj.thing, "number");
do_check_eq(count, 1);
do_check_eq(obj.thing, 1);
_("More lazy properties will constrct more");
do_check_eq(typeof obj.other, "undefined");
Utils.lazy2(obj, "other", Foo);
do_check_eq(typeof obj.other, "number");
do_check_eq(count, 2);
do_check_eq(obj.other, 2);
_("Sanity check that the original property didn't change");
do_check_eq(typeof obj.thing, "number");
do_check_eq(count, 2);
do_check_eq(obj.thing, 1);
}

Просмотреть файл

@ -1,36 +0,0 @@
_("Make sure lazySvc get the desired services");
Cu.import("resource://services-sync/util.js");
function run_test() {
_("Load the xul app info service as obj.app");
let obj = {}
do_check_eq(typeof obj.app, "undefined");
Utils.lazySvc(obj, "app", "@mozilla.org/xre/app-info;1", "nsIXULAppInfo");
do_check_eq(typeof obj.app.QueryInterface, "function");
do_check_eq(typeof obj.app.vendor, "string");
do_check_eq(typeof obj.app.name, "string");
_("Check other types of properties on other services");
Utils.lazySvc(obj, "io", "@mozilla.org/network/io-service;1", "nsIIOService");
do_check_eq(typeof obj.io.newURI, "function");
do_check_eq(typeof obj.io.offline, "boolean");
Utils.lazySvc(obj, "thread", "@mozilla.org/thread-manager;1", "nsIThreadManager");
do_check_true(obj.thread.currentThread instanceof Ci.nsIThread);
Utils.lazySvc(obj, "net", "@mozilla.org/network/util;1", "nsINetUtil");
do_check_eq(typeof obj.net.ESCAPE_ALL, "number");
do_check_eq(obj.net.ESCAPE_URL_SCHEME, 1);
_("Make sure fake services get loaded correctly (private browsing doesnt exist on all platforms)");
Utils.lazySvc(obj, "priv", "@mozilla.org/privatebrowsing;1", "nsIPrivateBrowsingService");
do_check_eq(typeof obj.priv.privateBrowsingEnabled, "boolean");
_("Definitely make sure services that should never exist will use fake service if available");
Utils.lazySvc(obj, "fake", "@labs.mozilla.com/Fake/Thing;1", "fake");
do_check_eq(obj.fake.isFake, true);
_("Nonexistant services that aren't fake-implemented will get nothing");
Utils.lazySvc(obj, "nonexist", "@something?@", "doesnt exist");
do_check_eq(obj.nonexist, undefined);
}