Bug 780379 - Add buttons to download prompts. r=mfinkle
authorWes Johnston <wjohnston@mozilla.com>
Mon, 21 Oct 2013 08:34:27 -0700
changeset 166305 04f77c1e3337dcec793622c5b47b6f67fcc76c23
parent 166304 dd9a6a9181bdee7b9af5ef07fdf736e08a952f2b
child 166306 685dc9d20b6b324d8a8ef7c55aa11bcbb8e3ddc6
push id428
push userbbajaj@mozilla.com
push dateTue, 28 Jan 2014 00:16:25 +0000
treeherdermozilla-release@cd72a7ff3a75 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmfinkle
bugs780379
milestone27.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 780379 - Add buttons to download prompts. r=mfinkle
mobile/android/components/HelperAppDialog.js
mobile/android/locales/en-US/chrome/browser.properties
--- a/mobile/android/components/HelperAppDialog.js
+++ b/mobile/android/components/HelperAppDialog.js
@@ -29,42 +29,102 @@ HelperAppLauncherDialog.prototype = {
     let defaultHandler = new Object();
     let apps = HelperApps.getAppsForUri(aLauncher.source, {
       mimeType: aLauncher.MIMEInfo.MIMEType,
     });
 
     // Add a fake intent for save to disk at the top of the list
     apps.unshift({
       name: bundle.GetStringFromName("helperapps.saveToDisk"),
+      packageName: "org.mozilla.gecko.Download",
       iconUri: "drawable://icon",
       launch: function() {
         // Reset the preferredAction here
         aLauncher.MIMEInfo.preferredAction = Ci.nsIMIMEInfo.saveToDisk;
         aLauncher.saveToDisk(null, false);
         return true;
       }
     });
 
-    let app = apps[0];
-    if (apps.length > 1) {
-      app = HelperApps.prompt(apps, {
-        title: bundle.GetStringFromName("helperapps.pick")
+    // See if the user already marked something as the default for this mimetype,
+    // and if that app is still installed.
+    let preferredApp = this._getPreferredApp(aLauncher);
+    if (preferredApp) {
+      let pref = apps.filter(function(app) {
+        return app.packageName === preferredApp;
       });
+
+      if (pref.length > 0) {
+        pref[0].launch(aLauncher.source);
+        return;
+      }
+    }
+
+    let callback = function(app) {
+      aLauncher.MIMEInfo.preferredAction = Ci.nsIMIMEInfo.useHelperApp;
+      app.launch(aLauncher.source);
+      if (!app.launch(aLauncher.source)) {
+        aLauncher.cancel(Cr.NS_BINDING_ABORTED);
+      }
     }
 
-    if (app) {
-      aLauncher.MIMEInfo.preferredAction = Ci.nsIMIMEInfo.useHelperApp;
-      if (!app.launch(aLauncher.source)) {
-        aLauncher.cancel();
-      }
+    if (apps.length > 1) {
+      HelperApps.prompt(apps, {
+        title: bundle.GetStringFromName("helperapps.pick"),
+        buttons: [
+          bundle.GetStringFromName("helperapps.alwaysUse"),
+          bundle.GetStringFromName("helperapps.useJustOnce")
+        ]
+      }, (data) => {
+        if (data.button < 0)
+          return;
+
+        callback(apps[data.icongrid0]);
+
+        if (data.button == 0)
+          this._setPreferredApp(aLauncher, apps[data.icongrid0]);
+      });
     } else {
-      // Something weird happened. Log an error
-      Services.console.logStringMessage("Unexpected selection from grid: " + app);
+      callback(apps[0]);
     }
+  },
 
+  _getPrefName: function getPrefName(mimetype) {
+    return "browser.download.preferred." + mimetype.replace("\\", ".");
+  },
+
+  _getMimeTypeFromLauncher: function getMimeTypeFromLauncher(launcher) {
+    let mime = launcher.MIMEInfo.MIMEType;
+    if (!mime)
+      mime = ContentAreaUtils.getMIMETypeForURI(launcher.source) || "";
+    return mime;
+  },
+
+  _getPreferredApp: function getPreferredApp(launcher) {
+    let mime = this._getMimeTypeFromLauncher(launcher);
+    if (!mime)
+      return;
+
+    try {
+      return Services.prefs.getCharPref(this._getPrefName(mime));
+    } catch(ex) {
+      Services.console.logStringMessage("Error getting pref for " + mime + " " + ex);
+    }
+    return null;
+  },
+
+  _setPreferredApp: function setPreferredApp(launcher, app) {
+    let mime = this._getMimeTypeFromLauncher(launcher);
+    if (!mime)
+      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;
 
     try {
--- a/mobile/android/locales/en-US/chrome/browser.properties
+++ b/mobile/android/locales/en-US/chrome/browser.properties
@@ -260,16 +260,18 @@ helperapps.open=Open
 helperapps.ignore=Ignore
 helperapps.dontAskAgain=Don't ask again for this site
 helperapps.openWithApp2=Open With %S App
 helperapps.openWithList2=Open With an App
 helperapps.always=Always
 helperapps.never=Never
 helperapps.pick=Complete action using
 helperapps.saveToDisk=Download
+helperapps.alwaysUse=Always
+helperapps.useJustOnce=Just once
 
 #Lightweight themes
 # LOCALIZATION NOTE (lwthemeInstallRequest.message): %S will be replaced with
 # the host name of the site.
 lwthemeInstallRequest.message=This site (%S) attempted to install a theme.
 lwthemeInstallRequest.allowButton=Allow
 
 # LOCALIZATION NOTE (getUserMedia.shareCamera.message, getUserMedia.shareMicrophone.message, getUserMedia.shareCameraAndMicrophone.message, getUserMedia.sharingCamera.message, getUserMedia.sharingMicrophone.message, getUserMedia.sharingCameraAndMicrophone.message): %S is the website origin (e.g. www.mozilla.org)