Use Content-Disposition headers for "Save Link As", when available (bug 299372); r=mconnor (browser bits), r/sr=biesi (uriloader bits), ui-r=beltzner, a1.9b4=beltzner

This commit is contained in:
dmose%mozilla.org 2008-02-29 23:59:20 +00:00
Родитель e94efef1c1
Коммит a891d57fd8
7 изменённых файлов: 153 добавлений и 7 удалений

Просмотреть файл

@ -221,6 +221,12 @@ pref("browser.urlbar.maxRichResults", 25);
pref("browser.urlbar.search.chunkSize", 1000);
pref("browser.urlbar.search.timeout", 50);
// Number of milliseconds to wait for the http headers (and thus
// the Content-Disposition filename) before giving up and falling back to
// picking a filename without that info in hand so that the user sees some
// feedback from their action.
pref("browser.download.saveLinkAsFilenameTimeout", 1000);
pref("browser.download.useDownloadDir", true);
pref("browser.download.folderList", 0);
pref("browser.download.manager.showAlertOnComplete", true);

Просмотреть файл

@ -41,6 +41,7 @@
# Simon Bünzli <zeniko@gmail.com>
# Gijs Kruitbosch <gijskruitbosch@gmail.com>
# Ehsan Akhgari <ehsan.akhgari@gmail.com>
# Dan Mosedale <dmose@mozilla.org>
#
# Alternatively, the contents of this file may be used under the terms of
# either the GNU General Public License Version 2 or later (the "GPL"), or
@ -826,10 +827,122 @@ nsContextMenu.prototype = {
// Save URL of clicked-on link.
saveLink: function() {
// canonical def in nsURILoader.h
const NS_ERROR_SAVE_LINK_AS_TIMEOUT = 0x805d0020;
var doc = this.target.ownerDocument;
urlSecurityCheck(this.linkURL, doc.nodePrincipal);
saveURL(this.linkURL, this.linkText(), null, true, false,
doc.documentURIObject);
var linkText = this.linkText();
var linkURL = this.linkURL;
// an object to proxy the data through to
// nsIExternalHelperAppService.doContent, which will wait for the
// appropriate MIME-type headers and then prompt the user with a
// file picker
function saveAsListener() {}
saveAsListener.prototype = {
extListener: null,
onStartRequest: function saveLinkAs_onStartRequest(aRequest, aContext) {
// if the timer fired, the error status will have been caused by that,
// and we'll be restarting in onStopRequest, so no reason to notify
// the user
if (aRequest.status == NS_ERROR_SAVE_LINK_AS_TIMEOUT)
return;
timer.cancel();
// some other error occured; notify the user...
if (!Components.isSuccessCode(aRequest.status)) {
try {
const sbs = Cc["@mozilla.org/intl/stringbundle;1"].
getService(Ci.nsIStringBundleService);
const bundle = sbs.createBundle(
"chrome://mozapps/locale/downloads/downloads.properties");
const title = bundle.GetStringFromName("downloadErrorAlertTitle");
const msg = bundle.GetStringFromName("downloadErrorGeneric");
const promptSvc = Cc["@mozilla.org/embedcomp/prompt-service;1"].
getService(Ci.nsIPromptService);
promptSvc.alert(doc.defaultView, title, msg);
} catch (ex) {}
return;
}
var extHelperAppSvc =
Cc["@mozilla.org/uriloader/external-helper-app-service;1"].
getService(Ci.nsIExternalHelperAppService);
this.extListener =
extHelperAppSvc.doContent(aRequest.contentType, aRequest,
doc.defaultView, true);
this.extListener.onStartRequest(aRequest, aContext);
},
onStopRequest: function saveLinkAs_onStopRequest(aRequest, aContext,
aStatusCode) {
if (aStatusCode == NS_ERROR_SAVE_LINK_AS_TIMEOUT) {
// do it the old fashioned way, which will pick the best filename
// it can without waiting.
saveURL(linkURL, linkText, null, true, false, doc.documentURIObject);
}
if (this.extListener)
this.extListener.onStopRequest(aRequest, aContext, aStatusCode);
},
onDataAvailable: function saveLinkAs_onDataAvailable(aRequest, aContext,
aInputStream,
aOffset, aCount) {
this.extListener.onDataAvailable(aRequest, aContext, aInputStream,
aOffset, aCount);
}
}
// in case we need to prompt the user for authentication
function callbacks() {}
callbacks.prototype = {
getInterface: function sLA_callbacks_getInterface(aIID) {
if (aIID.equals(Ci.nsIAuthPrompt) || aIID.equals(Ci.nsIAuthPrompt2)) {
var ww = Cc["@mozilla.org/embedcomp/window-watcher;1"].
getService(Ci.nsIPromptFactory);
return ww.getPrompt(doc.defaultView, aIID);
}
throw Cr.NS_ERROR_NO_INTERFACE;
}
}
// if it we don't have the headers after a short time, the user
// won't have received any feedback from their click. that's bad. so
// we give up waiting for the filename.
function timerCallback() {}
timerCallback.prototype = {
notify: function sLA_timer_notify(aTimer) {
channel.cancel(NS_ERROR_SAVE_LINK_AS_TIMEOUT);
return;
}
}
// set up a channel to do the saving
var ioService = Cc["@mozilla.org/network/io-service;1"].
getService(Ci.nsIIOService);
var channel = ioService.newChannelFromURI(this.getLinkURI());
channel.notificationCallbacks = new callbacks();
channel.loadFlags |= Ci.nsIRequest.LOAD_BYPASS_CACHE |
Ci.nsIChannel.LOAD_CALL_CONTENT_SNIFFERS;
if (channel instanceof Ci.nsIHttpChannel)
channel.referrer = doc.documentURIObject;
// fallback to the old way if we don't see the headers quickly
var timeToWait =
gPrefService.getIntPref("browser.download.saveLinkAsFilenameTimeout");
var timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
timer.initWithCallback(new timerCallback(), timeToWait,
timer.TYPE_ONE_SHOT);
// kick off the channel with our proxy object as the listener
channel.asyncOpen(new saveAsListener(), null);
},
sendLink: function() {

Просмотреть файл

@ -594,6 +594,7 @@ nsresult nsDocumentOpenInfo::DispatchContent(nsIRequest *request, nsISupports *
rv = helperAppService->DoContent(mContentType,
request,
m_originalContext,
PR_FALSE,
getter_AddRefs(m_targetStreamListener));
if (NS_FAILED(rv)) {
request->SetLoadFlags(loadFlags);

Просмотреть файл

@ -99,4 +99,10 @@ protected:
#define NS_ERROR_MALWARE_URI NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_URILOADER, 30)
#define NS_ERROR_PHISHING_URI NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_URILOADER, 31)
/**
* Used when "Save Link As..." doesn't see the headers quickly enough to choose
* a filename. See nsContextMenu.js.
*/
#define NS_ERROR_SAVE_LINK_AS_TIMEOUT NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_URILOADER, 32);
#endif /* nsURILoader_h__ */

Просмотреть файл

@ -524,6 +524,7 @@ nsExternalHelperAppService::~nsExternalHelperAppService()
NS_IMETHODIMP nsExternalHelperAppService::DoContent(const nsACString& aMimeContentType,
nsIRequest *aRequest,
nsIInterfaceRequestor *aWindowContext,
PRBool aForceSave,
nsIStreamListener ** aStreamListener)
{
nsAutoString fileName;
@ -640,7 +641,8 @@ NS_IMETHODIMP nsExternalHelperAppService::DoContent(const nsACString& aMimeConte
buf,
aWindowContext,
fileName,
reason);
reason,
aForceSave);
if (!handler)
return NS_ERROR_OUT_OF_MEMORY;
NS_ADDREF(*aStreamListener = handler);
@ -991,11 +993,12 @@ nsExternalAppHandler::nsExternalAppHandler(nsIMIMEInfo * aMIMEInfo,
const nsCSubstring& aTempFileExtension,
nsIInterfaceRequestor* aWindowContext,
const nsAString& aSuggestedFilename,
PRUint32 aReason)
PRUint32 aReason, PRBool aForceSave)
: mMimeInfo(aMIMEInfo)
, mWindowContext(aWindowContext)
, mWindowToClose(nsnull)
, mSuggestedFileName(aSuggestedFilename)
, mForceSave(aForceSave)
, mCanceled(PR_FALSE)
, mShouldCloseWindow(PR_FALSE)
, mReceivedDispositionInfo(PR_FALSE)
@ -1470,6 +1473,13 @@ NS_IMETHODIMP nsExternalAppHandler::OnStartRequest(nsIRequest *request, nsISuppo
alwaysAsk = (action != nsIMIMEInfo::saveToDisk);
}
// if we were told that we _must_ save to disk without asking, all the stuff
// before this is irrelevant; override it
if (mForceSave) {
alwaysAsk = PR_FALSE;
action = nsIMIMEInfo::saveToDisk;
}
if (alwaysAsk)
{
// do this first! make sure we don't try to take an action until the user tells us what they want to do

Просмотреть файл

@ -251,7 +251,7 @@ public:
nsExternalAppHandler(nsIMIMEInfo * aMIMEInfo, const nsCSubstring& aFileExtension,
nsIInterfaceRequestor * aWindowContext,
const nsAString& aFilename,
PRUint32 aReason);
PRUint32 aReason, PRBool aForceSave);
~nsExternalAppHandler();
@ -280,6 +280,13 @@ protected:
*/
nsString mSuggestedFileName;
/**
* If set, this handler should forcibly save the file to disk regardless of
* MIME info settings or anything else, without ever popping up the
* unknown content type handling dialog.
*/
PRPackedBool mForceSave;
/**
* The canceled flag is set if the user canceled the launching of this
* application before we finished saving the data to a temp file.

Просмотреть файл

@ -51,7 +51,7 @@ interface nsIInterfaceRequestor;
* The external helper app service is used for finding and launching
* platform specific external applications for a given mime content type.
*/
[scriptable, uuid(0ea90cf3-2dd9-470f-8f76-f141743c5678)]
[scriptable, uuid(9e456297-ba3e-42b1-92bd-b7db014268cb)]
interface nsIExternalHelperAppService : nsISupports
{
/**
@ -64,10 +64,13 @@ interface nsIExternalHelperAppService : nsISupports
* @param aWindowContext Use GetInterface to retrieve properties like the
* dom window or parent window...
* The service might need this in order to bring up dialogs.
* @param aForceSave True to always save this content to disk, regardless of
* nsIMIMEInfo and other such influences.
* @return A nsIStreamListener which the caller should pump the data into.
*/
nsIStreamListener doContent (in ACString aMimeContentType, in nsIRequest aRequest,
in nsIInterfaceRequestor aWindowContext);
in nsIInterfaceRequestor aWindowContext,
in boolean aForceSave);
/**
* Returns true if data from a URL with this extension combination