2006-07-29 09:38:04 +04:00
|
|
|
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: NPL 1.1/GPL 2.0/LGPL 2.1
|
2006-07-29 09:33:03 +04:00
|
|
|
*
|
2006-07-29 09:38:04 +04:00
|
|
|
* The contents of this file are subject to the Netscape Public License
|
|
|
|
* Version 1.1 (the "License"); you may not use this file except in
|
|
|
|
* compliance with the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/NPL/
|
2006-07-29 09:33:03 +04:00
|
|
|
*
|
2006-07-29 09:38:04 +04:00
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
2006-07-29 09:33:03 +04:00
|
|
|
*
|
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
2006-07-29 09:38:04 +04:00
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1998
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
2006-07-29 09:33:03 +04:00
|
|
|
*
|
2006-07-29 09:38:04 +04:00
|
|
|
* Contributor(s):
|
2006-07-29 09:39:02 +04:00
|
|
|
* Ben Goodger <ben@netscape.com> (Save File)
|
2006-07-29 09:38:04 +04:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the NPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the NPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
2006-07-29 09:33:03 +04:00
|
|
|
|
2006-07-29 09:33:17 +04:00
|
|
|
/**
|
|
|
|
* Determine whether or not a given focused DOMWindow is in the content
|
|
|
|
* area.
|
|
|
|
**/
|
2006-07-29 09:41:24 +04:00
|
|
|
function isContentFrame(aFocusedWindow)
|
2006-07-29 09:39:02 +04:00
|
|
|
{
|
2006-07-29 09:41:25 +04:00
|
|
|
if (!aFocusedWindow)
|
|
|
|
return false;
|
|
|
|
|
2006-07-29 09:41:24 +04:00
|
|
|
var focusedTop = Components.lookupMethod(aFocusedWindow, 'top')
|
|
|
|
.call(aFocusedWindow);
|
|
|
|
|
|
|
|
return (focusedTop == window.content);
|
2006-07-29 09:39:02 +04:00
|
|
|
}
|
2006-07-29 09:33:17 +04:00
|
|
|
|
2006-07-29 09:39:02 +04:00
|
|
|
function urlSecurityCheck(url, doc)
|
|
|
|
{
|
|
|
|
// URL Loading Security Check
|
|
|
|
var focusedWindow = doc.commandDispatcher.focusedWindow;
|
2006-07-29 09:41:24 +04:00
|
|
|
var sourceURL = getContentFrameURI(focusedWindow);
|
2006-07-29 09:39:02 +04:00
|
|
|
const nsIScriptSecurityManager = Components.interfaces.nsIScriptSecurityManager;
|
2006-07-29 09:41:24 +04:00
|
|
|
var secMan = Components.classes["@mozilla.org/scriptsecuritymanager;1"]
|
|
|
|
.getService(nsIScriptSecurityManager);
|
2006-07-29 09:39:02 +04:00
|
|
|
try {
|
2006-07-29 09:41:24 +04:00
|
|
|
secMan.checkLoadURIStr(sourceURL, url, nsIScriptSecurityManager.STANDARD);
|
2006-07-29 09:39:02 +04:00
|
|
|
} catch (e) {
|
2006-07-29 09:41:24 +04:00
|
|
|
throw "Load of " + url + " denied.";
|
2006-07-29 09:37:09 +04:00
|
|
|
}
|
2006-07-29 09:39:02 +04:00
|
|
|
}
|
2006-07-29 09:37:09 +04:00
|
|
|
|
2006-07-29 09:41:24 +04:00
|
|
|
function getContentFrameURI(aFocusedWindow)
|
|
|
|
{
|
|
|
|
var contentFrame = isContentFrame(aFocusedWindow) ? aFocusedWindow : window.content;
|
|
|
|
return Components.lookupMethod(contentFrame, 'location').call(contentFrame).href;
|
|
|
|
}
|
|
|
|
|
2006-07-29 09:39:18 +04:00
|
|
|
function getReferrer(doc)
|
|
|
|
{
|
|
|
|
var focusedWindow = doc.commandDispatcher.focusedWindow;
|
2006-07-29 09:41:24 +04:00
|
|
|
var sourceURL = getContentFrameURI(focusedWindow);
|
|
|
|
|
2006-07-29 09:39:18 +04:00
|
|
|
try {
|
|
|
|
var uri = Components.classes["@mozilla.org/network/standard-url;1"].createInstance(Components.interfaces.nsIURI);
|
|
|
|
uri.spec = sourceURL;
|
|
|
|
return uri;
|
|
|
|
} catch (e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-29 09:41:33 +04:00
|
|
|
function openNewWindowWith(url, sendReferrer)
|
2006-07-29 09:39:02 +04:00
|
|
|
{
|
|
|
|
urlSecurityCheck(url, document);
|
2006-07-29 09:37:09 +04:00
|
|
|
|
2006-07-29 09:39:02 +04:00
|
|
|
// if and only if the current window is a browser window and it has a document with a character
|
|
|
|
// set, then extract the current charset menu setting from the current document and use it to
|
|
|
|
// initialize the new browser window...
|
2006-07-29 09:40:23 +04:00
|
|
|
var charsetArg = null;
|
|
|
|
var wintype = document.firstChild.getAttribute('windowtype');
|
|
|
|
if (wintype == "navigator:browser")
|
|
|
|
charsetArg = "charset=" + window._content.document.characterSet;
|
2006-07-29 09:37:09 +04:00
|
|
|
|
2006-07-29 09:41:33 +04:00
|
|
|
var referrer = sendReferrer ? getReferrer(document) : null;
|
2006-07-29 09:40:23 +04:00
|
|
|
window.openDialog(getBrowserURL(), "_blank", "chrome,all,dialog=no", url, charsetArg, referrer);
|
2006-07-29 09:39:02 +04:00
|
|
|
}
|
2006-07-29 09:37:42 +04:00
|
|
|
|
2006-07-29 09:42:06 +04:00
|
|
|
function openTopBrowserWith(url)
|
|
|
|
{
|
2006-07-29 09:42:11 +04:00
|
|
|
urlSecurityCheck(url, document);
|
|
|
|
|
2006-07-29 09:42:06 +04:00
|
|
|
var windowMediator = Components.classes["@mozilla.org/appshell/window-mediator;1"].getService(Components.interfaces.nsIWindowMediator);
|
|
|
|
var browserWin = windowMediator.getMostRecentWindow("navigator:browser");
|
|
|
|
|
|
|
|
// if there's an existing browser window, open this url in one
|
2006-07-29 09:42:07 +04:00
|
|
|
if (browserWin) {
|
2006-07-29 09:42:06 +04:00
|
|
|
browserWin.getBrowser().loadURI(url); // Just do a normal load.
|
2006-07-29 09:42:08 +04:00
|
|
|
browserWin.content.focus();
|
2006-07-29 09:42:07 +04:00
|
|
|
}
|
2006-07-29 09:42:06 +04:00
|
|
|
else
|
|
|
|
window.openDialog(getBrowserURL(), "_blank", "chrome,all,dialog=no", url, null, null);
|
|
|
|
}
|
|
|
|
|
2006-07-29 09:41:33 +04:00
|
|
|
function openNewTabWith(url, sendReferrer, reverseBackgroundPref)
|
2006-07-29 09:39:02 +04:00
|
|
|
{
|
2006-07-29 09:41:35 +04:00
|
|
|
var browser;
|
|
|
|
try {
|
|
|
|
// if we're running in a browser window, this should work
|
|
|
|
//
|
|
|
|
browser = getBrowser();
|
|
|
|
|
|
|
|
} catch (ex if ex instanceof ReferenceError) {
|
|
|
|
|
|
|
|
// must be running somewhere else (eg mailnews message pane); need to
|
|
|
|
// find a browser window first
|
|
|
|
//
|
|
|
|
var windowMediator =
|
|
|
|
Components.classes["@mozilla.org/appshell/window-mediator;1"]
|
|
|
|
.getService(Components.interfaces.nsIWindowMediator);
|
|
|
|
|
|
|
|
var browserWin = windowMediator.getMostRecentWindow("navigator:browser");
|
|
|
|
|
2006-07-29 09:42:19 +04:00
|
|
|
// if there's no existing browser window, then, as long as
|
|
|
|
// we are allowed to, open this url in one and return
|
2006-07-29 09:41:35 +04:00
|
|
|
//
|
|
|
|
if (!browserWin) {
|
2006-07-29 09:42:19 +04:00
|
|
|
urlSecurityCheck(url, document);
|
2006-07-29 09:41:35 +04:00
|
|
|
window.openDialog(getBrowserURL(), "_blank", "chrome,all,dialog=no",
|
|
|
|
url, null, referrer);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// otherwise, get the existing browser object
|
|
|
|
//
|
|
|
|
browser = browserWin.getBrowser();
|
|
|
|
}
|
|
|
|
|
2006-07-29 09:41:36 +04:00
|
|
|
// Get the XUL document that the browser is actually contained in.
|
|
|
|
// This is needed if we are trying to load a URL from a non-navigator
|
|
|
|
// window such as the JS Console.
|
|
|
|
var browserDocument = browser.ownerDocument;
|
|
|
|
|
|
|
|
urlSecurityCheck(url, browserDocument);
|
|
|
|
|
|
|
|
var referrer = sendReferrer ? getReferrer(browserDocument) : null;
|
|
|
|
|
2006-07-29 09:42:17 +04:00
|
|
|
// As in openNewWindowWith(), we want to pass the charset of the
|
|
|
|
// current document over to a new tab.
|
|
|
|
var wintype = browserDocument.firstChild.getAttribute('windowtype');
|
|
|
|
var originCharset;
|
|
|
|
if (wintype == "navigator:browser") {
|
|
|
|
originCharset = window._content.document.characterSet;
|
|
|
|
}
|
|
|
|
|
|
|
|
// open link in new tab
|
|
|
|
var tab = browser.addTab(url, referrer, originCharset);
|
2006-07-29 09:41:33 +04:00
|
|
|
if (pref) {
|
|
|
|
var loadInBackground = pref.getBoolPref("browser.tabs.loadInBackground");
|
|
|
|
if (reverseBackgroundPref)
|
|
|
|
loadInBackground = !loadInBackground;
|
2006-07-29 09:37:42 +04:00
|
|
|
|
2006-07-29 09:41:33 +04:00
|
|
|
if (!loadInBackground)
|
|
|
|
browser.selectedTab = tab;
|
|
|
|
}
|
2006-07-29 09:39:02 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
function findParentNode(node, parentNode)
|
|
|
|
{
|
|
|
|
if (node && node.nodeType == Node.TEXT_NODE) {
|
|
|
|
node = node.parentNode;
|
2006-07-29 09:37:42 +04:00
|
|
|
}
|
2006-07-29 09:39:02 +04:00
|
|
|
while (node) {
|
|
|
|
var nodeName = node.localName;
|
|
|
|
if (!nodeName)
|
|
|
|
return null;
|
|
|
|
nodeName = nodeName.toLowerCase();
|
|
|
|
if (nodeName == "body" || nodeName == "html" ||
|
|
|
|
nodeName == "#document") {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (nodeName == parentNode)
|
|
|
|
return node;
|
|
|
|
node = node.parentNode;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clientelle: (Make sure you don't break any of these)
|
|
|
|
// - File -> Save Page/Frame As...
|
|
|
|
// - Context -> Save Page/Frame As...
|
|
|
|
// - Context -> Save Link As...
|
|
|
|
// - Context -> Save Image As...
|
|
|
|
// - Shift-Click Save Link As
|
|
|
|
//
|
|
|
|
// Try saving each of these types:
|
|
|
|
// - A complete webpage using File->Save Page As, and Context->Save Page As
|
|
|
|
// - A webpage as HTML only using the above methods
|
|
|
|
// - A webpage as Text only using the above methods
|
|
|
|
// - An image with an extension (e.g. .jpg) in its file name, using
|
|
|
|
// Context->Save Image As...
|
|
|
|
// - An image without an extension (e.g. a banner ad on cnn.com) using
|
|
|
|
// the above method.
|
|
|
|
// - A linked document using Save Link As...
|
|
|
|
// - A linked document using shift-click Save Link As...
|
|
|
|
//
|
2006-07-29 09:39:15 +04:00
|
|
|
function saveURL(aURL, aFileName, aFilePickerTitleKey, aShouldBypassCache)
|
2006-07-29 09:39:04 +04:00
|
|
|
{
|
2006-07-29 09:39:15 +04:00
|
|
|
saveInternal(aURL, null, aFileName, aFilePickerTitleKey, aShouldBypassCache);
|
2006-07-29 09:39:04 +04:00
|
|
|
}
|
|
|
|
|
2006-07-29 09:39:35 +04:00
|
|
|
function saveFrameDocument()
|
|
|
|
{
|
|
|
|
var focusedWindow = document.commandDispatcher.focusedWindow;
|
2006-07-29 09:41:24 +04:00
|
|
|
if (isContentFrame(focusedWindow))
|
2006-07-29 09:39:35 +04:00
|
|
|
saveDocument(focusedWindow.document);
|
|
|
|
}
|
|
|
|
|
2006-07-29 09:39:04 +04:00
|
|
|
function saveDocument(aDocument)
|
|
|
|
{
|
2006-07-29 09:39:15 +04:00
|
|
|
// In both cases here, we want to use cached data because the
|
|
|
|
// document is currently visible.
|
|
|
|
if (aDocument)
|
|
|
|
saveInternal(aDocument.location.href, aDocument, false);
|
|
|
|
else
|
|
|
|
saveInternal(_content.location.href, null, false);
|
2006-07-29 09:39:04 +04:00
|
|
|
}
|
|
|
|
|
2006-07-29 09:39:15 +04:00
|
|
|
function saveInternal(aURL, aDocument,
|
|
|
|
aFileName, aFilePickerTitleKey,
|
|
|
|
aShouldBypassCache)
|
2006-07-29 09:39:02 +04:00
|
|
|
{
|
|
|
|
var data = {
|
2006-07-29 09:39:48 +04:00
|
|
|
url: aURL,
|
2006-07-29 09:39:02 +04:00
|
|
|
fileName: aFileName,
|
2006-07-29 09:39:04 +04:00
|
|
|
filePickerTitle: aFilePickerTitleKey,
|
2006-07-29 09:39:15 +04:00
|
|
|
document: aDocument,
|
2006-07-29 09:39:45 +04:00
|
|
|
bypassCache: aShouldBypassCache,
|
|
|
|
window: window
|
2006-07-29 09:39:02 +04:00
|
|
|
};
|
|
|
|
var sniffer = new nsHeaderSniffer(aURL, foundHeaderInfo, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
function foundHeaderInfo(aSniffer, aData)
|
|
|
|
{
|
|
|
|
var contentType = aSniffer.contentType;
|
2006-07-29 09:39:55 +04:00
|
|
|
var contentEncodingType = aSniffer.contentEncodingType;
|
|
|
|
|
|
|
|
var shouldDecode = false;
|
|
|
|
// Are we allowed to decode?
|
|
|
|
try {
|
|
|
|
const helperAppService =
|
|
|
|
Components.classes["@mozilla.org/uriloader/external-helper-app-service;1"].
|
|
|
|
getService(Components.interfaces.nsIExternalHelperAppService);
|
|
|
|
var url = aSniffer.uri.QueryInterface(Components.interfaces.nsIURL);
|
|
|
|
var urlExt = url.fileExtension;
|
2006-07-29 09:42:21 +04:00
|
|
|
if (helperAppService.applyDecodingForExtension(urlExt,
|
|
|
|
contentEncodingType)) {
|
2006-07-29 09:39:55 +04:00
|
|
|
shouldDecode = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
}
|
2006-07-29 09:37:42 +04:00
|
|
|
|
2006-07-29 09:39:02 +04:00
|
|
|
var fp = makeFilePicker();
|
|
|
|
var titleKey = aData.filePickerTitle || "SaveLinkTitle";
|
|
|
|
var bundle = getStringBundle();
|
|
|
|
fp.init(window, bundle.GetStringFromName(titleKey),
|
|
|
|
Components.interfaces.nsIFilePicker.modeSave);
|
|
|
|
|
2006-07-29 09:42:05 +04:00
|
|
|
var saveMode = GetSaveModeForContentType(contentType);
|
|
|
|
var isDocument = aData.document != null && saveMode;
|
2006-07-29 09:39:55 +04:00
|
|
|
if (!isDocument && !shouldDecode && contentEncodingType) {
|
|
|
|
// The data is encoded, we are not going to decode it, and this is not a
|
|
|
|
// document save so we won't be doing a "save as, complete" (which would
|
|
|
|
// break if we reset the type here). So just set our content type to
|
|
|
|
// correspond to the outermost encoding so we get extensions and the like
|
|
|
|
// right.
|
|
|
|
contentType = contentEncodingType;
|
|
|
|
}
|
2006-07-29 09:42:05 +04:00
|
|
|
|
2006-07-29 09:39:55 +04:00
|
|
|
appendFiltersForContentType(fp, contentType,
|
2006-07-29 09:42:05 +04:00
|
|
|
isDocument ? saveMode : SAVEMODE_FILEONLY);
|
2006-07-29 09:39:15 +04:00
|
|
|
|
|
|
|
const prefSvcContractID = "@mozilla.org/preferences-service;1";
|
|
|
|
const prefSvcIID = Components.interfaces.nsIPrefService;
|
2006-07-29 09:39:17 +04:00
|
|
|
var prefs = Components.classes[prefSvcContractID].getService(prefSvcIID).getBranch("browser.download.");
|
2006-07-29 09:39:15 +04:00
|
|
|
|
|
|
|
const nsILocalFile = Components.interfaces.nsILocalFile;
|
|
|
|
try {
|
|
|
|
fp.displayDirectory = prefs.getComplexValue("dir", nsILocalFile);
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
}
|
2006-07-29 09:39:02 +04:00
|
|
|
|
2006-07-29 09:39:15 +04:00
|
|
|
if (isDocument) {
|
|
|
|
try {
|
|
|
|
fp.filterIndex = prefs.getIntPref("save_converter_index");
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-29 09:39:02 +04:00
|
|
|
// Determine what the 'default' string to display in the File Picker dialog
|
|
|
|
// should be.
|
2006-07-29 09:39:08 +04:00
|
|
|
var defaultFileName = getDefaultFileName(aData.fileName,
|
|
|
|
aSniffer.suggestedFileName,
|
2006-07-29 09:39:56 +04:00
|
|
|
aSniffer.uri,
|
|
|
|
aData.document);
|
2006-07-29 09:39:46 +04:00
|
|
|
var defaultExtension = getDefaultExtension(defaultFileName, aSniffer.uri, contentType);
|
|
|
|
fp.defaultExtension = defaultExtension;
|
|
|
|
fp.defaultString = getNormalizedLeafName(defaultFileName, defaultExtension);
|
2006-07-29 09:39:02 +04:00
|
|
|
|
|
|
|
if (fp.show() == Components.interfaces.nsIFilePicker.returnCancel || !fp.file)
|
|
|
|
return;
|
2006-07-29 09:42:05 +04:00
|
|
|
|
2006-07-29 09:39:15 +04:00
|
|
|
if (isDocument)
|
|
|
|
prefs.setIntPref("save_converter_index", fp.filterIndex);
|
|
|
|
var directory = fp.file.parent.QueryInterface(nsILocalFile);
|
|
|
|
prefs.setComplexValue("dir", nsILocalFile, directory);
|
2006-07-29 09:42:05 +04:00
|
|
|
|
2006-07-29 09:39:02 +04:00
|
|
|
fp.file.leafName = validateFileName(fp.file.leafName);
|
2006-07-29 09:42:05 +04:00
|
|
|
|
|
|
|
// XXX We depend on the following holding true in appendFiltersForContentType():
|
|
|
|
// If we should save as a complete page, the filterIndex is 0.
|
|
|
|
// If we should save as text, the filterIndex is 2.
|
|
|
|
var useSaveDocument = isDocument &&
|
|
|
|
((saveMode & SAVEMODE_COMPLETE_DOM && fp.filterIndex == 0) ||
|
|
|
|
(saveMode & SAVEMODE_COMPLETE_TEXT && fp.filterIndex == 2));
|
|
|
|
|
2006-07-29 09:39:15 +04:00
|
|
|
// If we're saving a document, and are saving either in complete mode or
|
|
|
|
// as converted text, pass the document to the web browser persist component.
|
|
|
|
// If we're just saving the HTML (second option in the list), send only the URI.
|
2006-07-29 09:42:05 +04:00
|
|
|
var source = useSaveDocument ? aData.document : aSniffer.uri;
|
2006-07-29 09:39:02 +04:00
|
|
|
var persistArgs = {
|
2006-07-29 09:39:15 +04:00
|
|
|
source : source,
|
2006-07-29 09:42:05 +04:00
|
|
|
contentType : (useSaveDocument && fp.filterIndex == 2) ? "text/plain" : contentType,
|
2006-07-29 09:39:15 +04:00
|
|
|
target : fp.file,
|
2006-07-29 09:42:03 +04:00
|
|
|
postData : isDocument ? getPostData() : null,
|
2006-07-29 09:39:15 +04:00
|
|
|
bypassCache : aData.bypassCache
|
2006-07-29 09:39:02 +04:00
|
|
|
};
|
|
|
|
|
2006-07-29 09:39:48 +04:00
|
|
|
var persist = makeWebBrowserPersist();
|
|
|
|
|
|
|
|
// Calculate persist flags.
|
|
|
|
const nsIWBP = Components.interfaces.nsIWebBrowserPersist;
|
|
|
|
const flags = nsIWBP.PERSIST_FLAGS_NO_CONVERSION | nsIWBP.PERSIST_FLAGS_REPLACE_EXISTING_FILES;
|
|
|
|
if (aData.bypassCache)
|
|
|
|
persist.persistFlags = flags | nsIWBP.PERSIST_FLAGS_BYPASS_CACHE;
|
|
|
|
else
|
|
|
|
persist.persistFlags = flags | nsIWBP.PERSIST_FLAGS_FROM_CACHE;
|
|
|
|
|
2006-07-29 09:39:55 +04:00
|
|
|
if (shouldDecode)
|
|
|
|
persist.persistFlags &= ~nsIWBP.PERSIST_FLAGS_NO_CONVERSION;
|
|
|
|
|
2006-07-29 09:40:22 +04:00
|
|
|
// Create download and initiate it (below)
|
|
|
|
var dl = Components.classes["@mozilla.org/download;1"].createInstance(Components.interfaces.nsIDownload);
|
|
|
|
|
2006-07-29 09:42:05 +04:00
|
|
|
if (useSaveDocument) {
|
2006-07-29 09:39:48 +04:00
|
|
|
// Saving a Document, not a URI:
|
|
|
|
var filesFolder = null;
|
|
|
|
if (persistArgs.contentType != "text/plain") {
|
|
|
|
// Create the local directory into which to save associated files.
|
|
|
|
const lfContractID = "@mozilla.org/file/local;1";
|
|
|
|
const lfIID = Components.interfaces.nsILocalFile;
|
|
|
|
filesFolder = Components .classes[lfContractID].createInstance(lfIID);
|
2006-07-29 09:40:25 +04:00
|
|
|
filesFolder.initWithPath(persistArgs.target.path);
|
2006-07-29 09:39:48 +04:00
|
|
|
|
2006-07-29 09:42:10 +04:00
|
|
|
var nameWithoutExtension = filesFolder.leafName.replace(/\.[^.]*$/, "");
|
2006-07-29 09:39:48 +04:00
|
|
|
var filesFolderLeafName = getStringBundle().formatStringFromName("filesFolder",
|
|
|
|
[nameWithoutExtension],
|
|
|
|
1);
|
|
|
|
|
|
|
|
filesFolder.leafName = filesFolderLeafName;
|
|
|
|
}
|
|
|
|
|
|
|
|
var encodingFlags = 0;
|
|
|
|
if (persistArgs.contentType == "text/plain") {
|
|
|
|
encodingFlags |= nsIWBP.ENCODE_FLAGS_FORMATTED;
|
|
|
|
encodingFlags |= nsIWBP.ENCODE_FLAGS_ABSOLUTE_LINKS;
|
|
|
|
encodingFlags |= nsIWBP.ENCODE_FLAGS_NOFRAMES_CONTENT;
|
|
|
|
}
|
2006-07-29 09:42:09 +04:00
|
|
|
else {
|
|
|
|
encodingFlags |= nsIWBP.ENCODE_FLAGS_ENCODE_BASIC_ENTITIES;
|
|
|
|
}
|
|
|
|
|
2006-07-29 09:39:48 +04:00
|
|
|
const kWrapColumn = 80;
|
2006-07-29 09:40:22 +04:00
|
|
|
dl.init(aSniffer.uri, persistArgs.target, null, null, null, persist);
|
2006-07-29 09:39:48 +04:00
|
|
|
persist.saveDocument(persistArgs.source, persistArgs.target, filesFolder,
|
|
|
|
persistArgs.contentType, encodingFlags, kWrapColumn);
|
|
|
|
} else {
|
2006-07-29 09:40:22 +04:00
|
|
|
dl.init(source, persistArgs.target, null, null, null, persist);
|
2006-07-29 09:42:04 +04:00
|
|
|
persist.saveURI(source, null, null, persistArgs.postData, null, persistArgs.target);
|
2006-07-29 09:39:48 +04:00
|
|
|
}
|
2006-07-29 09:39:02 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
function nsHeaderSniffer(aURL, aCallback, aData)
|
|
|
|
{
|
|
|
|
this.mCallback = aCallback;
|
|
|
|
this.mData = aData;
|
|
|
|
|
2006-07-29 09:39:48 +04:00
|
|
|
this.uri = makeURL(aURL);
|
2006-07-29 09:39:02 +04:00
|
|
|
|
2006-07-29 09:39:38 +04:00
|
|
|
this.linkChecker = Components.classes["@mozilla.org/network/urichecker;1"]
|
|
|
|
.createInstance().QueryInterface(Components.interfaces.nsIURIChecker);
|
|
|
|
|
|
|
|
var flags;
|
|
|
|
if (aData.bypassCache) {
|
|
|
|
flags = Components.interfaces.nsIRequest.LOAD_BYPASS_CACHE;
|
|
|
|
} else {
|
|
|
|
flags = Components.interfaces.nsIRequest.LOAD_FROM_CACHE;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.linkChecker.asyncCheckURI(aURL, this, null, flags);
|
2006-07-29 09:39:02 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsHeaderSniffer.prototype = {
|
2006-07-29 09:39:53 +04:00
|
|
|
|
|
|
|
// ---------- nsISupports methods ----------
|
|
|
|
QueryInterface: function (iid) {
|
|
|
|
if (!iid.equals(Components.interfaces.nsIRequestObserver) &&
|
|
|
|
!iid.equals(Components.interfaces.nsISupports) &&
|
|
|
|
!iid.equals(Components.interfaces.nsIInterfaceRequestor) &&
|
|
|
|
!iid.equals(Components.interfaces.nsIAuthPrompt)) {
|
|
|
|
throw Components.results.NS_ERROR_NO_INTERFACE;
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
// ---------- nsIInterfaceRequestor methods ----------
|
|
|
|
getInterface : function(iid) {
|
|
|
|
return this.QueryInterface(iid);
|
|
|
|
},
|
|
|
|
|
|
|
|
// ---------- nsIAuthPrompt methods ----------
|
|
|
|
prompt : function(dlgTitle, text, pwrealm, savePW, defaultText, result)
|
|
|
|
{
|
|
|
|
dump("authprompt prompt! pwrealm="+pwrealm+"\n");
|
|
|
|
var promptServ = this.promptService;
|
|
|
|
if (!promptServ)
|
|
|
|
return false;
|
|
|
|
var saveCheck = {value:savePW};
|
|
|
|
return promptServ.prompt(window, dlgTitle, text, defaultText, pwrealm, saveCheck);
|
|
|
|
},
|
|
|
|
promptUsernameAndPassword : function(dlgTitle, text, pwrealm, savePW, user, pw)
|
|
|
|
{
|
|
|
|
dump("authprompt promptUsernameAndPassword! "+dlgTitle+" "+text+", pwrealm="+pwrealm+"\n");
|
|
|
|
var promptServ = this.promptService;
|
|
|
|
if (!promptServ)
|
|
|
|
return false;
|
|
|
|
var saveCheck = {value:savePW};
|
|
|
|
return promptServ.promptUsernameAndPassword(window, dlgTitle, text, user, pw, pwrealm, saveCheck);
|
|
|
|
},
|
|
|
|
promptPassword : function(dlgTitle, text, pwrealm, savePW, pw)
|
|
|
|
{
|
|
|
|
dump("auth promptPassword! "+dlgTitle+" "+text+", pwrealm="+pwrealm+"\n");
|
|
|
|
var promptServ = this.promptService;
|
|
|
|
if (!promptServ)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
var saveCheck = {value:savePW};
|
|
|
|
return promptServ.promptPassword(window, dlgTitle, text, pw, pwrealm, saveCheck);
|
|
|
|
},
|
|
|
|
|
|
|
|
// ---------- nsIRequestObserver methods ----------
|
2006-07-29 09:39:38 +04:00
|
|
|
onStartRequest: function (aRequest, aContext) { },
|
|
|
|
|
|
|
|
onStopRequest: function (aRequest, aContext, aStatus) {
|
|
|
|
try {
|
|
|
|
if (aStatus == 0) { // NS_BINDING_SUCCEEDED, so there's something there
|
|
|
|
var linkChecker = aRequest.QueryInterface(Components.interfaces.nsIURIChecker);
|
|
|
|
var channel = linkChecker.baseRequest.QueryInterface(Components.interfaces.nsIChannel);
|
2006-07-29 09:39:02 +04:00
|
|
|
this.contentType = channel.contentType;
|
|
|
|
try {
|
2006-07-29 09:39:38 +04:00
|
|
|
var httpChannel = channel.QueryInterface(Components.interfaces.nsIHttpChannel);
|
2006-07-29 09:41:23 +04:00
|
|
|
var encodedChannel = channel.QueryInterface(Components.interfaces.nsIEncodedChannel);
|
2006-07-29 09:39:55 +04:00
|
|
|
this.contentEncodingType = null;
|
|
|
|
// There may be content-encodings on the channel. Multiple content
|
|
|
|
// encodings are allowed, eg "Content-Encoding: gzip, uuencode". This
|
|
|
|
// header would mean that the content was first gzipped and then
|
|
|
|
// uuencoded. The encoding enumerator returns MIME types
|
|
|
|
// corresponding to each encoding starting from the end, so the first
|
|
|
|
// thing it returns corresponds to the outermost encoding.
|
2006-07-29 09:41:23 +04:00
|
|
|
var encodingEnumerator = encodedChannel.contentEncodings;
|
2006-07-29 09:42:20 +04:00
|
|
|
if (encodingEnumerator && encodingEnumerator.hasMore()) {
|
2006-07-29 09:39:55 +04:00
|
|
|
try {
|
2006-07-29 09:42:20 +04:00
|
|
|
this.contentEncodingType = encodingEnumerator.getNext();
|
2006-07-29 09:39:55 +04:00
|
|
|
} catch (e) {
|
|
|
|
}
|
|
|
|
}
|
2006-07-29 09:39:02 +04:00
|
|
|
this.mContentDisposition = httpChannel.getResponseHeader("content-disposition");
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
}
|
2006-07-29 09:39:51 +04:00
|
|
|
if (!this.contentType || this.contentType == "application/x-unknown-content-type") {
|
|
|
|
// We didn't get a type from the server. Fall back on other type detection mechanisms
|
|
|
|
throw "Unknown Type";
|
|
|
|
}
|
2006-07-29 09:39:45 +04:00
|
|
|
}
|
|
|
|
else {
|
2006-07-29 09:39:53 +04:00
|
|
|
dump("Error saving link aStatus = 0x" + aStatus.toString(16) + "\n");
|
2006-07-29 09:39:45 +04:00
|
|
|
var bundle = getStringBundle();
|
|
|
|
var errorTitle = bundle.GetStringFromName("saveLinkErrorTitle");
|
|
|
|
var errorMsg = bundle.GetStringFromName("saveLinkErrorMsg");
|
|
|
|
const promptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"].getService(Components.interfaces.nsIPromptService);
|
|
|
|
promptService.alert(this.mData.window, errorTitle, errorMsg);
|
|
|
|
return;
|
2006-07-29 09:39:02 +04:00
|
|
|
}
|
2006-07-29 09:39:38 +04:00
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
if (this.mData.document) {
|
|
|
|
this.contentType = this.mData.document.contentType;
|
|
|
|
} else {
|
2006-07-29 09:42:24 +04:00
|
|
|
var type = getMIMETypeForURI(this.uri);
|
|
|
|
if (type)
|
|
|
|
this.contentType = type;
|
2006-07-29 09:39:02 +04:00
|
|
|
}
|
|
|
|
}
|
2006-07-29 09:39:38 +04:00
|
|
|
this.mCallback(this, this.mData);
|
2006-07-29 09:39:02 +04:00
|
|
|
},
|
2006-07-29 09:39:53 +04:00
|
|
|
|
|
|
|
// ------------------------------------------------
|
|
|
|
|
|
|
|
get promptService()
|
|
|
|
{
|
|
|
|
var promptSvc;
|
|
|
|
try {
|
|
|
|
promptSvc = Components.classes["@mozilla.org/embedcomp/prompt-service;1"].getService();
|
|
|
|
promptSvc = promptSvc.QueryInterface(Components.interfaces.nsIPromptService);
|
|
|
|
}
|
|
|
|
catch (e) {}
|
|
|
|
return promptSvc;
|
|
|
|
},
|
|
|
|
|
2006-07-29 09:39:02 +04:00
|
|
|
get suggestedFileName()
|
2006-07-29 09:33:03 +04:00
|
|
|
{
|
2006-07-29 09:42:13 +04:00
|
|
|
var fileName = "";
|
|
|
|
|
|
|
|
if (this.mContentDisposition) {
|
|
|
|
const mhpContractID = "@mozilla.org/network/mime-hdrparam;1"
|
|
|
|
const mhpIID = Components.interfaces.nsIMIMEHeaderParam;
|
|
|
|
const mhp = Components.classes[mhpContractID].getService(mhpIID);
|
|
|
|
var dummy = { value: null }; // To make JS engine happy.
|
|
|
|
var charset = getCharsetforSave(null);
|
|
|
|
|
|
|
|
try {
|
|
|
|
fileName = mhp.getParameter(this.mContentDisposition, "filename", charset, true, dummy);
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
try {
|
|
|
|
fileName = mhp.getParameter(this.mContentDisposition, "name", charset, true, dummy);
|
|
|
|
}
|
|
|
|
catch (e) {
|
2006-07-29 09:39:02 +04:00
|
|
|
}
|
|
|
|
}
|
2006-07-29 09:33:03 +04:00
|
|
|
}
|
2006-07-29 09:39:02 +04:00
|
|
|
|
2006-07-29 09:42:13 +04:00
|
|
|
fileName = fileName.replace(/^"|"$/g, "");
|
|
|
|
return fileName;
|
|
|
|
}
|
2006-07-29 09:39:02 +04:00
|
|
|
};
|
|
|
|
|
2006-07-29 09:42:05 +04:00
|
|
|
// We have no DOM, and can only save the URL as is.
|
|
|
|
const SAVEMODE_FILEONLY = 0x00;
|
|
|
|
// We have a DOM and can save as complete.
|
|
|
|
const SAVEMODE_COMPLETE_DOM = 0x01;
|
|
|
|
// We have a DOM which we can serialize as text.
|
|
|
|
const SAVEMODE_COMPLETE_TEXT = 0x02;
|
|
|
|
|
|
|
|
// If we are able to save a complete DOM, the 'save as complete' filter
|
|
|
|
// must be the first filter appended. The 'save page only' counterpart
|
|
|
|
// must be the second filter appended. And the 'save as complete text'
|
|
|
|
// filter must be the third filter appended.
|
2006-07-29 09:39:02 +04:00
|
|
|
function appendFiltersForContentType(aFilePicker, aContentType, aSaveMode)
|
|
|
|
{
|
|
|
|
var bundle = getStringBundle();
|
2006-07-29 09:42:05 +04:00
|
|
|
// The bundle name for saving only a specific content type.
|
|
|
|
var bundleName;
|
|
|
|
// The corresponding filter string for a specific content type.
|
|
|
|
var filterString;
|
|
|
|
|
|
|
|
// XXX all the cases that are handled explicitly here MUST be handled
|
|
|
|
// in GetSaveModeForContentType to return a non-fileonly filter.
|
2006-07-29 09:39:02 +04:00
|
|
|
switch (aContentType) {
|
|
|
|
case "text/html":
|
2006-07-29 09:42:05 +04:00
|
|
|
bundleName = "WebPageHTMLOnlyFilter";
|
|
|
|
filterString = "*.htm; *.html";
|
2006-07-29 09:39:02 +04:00
|
|
|
break;
|
2006-07-29 09:42:03 +04:00
|
|
|
|
|
|
|
case "application/xhtml+xml":
|
2006-07-29 09:42:05 +04:00
|
|
|
bundleName = "WebPageXHTMLOnlyFilter";
|
|
|
|
filterString = "*.xht; *.xhtml";
|
2006-07-29 09:42:03 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case "text/xml":
|
|
|
|
case "application/xml":
|
2006-07-29 09:42:05 +04:00
|
|
|
bundleName = "WebPageXMLOnlyFilter";
|
|
|
|
filterString = "*.xml";
|
2006-07-29 09:42:03 +04:00
|
|
|
break;
|
|
|
|
|
2006-07-29 09:39:02 +04:00
|
|
|
default:
|
2006-07-29 09:42:05 +04:00
|
|
|
if (aSaveMode != SAVEMODE_FILEONLY) {
|
|
|
|
throw "Invalid save mode for type '" + aContentType + "'";
|
|
|
|
}
|
|
|
|
|
2006-07-29 09:39:02 +04:00
|
|
|
var mimeInfo = getMIMEInfoForType(aContentType);
|
|
|
|
if (mimeInfo) {
|
2006-07-29 09:42:12 +04:00
|
|
|
|
|
|
|
var extEnumerator = mimeInfo.getFileExtensions();
|
2006-07-29 09:42:05 +04:00
|
|
|
|
2006-07-29 09:39:02 +04:00
|
|
|
var extString = "";
|
2006-07-29 09:42:12 +04:00
|
|
|
while (extEnumerator.hasMore()) {
|
|
|
|
var extension = extEnumerator.getNext();
|
|
|
|
if (extString)
|
|
|
|
extString += "; "; // If adding more than one extension,
|
|
|
|
// separate by semi-colon
|
|
|
|
extString += "*." + extension;
|
2006-07-29 09:39:02 +04:00
|
|
|
}
|
2006-07-29 09:42:12 +04:00
|
|
|
|
|
|
|
if (extString) {
|
2006-07-29 09:39:39 +04:00
|
|
|
aFilePicker.appendFilter(mimeInfo.Description, extString);
|
2006-07-29 09:42:05 +04:00
|
|
|
}
|
2006-07-29 09:33:03 +04:00
|
|
|
}
|
2006-07-29 09:42:05 +04:00
|
|
|
|
2006-07-29 09:39:02 +04:00
|
|
|
break;
|
|
|
|
}
|
2006-07-29 09:42:05 +04:00
|
|
|
|
|
|
|
if (aSaveMode & SAVEMODE_COMPLETE_DOM) {
|
|
|
|
aFilePicker.appendFilter(bundle.GetStringFromName("WebPageCompleteFilter"), filterString);
|
|
|
|
// We should always offer a choice to save document only if
|
|
|
|
// we allow saving as complete.
|
|
|
|
aFilePicker.appendFilter(bundle.GetStringFromName(bundleName), filterString);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aSaveMode & SAVEMODE_COMPLETE_TEXT) {
|
|
|
|
aFilePicker.appendFilters(Components.interfaces.nsIFilePicker.filterText);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Always append the all files (*) filter
|
|
|
|
aFilePicker.appendFilters(Components.interfaces.nsIFilePicker.filterAll);
|
2006-07-29 09:39:02 +04:00
|
|
|
}
|
2006-07-29 09:33:03 +04:00
|
|
|
|
2006-07-29 09:39:02 +04:00
|
|
|
function getPostData()
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
var sessionHistory = getWebNavigation().sessionHistory;
|
|
|
|
entry = sessionHistory.getEntryAtIndex(sessionHistory.index, false);
|
|
|
|
entry = entry.QueryInterface(Components.interfaces.nsISHEntry);
|
|
|
|
return entry.postData;
|
|
|
|
}
|
|
|
|
catch (e) {
|
2006-07-29 09:33:12 +04:00
|
|
|
}
|
2006-07-29 09:39:02 +04:00
|
|
|
return null;
|
|
|
|
}
|
2006-07-29 09:33:12 +04:00
|
|
|
|
2006-07-29 09:39:02 +04:00
|
|
|
function getStringBundle()
|
|
|
|
{
|
|
|
|
const bundleURL = "chrome://communicator/locale/contentAreaCommands.properties";
|
|
|
|
|
|
|
|
const sbsContractID = "@mozilla.org/intl/stringbundle;1";
|
|
|
|
const sbsIID = Components.interfaces.nsIStringBundleService;
|
|
|
|
const sbs = Components.classes[sbsContractID].getService(sbsIID);
|
|
|
|
|
|
|
|
const lsContractID = "@mozilla.org/intl/nslocaleservice;1";
|
|
|
|
const lsIID = Components.interfaces.nsILocaleService;
|
|
|
|
const ls = Components.classes[lsContractID].getService(lsIID);
|
2006-07-29 09:42:22 +04:00
|
|
|
var appLocale = ls.getApplicationLocale();
|
2006-07-29 09:39:02 +04:00
|
|
|
return sbs.createBundle(bundleURL, appLocale);
|
|
|
|
}
|
2006-07-29 09:33:12 +04:00
|
|
|
|
2006-07-29 09:39:02 +04:00
|
|
|
function makeWebBrowserPersist()
|
|
|
|
{
|
|
|
|
const persistContractID = "@mozilla.org/embedding/browser/nsWebBrowserPersist;1";
|
|
|
|
const persistIID = Components.interfaces.nsIWebBrowserPersist;
|
|
|
|
return Components.classes[persistContractID].createInstance(persistIID);
|
|
|
|
}
|
|
|
|
|
2006-07-29 09:39:48 +04:00
|
|
|
function makeProgressDialog()
|
|
|
|
{
|
|
|
|
const progressDialogContractID = "@mozilla.org/progressdialog;1";
|
|
|
|
const progressDialogIID = Components.interfaces.nsIProgressDialog;
|
|
|
|
return Components.classes[progressDialogContractID].createInstance(progressDialogIID);
|
|
|
|
}
|
|
|
|
|
|
|
|
function makeURL(aURL)
|
|
|
|
{
|
2006-07-29 09:39:54 +04:00
|
|
|
var ioService = Components.classes["@mozilla.org/network/io-service;1"]
|
|
|
|
.getService(Components.interfaces.nsIIOService);
|
|
|
|
return ioService.newURI(aURL, null, null);
|
|
|
|
|
2006-07-29 09:39:48 +04:00
|
|
|
}
|
|
|
|
|
2006-07-29 09:39:02 +04:00
|
|
|
function makeFilePicker()
|
|
|
|
{
|
|
|
|
const fpContractID = "@mozilla.org/filepicker;1";
|
|
|
|
const fpIID = Components.interfaces.nsIFilePicker;
|
|
|
|
return Components.classes[fpContractID].createInstance(fpIID);
|
|
|
|
}
|
|
|
|
|
|
|
|
function makeTempFile()
|
|
|
|
{
|
|
|
|
const mimeTypes = "TmpD";
|
|
|
|
const flContractID = "@mozilla.org/file/directory_service;1";
|
|
|
|
const flIID = Components.interfaces.nsIProperties;
|
|
|
|
var fileLocator = Components.classes[flContractID].getService(flIID);
|
|
|
|
var tempFile = fileLocator.get(mimeTypes, Components.interfaces.nsIFile);
|
|
|
|
tempFile.append("~sav" + Math.floor(Math.random() * 1000) + ".tmp");
|
|
|
|
return tempFile;
|
|
|
|
}
|
|
|
|
|
2006-07-29 09:39:38 +04:00
|
|
|
function getMIMEService()
|
2006-07-29 09:39:02 +04:00
|
|
|
{
|
2006-07-29 09:39:37 +04:00
|
|
|
const mimeSvcContractID = "@mozilla.org/mime;1";
|
2006-07-29 09:39:02 +04:00
|
|
|
const mimeSvcIID = Components.interfaces.nsIMIMEService;
|
2006-07-29 09:39:37 +04:00
|
|
|
const mimeSvc = Components.classes[mimeSvcContractID].getService(mimeSvcIID);
|
2006-07-29 09:39:38 +04:00
|
|
|
return mimeSvc;
|
|
|
|
}
|
|
|
|
|
2006-07-29 09:42:24 +04:00
|
|
|
function getMIMETypeForURI(aURI)
|
2006-07-29 09:39:38 +04:00
|
|
|
{
|
|
|
|
try {
|
2006-07-29 09:42:24 +04:00
|
|
|
return getMIMEService().GetTypeFromURI(aURI);
|
2006-07-29 09:39:38 +04:00
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2006-07-29 09:39:02 +04:00
|
|
|
|
2006-07-29 09:39:38 +04:00
|
|
|
function getMIMEInfoForType(aMIMEType)
|
|
|
|
{
|
2006-07-29 09:39:02 +04:00
|
|
|
try {
|
2006-07-29 09:42:16 +04:00
|
|
|
return getMIMEService().GetFromTypeAndExtension(aMIMEType, null);
|
2006-07-29 09:39:02 +04:00
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getDefaultFileName(aDefaultFileName, aNameFromHeaders, aDocumentURI, aDocument)
|
|
|
|
{
|
|
|
|
if (aNameFromHeaders)
|
2006-07-29 09:41:17 +04:00
|
|
|
// 1) Use the name suggested by the HTTP headers
|
|
|
|
return validateFileName(aNameFromHeaders);
|
2006-07-29 09:39:02 +04:00
|
|
|
|
2006-07-29 09:40:26 +04:00
|
|
|
try {
|
|
|
|
var url = aDocumentURI.QueryInterface(Components.interfaces.nsIURL);
|
2006-07-29 09:41:19 +04:00
|
|
|
if (url.fileName != "") {
|
2006-07-29 09:41:17 +04:00
|
|
|
// 2) Use the actual file name, if present
|
2006-07-29 09:42:18 +04:00
|
|
|
return validateFileName(unescape(url.fileName));
|
2006-07-29 09:41:19 +04:00
|
|
|
}
|
2006-07-29 09:40:26 +04:00
|
|
|
} catch (e) {
|
2006-07-29 09:41:22 +04:00
|
|
|
try {
|
|
|
|
// the file name might be non ASCII
|
|
|
|
// try unescape again with a characterSet
|
|
|
|
var textToSubURI = Components.classes["@mozilla.org/intl/texttosuburi;1"]
|
|
|
|
.getService(Components.interfaces.nsITextToSubURI);
|
2006-07-29 09:42:13 +04:00
|
|
|
var charset = getCharsetforSave(aDocument);
|
2006-07-29 09:41:22 +04:00
|
|
|
return textToSubURI.unEscapeURIForUI(charset, url.fileName);
|
|
|
|
} catch (e) {
|
|
|
|
// This is something like a wyciwyg:, data:, and so forth
|
|
|
|
// URI... no usable filename here.
|
|
|
|
}
|
2006-07-29 09:40:26 +04:00
|
|
|
}
|
2006-07-29 09:39:02 +04:00
|
|
|
|
2006-07-29 09:41:19 +04:00
|
|
|
if (aDocument) {
|
2006-07-29 09:42:23 +04:00
|
|
|
var docTitle = GenerateValidFilename(aDocument.title, "");
|
2006-07-29 09:39:15 +04:00
|
|
|
|
2006-07-29 09:42:23 +04:00
|
|
|
if (docTitle) {
|
2006-07-29 09:41:19 +04:00
|
|
|
// 3) Use the document title
|
2006-07-29 09:41:20 +04:00
|
|
|
return docTitle;
|
2006-07-29 09:41:19 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-29 09:39:15 +04:00
|
|
|
if (aDefaultFileName)
|
2006-07-29 09:41:17 +04:00
|
|
|
// 4) Use the caller-provided name, if any
|
|
|
|
return validateFileName(aDefaultFileName);
|
2006-07-29 09:39:15 +04:00
|
|
|
|
2006-07-29 09:42:14 +04:00
|
|
|
// 5) If this is a directory, use the last directory name
|
2006-07-29 09:42:15 +04:00
|
|
|
var path = aDocumentURI.path.match(/\/([^\/]+)\/$/);
|
2006-07-29 09:42:14 +04:00
|
|
|
if (path && path.length > 1) {
|
|
|
|
return validateFileName(path[1]);
|
|
|
|
}
|
|
|
|
|
2006-07-29 09:41:17 +04:00
|
|
|
try {
|
|
|
|
if (aDocumentURI.host)
|
2006-07-29 09:42:14 +04:00
|
|
|
// 6) Use the host.
|
2006-07-29 09:41:17 +04:00
|
|
|
return aDocumentURI.host;
|
|
|
|
} catch (e) {
|
|
|
|
// Some files have no information at all, like Javascript generated pages
|
|
|
|
}
|
|
|
|
try {
|
2006-07-29 09:42:14 +04:00
|
|
|
// 7) Use the default file name
|
2006-07-29 09:41:17 +04:00
|
|
|
return getStringBundle().GetStringFromName("DefaultSaveFileName");
|
|
|
|
} catch (e) {
|
|
|
|
//in case localized string cannot be found
|
|
|
|
}
|
2006-07-29 09:42:14 +04:00
|
|
|
// 8) If all else fails, use "index"
|
2006-07-29 09:41:17 +04:00
|
|
|
return "index";
|
2006-07-29 09:39:02 +04:00
|
|
|
}
|
|
|
|
|
2006-07-29 09:39:46 +04:00
|
|
|
function getNormalizedLeafName(aFile, aDefaultExtension)
|
2006-07-29 09:39:02 +04:00
|
|
|
{
|
2006-07-29 09:39:46 +04:00
|
|
|
if (!aDefaultExtension)
|
|
|
|
return aFile;
|
|
|
|
|
|
|
|
// Fix up the file name we're saving to to include the default extension
|
|
|
|
const stdURLContractID = "@mozilla.org/network/standard-url;1";
|
|
|
|
const stdURLIID = Components.interfaces.nsIURL;
|
|
|
|
var url = Components.classes[stdURLContractID].createInstance(stdURLIID);
|
|
|
|
url.filePath = aFile;
|
|
|
|
|
|
|
|
if (url.fileExtension != aDefaultExtension) {
|
|
|
|
return aFile + "." + aDefaultExtension;
|
|
|
|
}
|
|
|
|
|
|
|
|
return aFile;
|
|
|
|
}
|
2006-07-29 09:39:02 +04:00
|
|
|
|
2006-07-29 09:39:46 +04:00
|
|
|
function getDefaultExtension(aFilename, aURI, aContentType)
|
|
|
|
{
|
2006-07-29 09:40:27 +04:00
|
|
|
if (aContentType == "text/plain" || aContentType == "application/octet-stream" || aURI.scheme == "ftp")
|
|
|
|
return ""; // temporary fix for bug 120327
|
|
|
|
|
2006-07-29 09:39:46 +04:00
|
|
|
// This mirrors some code in nsExternalHelperAppService::DoContent
|
|
|
|
// Use the filename first and then the URI if that fails
|
|
|
|
|
2006-07-29 09:39:02 +04:00
|
|
|
var mimeInfo = getMIMEInfoForType(aContentType);
|
|
|
|
|
2006-07-29 09:39:46 +04:00
|
|
|
// First try the extension from the filename
|
|
|
|
const stdURLContractID = "@mozilla.org/network/standard-url;1";
|
|
|
|
const stdURLIID = Components.interfaces.nsIURL;
|
|
|
|
var url = Components.classes[stdURLContractID].createInstance(stdURLIID);
|
|
|
|
url.filePath = aFilename;
|
|
|
|
|
|
|
|
var ext = url.fileExtension;
|
|
|
|
|
2006-07-29 09:39:49 +04:00
|
|
|
if (ext && mimeInfo && mimeInfo.ExtensionExists(ext)) {
|
2006-07-29 09:39:46 +04:00
|
|
|
return ext;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Well, that failed. Now try the extension from the URI
|
|
|
|
var urlext;
|
|
|
|
try {
|
|
|
|
url = aURI.QueryInterface(Components.interfaces.nsIURL);
|
|
|
|
urlext = url.fileExtension;
|
|
|
|
} catch (e) {
|
|
|
|
}
|
|
|
|
|
2006-07-29 09:39:49 +04:00
|
|
|
if (urlext && mimeInfo && mimeInfo.ExtensionExists(urlext)) {
|
2006-07-29 09:39:46 +04:00
|
|
|
return urlext;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
try {
|
|
|
|
return mimeInfo.primaryExtension;
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
// Fall back on the extensions in the filename and URI for lack
|
|
|
|
// of anything better.
|
|
|
|
return ext || urlext;
|
|
|
|
}
|
2006-07-29 09:33:12 +04:00
|
|
|
}
|
2006-07-29 09:39:02 +04:00
|
|
|
}
|
2006-07-29 09:33:14 +04:00
|
|
|
|
2006-07-29 09:42:05 +04:00
|
|
|
function GetSaveModeForContentType(aContentType)
|
2006-07-29 09:39:15 +04:00
|
|
|
{
|
2006-07-29 09:42:05 +04:00
|
|
|
var saveMode = SAVEMODE_FILEONLY;
|
2006-07-29 09:39:15 +04:00
|
|
|
switch (aContentType) {
|
|
|
|
case "text/html":
|
|
|
|
case "application/xhtml+xml":
|
2006-07-29 09:42:05 +04:00
|
|
|
saveMode |= SAVEMODE_COMPLETE_TEXT;
|
|
|
|
// Fall through
|
|
|
|
case "text/xml":
|
2006-07-29 09:40:24 +04:00
|
|
|
case "application/xml":
|
2006-07-29 09:42:05 +04:00
|
|
|
saveMode |= SAVEMODE_COMPLETE_DOM;
|
|
|
|
break;
|
2006-07-29 09:39:15 +04:00
|
|
|
}
|
2006-07-29 09:42:05 +04:00
|
|
|
|
|
|
|
return saveMode;
|
2006-07-29 09:39:15 +04:00
|
|
|
}
|
2006-07-29 09:42:13 +04:00
|
|
|
|
|
|
|
function getCharsetforSave(aDocument)
|
|
|
|
{
|
|
|
|
if (aDocument)
|
|
|
|
return aDocument.characterSet;
|
|
|
|
|
|
|
|
if (document.commandDispatcher.focusedWindow)
|
|
|
|
return document.commandDispatcher.focusedWindow.document.characterSet;
|
|
|
|
|
|
|
|
return window._content.document.characterSet;
|
|
|
|
}
|