Bug 854299. Part 5. Make nsHelperAppDlg.promptForSaveToFileAsync actually async. r=bz
authorFelipe Gomes <felipc@gmail.com>
Mon, 01 Apr 2013 03:16:13 -0300
changeset 127236 9c72d5cfe1fbb2b92e82709338e0a4327d481f88
parent 127235 201be2cf5d880b46ed0627f33b97f62fb350cffd
child 127237 206bb159d62ffb02f39640cb7c4ec68903f27356
child 127247 7c804845016a9ef688c1bc276ac9e9a0d9cb59b6
push id1651
push userjandemooij@gmail.com
push dateMon, 01 Apr 2013 19:51:43 +0000
reviewersbz
bugs854299
milestone22.0a1
Bug 854299. Part 5. Make nsHelperAppDlg.promptForSaveToFileAsync actually async. r=bz
toolkit/mozapps/downloads/nsHelperAppDlg.js
--- a/toolkit/mozapps/downloads/nsHelperAppDlg.js
+++ b/toolkit/mozapps/downloads/nsHelperAppDlg.js
@@ -274,54 +274,49 @@ nsUnknownContentTypeDialog.prototype = {
 
     // Default to lastDir if it is valid, otherwise use the user's default
     // downloads directory.  userDownloadsDirectory should always return a
     // valid directory, so we can safely default to it.
     var dnldMgr = Components.classes["@mozilla.org/download-manager;1"]
                             .getService(Components.interfaces.nsIDownloadManager);
     picker.displayDirectory = dnldMgr.userDownloadsDirectory;
 
-    // The last directory preference may not exist, which will throw.
-    try {
-      var lastDir = gDownloadLastDir.getFile(aLauncher.source);
-      if (isUsableDirectory(lastDir))
+    gDownloadLastDir.getFileAsync(aLauncher.source, function LastDirCallback(lastDir) {
+      if (lastDir && isUsableDirectory(lastDir))
         picker.displayDirectory = lastDir;
-    }
-    catch (ex) {
-    }
 
-    if (picker.show() == nsIFilePicker.returnCancel) {
-      // null result means user cancelled.
-      aLauncher.saveDestinationAvailable(null);
-      return;
-    }
+      if (picker.show() == nsIFilePicker.returnCancel) {
+        // null result means user cancelled.
+        aLauncher.saveDestinationAvailable(null);
+        return;
+      }
+
+      // Be sure to save the directory the user chose through the Save As...
+      // dialog  as the new browser.download.dir since the old one
+      // didn't exist.
+      result = picker.file;
 
-    // Be sure to save the directory the user chose through the Save As...
-    // dialog  as the new browser.download.dir since the old one
-    // didn't exist.
-    result = picker.file;
+      if (result) {
+        try {
+          // Remove the file so that it's not there when we ensure non-existence later;
+          // this is safe because for the file to exist, the user would have had to
+          // confirm that he wanted the file overwritten.
+          if (result.exists())
+            result.remove(false);
+        }
+        catch (e) { }
+        var newDir = result.parent.QueryInterface(Components.interfaces.nsILocalFile);
 
-    if (result) {
-      try {
-        // Remove the file so that it's not there when we ensure non-existence later;
-        // this is safe because for the file to exist, the user would have had to
-        // confirm that he wanted the file overwritten.
-        if (result.exists())
-          result.remove(false);
+        // Do not store the last save directory as a pref inside the private browsing mode
+        gDownloadLastDir.setFile(aLauncher.source, newDir);
+
+        result = this.validateLeafName(newDir, result.leafName, null);
       }
-      catch (e) { }
-      var newDir = result.parent.QueryInterface(Components.interfaces.nsILocalFile);
-
-      // Do not store the last save directory as a pref inside the private browsing mode
-      gDownloadLastDir.setFile(aLauncher.source, newDir);
-
-      result = this.validateLeafName(newDir, result.leafName, null);
-    }
-    aLauncher.saveDestinationAvailable(result);
-    return;
+      aLauncher.saveDestinationAvailable(result);
+    }.bind(this));
   },
 
   /**
    * Ensures that a local folder/file combination does not already exist in
    * the file system (or finds such a combination with a reasonably similar
    * leaf name), creates the corresponding file, and returns it.
    *
    * @param   aLocalFolder