Bug 1240798 - Copy chrome user samples from default profile. r=Ratty a=Ratty
authorFrank-Rainer Grahl <frgrahl@gmx.net>
Sun, 11 Sep 2016 22:27:03 +0200
changeset 27367 b6b1c7404ae91f02929cc44ea718ed06a89130f4
parent 27366 00e2fcfe8dfb772f9453d0a717789e9e99dba624
child 27368 ed49e64ed546f119c1f12640e2693bf9ccb340ee
push id1850
push userclokep@gmail.com
push dateWed, 08 Mar 2017 19:29:12 +0000
treeherdercomm-esr52@028df196b2d9 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersRatty, Ratty
bugs1240798
Bug 1240798 - Copy chrome user samples from default profile. r=Ratty a=Ratty
suite/common/src/nsSuiteGlue.js
--- a/suite/common/src/nsSuiteGlue.js
+++ b/suite/common/src/nsSuiteGlue.js
@@ -353,16 +353,17 @@ SuiteGlue.prototype = {
   // profile is available
   _onProfileAfterChange: function()
   {
     // check if we're in safe mode
     if (Services.appinfo.inSafeMode) {
       Services.ww.openWindow(null, "chrome://communicator/content/safeMode.xul", 
                              "_blank", "chrome,centerscreen,modal,resizable=no", null);
     }
+    this._copyDefaultProfileFiles();
   },
 
   // profile startup handler (contains profile initialization routines)
   _onProfileStartup: function()
   {
     this._updatePrefs();
     this._migrateDownloadPrefs();
     migrateMailnews(); // mailnewsMigrator.js
@@ -379,16 +380,75 @@ SuiteGlue.prototype = {
 
     this._setUpUserAgentOverrides();
 
     var timer = Components.classes["@mozilla.org/timer;1"]
                           .createInstance(Components.interfaces.nsITimer);
     timer.init(this, 3000, timer.TYPE_ONE_SHOT);
   },
 
+  // Copies additional profile files from the default profile tho the current profile.
+  // Only files not covered by the regular profile creation process.
+  // Currently only the userchrome examples.
+  _copyDefaultProfileFiles: function()
+  {
+    // Copy default chrome example files if they do not exist in the current profile.
+    var profileDir = Services.dirsvc.get("ProfD", Components.interfaces.nsILocalFile);
+    profileDir.append("chrome");
+
+    // The chrome directory in the current/new profile already exists so no copying.
+    if (profileDir.exists())
+      return;
+
+    let defaultProfileDir = Services.dirsvc.get("DefRt",
+                                                Components.interfaces.nsIFile);
+    defaultProfileDir.append("profile");
+    defaultProfileDir.append("chrome");
+
+    if (defaultProfileDir.exists() && defaultProfileDir.isDirectory()) {
+      try {
+        this._copyDir(defaultProfileDir, profileDir);
+      } catch (e) {
+        Components.utils.reportError(e);
+      }
+    }
+  },
+
+  // Simple copy function for copying complete aSource Directory to aDestiniation.
+  _copyDir: function(aSource, aDestination)
+  {
+    let enumerator = aSource.directoryEntries;
+
+    while (enumerator.hasMoreElements()) {
+      let file = enumerator.getNext().QueryInterface(Components.interfaces.nsIFile);
+
+      if (file.isDirectory()) {
+        let subdir = aDestination.clone();
+        subdir.append(file.leafName);
+
+        // Create the target directory. If it already exists continue copying files.
+        try {
+          subdir.create(Components.interfaces.nsIFile.DIRECTORY_TYPE,
+                        FileUtils.PERMS_DIRECTORY);
+        } catch (ex) {
+           if (ex.result != Components.results.NS_ERROR_FILE_ALREADY_EXISTS)
+            throw ex;
+        }
+        // Directory created. Now copy the files.
+        this._copyDir(file, subdir);
+      } else {
+        try {
+          file.copyTo(aDestination, null);
+        } catch (e) {
+          Components.utils.reportError(e);
+        }
+      }
+    }
+  },
+
   _setUpUserAgentOverrides: function ()
   {
     UserAgentOverrides.init();
 
     function addMoodleOverride(aHttpChannel, aOriginalUA)
     {
       var cookies;
       try {