Expose the content-disposition header on documents (via nsIWindowUtils), and

use it for save as.  Bug 263697, r=biesi, sr=jst, a=asa
This commit is contained in:
bzbarsky%mit.edu 2004-11-18 20:50:16 +00:00
Родитель aa92a18252
Коммит 4425275728
6 изменённых файлов: 74 добавлений и 9 удалений

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

@ -297,6 +297,7 @@ nsContentSink::ProcessHTTPHeaders(nsIChannel* aChannel)
"default-style",
"content-style-type",
"content-language",
"content-disposition",
// add more http headers if you need
// XXXbz don't add content-location support without reading bug
// 238654 and its dependencies/dups first.

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

@ -154,6 +154,7 @@ HTML_ATOM(h5, "h5")
HTML_ATOM(h6, "h6")
HTML_ATOM(handler, "handler") //XML Events
HTML_ATOM(head, "head")
HTML_ATOM(headerContentDisposition, "content-disposition")
HTML_ATOM(headerContentLanguage, "content-language")
HTML_ATOM(headerContentScriptType, "content-script-type")
HTML_ATOM(headerContentStyleType, "content-style-type")

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

@ -154,6 +154,7 @@ HTML_ATOM(h5, "h5")
HTML_ATOM(h6, "h6")
HTML_ATOM(handler, "handler") //XML Events
HTML_ATOM(head, "head")
HTML_ATOM(headerContentDisposition, "content-disposition")
HTML_ATOM(headerContentLanguage, "content-language")
HTML_ATOM(headerContentScriptType, "content-script-type")
HTML_ATOM(headerContentStyleType, "content-style-type")

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

@ -43,7 +43,7 @@
* content script. Access this interface by calling getInterface on a DOMWindow.
*/
[scriptable, uuid(7e1ec6ff-2e67-4b26-aa39-a3eb2a3b8a4e)]
[scriptable, uuid(8a157a4f-a81e-489f-baf2-bc8970d60472)]
interface nsIDOMWindowUtils : nsISupports {
/**
@ -63,5 +63,12 @@ interface nsIDOMWindowUtils : nsISupports {
* this is not recursive to subwindows.
* @see imgIContainer
*/
attribute unsigned short imageAnimationMode;
attribute unsigned short imageAnimationMode;
/**
* Function to get metadata associated with the window's current document
* @param aName the name of the metadata. This should be all lowercase.
* @return the value of the metadata, or the empty string if it's not set
*/
AString getDocumentMetadata(in AString aName);
};

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

@ -40,6 +40,7 @@
#include "nsDOMWindowUtils.h"
#include "nsGlobalWindow.h"
#include "nsIDocument.h"
NS_INTERFACE_MAP_BEGIN(nsDOMWindowUtils)
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDOMWindowUtils)
@ -92,3 +93,20 @@ nsDOMWindowUtils::SetImageAnimationMode(PRUint16 aMode) {
}
return NS_ERROR_NOT_AVAILABLE;
}
NS_IMETHODIMP
nsDOMWindowUtils::GetDocumentMetadata(const nsAString& aName,
nsAString& aValue)
{
if (mWindow) {
nsCOMPtr<nsIDocument> doc(do_QueryInterface(mWindow->mDocument));
if (doc) {
nsCOMPtr<nsIAtom> name = do_GetAtom(aName);
doc->GetHeaderData(name, aValue);
return NS_OK;
}
}
aValue.Truncate();
return NS_OK;
}

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

@ -683,10 +683,47 @@ function getMIMEInfoForType(aMIMEType, aExtension)
function getDefaultFileName(aDefaultFileName, aDocumentURI, aDocument)
{
if (aDocument) {
// 1) look for a filename in the content-disposition header, if any
try {
// Get to the window as best we can, and get the header from it.
var dispHeader =
aDocument.defaultView
.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
.getInterface(Components.interfaces.nsIDOMWindowUtils)
.getDocumentMetadata("content-disposition");
if (dispHeader) {
const mhpContractID = "@mozilla.org/network/mime-hdrparam;1";
const mhpIID = Components.interfaces.nsIMIMEHeaderParam;
const mhp = Components.classes[mhpContractID].getService(mhpIID);
var dummy = { value: null }; // Need an out param...
var charset = getCharsetforSave(aDocument);
var fileName;
try {
fileName = mhp.getParameter(dispHeader, "filename", charset, true,
dummy);
}
catch (e) {
try {
fileName = mhp.getParameter(dispHeader, "name", charset, true,
dummy);
}
catch (e) {
}
}
if (fileName) {
return fileName;
}
}
} catch (e) {
// Move on
}
}
try {
var url = aDocumentURI.QueryInterface(Components.interfaces.nsIURL);
if (url.fileName != "") {
// 1) Use the actual file name, if present
// 2) Use the actual file name, if present
return validateFileName(decodeURIComponent(url.fileName));
}
} catch (e) {
@ -707,16 +744,16 @@ function getDefaultFileName(aDefaultFileName, aDocumentURI, aDocument)
var docTitle = GenerateValidFilename(aDocument.title, "");
if (docTitle) {
// 2) Use the document title
// 3) Use the document title
return docTitle;
}
}
if (aDefaultFileName)
// 3) Use the caller-provided name, if any
// 4) Use the caller-provided name, if any
return validateFileName(aDefaultFileName);
// 4) If this is a directory, use the last directory name
// 5) If this is a directory, use the last directory name
var path = aDocumentURI.path.match(/\/([^\/]+)\/$/);
if (path && path.length > 1) {
return validateFileName(path[1]);
@ -724,18 +761,18 @@ function getDefaultFileName(aDefaultFileName, aDocumentURI, aDocument)
try {
if (aDocumentURI.host)
// 5) Use the host.
// 6) Use the host.
return aDocumentURI.host;
} catch (e) {
// Some files have no information at all, like Javascript generated pages
}
try {
// 6) Use the default file name
// 7) Use the default file name
return getStringBundle().GetStringFromName("DefaultSaveFileName");
} catch (e) {
//in case localized string cannot be found
}
// 7) If all else fails, use "index"
// 8) If all else fails, use "index"
return "index";
}