Bug 931776 - Use Downloads.jsm functions to get download directories in Firefox for Android. r=margaret
authorPaolo Amadini <paolo.mozmail@amadzone.org>
Wed, 24 Dec 2014 18:10:04 +0000
changeset 221206 538c91fb967e01eb7692c9d4e7933bdfe9202a5d
parent 221205 a76ba87bf502a66ad94afa4c66d920683cc99437
child 221207 917838e59201524a6c601421236a0a309d05ce2b
push id28013
push userphilringnalda@gmail.com
push dateWed, 24 Dec 2014 23:31:28 +0000
treeherdermozilla-central@38471b0310c9 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmargaret
bugs931776
milestone37.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 931776 - Use Downloads.jsm functions to get download directories in Firefox for Android. r=margaret
mobile/android/components/HelperAppDialog.js
--- a/mobile/android/components/HelperAppDialog.js
+++ b/mobile/android/components/HelperAppDialog.js
@@ -4,19 +4,21 @@
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 const { classes: Cc, interfaces: Ci, utils: Cu, results: Cr } = Components;
 
 const APK_MIME_TYPE = "application/vnd.android.package-archive";
 const PREF_BD_USEDOWNLOADDIR = "browser.download.useDownloadDir";
 const URI_GENERIC_ICON_DOWNLOAD = "drawable://alert_download";
 
+Cu.import("resource://gre/modules/Downloads.jsm");
 Cu.import("resource://gre/modules/FileUtils.jsm");
 Cu.import("resource://gre/modules/HelperApps.jsm");
 Cu.import("resource://gre/modules/Services.jsm");
+Cu.import("resource://gre/modules/Task.jsm");
 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
 
 // -----------------------------------------------------------------------
 // HelperApp Launcher Dialog
 // -----------------------------------------------------------------------
 
 function HelperAppLauncherDialog() { }
 
@@ -224,26 +226,34 @@ HelperAppLauncherDialog.prototype = {
       return;
 
     if (app)
       Services.prefs.setCharPref(this._getPrefName(mime), app.packageName);
     else
       Services.prefs.clearUserPref(this._getPrefName(mime));
   },
 
-  promptForSaveToFile: function hald_promptForSaveToFile(aLauncher, aContext, aDefaultFile, aSuggestedFileExt, aForcePrompt) {
-    // Retrieve the user's default download directory
-    let dnldMgr = Cc["@mozilla.org/download-manager;1"].getService(Ci.nsIDownloadManager);
-    let defaultFolder = dnldMgr.userDownloadsDirectory;
+  promptForSaveToFile: function () {
+    throw new Components.Exception("Async version must be used",
+                                   Cr.NS_ERROR_NOT_AVAILABLE);
+  },
 
-    try {
-      file = this.validateLeafName(defaultFolder, aDefaultFile, aSuggestedFileExt);
-    } catch (e) { }
-
-    return file;
+  promptForSaveToFileAsync: function (aLauncher, aContext, aDefaultFile,
+                                      aSuggestedFileExt, aForcePrompt) {
+    Task.spawn(function* () {
+      let file = null;
+      try {
+        let preferredDir = yield Downloads.getPreferredDownloadsDirectory();
+        file = this.validateLeafName(new FileUtils.File(preferredDir),
+                                     aDefaultFile, aSuggestedFileExt);
+      } finally {
+        // The file argument will be null in case any exception occurred.
+        aLauncher.saveDestinationAvailable(file);
+      }
+    }.bind(this)).catch(Cu.reportError);
   },
 
   validateLeafName: function hald_validateLeafName(aLocalFile, aLeafName, aFileExt) {
     if (!(aLocalFile && this.isUsableDirectory(aLocalFile)))
       return null;
 
     // Remove any leading periods, since we don't want to save hidden files
     // automatically.