2016-02-05 23:16:39 +03: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/. */
|
2005-03-24 00:39:42 +03:00
|
|
|
|
2018-01-30 02:20:18 +03:00
|
|
|
ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");
|
2013-10-25 10:03:18 +04:00
|
|
|
|
2017-08-09 23:03:36 +03:00
|
|
|
XPCOMUtils.defineLazyModuleGetters(this, {
|
|
|
|
BrowserUtils: "resource://gre/modules/BrowserUtils.jsm",
|
|
|
|
Downloads: "resource://gre/modules/Downloads.jsm",
|
2017-09-27 14:13:07 +03:00
|
|
|
DownloadPaths: "resource://gre/modules/DownloadPaths.jsm",
|
2017-08-09 23:03:36 +03:00
|
|
|
DownloadLastDir: "resource://gre/modules/DownloadLastDir.jsm",
|
|
|
|
FileUtils: "resource://gre/modules/FileUtils.jsm",
|
|
|
|
OS: "resource://gre/modules/osfile.jsm",
|
|
|
|
PrivateBrowsingUtils: "resource://gre/modules/PrivateBrowsingUtils.jsm",
|
|
|
|
Services: "resource://gre/modules/Services.jsm",
|
|
|
|
Deprecated: "resource://gre/modules/Deprecated.jsm",
|
|
|
|
AppConstants: "resource://gre/modules/AppConstants.jsm",
|
|
|
|
NetUtil: "resource://gre/modules/NetUtil.jsm",
|
|
|
|
});
|
2016-01-29 00:23:30 +03:00
|
|
|
|
2009-07-29 09:55:03 +04:00
|
|
|
var ContentAreaUtils = {
|
2012-10-18 18:44:36 +04:00
|
|
|
|
|
|
|
// this is for backwards compatibility.
|
2009-07-29 09:55:03 +04:00
|
|
|
get ioService() {
|
2012-10-18 18:44:36 +04:00
|
|
|
return Services.io;
|
2010-03-25 16:16:20 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
get stringBundle() {
|
|
|
|
delete this.stringBundle;
|
|
|
|
return this.stringBundle =
|
2012-10-18 18:44:36 +04:00
|
|
|
Services.strings.createBundle("chrome://global/locale/contentAreaCommands.properties");
|
2009-07-29 09:55:03 +04:00
|
|
|
}
|
2017-10-15 21:50:30 +03:00
|
|
|
};
|
2009-07-29 09:55:03 +04:00
|
|
|
|
2016-12-31 05:47:25 +03:00
|
|
|
function urlSecurityCheck(aURL, aPrincipal, aFlags) {
|
2014-02-09 05:40:27 +04:00
|
|
|
return BrowserUtils.urlSecurityCheck(aURL, aPrincipal, aFlags);
|
2005-03-24 00:37:06 +03:00
|
|
|
}
|
|
|
|
|
2016-12-31 05:47:25 +03:00
|
|
|
function forbidCPOW(arg, func, argname) {
|
2015-02-11 23:55:29 +03:00
|
|
|
if (arg && (typeof(arg) == "object" || typeof(arg) == "function") &&
|
|
|
|
Components.utils.isCrossProcessWrapper(arg)) {
|
|
|
|
throw new Error(`no CPOWs allowed for argument ${argname} to ${func}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-27 12:47:05 +04:00
|
|
|
// Clientele: (Make sure you don't break any of these)
|
2005-03-24 00:36:58 +03:00
|
|
|
// - File -> Save Page/Frame As...
|
|
|
|
// - Context -> Save Page/Frame As...
|
|
|
|
// - Context -> Save Link As...
|
2005-03-24 00:37:39 +03:00
|
|
|
// - Alt-Click links in web pages
|
|
|
|
// - Alt-Click links in the UI
|
2005-03-24 00:36:58 +03:00
|
|
|
//
|
|
|
|
// 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 12:57:36 +03:00
|
|
|
// the above method.
|
2005-03-24 00:36:58 +03:00
|
|
|
// - A linked document using Save Link As...
|
|
|
|
// - A linked document using Alt-click Save Link As...
|
|
|
|
//
|
2005-06-28 18:41:11 +04:00
|
|
|
function saveURL(aURL, aFileName, aFilePickerTitleKey, aShouldBypassCache,
|
2016-12-31 05:47:25 +03:00
|
|
|
aSkipPrompt, aReferrer, aSourceDocument, aIsContentWindowPrivate) {
|
2015-02-11 23:55:29 +03:00
|
|
|
forbidCPOW(aURL, "saveURL", "aURL");
|
|
|
|
forbidCPOW(aReferrer, "saveURL", "aReferrer");
|
|
|
|
// Allow aSourceDocument to be a CPOW.
|
|
|
|
|
2005-06-28 18:41:11 +04:00
|
|
|
internalSave(aURL, null, aFileName, null, null, aShouldBypassCache,
|
2012-10-03 21:10:20 +04:00
|
|
|
aFilePickerTitleKey, null, aReferrer, aSourceDocument,
|
2015-07-09 00:18:38 +03:00
|
|
|
aSkipPrompt, null, aIsContentWindowPrivate);
|
2005-03-24 00:36:58 +03:00
|
|
|
}
|
|
|
|
|
2005-06-28 18:41:11 +04:00
|
|
|
// Just like saveURL, but will get some info off the image before
|
|
|
|
// calling internalSave
|
2009-07-27 12:47:05 +04:00
|
|
|
// Clientele: (Make sure you don't break any of these)
|
2005-06-28 18:41:11 +04:00
|
|
|
// - Context -> Save Image As...
|
|
|
|
const imgICache = Components.interfaces.imgICache;
|
|
|
|
const nsISupportsCString = Components.interfaces.nsISupportsCString;
|
|
|
|
|
2016-01-29 00:23:30 +03:00
|
|
|
/**
|
|
|
|
* Offers to save an image URL to the file system.
|
|
|
|
*
|
|
|
|
* @param aURL (string)
|
|
|
|
* The URL of the image to be saved.
|
|
|
|
* @param aFileName (string)
|
|
|
|
* The suggested filename for the saved file.
|
|
|
|
* @param aFilePickerTitleKey (string, optional)
|
|
|
|
* Localized string key for an alternate title for the file
|
2016-02-06 02:19:59 +03:00
|
|
|
* picker. If set to null, this will default to something sensible.
|
2016-01-29 00:23:30 +03:00
|
|
|
* @param aShouldBypassCache (bool)
|
|
|
|
* If true, the image will always be retrieved from the server instead
|
|
|
|
* of the network or image caches.
|
|
|
|
* @param aSkipPrompt (bool)
|
|
|
|
* If true, we will attempt to save the file with the suggested
|
|
|
|
* filename to the default downloads folder without showing the
|
|
|
|
* file picker.
|
|
|
|
* @param aReferrer (nsIURI, optional)
|
|
|
|
* The referrer URI object (not a URL string) to use, or null
|
|
|
|
* if no referrer should be sent.
|
|
|
|
* @param aDoc (nsIDocument, deprecated, optional)
|
|
|
|
* The content document that the save is being initiated from. If this
|
|
|
|
* is omitted, then aIsContentWindowPrivate must be provided.
|
|
|
|
* @param aContentType (string, optional)
|
|
|
|
* The content type of the image.
|
|
|
|
* @param aContentDisp (string, optional)
|
|
|
|
* The content disposition of the image.
|
|
|
|
* @param aIsContentWindowPrivate (bool)
|
|
|
|
* Whether or not the containing window is in private browsing mode.
|
|
|
|
* Does not need to be provided is aDoc is passed.
|
|
|
|
*/
|
2005-06-28 18:41:11 +04:00
|
|
|
function saveImageURL(aURL, aFileName, aFilePickerTitleKey, aShouldBypassCache,
|
2016-01-29 00:23:30 +03:00
|
|
|
aSkipPrompt, aReferrer, aDoc, aContentType, aContentDisp,
|
2016-12-31 05:47:25 +03:00
|
|
|
aIsContentWindowPrivate) {
|
2015-02-11 23:55:29 +03:00
|
|
|
forbidCPOW(aURL, "saveImageURL", "aURL");
|
|
|
|
forbidCPOW(aReferrer, "saveImageURL", "aReferrer");
|
2016-02-06 02:19:59 +03:00
|
|
|
|
|
|
|
if (aDoc && aIsContentWindowPrivate == undefined) {
|
|
|
|
if (Components.utils.isCrossProcessWrapper(aDoc)) {
|
|
|
|
Deprecated.warning("saveImageURL should not be passed document CPOWs. " +
|
|
|
|
"The caller should pass in the content type and " +
|
|
|
|
"disposition themselves",
|
2016-01-29 00:23:30 +03:00
|
|
|
"https://bugzilla.mozilla.org/show_bug.cgi?id=1243643");
|
|
|
|
}
|
2016-02-06 02:19:59 +03:00
|
|
|
// This will definitely not work for in-browser code or multi-process compatible
|
|
|
|
// add-ons due to bug 1233497, which makes unsafe CPOW usage throw by default.
|
|
|
|
Deprecated.warning("saveImageURL should be passed the private state of " +
|
|
|
|
"the containing window.",
|
|
|
|
"https://bugzilla.mozilla.org/show_bug.cgi?id=1243643");
|
|
|
|
aIsContentWindowPrivate =
|
|
|
|
PrivateBrowsingUtils.isContentWindowPrivate(aDoc.defaultView);
|
2016-01-29 00:23:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// We'd better have the private state by now.
|
|
|
|
if (aIsContentWindowPrivate == undefined) {
|
|
|
|
throw new Error("saveImageURL couldn't compute private state of content window");
|
|
|
|
}
|
2015-02-11 23:55:29 +03:00
|
|
|
|
2016-01-29 00:23:30 +03:00
|
|
|
if (!aShouldBypassCache && (aDoc && !Components.utils.isCrossProcessWrapper(aDoc)) &&
|
2015-03-31 20:59:06 +03:00
|
|
|
(!aContentType && !aContentDisp)) {
|
2005-06-28 18:41:11 +04:00
|
|
|
try {
|
2012-09-17 20:18:40 +04:00
|
|
|
var imageCache = Components.classes["@mozilla.org/image/tools;1"]
|
|
|
|
.getService(Components.interfaces.imgITools)
|
|
|
|
.getImgCacheForDocument(aDoc);
|
2005-06-28 18:41:11 +04:00
|
|
|
var props =
|
2015-10-26 23:03:26 +03:00
|
|
|
imageCache.findEntryProperties(makeURI(aURL, getCharsetforSave(null)), aDoc);
|
2005-06-28 18:41:11 +04:00
|
|
|
if (props) {
|
2015-03-31 20:59:06 +03:00
|
|
|
aContentType = props.get("type", nsISupportsCString);
|
|
|
|
aContentDisp = props.get("content-disposition", nsISupportsCString);
|
2005-06-28 18:41:11 +04:00
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
// Failure to get type and content-disposition off the image is non-fatal
|
|
|
|
}
|
|
|
|
}
|
2016-01-29 00:23:30 +03:00
|
|
|
|
2015-03-31 20:59:06 +03:00
|
|
|
internalSave(aURL, null, aFileName, aContentDisp, aContentType,
|
2009-02-19 22:18:02 +03:00
|
|
|
aShouldBypassCache, aFilePickerTitleKey, null, aReferrer,
|
2016-01-29 00:23:30 +03:00
|
|
|
null, aSkipPrompt, null, aIsContentWindowPrivate);
|
2005-03-24 00:36:58 +03:00
|
|
|
}
|
|
|
|
|
2015-08-06 00:25:39 +03:00
|
|
|
// This is like saveDocument, but takes any browser/frame-like element
|
2017-08-19 23:32:58 +03:00
|
|
|
// and saves the current document inside it,
|
2015-08-06 00:25:39 +03:00
|
|
|
// whether in-process or out-of-process.
|
2016-12-31 05:47:25 +03:00
|
|
|
function saveBrowser(aBrowser, aSkipPrompt, aOuterWindowID = 0) {
|
2015-08-06 00:25:39 +03:00
|
|
|
if (!aBrowser) {
|
|
|
|
throw "Must have a browser when calling saveBrowser";
|
|
|
|
}
|
2017-08-19 23:32:58 +03:00
|
|
|
let persistable = aBrowser.frameLoader;
|
2015-08-14 04:08:02 +03:00
|
|
|
let stack = Components.stack.caller;
|
2015-08-06 17:44:16 +03:00
|
|
|
persistable.startPersistence(aOuterWindowID, {
|
2016-12-30 02:34:54 +03:00
|
|
|
onDocumentReady(document) {
|
2015-08-06 00:25:39 +03:00
|
|
|
saveDocument(document, aSkipPrompt);
|
2015-08-14 04:08:02 +03:00
|
|
|
},
|
2016-12-30 02:34:54 +03:00
|
|
|
onError(status) {
|
2015-08-14 04:08:02 +03:00
|
|
|
throw new Components.Exception("saveBrowser failed asynchronously in startPersistence",
|
|
|
|
status, stack);
|
2015-08-06 00:25:39 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Saves a document; aDocument can be an nsIWebBrowserPersistDocument
|
|
|
|
// (see saveBrowser, above) or an nsIDOMDocument.
|
|
|
|
//
|
|
|
|
// aDocument can also be a CPOW for a remote nsIDOMDocument, in which
|
|
|
|
// case "save as" modes that serialize the document's DOM are
|
|
|
|
// unavailable. This is a temporary measure for the "Save Frame As"
|
2015-08-14 04:08:02 +03:00
|
|
|
// command (bug 1141337) and pre-e10s add-ons.
|
2016-12-31 05:47:25 +03:00
|
|
|
function saveDocument(aDocument, aSkipPrompt) {
|
2015-08-06 00:25:39 +03:00
|
|
|
const Ci = Components.interfaces;
|
|
|
|
|
2005-06-28 18:41:11 +04:00
|
|
|
if (!aDocument)
|
|
|
|
throw "Must have a document when calling saveDocument";
|
2005-03-24 00:36:58 +03:00
|
|
|
|
2015-08-06 00:25:39 +03:00
|
|
|
let contentDisposition = null;
|
|
|
|
let cacheKeyInt = null;
|
2009-02-19 22:18:02 +03:00
|
|
|
|
2015-08-06 00:25:39 +03:00
|
|
|
if (aDocument instanceof Ci.nsIWebBrowserPersistDocument) {
|
|
|
|
// nsIWebBrowserPersistDocument exposes these directly.
|
|
|
|
contentDisposition = aDocument.contentDisposition;
|
|
|
|
cacheKeyInt = aDocument.cacheKey;
|
|
|
|
} else if (aDocument instanceof Ci.nsIDOMDocument) {
|
|
|
|
// Otherwise it's an actual nsDocument (and possibly a CPOW).
|
|
|
|
// We want to use cached data because the document is currently visible.
|
|
|
|
let ifreq =
|
|
|
|
aDocument.defaultView
|
|
|
|
.QueryInterface(Ci.nsIInterfaceRequestor);
|
2009-02-19 22:18:02 +03:00
|
|
|
|
2015-08-06 00:25:39 +03:00
|
|
|
try {
|
|
|
|
contentDisposition =
|
|
|
|
ifreq.getInterface(Ci.nsIDOMWindowUtils)
|
|
|
|
.getDocumentMetadata("content-disposition");
|
|
|
|
} catch (ex) {
|
|
|
|
// Failure to get a content-disposition is ok
|
|
|
|
}
|
2015-05-05 04:58:21 +03:00
|
|
|
|
2015-08-06 00:25:39 +03:00
|
|
|
try {
|
|
|
|
let shEntry =
|
|
|
|
ifreq.getInterface(Ci.nsIWebNavigation)
|
|
|
|
.QueryInterface(Ci.nsIWebPageDescriptor)
|
|
|
|
.currentDescriptor
|
|
|
|
.QueryInterface(Ci.nsISHEntry);
|
|
|
|
|
|
|
|
let cacheKey = shEntry.cacheKey
|
|
|
|
.QueryInterface(Ci.nsISupportsPRUint32)
|
|
|
|
.data;
|
|
|
|
// cacheKey might be a CPOW, which can't be passed to native
|
|
|
|
// code, but the data attribute is just a number.
|
|
|
|
cacheKeyInt = cacheKey.data;
|
|
|
|
} catch (ex) {
|
|
|
|
// We might not find it in the cache. Oh, well.
|
|
|
|
}
|
|
|
|
}
|
2015-05-05 04:58:21 +03:00
|
|
|
|
2015-08-06 00:25:39 +03:00
|
|
|
// Convert the cacheKey back into an XPCOM object.
|
|
|
|
let cacheKey = null;
|
|
|
|
if (cacheKeyInt) {
|
2015-05-05 04:58:21 +03:00
|
|
|
cacheKey = Cc["@mozilla.org/supports-PRUint32;1"]
|
2015-08-06 00:25:39 +03:00
|
|
|
.createInstance(Ci.nsISupportsPRUint32);
|
|
|
|
cacheKey.data = cacheKeyInt;
|
2009-02-19 22:18:02 +03:00
|
|
|
}
|
|
|
|
|
2015-08-06 00:25:39 +03:00
|
|
|
internalSave(aDocument.documentURI, aDocument, null, contentDisposition,
|
2007-11-27 03:25:43 +03:00
|
|
|
aDocument.contentType, false, null, null,
|
|
|
|
aDocument.referrer ? makeURI(aDocument.referrer) : null,
|
2012-10-03 21:10:20 +04:00
|
|
|
aDocument, aSkipPrompt, cacheKey);
|
2005-06-28 18:41:11 +04:00
|
|
|
}
|
2005-03-24 00:36:58 +03:00
|
|
|
|
2007-07-16 04:02:09 +04:00
|
|
|
function DownloadListener(win, transfer) {
|
|
|
|
function makeClosure(name) {
|
|
|
|
return function() {
|
|
|
|
transfer[name].apply(transfer, arguments);
|
2017-10-15 21:50:30 +03:00
|
|
|
};
|
2007-07-16 04:02:09 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
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 = {
|
2016-12-31 05:47:25 +03:00
|
|
|
QueryInterface: function dl_qi(aIID) {
|
2007-07-16 04:02:09 +04:00
|
|
|
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;
|
|
|
|
},
|
|
|
|
|
2016-12-31 05:47:25 +03:00
|
|
|
getInterface: function dl_gi(aIID) {
|
2007-07-16 04:02:09 +04:00
|
|
|
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;
|
|
|
|
}
|
2017-10-15 21:50:30 +03:00
|
|
|
};
|
2007-07-16 04:02:09 +04:00
|
|
|
|
2007-12-11 12:57:36 +03:00
|
|
|
const kSaveAsType_Complete = 0; // Save document with attached objects.
|
2015-10-07 15:03:21 +03:00
|
|
|
XPCOMUtils.defineConstant(this, "kSaveAsType_Complete", 0);
|
2007-12-11 12:57:36 +03:00
|
|
|
// const kSaveAsType_URL = 1; // Save document or URL by itself.
|
|
|
|
const kSaveAsType_Text = 2; // Save document, converting to plain text.
|
2015-10-07 15:03:21 +03:00
|
|
|
XPCOMUtils.defineConstant(this, "kSaveAsType_Text", kSaveAsType_Text);
|
2007-12-11 12:57:36 +03:00
|
|
|
|
2005-06-28 18:41:11 +04:00
|
|
|
/**
|
2009-03-30 22:47:31 +04:00
|
|
|
* internalSave: Used when saving a document or URL.
|
|
|
|
*
|
|
|
|
* If aChosenData is null, this method:
|
|
|
|
* - Determines a local target filename to use
|
2005-06-28 18:41:11 +04:00
|
|
|
* - Prompts the user to confirm the destination filename and save mode
|
2009-03-30 22:47:31 +04: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:
|
2005-06-28 18:41:11 +04:00
|
|
|
* - Creates a 'Persist' object (which will perform the saving in the
|
|
|
|
* background) and then starts it.
|
2009-03-30 22:47:31 +04: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.
|
2005-06-28 18:41:11 +04:00
|
|
|
*
|
2008-11-16 02:19:10 +03:00
|
|
|
* @param aURL
|
|
|
|
* The String representation of the URL of the document being saved
|
|
|
|
* @param aDocument
|
|
|
|
* The document to be saved
|
|
|
|
* @param aDefaultFileName
|
2016-02-05 23:16:39 +03:00
|
|
|
* The caller-provided suggested filename if we don't
|
2005-06-28 18:41:11 +04:00
|
|
|
* find a better one
|
2008-11-16 02:19:10 +03: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.
|
2015-07-09 00:18:38 +03:00
|
|
|
* @param aInitiatingDocument [optional]
|
2012-10-03 21:10:20 +04:00
|
|
|
* The document from which the save was initiated.
|
2015-07-09 00:18:38 +03:00
|
|
|
* If this is omitted then aIsContentWindowPrivate has to be provided.
|
2008-11-16 02:19:10 +03: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 22:18:02 +03:00
|
|
|
* @param aCacheKey [optional]
|
|
|
|
* If set will be passed to saveURI. See nsIWebBrowserPersist for
|
|
|
|
* allowed values.
|
2015-07-09 00:18:38 +03:00
|
|
|
* @param aIsContentWindowPrivate [optional]
|
|
|
|
* This parameter is provided when the aInitiatingDocument is not a
|
|
|
|
* real document object. Stores whether aInitiatingDocument.defaultView
|
|
|
|
* was private or not.
|
2005-06-28 18:41:11 +04:00
|
|
|
*/
|
|
|
|
function internalSave(aURL, aDocument, aDefaultFileName, aContentDisposition,
|
|
|
|
aContentType, aShouldBypassCache, aFilePickerTitleKey,
|
2012-10-03 21:10:20 +04:00
|
|
|
aChosenData, aReferrer, aInitiatingDocument, aSkipPrompt,
|
2016-12-31 05:47:25 +03:00
|
|
|
aCacheKey, aIsContentWindowPrivate) {
|
2015-02-11 23:55:29 +03:00
|
|
|
forbidCPOW(aURL, "internalSave", "aURL");
|
|
|
|
forbidCPOW(aReferrer, "internalSave", "aReferrer");
|
|
|
|
forbidCPOW(aCacheKey, "internalSave", "aCacheKey");
|
|
|
|
// Allow aInitiatingDocument to be a CPOW.
|
|
|
|
|
2005-06-28 18:41:11 +04:00
|
|
|
if (aSkipPrompt == undefined)
|
|
|
|
aSkipPrompt = false;
|
|
|
|
|
2009-02-19 22:18:02 +03:00
|
|
|
if (aCacheKey == undefined)
|
|
|
|
aCacheKey = null;
|
|
|
|
|
2005-06-28 18:41:11 +04:00
|
|
|
// Note: aDocument == null when this code is used by save-link-as...
|
2009-03-30 22:47:31 +04:00
|
|
|
var saveMode = GetSaveModeForContentType(aContentType, aDocument);
|
2005-06-28 18:41:11 +04:00
|
|
|
|
2009-03-30 22:47:31 +04:00
|
|
|
var file, sourceURI, saveAsType;
|
2005-06-28 18:41:11 +04: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 22:47:31 +04:00
|
|
|
if (aChosenData) {
|
2005-06-28 18:41:11 +04:00
|
|
|
file = aChosenData.file;
|
2009-03-30 22:47:31 +04:00
|
|
|
sourceURI = aChosenData.uri;
|
|
|
|
saveAsType = kSaveAsType_Complete;
|
2013-04-01 10:13:37 +04:00
|
|
|
|
|
|
|
continueSave();
|
2009-03-30 22:47:31 +04:00
|
|
|
} else {
|
2005-12-21 21:28:46 +03:00
|
|
|
var charset = null;
|
|
|
|
if (aDocument)
|
|
|
|
charset = aDocument.characterSet;
|
2009-03-30 22:47:31 +04:00
|
|
|
var fileInfo = new FileInfo(aDefaultFileName);
|
2005-12-21 21:28:46 +03:00
|
|
|
initFileInfo(fileInfo, aURL, charset, aDocument,
|
|
|
|
aContentType, aContentDisposition);
|
2009-03-30 22:47:31 +04:00
|
|
|
sourceURI = fileInfo.uri;
|
|
|
|
|
2005-06-28 18:41:11 +04:00
|
|
|
var fpParams = {
|
|
|
|
fpTitleKey: aFilePickerTitleKey,
|
2016-12-30 02:34:54 +03:00
|
|
|
fileInfo,
|
2005-06-28 18:41:11 +04:00
|
|
|
contentType: aContentType,
|
2016-12-30 02:34:54 +03:00
|
|
|
saveMode,
|
2009-03-30 22:47:31 +04:00
|
|
|
saveAsType: kSaveAsType_Complete,
|
2016-12-30 02:34:54 +03:00
|
|
|
file
|
2005-06-28 18:41:11 +04:00
|
|
|
};
|
|
|
|
|
2011-06-10 03:19:16 +04:00
|
|
|
// Find a URI to use for determining last-downloaded-to directory
|
|
|
|
let relatedURI = aReferrer || sourceURI;
|
|
|
|
|
2013-10-25 10:03:18 +04:00
|
|
|
promiseTargetFile(fpParams, aSkipPrompt, relatedURI).then(aDialogAccepted => {
|
|
|
|
if (!aDialogAccepted)
|
2013-04-01 10:13:37 +04:00
|
|
|
return;
|
|
|
|
|
|
|
|
saveAsType = fpParams.saveAsType;
|
|
|
|
file = fpParams.file;
|
2005-06-28 18:41:11 +04:00
|
|
|
|
2013-04-01 10:13:37 +04:00
|
|
|
continueSave();
|
2017-06-19 13:32:37 +03:00
|
|
|
}).catch(Components.utils.reportError);
|
2005-03-24 00:37:22 +03:00
|
|
|
}
|
|
|
|
|
2013-04-01 10:13:37 +04: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.
|
2015-05-05 04:58:21 +03:00
|
|
|
let nonCPOWDocument =
|
|
|
|
aDocument && !Components.utils.isCrossProcessWrapper(aDocument);
|
|
|
|
|
2015-08-06 00:25:39 +03:00
|
|
|
let isPrivate = aIsContentWindowPrivate;
|
|
|
|
if (isPrivate === undefined) {
|
|
|
|
isPrivate = aInitiatingDocument instanceof Components.interfaces.nsIDOMDocument
|
|
|
|
? PrivateBrowsingUtils.isContentWindowPrivate(aInitiatingDocument.defaultView)
|
|
|
|
: aInitiatingDocument.isPrivate;
|
|
|
|
}
|
|
|
|
|
2013-04-01 10:13:37 +04:00
|
|
|
var persistArgs = {
|
2016-12-30 02:34:54 +03:00
|
|
|
sourceURI,
|
2017-03-21 21:29:43 +03:00
|
|
|
sourceReferrer: aReferrer,
|
|
|
|
sourceDocument: useSaveDocument ? aDocument : null,
|
|
|
|
targetContentType: (saveAsType == kSaveAsType_Text) ? "text/plain" : null,
|
|
|
|
targetFile: file,
|
|
|
|
sourceCacheKey: aCacheKey,
|
|
|
|
sourcePostData: nonCPOWDocument ? getPostData(aDocument) : null,
|
|
|
|
bypassCache: aShouldBypassCache,
|
2016-12-30 02:34:54 +03:00
|
|
|
isPrivate,
|
2013-04-01 10:13:37 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
// Start the actual save process
|
|
|
|
internalPersist(persistArgs);
|
|
|
|
}
|
2009-03-30 22:47:31 +04: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
|
2015-08-06 00:25:39 +03:00
|
|
|
* @param persistArgs.isPrivate
|
|
|
|
* Indicates whether this is taking place in a private browsing context.
|
2009-03-30 22:47:31 +04:00
|
|
|
*/
|
2016-12-31 05:47:25 +03:00
|
|
|
function internalPersist(persistArgs) {
|
2005-03-24 00:37:22 +03:00
|
|
|
var persist = makeWebBrowserPersist();
|
|
|
|
|
|
|
|
// Calculate persist flags.
|
|
|
|
const nsIWBP = Components.interfaces.nsIWebBrowserPersist;
|
2009-06-16 18:30:25 +04:00
|
|
|
const flags = nsIWBP.PERSIST_FLAGS_REPLACE_EXISTING_FILES |
|
|
|
|
nsIWBP.PERSIST_FLAGS_FORCE_ALLOW_COOKIES;
|
2009-03-30 22:47:31 +04:00
|
|
|
if (persistArgs.bypassCache)
|
2005-03-24 00:37:22 +03:00
|
|
|
persist.persistFlags = flags | nsIWBP.PERSIST_FLAGS_BYPASS_CACHE;
|
2005-06-28 18:41:11 +04:00
|
|
|
else
|
2005-03-24 00:37:22 +03:00
|
|
|
persist.persistFlags = flags | nsIWBP.PERSIST_FLAGS_FROM_CACHE;
|
|
|
|
|
2005-06-28 18:41:11 +04:00
|
|
|
// Leave it to WebBrowserPersist to discover the encoding type (or lack thereof):
|
|
|
|
persist.persistFlags |= nsIWBP.PERSIST_FLAGS_AUTODETECT_APPLY_CONVERSION;
|
|
|
|
|
2009-03-30 22:47:31 +04:00
|
|
|
// Find the URI associated with the target file
|
|
|
|
var targetFileURL = makeFileURI(persistArgs.targetFile);
|
|
|
|
|
2005-03-24 00:37:22 +03:00
|
|
|
// Create download and initiate it (below)
|
2005-03-24 00:39:43 +03:00
|
|
|
var tr = Components.classes["@mozilla.org/transfer;1"].createInstance(Components.interfaces.nsITransfer);
|
2009-03-30 22:47:31 +04:00
|
|
|
tr.init(persistArgs.sourceURI,
|
2015-08-06 00:25:39 +03:00
|
|
|
targetFileURL, "", null, null, null, persist, persistArgs.isPrivate);
|
2009-03-30 22:47:31 +04:00
|
|
|
persist.progressListener = new DownloadListener(window, tr);
|
2005-03-24 00:37:22 +03:00
|
|
|
|
2009-03-30 22:47:31 +04:00
|
|
|
if (persistArgs.sourceDocument) {
|
2005-03-24 00:37:22 +03:00
|
|
|
// Saving a Document, not a URI:
|
|
|
|
var filesFolder = null;
|
2009-03-30 22:47:31 +04:00
|
|
|
if (persistArgs.targetContentType != "text/plain") {
|
2005-06-28 18:41:11 +04:00
|
|
|
// Create the local directory into which to save associated files.
|
2009-03-30 22:47:31 +04:00
|
|
|
filesFolder = persistArgs.targetFile.clone();
|
2005-06-28 18:41:11 +04:00
|
|
|
|
2007-12-18 08:48:09 +03:00
|
|
|
var nameWithoutExtension = getFileBaseName(filesFolder.leafName);
|
2010-03-25 16:16:20 +03:00
|
|
|
var filesFolderLeafName =
|
|
|
|
ContentAreaUtils.stringBundle
|
|
|
|
.formatStringFromName("filesFolder", [nameWithoutExtension], 1);
|
2005-03-24 00:37:22 +03:00
|
|
|
|
|
|
|
filesFolder.leafName = filesFolderLeafName;
|
|
|
|
}
|
2005-06-28 18:41:11 +04:00
|
|
|
|
2005-03-24 00:37:22 +03:00
|
|
|
var encodingFlags = 0;
|
2009-03-30 22:47:31 +04:00
|
|
|
if (persistArgs.targetContentType == "text/plain") {
|
2005-03-24 00:37:22 +03:00
|
|
|
encodingFlags |= nsIWBP.ENCODE_FLAGS_FORMATTED;
|
|
|
|
encodingFlags |= nsIWBP.ENCODE_FLAGS_ABSOLUTE_LINKS;
|
2005-06-28 18:41:11 +04:00
|
|
|
encodingFlags |= nsIWBP.ENCODE_FLAGS_NOFRAMES_CONTENT;
|
2016-12-31 05:47:25 +03:00
|
|
|
} else {
|
2005-03-24 00:37:43 +03:00
|
|
|
encodingFlags |= nsIWBP.ENCODE_FLAGS_ENCODE_BASIC_ENTITIES;
|
|
|
|
}
|
2005-06-28 18:41:11 +04:00
|
|
|
|
2005-03-24 00:37:22 +03:00
|
|
|
const kWrapColumn = 80;
|
2009-03-30 22:47:31 +04:00
|
|
|
persist.saveDocument(persistArgs.sourceDocument, targetFileURL, filesFolder,
|
|
|
|
persistArgs.targetContentType, encodingFlags, kWrapColumn);
|
2005-03-24 00:37:22 +03:00
|
|
|
} else {
|
2015-02-11 23:55:29 +03:00
|
|
|
persist.savePrivacyAwareURI(persistArgs.sourceURI,
|
|
|
|
persistArgs.sourceCacheKey,
|
|
|
|
persistArgs.sourceReferrer,
|
2017-01-05 06:29:56 +03:00
|
|
|
Components.interfaces.nsIHttpChannel.REFERRER_POLICY_UNSET,
|
2015-02-11 23:55:29 +03:00
|
|
|
persistArgs.sourcePostData,
|
|
|
|
null,
|
|
|
|
targetFileURL,
|
2015-08-06 00:25:39 +03:00
|
|
|
persistArgs.isPrivate);
|
2005-03-24 00:37:22 +03:00
|
|
|
}
|
|
|
|
}
|
2005-03-24 00:36:59 +03:00
|
|
|
|
2005-06-28 18:41:11 +04: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-21 09:54:41 +04:00
|
|
|
* @param aFileAutoChosen This is an nsIFile object that has been
|
2005-06-28 18:41:11 +04: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
|
2016-02-05 23:16:39 +03:00
|
|
|
* cannot 'discover' the filename from the url
|
2005-06-28 18:41:11 +04:00
|
|
|
* @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
|
2005-12-21 21:28:46 +03:00
|
|
|
* @param aURLCharset The charset of aURL.
|
2005-06-28 18:41:11 +04:00
|
|
|
* @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.
|
|
|
|
*/
|
2005-12-21 21:28:46 +03:00
|
|
|
function initFileInfo(aFI, aURL, aURLCharset, aDocument,
|
2016-12-31 05:47:25 +03:00
|
|
|
aContentType, aContentDisposition) {
|
2005-06-28 18:41:11 +04:00
|
|
|
try {
|
|
|
|
// Get an nsIURI object from aURL if possible:
|
|
|
|
try {
|
2005-12-21 21:28:46 +03:00
|
|
|
aFI.uri = makeURI(aURL, aURLCharset);
|
2005-06-28 18:41:11 +04:00
|
|
|
// 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) {
|
|
|
|
}
|
2005-03-24 00:36:59 +03:00
|
|
|
|
2005-06-28 18:41:11 +04:00
|
|
|
// 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-18 08:48:09 +03:00
|
|
|
aFI.fileBaseName = getFileBaseName(aFI.fileName);
|
2005-06-28 18:41:11 +04:00
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
}
|
|
|
|
}
|
2005-03-24 00:36:58 +03:00
|
|
|
|
2016-02-05 23:16:39 +03:00
|
|
|
/**
|
2009-08-08 14:01:50 +04: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.
|
|
|
|
* @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-10 03:19:16 +04:00
|
|
|
* @param aRelatedURI
|
|
|
|
* An nsIURI associated with the download. The last used
|
2016-02-05 23:16:39 +03:00
|
|
|
* directory of the picker is retrieved from/stored in the
|
2011-06-10 03:19:16 +04:00
|
|
|
* Content Pref Service using this URI.
|
2013-10-25 10:03:18 +04:00
|
|
|
* @return Promise
|
|
|
|
* @resolve a boolean. When true, it indicates that the file picker dialog
|
|
|
|
* is accepted.
|
2009-08-08 14:01:50 +04:00
|
|
|
*/
|
2016-12-31 05:47:25 +03:00
|
|
|
function promiseTargetFile(aFpP, /* optional */ aSkipPrompt, /* optional */ aRelatedURI) {
|
2017-05-12 15:42:39 +03:00
|
|
|
return (async function() {
|
2013-10-25 10:03:18 +04:00
|
|
|
let downloadLastDir = new DownloadLastDir(window);
|
|
|
|
let prefBranch = Services.prefs.getBranch("browser.download.");
|
|
|
|
let useDownloadDir = prefBranch.getBoolPref("useDownloadDir");
|
|
|
|
|
|
|
|
if (!aSkipPrompt)
|
|
|
|
useDownloadDir = false;
|
|
|
|
|
|
|
|
// Default to the user's default downloads directory configured
|
|
|
|
// through download prefs.
|
2017-05-12 15:42:39 +03:00
|
|
|
let dirPath = await Downloads.getPreferredDownloadsDirectory();
|
|
|
|
let dirExists = await OS.File.exists(dirPath);
|
2013-10-25 10:03:18 +04:00
|
|
|
let dir = new FileUtils.File(dirPath);
|
|
|
|
|
|
|
|
if (useDownloadDir && dirExists) {
|
|
|
|
dir.append(getNormalizedLeafName(aFpP.fileInfo.fileName,
|
|
|
|
aFpP.fileInfo.fileExt));
|
|
|
|
aFpP.file = uniqueFile(dir);
|
2016-02-05 23:16:39 +03:00
|
|
|
return true;
|
2013-10-25 10:03:18 +04:00
|
|
|
}
|
2009-08-08 14:01:50 +04:00
|
|
|
|
2013-10-25 10:03:18 +04:00
|
|
|
// We must prompt for the file name explicitly.
|
|
|
|
// If we must prompt because we were asked to...
|
2017-05-12 15:55:27 +03:00
|
|
|
let file = await new Promise(resolve => {
|
|
|
|
if (useDownloadDir) {
|
|
|
|
// Keep async behavior in both branches
|
|
|
|
Services.tm.dispatchToMainThread(function() {
|
|
|
|
resolve(null);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
downloadLastDir.getFileAsync(aRelatedURI, function getFileAsyncCB(aFile) {
|
|
|
|
resolve(aFile);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2017-05-12 15:42:39 +03:00
|
|
|
if (file && (await OS.File.exists(file.path))) {
|
2013-10-25 10:03:18 +04:00
|
|
|
dir = file;
|
|
|
|
dirExists = true;
|
|
|
|
}
|
2007-08-29 23:58:59 +04:00
|
|
|
|
2013-04-01 10:13:37 +04:00
|
|
|
if (!dirExists) {
|
|
|
|
// Default to desktop.
|
2013-10-25 10:03:18 +04:00
|
|
|
dir = Services.dirsvc.get("Desk", Components.interfaces.nsIFile);
|
2005-03-24 00:37:11 +03:00
|
|
|
}
|
2013-04-01 10:13:37 +04:00
|
|
|
|
2013-10-25 10:03:18 +04:00
|
|
|
let fp = makeFilePicker();
|
|
|
|
let titleKey = aFpP.fpTitleKey || "SaveLinkTitle";
|
2013-04-01 10:13:37 +04:00
|
|
|
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) {
|
2017-03-07 17:29:48 +03:00
|
|
|
// eslint-disable-next-line mozilla/use-default-preference-values
|
2013-04-01 10:13:37 +04:00
|
|
|
try {
|
2013-10-25 10:03:18 +04:00
|
|
|
fp.filterIndex = prefBranch.getIntPref("save_converter_index");
|
2016-12-31 05:47:25 +03:00
|
|
|
} catch (e) {
|
2013-04-01 10:13:37 +04:00
|
|
|
}
|
2005-03-24 00:36:58 +03:00
|
|
|
}
|
2007-08-21 21:22:02 +04:00
|
|
|
|
2017-05-12 15:55:27 +03:00
|
|
|
let result = await new Promise(resolve => {
|
|
|
|
fp.open(function(aResult) {
|
|
|
|
resolve(aResult);
|
|
|
|
});
|
2013-10-25 10:03:18 +04:00
|
|
|
});
|
|
|
|
if (result == Components.interfaces.nsIFilePicker.returnCancel || !fp.file) {
|
2016-02-05 23:16:39 +03:00
|
|
|
return false;
|
2013-04-01 10:13:37 +04:00
|
|
|
}
|
2008-11-14 01:34:41 +03:00
|
|
|
|
2013-04-01 10:13:37 +04:00
|
|
|
if (aFpP.saveMode != SAVEMODE_FILEONLY)
|
2013-10-25 10:03:18 +04:00
|
|
|
prefBranch.setIntPref("save_converter_index", fp.filterIndex);
|
2005-03-24 00:37:12 +03:00
|
|
|
|
2013-04-01 10:13:37 +04:00
|
|
|
// Do not store the last save directory as a pref inside the private browsing mode
|
2013-10-25 10:03:18 +04:00
|
|
|
downloadLastDir.setFile(aRelatedURI, fp.file.parent);
|
2005-03-24 00:37:22 +03:00
|
|
|
|
2013-04-01 10:13:37 +04:00
|
|
|
fp.file.leafName = validateFileName(fp.file.leafName);
|
2012-10-18 18:44:36 +04:00
|
|
|
|
2013-04-01 10:13:37 +04:00
|
|
|
aFpP.saveAsType = fp.filterIndex;
|
|
|
|
aFpP.file = fp.file;
|
|
|
|
aFpP.fileURL = fp.fileURL;
|
2013-10-25 10:03:18 +04:00
|
|
|
|
2016-02-05 23:16:39 +03:00
|
|
|
return true;
|
2017-05-12 15:42:39 +03:00
|
|
|
})();
|
2009-08-08 14:01:50 +04: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.
|
2016-12-31 05:47:25 +03:00
|
|
|
function uniqueFile(aLocalFile) {
|
2009-08-08 14:01:50 +04:00
|
|
|
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)$&");
|
2016-12-31 05:47:25 +03:00
|
|
|
} else {
|
2009-08-08 14:01:50 +04:00
|
|
|
// replace the last (n) in the filename with (n+1)
|
|
|
|
aLocalFile.leafName = aLocalFile.leafName.replace(/^(.*\()\d+\)/, "$1" + (collisionCount + 1) + ")");
|
2005-03-24 00:37:13 +03:00
|
|
|
}
|
2005-03-24 00:36:59 +03:00
|
|
|
}
|
2009-08-08 14:01:50 +04:00
|
|
|
return aLocalFile;
|
2005-03-24 00:36:58 +03:00
|
|
|
}
|
|
|
|
|
2014-01-30 02:00:17 +04:00
|
|
|
/**
|
|
|
|
* Download a URL using the new jsdownloads API.
|
|
|
|
*
|
|
|
|
* @param aURL
|
|
|
|
* the url to download
|
|
|
|
* @param [optional] aFileName
|
|
|
|
* the destination file name, if omitted will be obtained from the url.
|
|
|
|
* @param aInitiatingDocument
|
|
|
|
* The document from which the download was initiated.
|
|
|
|
*/
|
|
|
|
function DownloadURL(aURL, aFileName, aInitiatingDocument) {
|
|
|
|
// For private browsing, try to get document out of the most recent browser
|
|
|
|
// window, or provide our own if there's no browser window.
|
|
|
|
let isPrivate = aInitiatingDocument.defaultView
|
2014-12-16 20:09:12 +03:00
|
|
|
.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
|
|
|
|
.getInterface(Components.interfaces.nsIWebNavigation)
|
|
|
|
.QueryInterface(Components.interfaces.nsILoadContext)
|
2014-01-30 02:00:17 +04:00
|
|
|
.usePrivateBrowsing;
|
|
|
|
|
|
|
|
let fileInfo = new FileInfo(aFileName);
|
|
|
|
initFileInfo(fileInfo, aURL, null, null, null, null);
|
|
|
|
|
|
|
|
let filepickerParams = {
|
2016-12-30 02:34:54 +03:00
|
|
|
fileInfo,
|
2014-01-30 02:00:17 +04:00
|
|
|
saveMode: SAVEMODE_FILEONLY
|
|
|
|
};
|
|
|
|
|
2017-05-12 15:42:39 +03:00
|
|
|
(async function() {
|
|
|
|
let accepted = await promiseTargetFile(filepickerParams, true, fileInfo.uri);
|
2014-01-30 02:00:17 +04:00
|
|
|
if (!accepted)
|
|
|
|
return;
|
|
|
|
|
|
|
|
let file = filepickerParams.file;
|
2017-05-12 15:42:39 +03:00
|
|
|
let download = await Downloads.createDownload({
|
2016-12-30 02:34:54 +03:00
|
|
|
source: { url: aURL, isPrivate },
|
2014-01-30 02:00:17 +04:00
|
|
|
target: { path: file.path, partFilePath: file.path + ".part" }
|
|
|
|
});
|
|
|
|
download.tryToKeepPartialData = true;
|
2016-08-22 16:22:18 +03:00
|
|
|
|
|
|
|
// Ignore errors because failures are reported through the download list.
|
|
|
|
download.start().catch(() => {});
|
2014-01-30 02:00:17 +04:00
|
|
|
|
|
|
|
// Add the download to the list, allowing it to be managed.
|
2017-05-12 15:42:39 +03:00
|
|
|
let list = await Downloads.getList(Downloads.ALL);
|
2014-01-30 02:00:17 +04:00
|
|
|
list.add(download);
|
2017-06-19 13:32:37 +03:00
|
|
|
})().catch(Components.utils.reportError);
|
2014-01-30 02:00:17 +04:00
|
|
|
}
|
|
|
|
|
2005-06-28 18:41:11 +04:00
|
|
|
// We have no DOM, and can only save the URL as is.
|
|
|
|
const SAVEMODE_FILEONLY = 0x00;
|
2015-10-07 15:03:21 +03:00
|
|
|
XPCOMUtils.defineConstant(this, "SAVEMODE_FILEONLY", SAVEMODE_FILEONLY);
|
2005-06-28 18:41:11 +04:00
|
|
|
// We have a DOM and can save as complete.
|
|
|
|
const SAVEMODE_COMPLETE_DOM = 0x01;
|
2015-10-07 15:03:21 +03:00
|
|
|
XPCOMUtils.defineConstant(this, "SAVEMODE_COMPLETE_DOM", SAVEMODE_COMPLETE_DOM);
|
2005-06-28 18:41:11 +04:00
|
|
|
// We have a DOM which we can serialize as text.
|
|
|
|
const SAVEMODE_COMPLETE_TEXT = 0x02;
|
2015-10-07 15:03:21 +03:00
|
|
|
XPCOMUtils.defineConstant(this, "SAVEMODE_COMPLETE_TEXT", SAVEMODE_COMPLETE_TEXT);
|
2005-06-28 18:41:11 +04:00
|
|
|
|
|
|
|
// 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.
|
2016-12-31 05:47:25 +03:00
|
|
|
function appendFiltersForContentType(aFilePicker, aContentType, aFileExtension, aSaveMode) {
|
2005-06-28 18:41:11 +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;
|
|
|
|
|
2014-11-18 22:35:47 +03:00
|
|
|
// Every case where GetSaveModeForContentType can return non-FILEONLY
|
|
|
|
// modes must be handled here.
|
|
|
|
if (aSaveMode != SAVEMODE_FILEONLY) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2005-06-28 18:41:11 +04:00
|
|
|
|
2014-11-18 22:35:47 +03:00
|
|
|
if (!bundleName) {
|
2005-06-28 18:41:11 +04:00
|
|
|
if (aSaveMode != SAVEMODE_FILEONLY)
|
|
|
|
throw "Invalid save mode for type '" + aContentType + "'";
|
|
|
|
|
2005-03-24 00:37:26 +03:00
|
|
|
var mimeInfo = getMIMEInfoForType(aContentType, aFileExtension);
|
2005-03-24 00:36:58 +03:00
|
|
|
if (mimeInfo) {
|
2005-03-24 00:37:09 +03:00
|
|
|
|
|
|
|
var extEnumerator = mimeInfo.getFileExtensions();
|
|
|
|
|
2005-03-24 00:36:58 +03:00
|
|
|
var extString = "";
|
2005-03-24 00:37:09 +03:00
|
|
|
while (extEnumerator.hasMore()) {
|
|
|
|
var extension = extEnumerator.getNext();
|
|
|
|
if (extString)
|
2017-10-26 13:47:01 +03:00
|
|
|
extString += "; "; // If adding more than one extension,
|
2005-03-24 00:37:09 +03:00
|
|
|
// separate by semi-colon
|
|
|
|
extString += "*." + extension;
|
2005-03-24 00:36:58 +03:00
|
|
|
}
|
2005-03-24 00:37:09 +03:00
|
|
|
|
2005-06-28 18:41:11 +04:00
|
|
|
if (extString)
|
|
|
|
aFilePicker.appendFilter(mimeInfo.description, extString);
|
2005-03-24 00:36:58 +03:00
|
|
|
}
|
|
|
|
}
|
2005-06-28 18:41:11 +04:00
|
|
|
|
|
|
|
if (aSaveMode & SAVEMODE_COMPLETE_DOM) {
|
2010-03-25 16:16:20 +03:00
|
|
|
aFilePicker.appendFilter(ContentAreaUtils.stringBundle.GetStringFromName("WebPageCompleteFilter"),
|
|
|
|
filterString);
|
2005-06-28 18:41:11 +04:00
|
|
|
// We should always offer a choice to save document only if
|
|
|
|
// we allow saving as complete.
|
2010-03-25 16:16:20 +03:00
|
|
|
aFilePicker.appendFilter(ContentAreaUtils.stringBundle.GetStringFromName(bundleName),
|
|
|
|
filterString);
|
2005-06-28 18:41:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (aSaveMode & SAVEMODE_COMPLETE_TEXT)
|
|
|
|
aFilePicker.appendFilters(Components.interfaces.nsIFilePicker.filterText);
|
|
|
|
|
|
|
|
// Always append the all files (*) filter
|
|
|
|
aFilePicker.appendFilters(Components.interfaces.nsIFilePicker.filterAll);
|
|
|
|
}
|
|
|
|
|
2016-12-31 05:47:25 +03:00
|
|
|
function getPostData(aDocument) {
|
2015-08-06 00:25:39 +03:00
|
|
|
const Ci = Components.interfaces;
|
|
|
|
|
|
|
|
if (aDocument instanceof Ci.nsIWebBrowserPersistDocument) {
|
|
|
|
return aDocument.postData;
|
|
|
|
}
|
2005-03-24 00:36:58 +03:00
|
|
|
try {
|
2010-06-08 11:15:00 +04:00
|
|
|
// Find the session history entry corresponding to the given document. In
|
|
|
|
// the current implementation, nsIWebPageDescriptor.currentDescriptor always
|
|
|
|
// returns a session history entry.
|
2015-08-06 00:25:39 +03:00
|
|
|
let sessionHistoryEntry =
|
2010-06-08 11:15:00 +04:00
|
|
|
aDocument.defaultView
|
2015-08-06 00:25:39 +03:00
|
|
|
.QueryInterface(Ci.nsIInterfaceRequestor)
|
|
|
|
.getInterface(Ci.nsIWebNavigation)
|
|
|
|
.QueryInterface(Ci.nsIWebPageDescriptor)
|
2010-06-08 11:15:00 +04:00
|
|
|
.currentDescriptor
|
2015-08-06 00:25:39 +03:00
|
|
|
.QueryInterface(Ci.nsISHEntry);
|
2010-06-08 11:15:00 +04:00
|
|
|
return sessionHistoryEntry.postData;
|
2016-12-31 05:47:25 +03:00
|
|
|
} catch (e) {
|
2005-03-24 00:36:58 +03:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-12-31 05:47:25 +03:00
|
|
|
function makeWebBrowserPersist() {
|
2005-03-24 00:36:58 +03:00
|
|
|
const persistContractID = "@mozilla.org/embedding/browser/nsWebBrowserPersist;1";
|
|
|
|
const persistIID = Components.interfaces.nsIWebBrowserPersist;
|
|
|
|
return Components.classes[persistContractID].createInstance(persistIID);
|
|
|
|
}
|
|
|
|
|
2016-12-31 05:47:25 +03:00
|
|
|
function makeURI(aURL, aOriginCharset, aBaseURI) {
|
2017-04-25 10:21:49 +03:00
|
|
|
return Services.io.newURI(aURL, aOriginCharset, aBaseURI);
|
2005-03-24 00:37:32 +03:00
|
|
|
}
|
|
|
|
|
2016-12-31 05:47:25 +03:00
|
|
|
function makeFileURI(aFile) {
|
2017-04-25 10:21:50 +03:00
|
|
|
return Services.io.newFileURI(aFile);
|
2005-03-24 00:36:58 +03:00
|
|
|
}
|
|
|
|
|
2016-12-31 05:47:25 +03:00
|
|
|
function makeFilePicker() {
|
2005-03-24 00:36:58 +03:00
|
|
|
const fpContractID = "@mozilla.org/filepicker;1";
|
|
|
|
const fpIID = Components.interfaces.nsIFilePicker;
|
|
|
|
return Components.classes[fpContractID].createInstance(fpIID);
|
|
|
|
}
|
|
|
|
|
2016-12-31 05:47:25 +03:00
|
|
|
function getMIMEService() {
|
2005-03-24 00:36:58 +03:00
|
|
|
const mimeSvcContractID = "@mozilla.org/mime;1";
|
|
|
|
const mimeSvcIID = Components.interfaces.nsIMIMEService;
|
|
|
|
const mimeSvc = Components.classes[mimeSvcContractID].getService(mimeSvcIID);
|
|
|
|
return mimeSvc;
|
|
|
|
}
|
|
|
|
|
2005-06-28 18:41:11 +04:00
|
|
|
// Given aFileName, find the fileName without the extension on the end.
|
2016-12-31 05:47:25 +03:00
|
|
|
function getFileBaseName(aFileName) {
|
2005-06-28 18:41:11 +04:00
|
|
|
// Remove the file extension from aFileName:
|
|
|
|
return aFileName.replace(/\.[^.]*$/, "");
|
|
|
|
}
|
|
|
|
|
2016-12-31 05:47:25 +03:00
|
|
|
function getMIMETypeForURI(aURI) {
|
2007-12-11 12:57:36 +03:00
|
|
|
try {
|
2005-03-24 00:37:26 +03:00
|
|
|
return getMIMEService().getTypeFromURI(aURI);
|
2016-12-31 05:47:25 +03:00
|
|
|
} catch (e) {
|
2005-03-24 00:36:58 +03:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-12-31 05:47:25 +03:00
|
|
|
function getMIMEInfoForType(aMIMEType, aExtension) {
|
2005-03-24 00:39:39 +03:00
|
|
|
if (aMIMEType || aExtension) {
|
|
|
|
try {
|
|
|
|
return getMIMEService().getFromTypeAndExtension(aMIMEType, aExtension);
|
2016-12-31 05:47:25 +03:00
|
|
|
} catch (e) {
|
2005-03-24 00:39:39 +03:00
|
|
|
}
|
2005-03-24 00:36:58 +03:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2005-06-28 18:41:11 +04:00
|
|
|
function getDefaultFileName(aDefaultFileName, aURI, aDocument,
|
2016-12-31 05:47:25 +03:00
|
|
|
aContentDisposition) {
|
2005-06-28 18:41:11 +04:00
|
|
|
// 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);
|
2017-10-26 13:47:01 +03:00
|
|
|
var dummy = { value: null }; // Need an out param...
|
2005-06-28 18:41:11 +04:00
|
|
|
var charset = getCharsetforSave(aDocument);
|
|
|
|
|
|
|
|
var fileName = null;
|
|
|
|
try {
|
|
|
|
fileName = mhp.getParameter(aContentDisposition, "filename", charset,
|
|
|
|
true, dummy);
|
2016-12-31 05:47:25 +03:00
|
|
|
} catch (e) {
|
2005-06-28 18:41:11 +04:00
|
|
|
try {
|
|
|
|
fileName = mhp.getParameter(aContentDisposition, "name", charset, true,
|
|
|
|
dummy);
|
2016-12-31 05:47:25 +03:00
|
|
|
} catch (e) {
|
2005-06-28 18:41:11 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (fileName)
|
2017-11-06 12:30:33 +03:00
|
|
|
return validateFileName(fileName);
|
2005-06-28 18:41:11 +04:00
|
|
|
}
|
2005-03-24 00:36:58 +03:00
|
|
|
|
2012-08-03 11:12:24 +04:00
|
|
|
let docTitle;
|
2017-09-27 14:13:07 +03:00
|
|
|
if (aDocument && aDocument.title && aDocument.title.trim()) {
|
2012-08-03 11:12:24 +04:00
|
|
|
// If the document looks like HTML or XML, try to use its original title.
|
2017-09-27 14:13:07 +03:00
|
|
|
docTitle = validateFileName(aDocument.title);
|
|
|
|
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;
|
2012-08-03 11:12:24 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-03-24 00:36:58 +03:00
|
|
|
try {
|
2005-06-28 18:41:11 +04:00
|
|
|
var url = aURI.QueryInterface(Components.interfaces.nsIURL);
|
2005-03-24 00:36:58 +03:00
|
|
|
if (url.fileName != "") {
|
2012-08-03 11:12:24 +04:00
|
|
|
// 3) Use the actual file name, if present
|
2005-03-24 00:36:58 +03:00
|
|
|
var textToSubURI = Components.classes["@mozilla.org/intl/texttosuburi;1"]
|
|
|
|
.getService(Components.interfaces.nsITextToSubURI);
|
2017-08-02 14:43:30 +03:00
|
|
|
return validateFileName(textToSubURI.unEscapeURIForUI("UTF-8", url.fileName));
|
2005-03-24 00:36:58 +03:00
|
|
|
}
|
2005-06-28 18:41:11 +04:00
|
|
|
} catch (e) {
|
|
|
|
// This is something like a data: and so forth URI... no filename here.
|
2005-03-24 00:36:58 +03:00
|
|
|
}
|
2005-06-28 18:41:11 +04:00
|
|
|
|
2012-08-03 11:12:24 +04:00
|
|
|
if (docTitle)
|
|
|
|
// 4) Use the document title
|
|
|
|
return docTitle;
|
2005-06-28 18:41:11 +04:00
|
|
|
|
2005-03-24 00:36:58 +03:00
|
|
|
if (aDefaultFileName)
|
2012-08-03 11:12:24 +04:00
|
|
|
// 5) Use the caller-provided name, if any
|
2005-03-24 00:36:58 +03:00
|
|
|
return validateFileName(aDefaultFileName);
|
|
|
|
|
2012-08-03 11:12:24 +04:00
|
|
|
// 6) If this is a directory, use the last directory name
|
2017-07-29 14:50:21 +03:00
|
|
|
var path = aURI.pathQueryRef.match(/\/([^\/]+)\/$/);
|
2005-06-28 18:41:11 +04:00
|
|
|
if (path && path.length > 1)
|
|
|
|
return validateFileName(path[1]);
|
|
|
|
|
2005-03-24 00:36:58 +03:00
|
|
|
try {
|
2005-06-28 18:41:11 +04:00
|
|
|
if (aURI.host)
|
2012-08-03 11:12:24 +04:00
|
|
|
// 7) Use the host.
|
2017-11-06 12:30:33 +03:00
|
|
|
return validateFileName(aURI.host);
|
2005-03-24 00:36:58 +03:00
|
|
|
} catch (e) {
|
|
|
|
// Some files have no information at all, like Javascript generated pages
|
|
|
|
}
|
|
|
|
try {
|
2012-08-03 11:12:24 +04:00
|
|
|
// 8) Use the default file name
|
2010-03-25 16:16:20 +03:00
|
|
|
return ContentAreaUtils.stringBundle.GetStringFromName("DefaultSaveFileName");
|
2005-03-24 00:36:58 +03:00
|
|
|
} catch (e) {
|
2016-10-24 20:14:19 +03:00
|
|
|
// in case localized string cannot be found
|
2005-03-24 00:36:58 +03:00
|
|
|
}
|
2012-08-03 11:12:24 +04:00
|
|
|
// 9) If all else fails, use "index"
|
2005-03-24 00:36:58 +03:00
|
|
|
return "index";
|
|
|
|
}
|
|
|
|
|
2016-12-31 05:47:25 +03:00
|
|
|
function validateFileName(aFileName) {
|
2017-09-27 14:13:07 +03:00
|
|
|
let processed = DownloadPaths.sanitize(aFileName) || "_";
|
|
|
|
if (AppConstants.platform == "android") {
|
2011-03-05 02:29:01 +03:00
|
|
|
// If a large part of the filename has been sanitized, then we
|
|
|
|
// will use a default filename instead
|
2016-11-11 01:48:04 +03:00
|
|
|
if (processed.replace(/_/g, "").length <= processed.length / 2) {
|
2011-03-05 02:29:01 +03:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-09-27 14:13:07 +03:00
|
|
|
return processed;
|
2005-03-24 00:36:58 +03:00
|
|
|
}
|
|
|
|
|
2016-12-31 05:47:25 +03:00
|
|
|
function getNormalizedLeafName(aFile, aDefaultExtension) {
|
2005-03-24 00:36:58 +03:00
|
|
|
if (!aDefaultExtension)
|
|
|
|
return aFile;
|
2005-09-17 06:27:45 +04:00
|
|
|
|
2016-02-05 23:16:39 +03:00
|
|
|
if (AppConstants.platform == "win") {
|
|
|
|
// Remove trailing dots and spaces on windows
|
|
|
|
aFile = aFile.replace(/[\s.]+$/, "");
|
|
|
|
}
|
2007-04-17 22:51:33 +04:00
|
|
|
|
|
|
|
// Remove leading dots
|
|
|
|
aFile = aFile.replace(/^\.+/, "");
|
2007-12-11 12:57:36 +03:00
|
|
|
|
2005-03-24 00:36:58 +03:00
|
|
|
// Fix up the file name we're saving to to include the default extension
|
2005-09-17 06:27:45 +04:00
|
|
|
var i = aFile.lastIndexOf(".");
|
|
|
|
if (aFile.substr(i + 1) != aDefaultExtension)
|
2005-03-24 00:36:58 +03:00
|
|
|
return aFile + "." + aDefaultExtension;
|
|
|
|
|
|
|
|
return aFile;
|
|
|
|
}
|
|
|
|
|
2016-12-31 05:47:25 +03:00
|
|
|
function getDefaultExtension(aFilename, aURI, aContentType) {
|
2005-03-24 00:36:58 +03:00
|
|
|
if (aContentType == "text/plain" || aContentType == "application/octet-stream" || aURI.scheme == "ftp")
|
2017-10-26 13:47:01 +03:00
|
|
|
return ""; // temporary fix for bug 120327
|
2005-03-24 00:36:58 +03: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;
|
|
|
|
|
2005-03-24 00:37:26 +03:00
|
|
|
// This mirrors some code in nsExternalHelperAppService::DoContent
|
|
|
|
// Use the filename first and then the URI if that fails
|
2007-12-11 12:57:36 +03:00
|
|
|
|
2005-03-24 00:37:26 +03:00
|
|
|
var mimeInfo = getMIMEInfoForType(aContentType, ext);
|
|
|
|
|
2005-06-28 18:41:11 +04:00
|
|
|
if (ext && mimeInfo && mimeInfo.extensionExists(ext))
|
2005-03-24 00:36:58 +03:00
|
|
|
return ext;
|
2007-12-11 12:57:36 +03:00
|
|
|
|
2005-03-24 00:36:58 +03: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) {
|
|
|
|
}
|
|
|
|
|
2005-03-24 00:39:33 +03:00
|
|
|
if (urlext && mimeInfo && mimeInfo.extensionExists(urlext)) {
|
2005-03-24 00:36:58 +03:00
|
|
|
return urlext;
|
|
|
|
}
|
2016-08-04 01:54:59 +03:00
|
|
|
try {
|
|
|
|
if (mimeInfo)
|
|
|
|
return mimeInfo.primaryExtension;
|
2016-12-31 05:47:25 +03:00
|
|
|
} catch (e) {
|
2005-03-24 00:36:58 +03:00
|
|
|
}
|
2016-08-04 01:54:59 +03:00
|
|
|
// Fall back on the extensions in the filename and URI for lack
|
|
|
|
// of anything better.
|
|
|
|
return ext || urlext;
|
2005-03-24 00:36:58 +03:00
|
|
|
}
|
|
|
|
|
2016-12-31 05:47:25 +03:00
|
|
|
function GetSaveModeForContentType(aContentType, aDocument) {
|
2014-11-18 22:35:47 +03:00
|
|
|
// We can only save a complete page if we have a loaded document,
|
|
|
|
// and it's not a CPOW -- nsWebBrowserPersist needs a real document.
|
|
|
|
if (!aDocument || Components.utils.isCrossProcessWrapper(aDocument))
|
2009-03-30 22:47:31 +04:00
|
|
|
return SAVEMODE_FILEONLY;
|
|
|
|
|
|
|
|
// Find the possible save modes using the provided content type
|
2005-06-28 18:41:11 +04:00
|
|
|
var saveMode = SAVEMODE_FILEONLY;
|
2005-03-24 00:36:58 +03:00
|
|
|
switch (aContentType) {
|
|
|
|
case "text/html":
|
|
|
|
case "application/xhtml+xml":
|
2006-01-02 05:43:53 +03:00
|
|
|
case "image/svg+xml":
|
2005-06-28 18:41:11 +04:00
|
|
|
saveMode |= SAVEMODE_COMPLETE_TEXT;
|
|
|
|
// Fall through
|
|
|
|
case "text/xml":
|
2005-03-24 00:36:58 +03:00
|
|
|
case "application/xml":
|
2005-06-28 18:41:11 +04:00
|
|
|
saveMode |= SAVEMODE_COMPLETE_DOM;
|
|
|
|
break;
|
2005-03-24 00:36:58 +03:00
|
|
|
}
|
2005-06-28 18:41:11 +04:00
|
|
|
|
|
|
|
return saveMode;
|
2005-03-24 00:36:58 +03:00
|
|
|
}
|
2005-03-24 00:37:14 +03:00
|
|
|
|
2016-12-31 05:47:25 +03:00
|
|
|
function getCharsetforSave(aDocument) {
|
2005-03-24 00:37:14 +03:00
|
|
|
if (aDocument)
|
|
|
|
return aDocument.characterSet;
|
|
|
|
|
|
|
|
if (document.commandDispatcher.focusedWindow)
|
|
|
|
return document.commandDispatcher.focusedWindow.document.characterSet;
|
|
|
|
|
2005-06-28 18:41:11 +04:00
|
|
|
return window.content.document.characterSet;
|
2005-03-24 00:37:14 +03:00
|
|
|
}
|
2007-08-09 21:59:08 +04: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
|
2015-02-11 07:50:02 +03:00
|
|
|
*
|
|
|
|
* WARNING: Please note that openURL() does not perform any content security checks!!!
|
2007-08-09 21:59:08 +04:00
|
|
|
*/
|
2016-12-31 05:47:25 +03:00
|
|
|
function openURL(aURL) {
|
2009-07-29 09:55:03 +04:00
|
|
|
var uri = makeURI(aURL);
|
2007-08-09 21:59:08 +04: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.
|
2017-08-06 05:25:42 +03:00
|
|
|
protocolSvc.loadURI(uri);
|
2016-12-31 05:47:25 +03:00
|
|
|
} else {
|
2013-03-18 19:46:17 +04:00
|
|
|
var recentWindow = Services.wm.getMostRecentWindow("navigator:browser");
|
2012-05-18 01:53:05 +04:00
|
|
|
if (recentWindow) {
|
2013-09-16 13:27:22 +04:00
|
|
|
recentWindow.openUILinkIn(uri.spec, "tab");
|
2012-05-18 01:53:05 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-08-09 21:59:08 +04:00
|
|
|
var loadgroup = Components.classes["@mozilla.org/network/load-group;1"]
|
|
|
|
.createInstance(Components.interfaces.nsILoadGroup);
|
2012-10-18 18:44:36 +04:00
|
|
|
var appstartup = Services.startup;
|
2007-08-09 21:59:08 +04: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;
|
|
|
|
}
|
2017-10-15 21:50:30 +03:00
|
|
|
};
|
2007-08-09 21:59:08 +04:00
|
|
|
loadgroup.groupObserver = loadListener;
|
|
|
|
|
|
|
|
var uriListener = {
|
2016-12-30 02:34:54 +03:00
|
|
|
onStartURIOpen(uri) { return false; },
|
|
|
|
doContent(ctype, preferred, request, handler) { return false; },
|
|
|
|
isPreferred(ctype, desired) { return false; },
|
|
|
|
canHandleContent(ctype, preferred, desired) { return false; },
|
2007-08-09 21:59:08 +04:00
|
|
|
loadCookie: null,
|
|
|
|
parentContentListener: null,
|
2016-12-30 02:34:54 +03:00
|
|
|
getInterface(iid) {
|
2007-08-09 21:59:08 +04:00
|
|
|
if (iid.equals(Components.interfaces.nsIURIContentListener))
|
|
|
|
return this;
|
|
|
|
if (iid.equals(Components.interfaces.nsILoadGroup))
|
|
|
|
return loadgroup;
|
|
|
|
throw Components.results.NS_ERROR_NO_INTERFACE;
|
|
|
|
}
|
2017-10-15 21:50:30 +03:00
|
|
|
};
|
2007-08-09 21:59:08 +04:00
|
|
|
|
2016-05-17 11:00:45 +03:00
|
|
|
var channel = NetUtil.newChannel({
|
2016-12-30 02:34:54 +03:00
|
|
|
uri,
|
2016-05-17 11:00:45 +03:00
|
|
|
loadUsingSystemPrincipal: true
|
|
|
|
});
|
|
|
|
|
2017-09-23 19:56:04 +03:00
|
|
|
if (channel) {
|
|
|
|
channel.channelIsForDownload = true;
|
|
|
|
}
|
|
|
|
|
2007-08-09 21:59:08 +04:00
|
|
|
var uriLoader = Components.classes["@mozilla.org/uriloader;1"]
|
|
|
|
.getService(Components.interfaces.nsIURILoader);
|
2013-08-24 12:10:50 +04:00
|
|
|
uriLoader.openURI(channel,
|
|
|
|
Components.interfaces.nsIURILoader.IS_CONTENT_PREFERRED,
|
|
|
|
uriListener);
|
2007-08-09 21:59:08 +04:00
|
|
|
}
|
|
|
|
}
|