From 8f8a18bd024a256dcaaae2bda5787ca46325f079 Mon Sep 17 00:00:00 2001 From: "bzbarsky%mit.edu" Date: Thu, 18 Nov 2004 20:50:16 +0000 Subject: [PATCH] Expose the content-disposition header on documents (via nsIWindowUtils), and use it for save as. Bug 263697, r=biesi, sr=jst, a=asa git-svn-id: svn://10.0.0.236/trunk@165478 18797224-902f-48f8-a5cc-f745e15eee43 --- mozilla/content/base/src/nsContentSink.cpp | 1 + .../content/html/content/src/nsHTMLAtomList.h | 1 + .../content/shared/public/nsHTMLAtomList.h | 1 + .../dom/public/idl/base/nsIDOMWindowUtils.idl | 11 +++- mozilla/dom/src/base/nsDOMWindowUtils.cpp | 18 +++++++ .../resources/content/contentAreaUtils.js | 51 ++++++++++++++++--- 6 files changed, 74 insertions(+), 9 deletions(-) diff --git a/mozilla/content/base/src/nsContentSink.cpp b/mozilla/content/base/src/nsContentSink.cpp index d431faae6a1..394968373ee 100644 --- a/mozilla/content/base/src/nsContentSink.cpp +++ b/mozilla/content/base/src/nsContentSink.cpp @@ -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. diff --git a/mozilla/content/html/content/src/nsHTMLAtomList.h b/mozilla/content/html/content/src/nsHTMLAtomList.h index 1e9ea7e4e7b..f4db70e431b 100644 --- a/mozilla/content/html/content/src/nsHTMLAtomList.h +++ b/mozilla/content/html/content/src/nsHTMLAtomList.h @@ -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") diff --git a/mozilla/content/shared/public/nsHTMLAtomList.h b/mozilla/content/shared/public/nsHTMLAtomList.h index 1e9ea7e4e7b..f4db70e431b 100644 --- a/mozilla/content/shared/public/nsHTMLAtomList.h +++ b/mozilla/content/shared/public/nsHTMLAtomList.h @@ -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") diff --git a/mozilla/dom/public/idl/base/nsIDOMWindowUtils.idl b/mozilla/dom/public/idl/base/nsIDOMWindowUtils.idl index 30e6968a443..41f6178dacf 100644 --- a/mozilla/dom/public/idl/base/nsIDOMWindowUtils.idl +++ b/mozilla/dom/public/idl/base/nsIDOMWindowUtils.idl @@ -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); }; diff --git a/mozilla/dom/src/base/nsDOMWindowUtils.cpp b/mozilla/dom/src/base/nsDOMWindowUtils.cpp index 480eebd78df..167b9262e60 100644 --- a/mozilla/dom/src/base/nsDOMWindowUtils.cpp +++ b/mozilla/dom/src/base/nsDOMWindowUtils.cpp @@ -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 doc(do_QueryInterface(mWindow->mDocument)); + if (doc) { + nsCOMPtr name = do_GetAtom(aName); + doc->GetHeaderData(name, aValue); + return NS_OK; + } + } + + aValue.Truncate(); + return NS_OK; +} diff --git a/mozilla/xpfe/communicator/resources/content/contentAreaUtils.js b/mozilla/xpfe/communicator/resources/content/contentAreaUtils.js index 81e02244383..9d407653a32 100644 --- a/mozilla/xpfe/communicator/resources/content/contentAreaUtils.js +++ b/mozilla/xpfe/communicator/resources/content/contentAreaUtils.js @@ -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"; }