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 151878 da79a0fe3d866959202425307342cc7d48e9f569
parent 151877 fb96c3abe9cd3ab9b0ca27080faf6752e0ba8433
child 151879 e287a3a5088e1e5b25e86c7a87096f31bc8e991a
push id2859
push userakeybl@mozilla.com
push dateMon, 16 Sep 2013 19:14:59 +0000
treeherdermozilla-beta@87d3c51cd2bf [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfroydnj
bugs889876
milestone25.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
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");