2012-07-20 22:54:41 -07:00
|
|
|
# -*- Mode: javascript; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
2012-05-21 04:12:37 -07:00
|
|
|
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-10-18 07:44:36 -07:00
|
|
|
Components.utils.import("resource://gre/modules/Services.jsm");
|
2012-10-03 10:10:20 -07:00
|
|
|
Components.utils.import("resource://gre/modules/PrivateBrowsingUtils.jsm");
|
|
|
|
|
2009-07-28 22:55:03 -07:00
|
|
|
var ContentAreaUtils = {
|
2012-10-18 07:44:36 -07:00
|
|
|
|
|
|
|
// this is for backwards compatibility.
|
2009-07-28 22:55:03 -07:00
|
|
|
get ioService() {
|
2012-10-18 07:44:36 -07:00
|
|
|
return Services.io;
|
2010-03-25 06:16:20 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
get stringBundle() {
|
|
|
|
delete this.stringBundle;
|
|
|
|
return this.stringBundle =
|
2012-10-18 07:44:36 -07:00
|
|
|
Services.strings.createBundle("chrome://global/locale/contentAreaCommands.properties");
|
2009-07-28 22:55:03 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/**
|
|
|
|
* urlSecurityCheck: JavaScript wrapper for checkLoadURIWithPrincipal
|
|
|
|
* and checkLoadURIStrWithPrincipal.
|
|
|
|
* If |aPrincipal| is not allowed to link to |aURL|, this function throws with
|
|
|
|
* an error message.
|
|
|
|
*
|
|
|
|
* @param aURL
|
|
|
|
* The URL a page has linked to. This could be passed either as a string
|
|
|
|
* or as a nsIURI object.
|
|
|
|
* @param aPrincipal
|
|
|
|
* The principal of the document from which aURL came.
|
|
|
|
* @param aFlags
|
|
|
|
* Flags to be passed to checkLoadURIStr. If undefined,
|
|
|
|
* nsIScriptSecurityManager.STANDARD will be passed.
|
|
|
|
*/
|
|
|
|
function urlSecurityCheck(aURL, aPrincipal, aFlags)
|
|
|
|
{
|
2012-10-18 07:44:36 -07:00
|
|
|
var secMan = Services.scriptSecurityManager;
|
|
|
|
if (aFlags === undefined) {
|
|
|
|
aFlags = secMan.STANDARD;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
try {
|
|
|
|
if (aURL instanceof Components.interfaces.nsIURI)
|
|
|
|
secMan.checkLoadURIWithPrincipal(aPrincipal, aURL, aFlags);
|
|
|
|
else
|
|
|
|
secMan.checkLoadURIStrWithPrincipal(aPrincipal, aURL, aFlags);
|
|
|
|
} catch (e) {
|
2010-12-22 03:10:00 -08:00
|
|
|
let principalStr = "";
|
|
|
|
try {
|
|
|
|
principalStr = " from " + aPrincipal.URI.spec;
|
|
|
|
}
|
|
|
|
catch(e2) { }
|
|
|
|
|
|
|
|
throw "Load of " + aURL + principalStr + " denied.";
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-11 01:57:36 -08:00
|
|
|
/**
|
|
|
|
* Determine whether or not a given focused DOMWindow is in the content area.
|
|
|
|
**/
|
2007-03-22 10:30:00 -07:00
|
|
|
function isContentFrame(aFocusedWindow)
|
|
|
|
{
|
|
|
|
if (!aFocusedWindow)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return (aFocusedWindow.top == window.content);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-07-27 01:47:05 -07:00
|
|
|
// Clientele: (Make sure you don't break any of these)
|
2007-03-22 10:30:00 -07:00
|
|
|
// - File -> Save Page/Frame As...
|
|
|
|
// - Context -> Save Page/Frame As...
|
|
|
|
// - Context -> Save Link As...
|
|
|
|
// - Alt-Click links in web pages
|
|
|
|
// - Alt-Click links in the UI
|
|
|
|
//
|
|
|
|
// 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
|
2007-12-11 01:57:36 -08:00
|
|
|
// the above method.
|
2007-03-22 10:30:00 -07:00
|
|
|
// - A linked document using Save Link As...
|
|
|
|
// - A linked document using Alt-click Save Link As...
|
|
|
|
//
|
|
|
|
function saveURL(aURL, aFileName, aFilePickerTitleKey, aShouldBypassCache,
|
2012-10-03 10:10:20 -07:00
|
|
|
aSkipPrompt, aReferrer, aSourceDocument)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
internalSave(aURL, null, aFileName, null, null, aShouldBypassCache,
|
2012-10-03 10:10:20 -07:00
|
|
|
aFilePickerTitleKey, null, aReferrer, aSourceDocument,
|
|
|
|
aSkipPrompt, null);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Just like saveURL, but will get some info off the image before
|
|
|
|
// calling internalSave
|
2009-07-27 01:47:05 -07:00
|
|
|
// Clientele: (Make sure you don't break any of these)
|
2007-03-22 10:30:00 -07:00
|
|
|
// - Context -> Save Image As...
|
|
|
|
const imgICache = Components.interfaces.imgICache;
|
|
|
|
const nsISupportsCString = Components.interfaces.nsISupportsCString;
|
|
|
|
|
|
|
|
function saveImageURL(aURL, aFileName, aFilePickerTitleKey, aShouldBypassCache,
|
2012-09-17 09:18:40 -07:00
|
|
|
aSkipPrompt, aReferrer, aDoc)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
var contentType = null;
|
|
|
|
var contentDisposition = null;
|
|
|
|
if (!aShouldBypassCache) {
|
|
|
|
try {
|
2012-09-17 09:18:40 -07:00
|
|
|
var imageCache = Components.classes["@mozilla.org/image/tools;1"]
|
|
|
|
.getService(Components.interfaces.imgITools)
|
|
|
|
.getImgCacheForDocument(aDoc);
|
2007-03-22 10:30:00 -07:00
|
|
|
var props =
|
|
|
|
imageCache.findEntryProperties(makeURI(aURL, getCharsetforSave(null)));
|
|
|
|
if (props) {
|
|
|
|
contentType = props.get("type", nsISupportsCString);
|
|
|
|
contentDisposition = props.get("content-disposition",
|
|
|
|
nsISupportsCString);
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
// Failure to get type and content-disposition off the image is non-fatal
|
|
|
|
}
|
|
|
|
}
|
|
|
|
internalSave(aURL, null, aFileName, contentDisposition, contentType,
|
2009-02-19 11:18:02 -08:00
|
|
|
aShouldBypassCache, aFilePickerTitleKey, null, aReferrer,
|
2012-10-03 10:10:20 -07:00
|
|
|
aDoc, aSkipPrompt, null);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function saveDocument(aDocument, aSkipPrompt)
|
|
|
|
{
|
|
|
|
if (!aDocument)
|
|
|
|
throw "Must have a document when calling saveDocument";
|
|
|
|
|
|
|
|
// We want to use cached data because the document is currently visible.
|
2009-02-19 11:18:02 -08:00
|
|
|
var ifreq =
|
|
|
|
aDocument.defaultView
|
|
|
|
.QueryInterface(Components.interfaces.nsIInterfaceRequestor);
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
var contentDisposition = null;
|
|
|
|
try {
|
|
|
|
contentDisposition =
|
2009-02-19 11:18:02 -08:00
|
|
|
ifreq.getInterface(Components.interfaces.nsIDOMWindowUtils)
|
2009-02-26 12:40:26 -08:00
|
|
|
.getDocumentMetadata("content-disposition");
|
2007-03-22 10:30:00 -07:00
|
|
|
} catch (ex) {
|
|
|
|
// Failure to get a content-disposition is ok
|
|
|
|
}
|
2009-02-19 11:18:02 -08:00
|
|
|
|
|
|
|
var cacheKey = null;
|
|
|
|
try {
|
|
|
|
cacheKey =
|
|
|
|
ifreq.getInterface(Components.interfaces.nsIWebNavigation)
|
|
|
|
.QueryInterface(Components.interfaces.nsIWebPageDescriptor);
|
|
|
|
} catch (ex) {
|
|
|
|
// We might not find it in the cache. Oh, well.
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
internalSave(aDocument.location.href, aDocument, null, contentDisposition,
|
2007-11-26 16:25:43 -08:00
|
|
|
aDocument.contentType, false, null, null,
|
|
|
|
aDocument.referrer ? makeURI(aDocument.referrer) : null,
|
2012-10-03 10:10:20 -07:00
|
|
|
aDocument, aSkipPrompt, cacheKey);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2007-07-15 17:02:09 -07:00
|
|
|
function DownloadListener(win, transfer) {
|
|
|
|
function makeClosure(name) {
|
|
|
|
return function() {
|
|
|
|
transfer[name].apply(transfer, arguments);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.window = win;
|
|
|
|
|
|
|
|
// Now... we need to forward all calls to our transfer
|
|
|
|
for (var i in transfer) {
|
|
|
|
if (i != "QueryInterface")
|
|
|
|
this[i] = makeClosure(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DownloadListener.prototype = {
|
|
|
|
QueryInterface: function dl_qi(aIID)
|
|
|
|
{
|
|
|
|
if (aIID.equals(Components.interfaces.nsIInterfaceRequestor) ||
|
|
|
|
aIID.equals(Components.interfaces.nsIWebProgressListener) ||
|
|
|
|
aIID.equals(Components.interfaces.nsIWebProgressListener2) ||
|
|
|
|
aIID.equals(Components.interfaces.nsISupports)) {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
throw Components.results.NS_ERROR_NO_INTERFACE;
|
|
|
|
},
|
|
|
|
|
|
|
|
getInterface: function dl_gi(aIID)
|
|
|
|
{
|
|
|
|
if (aIID.equals(Components.interfaces.nsIAuthPrompt) ||
|
|
|
|
aIID.equals(Components.interfaces.nsIAuthPrompt2)) {
|
|
|
|
var ww =
|
|
|
|
Components.classes["@mozilla.org/embedcomp/window-watcher;1"]
|
|
|
|
.getService(Components.interfaces.nsIPromptFactory);
|
|
|
|
return ww.getPrompt(this.window, aIID);
|
|
|
|
}
|
|
|
|
|
|
|
|
throw Components.results.NS_ERROR_NO_INTERFACE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-11 01:57:36 -08:00
|
|
|
const kSaveAsType_Complete = 0; // Save document with attached objects.
|
|
|
|
// const kSaveAsType_URL = 1; // Save document or URL by itself.
|
|
|
|
const kSaveAsType_Text = 2; // Save document, converting to plain text.
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/**
|
2009-03-30 11:47:31 -07:00
|
|
|
* internalSave: Used when saving a document or URL.
|
|
|
|
*
|
|
|
|
* If aChosenData is null, this method:
|
|
|
|
* - Determines a local target filename to use
|
2007-03-22 10:30:00 -07:00
|
|
|
* - Prompts the user to confirm the destination filename and save mode
|
2009-03-30 11:47:31 -07:00
|
|
|
* (aContentType affects this)
|
|
|
|
* - [Note] This process involves the parameters aURL, aReferrer (to determine
|
|
|
|
* how aURL was encoded), aDocument, aDefaultFileName, aFilePickerTitleKey,
|
|
|
|
* and aSkipPrompt.
|
|
|
|
*
|
|
|
|
* If aChosenData is non-null, this method:
|
|
|
|
* - Uses the provided source URI and save file name
|
|
|
|
* - Saves the document as complete DOM if possible (aDocument present and
|
|
|
|
* right aContentType)
|
|
|
|
* - [Note] The parameters aURL, aDefaultFileName, aFilePickerTitleKey, and
|
|
|
|
* aSkipPrompt are ignored.
|
|
|
|
*
|
|
|
|
* In any case, this method:
|
2007-03-22 10:30:00 -07:00
|
|
|
* - Creates a 'Persist' object (which will perform the saving in the
|
|
|
|
* background) and then starts it.
|
2009-03-30 11:47:31 -07:00
|
|
|
* - [Note] This part of the process only involves the parameters aDocument,
|
|
|
|
* aShouldBypassCache and aReferrer. The source, the save name and the save
|
|
|
|
* mode are the ones determined previously.
|
2007-03-22 10:30:00 -07:00
|
|
|
*
|
2008-11-15 15:19:10 -08:00
|
|
|
* @param aURL
|
|
|
|
* The String representation of the URL of the document being saved
|
|
|
|
* @param aDocument
|
|
|
|
* The document to be saved
|
|
|
|
* @param aDefaultFileName
|
|
|
|
* The caller-provided suggested filename if we don't
|
2007-03-22 10:30:00 -07:00
|
|
|
* find a better one
|
2008-11-15 15:19:10 -08:00
|
|
|
* @param aContentDisposition
|
|
|
|
* The caller-provided content-disposition header to use.
|
|
|
|
* @param aContentType
|
|
|
|
* The caller-provided content-type to use
|
|
|
|
* @param aShouldBypassCache
|
|
|
|
* If true, the document will always be refetched from the server
|
|
|
|
* @param aFilePickerTitleKey
|
|
|
|
* Alternate title for the file picker
|
|
|
|
* @param aChosenData
|
|
|
|
* If non-null this contains an instance of object AutoChosen (see below)
|
|
|
|
* which holds pre-determined data so that the user does not need to be
|
|
|
|
* prompted for a target filename.
|
|
|
|
* @param aReferrer
|
|
|
|
* the referrer URI object (not URL string) to use, or null
|
|
|
|
* if no referrer should be sent.
|
2012-10-03 10:10:20 -07:00
|
|
|
* @param aInitiatingDocument
|
|
|
|
* The document from which the save was initiated.
|
2008-11-15 15:19:10 -08:00
|
|
|
* @param aSkipPrompt [optional]
|
|
|
|
* If set to true, we will attempt to save the file to the
|
|
|
|
* default downloads folder without prompting.
|
2009-02-19 11:18:02 -08:00
|
|
|
* @param aCacheKey [optional]
|
|
|
|
* If set will be passed to saveURI. See nsIWebBrowserPersist for
|
|
|
|
* allowed values.
|
2007-03-22 10:30:00 -07:00
|
|
|
*/
|
|
|
|
function internalSave(aURL, aDocument, aDefaultFileName, aContentDisposition,
|
|
|
|
aContentType, aShouldBypassCache, aFilePickerTitleKey,
|
2012-10-03 10:10:20 -07:00
|
|
|
aChosenData, aReferrer, aInitiatingDocument, aSkipPrompt,
|
|
|
|
aCacheKey)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
if (aSkipPrompt == undefined)
|
|
|
|
aSkipPrompt = false;
|
|
|
|
|
2009-02-19 11:18:02 -08:00
|
|
|
if (aCacheKey == undefined)
|
|
|
|
aCacheKey = null;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// Note: aDocument == null when this code is used by save-link-as...
|
2009-03-30 11:47:31 -07:00
|
|
|
var saveMode = GetSaveModeForContentType(aContentType, aDocument);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-03-30 11:47:31 -07:00
|
|
|
var file, sourceURI, saveAsType;
|
2007-03-22 10:30:00 -07:00
|
|
|
// Find the URI object for aURL and the FileName/Extension to use when saving.
|
|
|
|
// FileName/Extension will be ignored if aChosenData supplied.
|
2009-03-30 11:47:31 -07:00
|
|
|
if (aChosenData) {
|
2007-03-22 10:30:00 -07:00
|
|
|
file = aChosenData.file;
|
2009-03-30 11:47:31 -07:00
|
|
|
sourceURI = aChosenData.uri;
|
|
|
|
saveAsType = kSaveAsType_Complete;
|
2013-03-31 23:13:37 -07:00
|
|
|
|
|
|
|
continueSave();
|
2009-03-30 11:47:31 -07:00
|
|
|
} else {
|
2007-03-22 10:30:00 -07:00
|
|
|
var charset = null;
|
|
|
|
if (aDocument)
|
|
|
|
charset = aDocument.characterSet;
|
|
|
|
else if (aReferrer)
|
|
|
|
charset = aReferrer.originCharset;
|
2009-03-30 11:47:31 -07:00
|
|
|
var fileInfo = new FileInfo(aDefaultFileName);
|
2007-03-22 10:30:00 -07:00
|
|
|
initFileInfo(fileInfo, aURL, charset, aDocument,
|
|
|
|
aContentType, aContentDisposition);
|
2009-03-30 11:47:31 -07:00
|
|
|
sourceURI = fileInfo.uri;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
var fpParams = {
|
|
|
|
fpTitleKey: aFilePickerTitleKey,
|
|
|
|
fileInfo: fileInfo,
|
|
|
|
contentType: aContentType,
|
|
|
|
saveMode: saveMode,
|
2009-03-30 11:47:31 -07:00
|
|
|
saveAsType: kSaveAsType_Complete,
|
|
|
|
file: file
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
2011-06-09 16:19:16 -07:00
|
|
|
// Find a URI to use for determining last-downloaded-to directory
|
|
|
|
let relatedURI = aReferrer || sourceURI;
|
|
|
|
|
2013-03-31 23:13:37 -07:00
|
|
|
getTargetFile(fpParams, function(aDialogCancelled) {
|
|
|
|
if (aDialogCancelled)
|
|
|
|
return;
|
|
|
|
|
|
|
|
saveAsType = fpParams.saveAsType;
|
|
|
|
file = fpParams.file;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-03-31 23:13:37 -07:00
|
|
|
continueSave();
|
|
|
|
}, aSkipPrompt, relatedURI);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-03-31 23:13:37 -07:00
|
|
|
function continueSave() {
|
|
|
|
// XXX We depend on the following holding true in appendFiltersForContentType():
|
|
|
|
// If we should save as a complete page, the saveAsType is kSaveAsType_Complete.
|
|
|
|
// If we should save as text, the saveAsType is kSaveAsType_Text.
|
|
|
|
var useSaveDocument = aDocument &&
|
|
|
|
(((saveMode & SAVEMODE_COMPLETE_DOM) && (saveAsType == kSaveAsType_Complete)) ||
|
|
|
|
((saveMode & SAVEMODE_COMPLETE_TEXT) && (saveAsType == kSaveAsType_Text)));
|
|
|
|
// 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.
|
|
|
|
var persistArgs = {
|
|
|
|
sourceURI : sourceURI,
|
|
|
|
sourceReferrer : aReferrer,
|
|
|
|
sourceDocument : useSaveDocument ? aDocument : null,
|
|
|
|
targetContentType : (saveAsType == kSaveAsType_Text) ? "text/plain" : null,
|
|
|
|
targetFile : file,
|
|
|
|
sourceCacheKey : aCacheKey,
|
|
|
|
sourcePostData : aDocument ? getPostData(aDocument) : null,
|
|
|
|
bypassCache : aShouldBypassCache,
|
|
|
|
initiatingWindow : aInitiatingDocument.defaultView
|
|
|
|
};
|
|
|
|
|
|
|
|
// Start the actual save process
|
|
|
|
internalPersist(persistArgs);
|
|
|
|
}
|
2009-03-30 11:47:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* internalPersist: Creates a 'Persist' object (which will perform the saving
|
|
|
|
* in the background) and then starts it.
|
|
|
|
*
|
|
|
|
* @param persistArgs.sourceURI
|
|
|
|
* The nsIURI of the document being saved
|
|
|
|
* @param persistArgs.sourceCacheKey [optional]
|
|
|
|
* If set will be passed to saveURI
|
|
|
|
* @param persistArgs.sourceDocument [optional]
|
|
|
|
* The document to be saved, or null if not saving a complete document
|
|
|
|
* @param persistArgs.sourceReferrer
|
|
|
|
* Required and used only when persistArgs.sourceDocument is NOT present,
|
|
|
|
* the nsIURI of the referrer to use, or null if no referrer should be
|
|
|
|
* sent.
|
|
|
|
* @param persistArgs.sourcePostData
|
|
|
|
* Required and used only when persistArgs.sourceDocument is NOT present,
|
|
|
|
* represents the POST data to be sent along with the HTTP request, and
|
|
|
|
* must be null if no POST data should be sent.
|
|
|
|
* @param persistArgs.targetFile
|
|
|
|
* The nsIFile of the file to create
|
|
|
|
* @param persistArgs.targetContentType
|
|
|
|
* Required and used only when persistArgs.sourceDocument is present,
|
|
|
|
* determines the final content type of the saved file, or null to use
|
|
|
|
* the same content type as the source document. Currently only
|
|
|
|
* "text/plain" is meaningful.
|
|
|
|
* @param persistArgs.bypassCache
|
|
|
|
* If true, the document will always be refetched from the server
|
2012-10-03 10:10:20 -07:00
|
|
|
* @param persistArgs.initiatingWindow
|
|
|
|
* The window from which the save operation was initiated.
|
2009-03-30 11:47:31 -07:00
|
|
|
*/
|
|
|
|
function internalPersist(persistArgs)
|
|
|
|
{
|
2007-03-22 10:30:00 -07:00
|
|
|
var persist = makeWebBrowserPersist();
|
|
|
|
|
|
|
|
// Calculate persist flags.
|
|
|
|
const nsIWBP = Components.interfaces.nsIWebBrowserPersist;
|
2009-06-16 07:30:25 -07:00
|
|
|
const flags = nsIWBP.PERSIST_FLAGS_REPLACE_EXISTING_FILES |
|
|
|
|
nsIWBP.PERSIST_FLAGS_FORCE_ALLOW_COOKIES;
|
2009-03-30 11:47:31 -07:00
|
|
|
if (persistArgs.bypassCache)
|
2007-03-22 10:30:00 -07:00
|
|
|
persist.persistFlags = flags | nsIWBP.PERSIST_FLAGS_BYPASS_CACHE;
|
|
|
|
else
|
|
|
|
persist.persistFlags = flags | nsIWBP.PERSIST_FLAGS_FROM_CACHE;
|
|
|
|
|
|
|
|
// Leave it to WebBrowserPersist to discover the encoding type (or lack thereof):
|
|
|
|
persist.persistFlags |= nsIWBP.PERSIST_FLAGS_AUTODETECT_APPLY_CONVERSION;
|
|
|
|
|
2009-03-30 11:47:31 -07:00
|
|
|
// Find the URI associated with the target file
|
|
|
|
var targetFileURL = makeFileURI(persistArgs.targetFile);
|
|
|
|
|
2012-10-03 10:10:20 -07:00
|
|
|
var isPrivate = PrivateBrowsingUtils.isWindowPrivate(persistArgs.initiatingWindow);
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// Create download and initiate it (below)
|
|
|
|
var tr = Components.classes["@mozilla.org/transfer;1"].createInstance(Components.interfaces.nsITransfer);
|
2009-03-30 11:47:31 -07:00
|
|
|
tr.init(persistArgs.sourceURI,
|
2012-10-03 10:10:20 -07:00
|
|
|
targetFileURL, "", null, null, null, persist, isPrivate);
|
2009-03-30 11:47:31 -07:00
|
|
|
persist.progressListener = new DownloadListener(window, tr);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-03-30 11:47:31 -07:00
|
|
|
if (persistArgs.sourceDocument) {
|
2007-03-22 10:30:00 -07:00
|
|
|
// Saving a Document, not a URI:
|
|
|
|
var filesFolder = null;
|
2009-03-30 11:47:31 -07:00
|
|
|
if (persistArgs.targetContentType != "text/plain") {
|
2007-03-22 10:30:00 -07:00
|
|
|
// Create the local directory into which to save associated files.
|
2009-03-30 11:47:31 -07:00
|
|
|
filesFolder = persistArgs.targetFile.clone();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-12-17 21:48:09 -08:00
|
|
|
var nameWithoutExtension = getFileBaseName(filesFolder.leafName);
|
2010-03-25 06:16:20 -07:00
|
|
|
var filesFolderLeafName =
|
|
|
|
ContentAreaUtils.stringBundle
|
|
|
|
.formatStringFromName("filesFolder", [nameWithoutExtension], 1);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
filesFolder.leafName = filesFolderLeafName;
|
|
|
|
}
|
|
|
|
|
|
|
|
var encodingFlags = 0;
|
2009-03-30 11:47:31 -07:00
|
|
|
if (persistArgs.targetContentType == "text/plain") {
|
2007-03-22 10:30:00 -07:00
|
|
|
encodingFlags |= nsIWBP.ENCODE_FLAGS_FORMATTED;
|
|
|
|
encodingFlags |= nsIWBP.ENCODE_FLAGS_ABSOLUTE_LINKS;
|
|
|
|
encodingFlags |= nsIWBP.ENCODE_FLAGS_NOFRAMES_CONTENT;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
encodingFlags |= nsIWBP.ENCODE_FLAGS_ENCODE_BASIC_ENTITIES;
|
|
|
|
}
|
|
|
|
|
|
|
|
const kWrapColumn = 80;
|
2009-03-30 11:47:31 -07:00
|
|
|
persist.saveDocument(persistArgs.sourceDocument, targetFileURL, filesFolder,
|
|
|
|
persistArgs.targetContentType, encodingFlags, kWrapColumn);
|
2007-03-22 10:30:00 -07:00
|
|
|
} else {
|
2012-10-04 12:07:51 -07:00
|
|
|
let privacyContext = persistArgs.initiatingWindow
|
2012-10-08 18:01:29 -07:00
|
|
|
.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
|
|
|
|
.getInterface(Components.interfaces.nsIWebNavigation)
|
|
|
|
.QueryInterface(Components.interfaces.nsILoadContext);
|
2009-03-30 11:47:31 -07:00
|
|
|
persist.saveURI(persistArgs.sourceURI,
|
|
|
|
persistArgs.sourceCacheKey, persistArgs.sourceReferrer, persistArgs.sourcePostData, null,
|
2012-10-04 12:07:51 -07:00
|
|
|
targetFileURL, privacyContext);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Structure for holding info about automatically supplied parameters for
|
|
|
|
* internalSave(...). This allows parameters to be supplied so the user does not
|
|
|
|
* need to be prompted for file info.
|
2012-07-20 22:54:41 -07:00
|
|
|
* @param aFileAutoChosen This is an nsIFile object that has been
|
2007-03-22 10:30:00 -07:00
|
|
|
* pre-determined as the filename for the target to save to
|
|
|
|
* @param aUriAutoChosen This is the nsIURI object for the target
|
|
|
|
*/
|
|
|
|
function AutoChosen(aFileAutoChosen, aUriAutoChosen) {
|
|
|
|
this.file = aFileAutoChosen;
|
|
|
|
this.uri = aUriAutoChosen;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Structure for holding info about a URL and the target filename it should be
|
|
|
|
* saved to. This structure is populated by initFileInfo(...).
|
|
|
|
* @param aSuggestedFileName This is used by initFileInfo(...) when it
|
|
|
|
* cannot 'discover' the filename from the url
|
|
|
|
* @param aFileName The target filename
|
|
|
|
* @param aFileBaseName The filename without the file extension
|
|
|
|
* @param aFileExt The extension of the filename
|
|
|
|
* @param aUri An nsIURI object for the url that is being saved
|
|
|
|
*/
|
|
|
|
function FileInfo(aSuggestedFileName, aFileName, aFileBaseName, aFileExt, aUri) {
|
|
|
|
this.suggestedFileName = aSuggestedFileName;
|
|
|
|
this.fileName = aFileName;
|
|
|
|
this.fileBaseName = aFileBaseName;
|
|
|
|
this.fileExt = aFileExt;
|
|
|
|
this.uri = aUri;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine what the 'default' filename string is, its file extension and the
|
|
|
|
* filename without the extension. This filename is used when prompting the user
|
|
|
|
* for confirmation in the file picker dialog.
|
|
|
|
* @param aFI A FileInfo structure into which we'll put the results of this method.
|
|
|
|
* @param aURL The String representation of the URL of the document being saved
|
|
|
|
* @param aURLCharset The charset of aURL.
|
|
|
|
* @param aDocument The document to be saved
|
|
|
|
* @param aContentType The content type we're saving, if it could be
|
|
|
|
* determined by the caller.
|
|
|
|
* @param aContentDisposition The content-disposition header for the object
|
|
|
|
* we're saving, if it could be determined by the caller.
|
|
|
|
*/
|
|
|
|
function initFileInfo(aFI, aURL, aURLCharset, aDocument,
|
|
|
|
aContentType, aContentDisposition)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
// Get an nsIURI object from aURL if possible:
|
|
|
|
try {
|
|
|
|
aFI.uri = makeURI(aURL, aURLCharset);
|
|
|
|
// Assuming nsiUri is valid, calling QueryInterface(...) on it will
|
|
|
|
// populate extra object fields (eg filename and file extension).
|
|
|
|
var url = aFI.uri.QueryInterface(Components.interfaces.nsIURL);
|
|
|
|
aFI.fileExt = url.fileExtension;
|
|
|
|
} catch (e) {
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the default filename:
|
|
|
|
aFI.fileName = getDefaultFileName((aFI.suggestedFileName || aFI.fileName),
|
|
|
|
aFI.uri, aDocument, aContentDisposition);
|
|
|
|
// If aFI.fileExt is still blank, consider: aFI.suggestedFileName is supplied
|
|
|
|
// if saveURL(...) was the original caller (hence both aContentType and
|
|
|
|
// aDocument are blank). If they were saving a link to a website then make
|
|
|
|
// the extension .htm .
|
|
|
|
if (!aFI.fileExt && !aDocument && !aContentType && (/^http(s?):\/\//i.test(aURL))) {
|
|
|
|
aFI.fileExt = "htm";
|
|
|
|
aFI.fileBaseName = aFI.fileName;
|
|
|
|
} else {
|
|
|
|
aFI.fileExt = getDefaultExtension(aFI.fileName, aFI.uri, aContentType);
|
2007-12-17 21:48:09 -08:00
|
|
|
aFI.fileBaseName = getFileBaseName(aFI.fileName);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-08 03:01:50 -07:00
|
|
|
/**
|
|
|
|
* Given the Filepicker Parameters (aFpP), show the file picker dialog,
|
|
|
|
* prompting the user to confirm (or change) the fileName.
|
|
|
|
* @param aFpP
|
|
|
|
* A structure (see definition in internalSave(...) method)
|
|
|
|
* containing all the data used within this method.
|
2013-03-31 23:13:37 -07:00
|
|
|
* @param aCallback
|
|
|
|
* A callback function that will be called once the function finishes.
|
|
|
|
* The first argument passed to the function will be a boolean that,
|
|
|
|
* when true, indicated that the user dismissed the file picker.
|
2009-08-08 03:01:50 -07:00
|
|
|
* @param aSkipPrompt
|
|
|
|
* If true, attempt to save the file automatically to the user's default
|
|
|
|
* download directory, thus skipping the explicit prompt for a file name,
|
|
|
|
* but only if the associated preference is set.
|
|
|
|
* If false, don't save the file automatically to the user's
|
|
|
|
* default download directory, even if the associated preference
|
|
|
|
* is set, but ask for the target explicitly.
|
2011-06-09 16:19:16 -07:00
|
|
|
* @param aRelatedURI
|
|
|
|
* An nsIURI associated with the download. The last used
|
|
|
|
* directory of the picker is retrieved from/stored in the
|
|
|
|
* Content Pref Service using this URI.
|
2009-08-08 03:01:50 -07:00
|
|
|
*/
|
2013-03-31 23:13:37 -07:00
|
|
|
function getTargetFile(aFpP, aCallback, /* optional */ aSkipPrompt, /* optional */ aRelatedURI)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-07-20 22:54:41 -07:00
|
|
|
if (!getTargetFile.DownloadLastDir)
|
2012-07-21 11:29:50 -07:00
|
|
|
Components.utils.import("resource://gre/modules/DownloadLastDir.jsm", getTargetFile);
|
2012-07-20 22:54:41 -07:00
|
|
|
var gDownloadLastDir = new getTargetFile.DownloadLastDir(window);
|
2009-07-17 11:38:19 -07:00
|
|
|
|
2012-10-18 07:44:36 -07:00
|
|
|
var prefs = Services.prefs.getBranch("browser.download.");
|
2009-08-08 03:01:50 -07:00
|
|
|
var useDownloadDir = prefs.getBoolPref("useDownloadDir");
|
2012-07-20 22:54:41 -07:00
|
|
|
const nsIFile = Components.interfaces.nsIFile;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-08-08 03:01:50 -07:00
|
|
|
if (!aSkipPrompt)
|
|
|
|
useDownloadDir = false;
|
|
|
|
|
|
|
|
// Default to the user's default downloads directory configured
|
|
|
|
// through download prefs.
|
2012-10-18 07:44:36 -07:00
|
|
|
var dir = Services.downloads.userDownloadsDirectory;
|
2009-08-08 03:01:50 -07:00
|
|
|
var dirExists = dir && dir.exists();
|
|
|
|
|
|
|
|
if (useDownloadDir && dirExists) {
|
|
|
|
dir.append(getNormalizedLeafName(aFpP.fileInfo.fileName,
|
|
|
|
aFpP.fileInfo.fileExt));
|
|
|
|
aFpP.file = uniqueFile(dir);
|
2013-03-31 23:13:37 -07:00
|
|
|
aCallback(false);
|
|
|
|
return;
|
2009-08-08 03:01:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// We must prompt for the file name explicitly.
|
|
|
|
// If we must prompt because we were asked to...
|
2013-03-31 23:13:37 -07:00
|
|
|
if (useDownloadDir) {
|
|
|
|
// Keep async behavior in both branches
|
|
|
|
Services.tm.mainThread.dispatch(function() {
|
|
|
|
displayPicker();
|
|
|
|
}, Components.interfaces.nsIThread.DISPATCH_NORMAL);
|
|
|
|
} else {
|
|
|
|
gDownloadLastDir.getFileAsync(aRelatedURI, function getFileAsyncCB(aFile) {
|
|
|
|
if (aFile && aFile.exists()) {
|
|
|
|
dir = aFile;
|
|
|
|
dirExists = true;
|
|
|
|
}
|
|
|
|
displayPicker();
|
|
|
|
});
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2007-08-29 12:58:59 -07:00
|
|
|
|
2013-03-31 23:13:37 -07:00
|
|
|
function displayPicker() {
|
|
|
|
if (!dirExists) {
|
|
|
|
// Default to desktop.
|
|
|
|
dir = Services.dirsvc.get("Desk", nsIFile);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2013-03-31 23:13:37 -07:00
|
|
|
|
|
|
|
var fp = makeFilePicker();
|
|
|
|
var titleKey = aFpP.fpTitleKey || "SaveLinkTitle";
|
|
|
|
fp.init(window, ContentAreaUtils.stringBundle.GetStringFromName(titleKey),
|
|
|
|
Components.interfaces.nsIFilePicker.modeSave);
|
|
|
|
|
|
|
|
fp.displayDirectory = dir;
|
|
|
|
fp.defaultExtension = aFpP.fileInfo.fileExt;
|
|
|
|
fp.defaultString = getNormalizedLeafName(aFpP.fileInfo.fileName,
|
|
|
|
aFpP.fileInfo.fileExt);
|
|
|
|
appendFiltersForContentType(fp, aFpP.contentType, aFpP.fileInfo.fileExt,
|
|
|
|
aFpP.saveMode);
|
|
|
|
|
|
|
|
// The index of the selected filter is only preserved and restored if there's
|
|
|
|
// more than one filter in addition to "All Files".
|
|
|
|
if (aFpP.saveMode != SAVEMODE_FILEONLY) {
|
|
|
|
try {
|
|
|
|
fp.filterIndex = prefs.getIntPref("save_converter_index");
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2007-08-21 10:22:02 -07:00
|
|
|
|
2013-03-31 23:13:37 -07:00
|
|
|
if (fp.show() == Components.interfaces.nsIFilePicker.returnCancel || !fp.file) {
|
|
|
|
aCallback(true);
|
|
|
|
return;
|
|
|
|
}
|
2008-11-13 14:34:41 -08:00
|
|
|
|
2013-03-31 23:13:37 -07:00
|
|
|
if (aFpP.saveMode != SAVEMODE_FILEONLY)
|
|
|
|
prefs.setIntPref("save_converter_index", fp.filterIndex);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-03-31 23:13:37 -07:00
|
|
|
// Do not store the last save directory as a pref inside the private browsing mode
|
|
|
|
var directory = fp.file.parent.QueryInterface(nsIFile);
|
|
|
|
gDownloadLastDir.setFile(aRelatedURI, directory);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-03-31 23:13:37 -07:00
|
|
|
fp.file.leafName = validateFileName(fp.file.leafName);
|
2012-10-18 07:44:36 -07:00
|
|
|
|
2013-03-31 23:13:37 -07:00
|
|
|
aFpP.saveAsType = fp.filterIndex;
|
|
|
|
aFpP.file = fp.file;
|
|
|
|
aFpP.fileURL = fp.fileURL;
|
|
|
|
aCallback(false);
|
|
|
|
}
|
2009-08-08 03:01:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Since we're automatically downloading, we don't get the file picker's
|
|
|
|
// logic to check for existing files, so we need to do that here.
|
|
|
|
//
|
|
|
|
// Note - this code is identical to that in
|
|
|
|
// mozilla/toolkit/mozapps/downloads/src/nsHelperAppDlg.js.in
|
|
|
|
// If you are updating this code, update that code too! We can't share code
|
|
|
|
// here since that code is called in a js component.
|
|
|
|
function uniqueFile(aLocalFile)
|
|
|
|
{
|
|
|
|
var collisionCount = 0;
|
|
|
|
while (aLocalFile.exists()) {
|
|
|
|
collisionCount++;
|
|
|
|
if (collisionCount == 1) {
|
|
|
|
// Append "(2)" before the last dot in (or at the end of) the filename
|
|
|
|
// special case .ext.gz etc files so we don't wind up with .tar(2).gz
|
|
|
|
if (aLocalFile.leafName.match(/\.[^\.]{1,3}\.(gz|bz2|Z)$/i))
|
|
|
|
aLocalFile.leafName = aLocalFile.leafName.replace(/\.[^\.]{1,3}\.(gz|bz2|Z)$/i, "(2)$&");
|
|
|
|
else
|
|
|
|
aLocalFile.leafName = aLocalFile.leafName.replace(/(\.[^\.]*)?$/, "(2)$&");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// replace the last (n) in the filename with (n+1)
|
|
|
|
aLocalFile.leafName = aLocalFile.leafName.replace(/^(.*\()\d+\)/, "$1" + (collisionCount + 1) + ")");
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
2009-08-08 03:01:50 -07:00
|
|
|
return aLocalFile;
|
2007-03-22 10:30:00 -07: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.
|
|
|
|
function appendFiltersForContentType(aFilePicker, aContentType, aFileExtension, aSaveMode)
|
|
|
|
{
|
|
|
|
// 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.
|
|
|
|
switch (aContentType) {
|
|
|
|
case "text/html":
|
|
|
|
bundleName = "WebPageHTMLOnlyFilter";
|
|
|
|
filterString = "*.htm; *.html";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "application/xhtml+xml":
|
|
|
|
bundleName = "WebPageXHTMLOnlyFilter";
|
|
|
|
filterString = "*.xht; *.xhtml";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "image/svg+xml":
|
|
|
|
bundleName = "WebPageSVGOnlyFilter";
|
|
|
|
filterString = "*.svg; *.svgz";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "text/xml":
|
|
|
|
case "application/xml":
|
|
|
|
bundleName = "WebPageXMLOnlyFilter";
|
|
|
|
filterString = "*.xml";
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
if (aSaveMode != SAVEMODE_FILEONLY)
|
|
|
|
throw "Invalid save mode for type '" + aContentType + "'";
|
|
|
|
|
|
|
|
var mimeInfo = getMIMEInfoForType(aContentType, aFileExtension);
|
|
|
|
if (mimeInfo) {
|
|
|
|
|
|
|
|
var extEnumerator = mimeInfo.getFileExtensions();
|
|
|
|
|
|
|
|
var extString = "";
|
|
|
|
while (extEnumerator.hasMore()) {
|
|
|
|
var extension = extEnumerator.getNext();
|
|
|
|
if (extString)
|
|
|
|
extString += "; "; // If adding more than one extension,
|
|
|
|
// separate by semi-colon
|
|
|
|
extString += "*." + extension;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (extString)
|
|
|
|
aFilePicker.appendFilter(mimeInfo.description, extString);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aSaveMode & SAVEMODE_COMPLETE_DOM) {
|
2010-03-25 06:16:20 -07:00
|
|
|
aFilePicker.appendFilter(ContentAreaUtils.stringBundle.GetStringFromName("WebPageCompleteFilter"),
|
|
|
|
filterString);
|
2007-03-22 10:30:00 -07:00
|
|
|
// We should always offer a choice to save document only if
|
|
|
|
// we allow saving as complete.
|
2010-03-25 06:16:20 -07:00
|
|
|
aFilePicker.appendFilter(ContentAreaUtils.stringBundle.GetStringFromName(bundleName),
|
|
|
|
filterString);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (aSaveMode & SAVEMODE_COMPLETE_TEXT)
|
|
|
|
aFilePicker.appendFilters(Components.interfaces.nsIFilePicker.filterText);
|
|
|
|
|
|
|
|
// Always append the all files (*) filter
|
|
|
|
aFilePicker.appendFilters(Components.interfaces.nsIFilePicker.filterAll);
|
|
|
|
}
|
|
|
|
|
2009-03-16 11:47:19 -07:00
|
|
|
function getPostData(aDocument)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
try {
|
2010-06-08 00:15:00 -07:00
|
|
|
// Find the session history entry corresponding to the given document. In
|
|
|
|
// the current implementation, nsIWebPageDescriptor.currentDescriptor always
|
|
|
|
// returns a session history entry.
|
|
|
|
var sessionHistoryEntry =
|
|
|
|
aDocument.defaultView
|
|
|
|
.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
|
|
|
|
.getInterface(Components.interfaces.nsIWebNavigation)
|
|
|
|
.QueryInterface(Components.interfaces.nsIWebPageDescriptor)
|
|
|
|
.currentDescriptor
|
|
|
|
.QueryInterface(Components.interfaces.nsISHEntry);
|
|
|
|
return sessionHistoryEntry.postData;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
function makeWebBrowserPersist()
|
|
|
|
{
|
|
|
|
const persistContractID = "@mozilla.org/embedding/browser/nsWebBrowserPersist;1";
|
|
|
|
const persistIID = Components.interfaces.nsIWebBrowserPersist;
|
|
|
|
return Components.classes[persistContractID].createInstance(persistIID);
|
|
|
|
}
|
|
|
|
|
2007-12-11 01:57:36 -08:00
|
|
|
/**
|
|
|
|
* Constructs a new URI, using nsIIOService.
|
|
|
|
* @param aURL The URI spec.
|
|
|
|
* @param aOriginCharset The charset of the URI.
|
|
|
|
* @param aBaseURI Base URI to resolve aURL, or null.
|
|
|
|
* @return an nsIURI object based on aURL.
|
|
|
|
*/
|
2007-03-22 10:30:00 -07:00
|
|
|
function makeURI(aURL, aOriginCharset, aBaseURI)
|
|
|
|
{
|
2012-10-18 07:44:36 -07:00
|
|
|
return Services.io.newURI(aURL, aOriginCharset, aBaseURI);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function makeFileURI(aFile)
|
|
|
|
{
|
2012-10-18 07:44:36 -07:00
|
|
|
return Services.io.newFileURI(aFile);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function makeFilePicker()
|
|
|
|
{
|
|
|
|
const fpContractID = "@mozilla.org/filepicker;1";
|
|
|
|
const fpIID = Components.interfaces.nsIFilePicker;
|
|
|
|
return Components.classes[fpContractID].createInstance(fpIID);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getMIMEService()
|
|
|
|
{
|
|
|
|
const mimeSvcContractID = "@mozilla.org/mime;1";
|
|
|
|
const mimeSvcIID = Components.interfaces.nsIMIMEService;
|
|
|
|
const mimeSvc = Components.classes[mimeSvcContractID].getService(mimeSvcIID);
|
|
|
|
return mimeSvc;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Given aFileName, find the fileName without the extension on the end.
|
2007-12-17 21:48:09 -08:00
|
|
|
function getFileBaseName(aFileName)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// Remove the file extension from aFileName:
|
|
|
|
return aFileName.replace(/\.[^.]*$/, "");
|
|
|
|
}
|
|
|
|
|
|
|
|
function getMIMETypeForURI(aURI)
|
|
|
|
{
|
2007-12-11 01:57:36 -08:00
|
|
|
try {
|
2007-03-22 10:30:00 -07:00
|
|
|
return getMIMEService().getTypeFromURI(aURI);
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getMIMEInfoForType(aMIMEType, aExtension)
|
|
|
|
{
|
|
|
|
if (aMIMEType || aExtension) {
|
|
|
|
try {
|
|
|
|
return getMIMEService().getFromTypeAndExtension(aMIMEType, aExtension);
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getDefaultFileName(aDefaultFileName, aURI, aDocument,
|
|
|
|
aContentDisposition)
|
|
|
|
{
|
|
|
|
// 1) look for a filename in the content-disposition header, if any
|
|
|
|
if (aContentDisposition) {
|
|
|
|
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 = null;
|
|
|
|
try {
|
|
|
|
fileName = mhp.getParameter(aContentDisposition, "filename", charset,
|
|
|
|
true, dummy);
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
try {
|
|
|
|
fileName = mhp.getParameter(aContentDisposition, "name", charset, true,
|
|
|
|
dummy);
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (fileName)
|
|
|
|
return fileName;
|
|
|
|
}
|
|
|
|
|
2012-08-03 00:12:24 -07:00
|
|
|
let docTitle;
|
|
|
|
if (aDocument) {
|
|
|
|
// If the document looks like HTML or XML, try to use its original title.
|
|
|
|
docTitle = validateFileName(aDocument.title).trim();
|
|
|
|
if (docTitle) {
|
|
|
|
let contentType = aDocument.contentType;
|
|
|
|
if (contentType == "application/xhtml+xml" ||
|
|
|
|
contentType == "application/xml" ||
|
|
|
|
contentType == "image/svg+xml" ||
|
|
|
|
contentType == "text/html" ||
|
|
|
|
contentType == "text/xml") {
|
|
|
|
// 2) Use the document title
|
|
|
|
return docTitle;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
try {
|
|
|
|
var url = aURI.QueryInterface(Components.interfaces.nsIURL);
|
|
|
|
if (url.fileName != "") {
|
2012-08-03 00:12:24 -07:00
|
|
|
// 3) Use the actual file name, if present
|
2007-03-22 10:30:00 -07:00
|
|
|
var textToSubURI = Components.classes["@mozilla.org/intl/texttosuburi;1"]
|
|
|
|
.getService(Components.interfaces.nsITextToSubURI);
|
|
|
|
return validateFileName(textToSubURI.unEscapeURIForUI(url.originCharset || "UTF-8", url.fileName));
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
// This is something like a data: and so forth URI... no filename here.
|
|
|
|
}
|
|
|
|
|
2012-08-03 00:12:24 -07:00
|
|
|
if (docTitle)
|
|
|
|
// 4) Use the document title
|
|
|
|
return docTitle;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (aDefaultFileName)
|
2012-08-03 00:12:24 -07:00
|
|
|
// 5) Use the caller-provided name, if any
|
2007-03-22 10:30:00 -07:00
|
|
|
return validateFileName(aDefaultFileName);
|
|
|
|
|
2012-08-03 00:12:24 -07:00
|
|
|
// 6) If this is a directory, use the last directory name
|
2007-03-22 10:30:00 -07:00
|
|
|
var path = aURI.path.match(/\/([^\/]+)\/$/);
|
|
|
|
if (path && path.length > 1)
|
|
|
|
return validateFileName(path[1]);
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (aURI.host)
|
2012-08-03 00:12:24 -07:00
|
|
|
// 7) Use the host.
|
2007-03-22 10:30:00 -07:00
|
|
|
return aURI.host;
|
|
|
|
} catch (e) {
|
|
|
|
// Some files have no information at all, like Javascript generated pages
|
|
|
|
}
|
|
|
|
try {
|
2012-08-03 00:12:24 -07:00
|
|
|
// 8) Use the default file name
|
2010-03-25 06:16:20 -07:00
|
|
|
return ContentAreaUtils.stringBundle.GetStringFromName("DefaultSaveFileName");
|
2007-03-22 10:30:00 -07:00
|
|
|
} catch (e) {
|
|
|
|
//in case localized string cannot be found
|
|
|
|
}
|
2012-08-03 00:12:24 -07:00
|
|
|
// 9) If all else fails, use "index"
|
2007-03-22 10:30:00 -07:00
|
|
|
return "index";
|
|
|
|
}
|
|
|
|
|
|
|
|
function validateFileName(aFileName)
|
|
|
|
{
|
|
|
|
var re = /[\/]+/g;
|
|
|
|
if (navigator.appVersion.indexOf("Windows") != -1) {
|
|
|
|
re = /[\\\/\|]+/g;
|
|
|
|
aFileName = aFileName.replace(/[\"]+/g, "'");
|
|
|
|
aFileName = aFileName.replace(/[\*\:\?]+/g, " ");
|
|
|
|
aFileName = aFileName.replace(/[\<]+/g, "(");
|
|
|
|
aFileName = aFileName.replace(/[\>]+/g, ")");
|
|
|
|
}
|
|
|
|
else if (navigator.appVersion.indexOf("Macintosh") != -1)
|
|
|
|
re = /[\:\/]+/g;
|
2011-03-04 15:29:01 -08:00
|
|
|
else if (navigator.appVersion.indexOf("Android") != -1 ||
|
|
|
|
navigator.appVersion.indexOf("Maemo") != -1) {
|
|
|
|
// On mobile devices, the filesystem may be very limited in what
|
|
|
|
// it considers valid characters. To avoid errors, we sanitize
|
|
|
|
// conservatively.
|
|
|
|
const dangerousChars = "*?<>|\":/\\[];,+=";
|
|
|
|
var processed = "";
|
|
|
|
for (var i = 0; i < aFileName.length; i++)
|
|
|
|
processed += aFileName.charCodeAt(i) >= 32 &&
|
|
|
|
!(dangerousChars.indexOf(aFileName[i]) >= 0) ? aFileName[i]
|
|
|
|
: "_";
|
|
|
|
|
|
|
|
// Last character should not be a space
|
|
|
|
processed = processed.trim();
|
|
|
|
|
|
|
|
// If a large part of the filename has been sanitized, then we
|
|
|
|
// will use a default filename instead
|
|
|
|
if (processed.replace(/_/g, "").length <= processed.length/2) {
|
|
|
|
// We purposefully do not use a localized default filename,
|
|
|
|
// which we could have done using
|
|
|
|
// ContentAreaUtils.stringBundle.GetStringFromName("DefaultSaveFileName")
|
|
|
|
// since it may contain invalid characters.
|
|
|
|
var original = processed;
|
|
|
|
processed = "download";
|
|
|
|
|
|
|
|
// Preserve a suffix, if there is one
|
|
|
|
if (original.indexOf(".") >= 0) {
|
|
|
|
var suffix = original.split(".").slice(-1)[0];
|
|
|
|
if (suffix && suffix.indexOf("_") < 0)
|
|
|
|
processed += "." + suffix;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return processed;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
return aFileName.replace(re, "_");
|
|
|
|
}
|
|
|
|
|
|
|
|
function getNormalizedLeafName(aFile, aDefaultExtension)
|
|
|
|
{
|
|
|
|
if (!aDefaultExtension)
|
|
|
|
return aFile;
|
|
|
|
|
|
|
|
#ifdef XP_WIN
|
|
|
|
// Remove trailing dots and spaces on windows
|
|
|
|
aFile = aFile.replace(/[\s.]+$/, "");
|
|
|
|
#endif
|
2007-04-17 11:51:33 -07:00
|
|
|
|
|
|
|
// Remove leading dots
|
|
|
|
aFile = aFile.replace(/^\.+/, "");
|
2007-12-11 01:57:36 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// Fix up the file name we're saving to to include the default extension
|
|
|
|
var i = aFile.lastIndexOf(".");
|
|
|
|
if (aFile.substr(i + 1) != aDefaultExtension)
|
|
|
|
return aFile + "." + aDefaultExtension;
|
|
|
|
|
|
|
|
return aFile;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getDefaultExtension(aFilename, aURI, aContentType)
|
|
|
|
{
|
|
|
|
if (aContentType == "text/plain" || aContentType == "application/octet-stream" || aURI.scheme == "ftp")
|
|
|
|
return ""; // temporary fix for bug 120327
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
|
|
|
// This mirrors some code in nsExternalHelperAppService::DoContent
|
|
|
|
// Use the filename first and then the URI if that fails
|
2007-12-11 01:57:36 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
var mimeInfo = getMIMEInfoForType(aContentType, ext);
|
|
|
|
|
|
|
|
if (ext && mimeInfo && mimeInfo.extensionExists(ext))
|
|
|
|
return ext;
|
2007-12-11 01:57:36 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// Well, that failed. Now try the extension from the URI
|
|
|
|
var urlext;
|
|
|
|
try {
|
|
|
|
url = aURI.QueryInterface(Components.interfaces.nsIURL);
|
|
|
|
urlext = url.fileExtension;
|
|
|
|
} catch (e) {
|
|
|
|
}
|
|
|
|
|
|
|
|
if (urlext && mimeInfo && mimeInfo.extensionExists(urlext)) {
|
|
|
|
return urlext;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
try {
|
2007-12-19 03:25:20 -08:00
|
|
|
if (mimeInfo)
|
|
|
|
return mimeInfo.primaryExtension;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
}
|
2007-12-19 03:25:20 -08:00
|
|
|
// Fall back on the extensions in the filename and URI for lack
|
|
|
|
// of anything better.
|
|
|
|
return ext || urlext;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-30 11:47:31 -07:00
|
|
|
function GetSaveModeForContentType(aContentType, aDocument)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-03-30 11:47:31 -07:00
|
|
|
// We can only save a complete page if we have a loaded document
|
|
|
|
if (!aDocument)
|
|
|
|
return SAVEMODE_FILEONLY;
|
|
|
|
|
|
|
|
// Find the possible save modes using the provided content type
|
2007-03-22 10:30:00 -07:00
|
|
|
var saveMode = SAVEMODE_FILEONLY;
|
|
|
|
switch (aContentType) {
|
|
|
|
case "text/html":
|
|
|
|
case "application/xhtml+xml":
|
|
|
|
case "image/svg+xml":
|
|
|
|
saveMode |= SAVEMODE_COMPLETE_TEXT;
|
|
|
|
// Fall through
|
|
|
|
case "text/xml":
|
|
|
|
case "application/xml":
|
|
|
|
saveMode |= SAVEMODE_COMPLETE_DOM;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return saveMode;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getCharsetforSave(aDocument)
|
|
|
|
{
|
|
|
|
if (aDocument)
|
|
|
|
return aDocument.characterSet;
|
|
|
|
|
|
|
|
if (document.commandDispatcher.focusedWindow)
|
|
|
|
return document.commandDispatcher.focusedWindow.document.characterSet;
|
|
|
|
|
|
|
|
return window.content.document.characterSet;
|
|
|
|
}
|
2007-08-09 10:59:08 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Open a URL from chrome, determining if we can handle it internally or need to
|
|
|
|
* launch an external application to handle it.
|
|
|
|
* @param aURL The URL to be opened
|
|
|
|
*/
|
|
|
|
function openURL(aURL)
|
|
|
|
{
|
2009-07-28 22:55:03 -07:00
|
|
|
var uri = makeURI(aURL);
|
2007-08-09 10:59:08 -07:00
|
|
|
|
|
|
|
var protocolSvc = Components.classes["@mozilla.org/uriloader/external-protocol-service;1"]
|
|
|
|
.getService(Components.interfaces.nsIExternalProtocolService);
|
|
|
|
|
|
|
|
if (!protocolSvc.isExposedProtocol(uri.scheme)) {
|
|
|
|
// If we're not a browser, use the external protocol service to load the URI.
|
|
|
|
protocolSvc.loadUrl(uri);
|
|
|
|
}
|
|
|
|
else {
|
2013-03-18 08:46:17 -07:00
|
|
|
var recentWindow = Services.wm.getMostRecentWindow("navigator:browser");
|
2012-05-17 14:53:05 -07:00
|
|
|
if (recentWindow) {
|
|
|
|
var win = recentWindow.browserDOMWindow.openURI(uri, null,
|
|
|
|
recentWindow.browserDOMWindow.OPEN_DEFAULTWINDOW,
|
|
|
|
recentWindow.browserDOMWindow.OPEN_NEW);
|
|
|
|
win.focus();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-08-09 10:59:08 -07:00
|
|
|
var loadgroup = Components.classes["@mozilla.org/network/load-group;1"]
|
|
|
|
.createInstance(Components.interfaces.nsILoadGroup);
|
2012-10-18 07:44:36 -07:00
|
|
|
var appstartup = Services.startup;
|
2007-08-09 10:59:08 -07:00
|
|
|
|
|
|
|
var loadListener = {
|
|
|
|
onStartRequest: function ll_start(aRequest, aContext) {
|
|
|
|
appstartup.enterLastWindowClosingSurvivalArea();
|
|
|
|
},
|
|
|
|
onStopRequest: function ll_stop(aRequest, aContext, aStatusCode) {
|
|
|
|
appstartup.exitLastWindowClosingSurvivalArea();
|
|
|
|
},
|
|
|
|
QueryInterface: function ll_QI(iid) {
|
|
|
|
if (iid.equals(Components.interfaces.nsISupports) ||
|
|
|
|
iid.equals(Components.interfaces.nsIRequestObserver) ||
|
|
|
|
iid.equals(Components.interfaces.nsISupportsWeakReference))
|
|
|
|
return this;
|
|
|
|
throw Components.results.NS_ERROR_NO_INTERFACE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
loadgroup.groupObserver = loadListener;
|
|
|
|
|
|
|
|
var uriListener = {
|
|
|
|
onStartURIOpen: function(uri) { return false; },
|
|
|
|
doContent: function(ctype, preferred, request, handler) { return false; },
|
|
|
|
isPreferred: function(ctype, desired) { return false; },
|
|
|
|
canHandleContent: function(ctype, preferred, desired) { return false; },
|
|
|
|
loadCookie: null,
|
|
|
|
parentContentListener: null,
|
|
|
|
getInterface: function(iid) {
|
|
|
|
if (iid.equals(Components.interfaces.nsIURIContentListener))
|
|
|
|
return this;
|
|
|
|
if (iid.equals(Components.interfaces.nsILoadGroup))
|
|
|
|
return loadgroup;
|
|
|
|
throw Components.results.NS_ERROR_NO_INTERFACE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-18 07:44:36 -07:00
|
|
|
var channel = Services.io.newChannelFromURI(uri);
|
2007-08-09 10:59:08 -07:00
|
|
|
var uriLoader = Components.classes["@mozilla.org/uriloader;1"]
|
|
|
|
.getService(Components.interfaces.nsIURILoader);
|
|
|
|
uriLoader.openURI(channel, true, uriListener);
|
|
|
|
}
|
|
|
|
}
|