Bug 889876 - Make OS.Constants.Path.profileDir earlier. r=froydnj

This commit is contained in:
David Rajchenbach-Teller 2013-07-23 09:20:54 -04:00
parent 41e0af678f
commit 2458ae39b8
2 changed files with 35 additions and 15 deletions

View File

@ -54,20 +54,42 @@ Components.utils.import("resource://gre/modules/osfile/_PromiseWorker.jsm", this
Components.utils.import("resource://gre/modules/Services.jsm", this);
LOG("Checking profileDir", OS.Constants.Path);
// If profileDir is not available, osfile.jsm has been imported before the
// profile is setup. In this case, we need to observe "profile-do-change"
// and set OS.Constants.Path.profileDir as soon as it becomes available.
if (!("profileDir" in OS.Constants.Path) || !("localProfileDir" in OS.Constants.Path)) {
let observer = function observer() {
Services.obs.removeObserver(observer, "profile-do-change");
// profile is setup. In this case, make this a lazy getter.
if (!("profileDir" in OS.Constants.Path)) {
Object.defineProperty(OS.Constants.Path, "profileDir", {
get: function() {
let path = undefined;
try {
path = Services.dirsvc.get("ProfD", Components.interfaces.nsIFile).path;
delete OS.Constants.Path.profileDir;
OS.Constants.Path.profileDir = path;
} catch (ex) {
// Ignore errors: profileDir is still not available
}
return path;
}
});
}
let profileDir = Services.dirsvc.get("ProfD", Components.interfaces.nsIFile).path;
OS.Constants.Path.profileDir = profileDir;
LOG("Checking localProfileDir");
let localProfileDir = Services.dirsvc.get("ProfLD", Components.interfaces.nsIFile).path;
OS.Constants.Path.localProfileDir = localProfileDir;
};
Services.obs.addObserver(observer, "profile-do-change", false);
if (!("localProfileDir" in OS.Constants.Path)) {
Object.defineProperty(OS.Constants.Path, "localProfileDir", {
get: function() {
let path = undefined;
try {
path = Services.dirsvc.get("ProfLD", Components.interfaces.nsIFile).path;
delete OS.Constants.Path.localProfileDir;
OS.Constants.Path.localProfileDir = path;
} catch (ex) {
// Ignore errors: localProfileDir is still not available
}
return path;
}
});
}
/**

View File

@ -13,19 +13,17 @@ function run_test() {
add_test(function test_initialize_profileDir() {
// Profile has not been set up yet, check that "profileDir" isn't either.
do_check_false("profileDir" in OS.Constants.Path);
do_check_false("localProfileDir" in OS.Constants.Path);
do_check_false(!!OS.Constants.Path.profileDir);
do_check_false(!!OS.Constants.Path.localProfileDir);
// Set up profile.
do_get_profile();
// Now that profile has been set up, check that "profileDir" is set.
do_check_true("profileDir" in OS.Constants.Path);
do_check_true(!!OS.Constants.Path.profileDir);
do_check_eq(OS.Constants.Path.profileDir,
Services.dirsvc.get("ProfD", Components.interfaces.nsIFile).path);
do_check_true("localProfileDir" in OS.Constants.Path);
do_check_true(!!OS.Constants.Path.localProfileDir);
do_check_eq(OS.Constants.Path.localProfileDir,
Services.dirsvc.get("ProfLD", Components.interfaces.nsIFile).path);