michael@0: // -*- Mode: js2; tab-width: 2; indent-tabs-mode: nil; js2-basic-offset: 2; js2-skip-preprocessor-directives: t; -*- michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: const { classes: Cc, interfaces: Ci, utils: Cu, results: Cr } = Components; michael@0: michael@0: const APK_MIME_TYPE = "application/vnd.android.package-archive"; michael@0: const PREF_BD_USEDOWNLOADDIR = "browser.download.useDownloadDir"; michael@0: const URI_GENERIC_ICON_DOWNLOAD = "drawable://alert_download"; michael@0: michael@0: Cu.import("resource://gre/modules/FileUtils.jsm"); michael@0: Cu.import("resource://gre/modules/HelperApps.jsm"); michael@0: Cu.import("resource://gre/modules/Prompt.jsm"); michael@0: Cu.import("resource://gre/modules/Services.jsm"); michael@0: Cu.import("resource://gre/modules/XPCOMUtils.jsm"); michael@0: michael@0: // ----------------------------------------------------------------------- michael@0: // HelperApp Launcher Dialog michael@0: // ----------------------------------------------------------------------- michael@0: michael@0: function HelperAppLauncherDialog() { } michael@0: michael@0: HelperAppLauncherDialog.prototype = { michael@0: classID: Components.ID("{e9d277a0-268a-4ec2-bb8c-10fdf3e44611}"), michael@0: QueryInterface: XPCOMUtils.generateQI([Ci.nsIHelperAppLauncherDialog]), michael@0: michael@0: getNativeWindow: function () { michael@0: try { michael@0: let win = Services.wm.getMostRecentWindow("navigator:browser"); michael@0: if (win && win.NativeWindow) { michael@0: return win.NativeWindow; michael@0: } michael@0: } catch (e) { michael@0: } michael@0: return null; michael@0: }, michael@0: michael@0: /** michael@0: * Returns false if `url` represents a local or special URL that we don't michael@0: * wish to ever download. michael@0: * michael@0: * Returns true otherwise. michael@0: */ michael@0: _canDownload: function (url, alreadyResolved=false) { michael@0: // The common case. michael@0: if (url.schemeIs("http") || michael@0: url.schemeIs("https") || michael@0: url.schemeIs("ftp")) { michael@0: return true; michael@0: } michael@0: michael@0: // The less-common opposite case. michael@0: if (url.schemeIs("chrome") || michael@0: url.schemeIs("jar") || michael@0: url.schemeIs("resource") || michael@0: url.schemeIs("wyciwyg")) { michael@0: return false; michael@0: } michael@0: michael@0: // For all other URIs, try to resolve them to an inner URI, and check that. michael@0: if (!alreadyResolved) { michael@0: let ioSvc = Cc["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService); michael@0: let innerURI = ioSvc.newChannelFromURI(url).URI; michael@0: if (!url.equals(innerURI)) { michael@0: return this._canDownload(innerURI, true); michael@0: } michael@0: } michael@0: michael@0: if (url.schemeIs("file")) { michael@0: // If it's in our app directory or profile directory, we never ever michael@0: // want to do anything with it, including saving to disk or passing the michael@0: // file to another application. michael@0: let file = url.QueryInterface(Ci.nsIFileURL).file; michael@0: michael@0: // Normalize the nsILocalFile in-place. This will ensure that paths michael@0: // can be correctly compared via `contains`, below. michael@0: file.normalize(); michael@0: michael@0: // TODO: pref blacklist? michael@0: michael@0: let appRoot = FileUtils.getFile("XREExeF", []); michael@0: if (appRoot.contains(file, true)) { michael@0: return false; michael@0: } michael@0: michael@0: let profileRoot = FileUtils.getFile("ProfD", []); michael@0: if (profileRoot.contains(file, true)) { michael@0: return false; michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: // Anything else is fine to download. michael@0: return true; michael@0: }, michael@0: michael@0: /** michael@0: * Returns true if `launcher` represents a download for which we wish michael@0: * to prompt. michael@0: */ michael@0: _shouldPrompt: function (launcher) { michael@0: let mimeType = this._getMimeTypeFromLauncher(launcher); michael@0: michael@0: // Straight equality: nsIMIMEInfo normalizes. michael@0: return APK_MIME_TYPE == mimeType; michael@0: }, michael@0: michael@0: show: function hald_show(aLauncher, aContext, aReason) { michael@0: if (!this._canDownload(aLauncher.source)) { michael@0: aLauncher.cancel(Cr.NS_BINDING_ABORTED); michael@0: michael@0: let win = this.getNativeWindow(); michael@0: if (!win) { michael@0: // Oops. michael@0: Services.console.logStringMessage("Refusing download, but can't show a toast."); michael@0: return; michael@0: } michael@0: michael@0: Services.console.logStringMessage("Refusing download of non-downloadable file."); michael@0: let bundle = Services.strings.createBundle("chrome://browser/locale/handling.properties"); michael@0: let failedText = bundle.GetStringFromName("protocol.failed"); michael@0: win.toast.show(failedText, "long"); michael@0: michael@0: return; michael@0: } michael@0: michael@0: let bundle = Services.strings.createBundle("chrome://browser/locale/browser.properties"); michael@0: michael@0: let defaultHandler = new Object(); michael@0: let apps = HelperApps.getAppsForUri(aLauncher.source, { michael@0: mimeType: aLauncher.MIMEInfo.MIMEType, michael@0: }); michael@0: michael@0: // Add a fake intent for save to disk at the top of the list. michael@0: apps.unshift({ michael@0: name: bundle.GetStringFromName("helperapps.saveToDisk"), michael@0: packageName: "org.mozilla.gecko.Download", michael@0: iconUri: "drawable://icon", michael@0: launch: function() { michael@0: // Reset the preferredAction here. michael@0: aLauncher.MIMEInfo.preferredAction = Ci.nsIMIMEInfo.saveToDisk; michael@0: aLauncher.saveToDisk(null, false); michael@0: return true; michael@0: } michael@0: }); michael@0: michael@0: // See if the user already marked something as the default for this mimetype, michael@0: // and if that app is still installed. michael@0: let preferredApp = this._getPreferredApp(aLauncher); michael@0: if (preferredApp) { michael@0: let pref = apps.filter(function(app) { michael@0: return app.packageName === preferredApp; michael@0: }); michael@0: michael@0: if (pref.length > 0) { michael@0: pref[0].launch(aLauncher.source); michael@0: return; michael@0: } michael@0: } michael@0: michael@0: let callback = function(app) { michael@0: aLauncher.MIMEInfo.preferredAction = Ci.nsIMIMEInfo.useHelperApp; michael@0: if (!app.launch(aLauncher.source)) { michael@0: aLauncher.cancel(Cr.NS_BINDING_ABORTED); michael@0: } michael@0: } michael@0: michael@0: // If there's only one choice, and we don't want to prompt, go right ahead michael@0: // and choose that app automatically. michael@0: if (!this._shouldPrompt(aLauncher) && (apps.length === 1)) { michael@0: callback(apps[0]); michael@0: return; michael@0: } michael@0: michael@0: // Otherwise, let's go through the prompt. michael@0: HelperApps.prompt(apps, { michael@0: title: bundle.GetStringFromName("helperapps.pick"), michael@0: buttons: [ michael@0: bundle.GetStringFromName("helperapps.alwaysUse"), michael@0: bundle.GetStringFromName("helperapps.useJustOnce") michael@0: ] michael@0: }, (data) => { michael@0: if (data.button < 0) { michael@0: return; michael@0: } michael@0: michael@0: callback(apps[data.icongrid0]); michael@0: michael@0: if (data.button === 0) { michael@0: this._setPreferredApp(aLauncher, apps[data.icongrid0]); michael@0: } michael@0: }); michael@0: }, michael@0: michael@0: _getPrefName: function getPrefName(mimetype) { michael@0: return "browser.download.preferred." + mimetype.replace("\\", "."); michael@0: }, michael@0: michael@0: _getMimeTypeFromLauncher: function (launcher) { michael@0: let mime = launcher.MIMEInfo.MIMEType; michael@0: if (!mime) michael@0: mime = ContentAreaUtils.getMIMETypeForURI(launcher.source) || ""; michael@0: return mime; michael@0: }, michael@0: michael@0: _getPreferredApp: function getPreferredApp(launcher) { michael@0: let mime = this._getMimeTypeFromLauncher(launcher); michael@0: if (!mime) michael@0: return; michael@0: michael@0: try { michael@0: return Services.prefs.getCharPref(this._getPrefName(mime)); michael@0: } catch(ex) { michael@0: Services.console.logStringMessage("Error getting pref for " + mime + "."); michael@0: } michael@0: return null; michael@0: }, michael@0: michael@0: _setPreferredApp: function setPreferredApp(launcher, app) { michael@0: let mime = this._getMimeTypeFromLauncher(launcher); michael@0: if (!mime) michael@0: return; michael@0: michael@0: if (app) michael@0: Services.prefs.setCharPref(this._getPrefName(mime), app.packageName); michael@0: else michael@0: Services.prefs.clearUserPref(this._getPrefName(mime)); michael@0: }, michael@0: michael@0: promptForSaveToFile: function hald_promptForSaveToFile(aLauncher, aContext, aDefaultFile, aSuggestedFileExt, aForcePrompt) { michael@0: // Retrieve the user's default download directory michael@0: let dnldMgr = Cc["@mozilla.org/download-manager;1"].getService(Ci.nsIDownloadManager); michael@0: let defaultFolder = dnldMgr.userDownloadsDirectory; michael@0: michael@0: try { michael@0: file = this.validateLeafName(defaultFolder, aDefaultFile, aSuggestedFileExt); michael@0: } catch (e) { } michael@0: michael@0: return file; michael@0: }, michael@0: michael@0: validateLeafName: function hald_validateLeafName(aLocalFile, aLeafName, aFileExt) { michael@0: if (!(aLocalFile && this.isUsableDirectory(aLocalFile))) michael@0: return null; michael@0: michael@0: // Remove any leading periods, since we don't want to save hidden files michael@0: // automatically. michael@0: aLeafName = aLeafName.replace(/^\.+/, ""); michael@0: michael@0: if (aLeafName == "") michael@0: aLeafName = "unnamed" + (aFileExt ? "." + aFileExt : ""); michael@0: aLocalFile.append(aLeafName); michael@0: michael@0: this.makeFileUnique(aLocalFile); michael@0: return aLocalFile; michael@0: }, michael@0: michael@0: makeFileUnique: function hald_makeFileUnique(aLocalFile) { michael@0: try { michael@0: // Note - this code is identical to that in michael@0: // toolkit/content/contentAreaUtils.js. michael@0: // If you are updating this code, update that code too! We can't share code michael@0: // here since this is called in a js component. michael@0: let collisionCount = 0; michael@0: while (aLocalFile.exists()) { michael@0: collisionCount++; michael@0: if (collisionCount == 1) { michael@0: // Append "(2)" before the last dot in (or at the end of) the filename michael@0: // special case .ext.gz etc files so we don't wind up with .tar(2).gz michael@0: if (aLocalFile.leafName.match(/\.[^\.]{1,3}\.(gz|bz2|Z)$/i)) michael@0: aLocalFile.leafName = aLocalFile.leafName.replace(/\.[^\.]{1,3}\.(gz|bz2|Z)$/i, "(2)$&"); michael@0: else michael@0: aLocalFile.leafName = aLocalFile.leafName.replace(/(\.[^\.]*)?$/, "(2)$&"); michael@0: } michael@0: else { michael@0: // replace the last (n) in the filename with (n+1) michael@0: aLocalFile.leafName = aLocalFile.leafName.replace(/^(.*\()\d+\)/, "$1" + (collisionCount+1) + ")"); michael@0: } michael@0: } michael@0: aLocalFile.create(Ci.nsIFile.NORMAL_FILE_TYPE, 0600); michael@0: } michael@0: catch (e) { michael@0: dump("*** exception in validateLeafName: " + e + "\n"); michael@0: michael@0: if (e.result == Cr.NS_ERROR_FILE_ACCESS_DENIED) michael@0: throw e; michael@0: michael@0: if (aLocalFile.leafName == "" || aLocalFile.isDirectory()) { michael@0: aLocalFile.append("unnamed"); michael@0: if (aLocalFile.exists()) michael@0: aLocalFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0600); michael@0: } michael@0: } michael@0: }, michael@0: michael@0: isUsableDirectory: function hald_isUsableDirectory(aDirectory) { michael@0: return aDirectory.exists() && aDirectory.isDirectory() && aDirectory.isWritable(); michael@0: }, michael@0: }; michael@0: michael@0: this.NSGetFactory = XPCOMUtils.generateNSGetFactory([HelperAppLauncherDialog]);