Bug 889876 - Make OS.Constants.Path.profileDir earlier. r=froydnj
authorDavid Rajchenbach-Teller <dteller@mozilla.com>
Tue, 23 Jul 2013 09:20:54 -0400
changeset 139593 da79a0fe3d866959202425307342cc7d48e9f569
parent 139592 fb96c3abe9cd3ab9b0ca27080faf6752e0ba8433
child 139594 e287a3a5088e1e5b25e86c7a87096f31bc8e991a
push id1
push userroot
push dateMon, 20 Oct 2014 17:29:22 +0000
reviewersfroydnj
bugs889876
milestone25.0a1
Bug 889876 - Make OS.Constants.Path.profileDir earlier. r=froydnj
toolkit/components/osfile/osfile_async_front.jsm
toolkit/components/osfile/tests/xpcshell/test_profiledir.js
--- a/toolkit/components/osfile/osfile_async_front.jsm
+++ b/toolkit/components/osfile/osfile_async_front.jsm
@@ -49,30 +49,52 @@ let Type = OS.Shared.Type;
 // The library of promises.
 Components.utils.import("resource://gre/modules/Promise.jsm", this);
 
 // The implementation of communications
 Components.utils.import("resource://gre/modules/osfile/_PromiseWorker.jsm", this);
 
 Components.utils.import("resource://gre/modules/Services.jsm", this);
 
-// 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");
+LOG("Checking profileDir", OS.Constants.Path);
 
-    let profileDir = Services.dirsvc.get("ProfD", Components.interfaces.nsIFile).path;
-    OS.Constants.Path.profileDir = profileDir;
+// If profileDir is not available, osfile.jsm has been imported before the
+// 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 localProfileDir = Services.dirsvc.get("ProfLD", Components.interfaces.nsIFile).path;
-    OS.Constants.Path.localProfileDir = localProfileDir;
-  };
-  Services.obs.addObserver(observer, "profile-do-change", false);
+LOG("Checking localProfileDir");
+
+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;
+    }
+  });
 }
 
 /**
  * A global constant used as a default refs parameter value when cloning.
  */
 const noRefs = [];
 
 /**
--- a/toolkit/components/osfile/tests/xpcshell/test_profiledir.js
+++ b/toolkit/components/osfile/tests/xpcshell/test_profiledir.js
@@ -8,29 +8,27 @@ Components.utils.import("resource://gre/
 Components.utils.import("resource://gre/modules/Services.jsm");
 
 function run_test() {
   run_next_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);
 
   let promise = OS.File.makeDir(OS.Path.join(OS.Constants.Path.profileDir, "foobar"));
   promise.then(
     function onSuccess() {
       do_print("Directory creation succeeded");