Bug 1391486 - Cloud Storage - Handle browser.download.dir unspecified in new profile r=Gijs draft
authorPunam <pdahiya@mozilla.com>
Thu, 17 Aug 2017 23:05:26 -0700
changeset 648665 460a4b7afec2e8f40de210817d05ecdc242a1241
parent 648343 ea24fa8ad6ac7484ffca2b1792acd110b0225ee5
child 726906 d5e777a43e52941d0392982e3fc5c9974fec9b15
push id74845
push userbmo:pdahiya@mozilla.com
push dateFri, 18 Aug 2017 06:13:36 +0000
reviewersGijs
bugs1391486
milestone57.0a1
Bug 1391486 - Cloud Storage - Handle browser.download.dir unspecified in new profile r=Gijs MozReview-Commit-ID: 3WfS5CQLtYY
browser/components/preferences/in-content-new/main.js
browser/components/preferences/in-content/main.js
toolkit/components/cloudstorage/CloudStorage.jsm
--- a/browser/components/preferences/in-content-new/main.js
+++ b/browser/components/preferences/in-content-new/main.js
@@ -2337,24 +2337,25 @@ var gMainPane = {
         downloadFolder.disabled = saveWhere.selectedIndex || useDownloadDirPref.locked;
         chooseFolder.disabled = saveWhere.selectedIndex || useDownloadDirPref.locked;
       }
 
       // Set folderListPref value depending on radio option
       // selected. folderListPref should be set to 3 if Save To Cloud Storage Provider
       // option is selected. If user switch back to 'Save To' custom path or system
       // default Downloads, check pref 'browser.download.dir' before setting respective
-      // folderListPref value
+      // folderListPref value. If currentDirPref is unspecified folderList should
+      // default to 1
       let folderListPref = document.getElementById("browser.download.folderList");
       let saveTo = document.getElementById("saveTo");
       if (saveWhere.selectedItem == saveToCloudRadio) {
         folderListPref.value = 3;
       } else if (saveWhere.selectedItem == saveTo) {
         let currentDirPref = document.getElementById("browser.download.dir");
-        folderListPref.value = await this._folderToIndex(currentDirPref.value);
+        folderListPref.value = currentDirPref.value ? await this._folderToIndex(currentDirPref.value) : 1;
       }
     }
   },
 
   /**
    * Displays a file picker in which the user can choose the location where
    * downloads are automatically saved, updating preferences and UI in
    * response to the choice, if one is made.
@@ -2422,17 +2423,18 @@ var gMainPane = {
                  .QueryInterface(Components.interfaces.nsIFileProtocolHandler);
     var iconUrlSpec;
 
     let folderIndex = folderListPref.value;
     if (folderIndex == 3) {
       // When user has selected cloud storage, use value in currentDirPref to
       // compute index to display download folder label and icon to avoid
       // displaying blank downloadFolder label and icon on load of preferences UI
-      folderIndex = await this._folderToIndex(currentDirPref.value);
+      // Set folderIndex to 1 if currentDirPref is unspecified
+      folderIndex = currentDirPref.value ? await this._folderToIndex(currentDirPref.value) : 1;
     }
 
     // Display a 'pretty' label or the path in the UI.
     if (folderIndex == 2) {
       // Custom path selected and is configured
       downloadFolder.label = this._getDisplayNameOfFile(currentDirPref.value);
       iconUrlSpec = fph.getURLSpecFromFile(currentDirPref.value);
     } else if (folderIndex == 1) {
--- a/browser/components/preferences/in-content/main.js
+++ b/browser/components/preferences/in-content/main.js
@@ -592,24 +592,25 @@ var gMainPane = {
         downloadFolder.disabled = saveWhere.selectedIndex || useDownloadDirPref.locked;
         chooseFolder.disabled = saveWhere.selectedIndex || useDownloadDirPref.locked;
       }
 
       // Set folderListPref value depending on radio option
       // selected. folderListPref should be set to 3 if Save To Cloud Storage Provider
       // option is selected. If user switch back to 'Save To' custom path or system
       // default Downloads, check pref 'browser.download.dir' before setting respective
-      // folderListPref value
+      // folderListPref value. If currentDirPref is unspecified folderList should
+      // default to 1
       let folderListPref = document.getElementById("browser.download.folderList");
       let saveTo = document.getElementById("saveTo");
       if (saveWhere.selectedItem == saveToCloudRadio) {
         folderListPref.value = 3;
       } else if (saveWhere.selectedItem == saveTo) {
         let currentDirPref = document.getElementById("browser.download.dir");
-        folderListPref.value = await this._folderToIndex(currentDirPref.value);
+        folderListPref.value = currentDirPref.value ? await this._folderToIndex(currentDirPref.value) : 1;
       }
     }
   },
 
   /**
    * Displays a file picker in which the user can choose the location where
    * downloads are automatically saved, updating preferences and UI in
    * response to the choice, if one is made.
@@ -677,17 +678,18 @@ var gMainPane = {
                  .QueryInterface(Components.interfaces.nsIFileProtocolHandler);
     var iconUrlSpec;
 
     let folderIndex = folderListPref.value;
     if (folderIndex == 3) {
       // When user has selected cloud storage, use value in currentDirPref to
       // compute index to display download folder label and icon to avoid
       // displaying blank downloadFolder label and icon on load of preferences UI
-      folderIndex = await this._folderToIndex(currentDirPref.value);
+      // Set folderIndex to 1 if currentDirPref is unspecified
+      folderIndex = currentDirPref.value ? await this._folderToIndex(currentDirPref.value) : 1;
     }
 
     // Display a 'pretty' label or the path in the UI.
     if (folderIndex == 2) {
       // Custom path selected and is configured
       downloadFolder.label = this._getDisplayNameOfFile(currentDirPref.value);
       iconUrlSpec = fph.getURLSpecFromFile(currentDirPref.value);
     } else if (folderIndex == 1) {
--- a/toolkit/components/cloudstorage/CloudStorage.jsm
+++ b/toolkit/components/cloudstorage/CloudStorage.jsm
@@ -258,25 +258,30 @@ var CloudStorageInternal = {
    * in pref 'browser.download.dir'.
    */
   async resetFolderListPref() {
     let folderListValue = Services.prefs.getIntPref("browser.download.folderList", 0);
     if (folderListValue !== 3) {
       return;
     }
 
-    let downloadDirPath = Services.prefs.getComplexValue("browser.download.dir",
-                                                         Ci.nsIFile).path;
+    let downloadDirPath = null;
+    try {
+      let file = Services.prefs.getComplexValue("browser.download.dir",
+                                                Ci.nsIFile);
+      downloadDirPath = file.path;
+    } catch (e) {}
+
     if (!downloadDirPath ||
-        (downloadDirPath === Services.dirsvc.get("Desk", Ci.nsIFile).path)) {
-      // if downloadDirPath is the Desktop path or is unspecified
+        (downloadDirPath === await Downloads.getSystemDownloadsDirectory())) {
+      // if downloadDirPath is the Downloads folder path or unspecified
+      folderListValue = 1;
+    } else if (downloadDirPath === Services.dirsvc.get("Desk", Ci.nsIFile).path) {
+      // if downloadDirPath is the Desktop path
       folderListValue = 0;
-    } else if (downloadDirPath === await Downloads.getSystemDownloadsDirectory()) {
-      // if downloadDirPath is the Downloads folder path
-      folderListValue = 1;
     } else {
       // otherwise
       folderListValue = 2;
     }
     Services.prefs.setIntPref("browser.download.folderList", folderListValue);
   },
 
   /**