Bug 1219078 - Delegate OMA downloads to external app (if available). r=mfinkle

This patch will prevent Firefox from downloading OMA download descriptors on
its own. Instead it will prompt to complete the action with an external app
if available. An error will be shown if no external app can handle the download.

--HG--
extra : commitid : KkCCwQrWwUv
extra : rebase_source : 2650328814b9ec5bba64d802b863d0ff7b3cacfc
This commit is contained in:
Sebastian Kaspari 2015-12-17 14:49:37 +01:00
parent 0c6226c0e1
commit 9537e298e8

View File

@ -8,6 +8,7 @@
const { classes: Cc, interfaces: Ci, utils: Cu, results: Cr } = Components;
const APK_MIME_TYPE = "application/vnd.android.package-archive";
const OMA_DOWNLOAD_DESCRIPTOR_MIME_TYPE = "application/vnd.oma.dd+xml";
const PREF_BD_USEDOWNLOADDIR = "browser.download.useDownloadDir";
const URI_GENERIC_ICON_DOWNLOAD = "drawable://alert_download";
@ -94,25 +95,23 @@ HelperAppLauncherDialog.prototype = {
let mimeType = this._getMimeTypeFromLauncher(launcher);
// Straight equality: nsIMIMEInfo normalizes.
return APK_MIME_TYPE == mimeType;
return APK_MIME_TYPE == mimeType || OMA_DOWNLOAD_DESCRIPTOR_MIME_TYPE == mimeType;
},
/**
* Returns true if `launcher` represents a download for which we wish to
* offer a "Save to disk" option.
*/
_shouldAddSaveToDiskIntent: function(launcher) {
let mimeType = this._getMimeTypeFromLauncher(launcher);
// We can't handle OMA downloads. So don't even try. (Bug 1219078)
return mimeType != OMA_DOWNLOAD_DESCRIPTOR_MIME_TYPE;
},
show: function hald_show(aLauncher, aContext, aReason) {
if (!this._canDownload(aLauncher.source)) {
aLauncher.cancel(Cr.NS_BINDING_ABORTED);
let win = this.getNativeWindow();
if (!win) {
// Oops.
Services.console.logStringMessage("Refusing download, but can't show a toast.");
return;
}
Services.console.logStringMessage("Refusing download of non-downloadable file.");
let bundle = Services.strings.createBundle("chrome://browser/locale/handling.properties");
let failedText = bundle.GetStringFromName("download.blocked");
win.toast.show(failedText, "long");
this._refuseDownload(aLauncher);
return;
}
@ -123,19 +122,27 @@ HelperAppLauncherDialog.prototype = {
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",
selected: true, // Default to download for files
launch: function() {
// Reset the preferredAction here.
aLauncher.MIMEInfo.preferredAction = Ci.nsIMIMEInfo.saveToDisk;
aLauncher.saveToDisk(null, false);
return true;
}
});
if (this._shouldAddSaveToDiskIntent(aLauncher)) {
// 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",
selected: true, // Default to download for files
launch: function() {
// Reset the preferredAction here.
aLauncher.MIMEInfo.preferredAction = Ci.nsIMIMEInfo.saveToDisk;
aLauncher.saveToDisk(null, false);
return true;
}
});
}
// We do not handle this download and there are no apps that want to do it
if (apps.length === 0) {
this._refuseDownload(aLauncher);
return;
}
let callback = function(app) {
aLauncher.MIMEInfo.preferredAction = Ci.nsIMIMEInfo.useHelperApp;
@ -187,6 +194,22 @@ HelperAppLauncherDialog.prototype = {
});
},
_refuseDownload: function(aLauncher) {
aLauncher.cancel(Cr.NS_BINDING_ABORTED);
let win = this.getNativeWindow();
if (!win) {
// Oops.
Services.console.logStringMessage("Refusing download, but can't show a toast.");
return;
}
Services.console.logStringMessage("Refusing download of non-downloadable file.");
let bundle = Services.strings.createBundle("chrome://browser/locale/handling.properties");
let failedText = bundle.GetStringFromName("download.blocked");
win.toast.show(failedText, "long");
},
_getPrefName: function getPrefName(mimetype) {
return "browser.download.preferred." + mimetype.replace("\\", ".");
},