2013-01-29 18:24:17 +04: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/. */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Provides functions to integrate with the host application, handling for
|
|
|
|
* example the global prompts on shutdown.
|
|
|
|
*/
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2018-02-23 22:50:01 +03:00
|
|
|
var EXPORTED_SYMBOLS = [
|
2013-01-29 18:24:17 +04:00
|
|
|
"DownloadIntegration",
|
|
|
|
];
|
|
|
|
|
2018-01-30 02:20:18 +03:00
|
|
|
ChromeUtils.import("resource://gre/modules/Integration.jsm");
|
|
|
|
ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");
|
2013-01-29 18:24:17 +04:00
|
|
|
|
2018-01-30 02:20:18 +03:00
|
|
|
ChromeUtils.defineModuleGetter(this, "AsyncShutdown",
|
|
|
|
"resource://gre/modules/AsyncShutdown.jsm");
|
|
|
|
ChromeUtils.defineModuleGetter(this, "AppConstants",
|
|
|
|
"resource://gre/modules/AppConstants.jsm");
|
|
|
|
ChromeUtils.defineModuleGetter(this, "DeferredTask",
|
|
|
|
"resource://gre/modules/DeferredTask.jsm");
|
|
|
|
ChromeUtils.defineModuleGetter(this, "Downloads",
|
|
|
|
"resource://gre/modules/Downloads.jsm");
|
|
|
|
ChromeUtils.defineModuleGetter(this, "DownloadStore",
|
|
|
|
"resource://gre/modules/DownloadStore.jsm");
|
|
|
|
ChromeUtils.defineModuleGetter(this, "DownloadUIHelper",
|
|
|
|
"resource://gre/modules/DownloadUIHelper.jsm");
|
|
|
|
ChromeUtils.defineModuleGetter(this, "FileUtils",
|
|
|
|
"resource://gre/modules/FileUtils.jsm");
|
|
|
|
ChromeUtils.defineModuleGetter(this, "NetUtil",
|
|
|
|
"resource://gre/modules/NetUtil.jsm");
|
|
|
|
ChromeUtils.defineModuleGetter(this, "OS",
|
|
|
|
"resource://gre/modules/osfile.jsm");
|
|
|
|
ChromeUtils.defineModuleGetter(this, "PlacesUtils",
|
|
|
|
"resource://gre/modules/PlacesUtils.jsm");
|
|
|
|
ChromeUtils.defineModuleGetter(this, "Services",
|
|
|
|
"resource://gre/modules/Services.jsm");
|
|
|
|
ChromeUtils.defineModuleGetter(this, "NetUtil",
|
|
|
|
"resource://gre/modules/NetUtil.jsm");
|
|
|
|
ChromeUtils.defineModuleGetter(this, "CloudStorage",
|
|
|
|
"resource://gre/modules/CloudStorage.jsm");
|
2013-09-12 23:20:30 +04:00
|
|
|
|
2013-09-03 21:23:37 +04:00
|
|
|
XPCOMUtils.defineLazyServiceGetter(this, "gDownloadPlatform",
|
|
|
|
"@mozilla.org/toolkit/download-platform;1",
|
|
|
|
"mozIDownloadPlatform");
|
2013-07-30 22:59:16 +04:00
|
|
|
XPCOMUtils.defineLazyServiceGetter(this, "gEnvironment",
|
2013-06-03 07:43:06 +04:00
|
|
|
"@mozilla.org/process/environment;1",
|
|
|
|
"nsIEnvironment");
|
2013-07-30 22:59:16 +04:00
|
|
|
XPCOMUtils.defineLazyServiceGetter(this, "gMIMEService",
|
|
|
|
"@mozilla.org/mime;1",
|
|
|
|
"nsIMIMEService");
|
|
|
|
XPCOMUtils.defineLazyServiceGetter(this, "gExternalProtocolService",
|
|
|
|
"@mozilla.org/uriloader/external-protocol-service;1",
|
|
|
|
"nsIExternalProtocolService");
|
2018-01-30 02:20:18 +03:00
|
|
|
ChromeUtils.defineModuleGetter(this, "RuntimePermissions",
|
|
|
|
"resource://gre/modules/RuntimePermissions.jsm");
|
2013-09-12 23:20:30 +04:00
|
|
|
|
2013-06-28 23:49:31 +04:00
|
|
|
XPCOMUtils.defineLazyGetter(this, "gParentalControlsService", function() {
|
|
|
|
if ("@mozilla.org/parental-controls-service;1" in Cc) {
|
|
|
|
return Cc["@mozilla.org/parental-controls-service;1"]
|
|
|
|
.createInstance(Ci.nsIParentalControlsService);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
|
2013-11-11 22:54:09 +04:00
|
|
|
XPCOMUtils.defineLazyServiceGetter(this, "gApplicationReputationService",
|
2017-10-23 11:18:52 +03:00
|
|
|
"@mozilla.org/reputationservice/application-reputation-service;1",
|
2013-11-11 22:54:09 +04:00
|
|
|
Ci.nsIApplicationReputationService);
|
|
|
|
|
2016-04-18 16:21:55 +03:00
|
|
|
// We have to use the gCombinedDownloadIntegration identifier because, in this
|
|
|
|
// module only, the DownloadIntegration identifier refers to the base version.
|
2017-08-29 20:57:11 +03:00
|
|
|
/* global gCombinedDownloadIntegration:false */
|
2016-04-18 16:21:55 +03:00
|
|
|
Integration.downloads.defineModuleGetter(this, "gCombinedDownloadIntegration",
|
|
|
|
"resource://gre/modules/DownloadIntegration.jsm",
|
|
|
|
"DownloadIntegration");
|
|
|
|
|
2013-08-16 13:02:18 +04:00
|
|
|
const Timer = Components.Constructor("@mozilla.org/timer;1", "nsITimer",
|
|
|
|
"initWithCallback");
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Indicates the delay between a change to the downloads data and the related
|
2015-03-17 00:40:00 +03:00
|
|
|
* save operation.
|
2013-08-16 13:02:18 +04:00
|
|
|
*
|
2015-03-17 00:40:00 +03:00
|
|
|
* For best efficiency, this value should be high enough that the input/output
|
|
|
|
* for opening or closing the target file does not overlap with the one for
|
|
|
|
* saving the list of downloads.
|
2013-08-16 13:02:18 +04:00
|
|
|
*/
|
|
|
|
const kSaveDelayMs = 1500;
|
|
|
|
|
2014-02-17 20:00:36 +04:00
|
|
|
/**
|
|
|
|
* List of observers to listen against
|
|
|
|
*/
|
|
|
|
const kObserverTopics = [
|
|
|
|
"quit-application-requested",
|
|
|
|
"offline-requested",
|
|
|
|
"last-pb-context-exiting",
|
|
|
|
"last-pb-context-exited",
|
|
|
|
"sleep_notification",
|
|
|
|
"suspend_process_notification",
|
|
|
|
"wake_notification",
|
|
|
|
"resume_process_notification",
|
|
|
|
"network:offline-about-to-go-offline",
|
|
|
|
"network:offline-status-changed",
|
|
|
|
"xpcom-will-shutdown",
|
|
|
|
];
|
|
|
|
|
2016-03-16 17:29:23 +03:00
|
|
|
/**
|
|
|
|
* Maps nsIApplicationReputationService verdicts with the DownloadError ones.
|
|
|
|
*/
|
|
|
|
const kVerdictMap = {
|
|
|
|
[Ci.nsIApplicationReputationService.VERDICT_DANGEROUS]:
|
|
|
|
Downloads.Error.BLOCK_VERDICT_MALWARE,
|
|
|
|
[Ci.nsIApplicationReputationService.VERDICT_UNCOMMON]:
|
|
|
|
Downloads.Error.BLOCK_VERDICT_UNCOMMON,
|
|
|
|
[Ci.nsIApplicationReputationService.VERDICT_POTENTIALLY_UNWANTED]:
|
|
|
|
Downloads.Error.BLOCK_VERDICT_POTENTIALLY_UNWANTED,
|
|
|
|
[Ci.nsIApplicationReputationService.VERDICT_DANGEROUS_HOST]:
|
|
|
|
Downloads.Error.BLOCK_VERDICT_MALWARE,
|
|
|
|
};
|
|
|
|
|
2013-01-29 18:24:17 +04:00
|
|
|
/**
|
|
|
|
* Provides functions to integrate with the host application, handling for
|
|
|
|
* example the global prompts on shutdown.
|
|
|
|
*/
|
2018-02-23 22:50:01 +03:00
|
|
|
var DownloadIntegration = {
|
2013-06-26 16:21:06 +04:00
|
|
|
/**
|
|
|
|
* Main DownloadStore object for loading and saving the list of persistent
|
|
|
|
* downloads, or null if the download list was never requested and thus it
|
|
|
|
* doesn't need to be persisted.
|
|
|
|
*/
|
|
|
|
_store: null,
|
|
|
|
|
2014-12-09 00:31:03 +03:00
|
|
|
/**
|
|
|
|
* Returns whether data for blocked downloads should be kept on disk.
|
|
|
|
* Implementations which support unblocking downloads may return true to
|
|
|
|
* keep the blocked download on disk until its fate is decided.
|
|
|
|
*
|
|
|
|
* If a download is blocked and the partial data is kept the Download's
|
|
|
|
* 'hasBlockedData' property will be true. In this state Download.unblock()
|
|
|
|
* or Download.confirmBlock() may be used to either unblock the download or
|
|
|
|
* remove the downloaded data respectively.
|
|
|
|
*
|
|
|
|
* Even if shouldKeepBlockedData returns true, if the download did not use a
|
|
|
|
* partFile the blocked data will be removed - preventing the complete
|
|
|
|
* download from existing on disk with its final filename.
|
|
|
|
*
|
|
|
|
* @return boolean True if data should be kept.
|
|
|
|
*/
|
2016-04-18 16:21:55 +03:00
|
|
|
shouldKeepBlockedData() {
|
2015-03-05 20:25:41 +03:00
|
|
|
const FIREFOX_ID = "{ec8030f7-c20a-464f-9b0e-13a3a9e97384}";
|
2015-04-01 09:27:00 +03:00
|
|
|
return Services.appinfo.ID == FIREFOX_ID;
|
2014-12-09 00:31:03 +03:00
|
|
|
},
|
|
|
|
|
2013-06-26 16:21:06 +04:00
|
|
|
/**
|
|
|
|
* Performs initialization of the list of persistent downloads, before its
|
|
|
|
* first use by the host application. This function may be called only once
|
|
|
|
* during the entire lifetime of the application.
|
|
|
|
*
|
2016-04-18 16:21:55 +03:00
|
|
|
* @param list
|
|
|
|
* DownloadList object to be initialized.
|
|
|
|
*
|
|
|
|
* @return {Promise}
|
|
|
|
* @resolves When the list has been initialized.
|
|
|
|
* @rejects JavaScript exception.
|
|
|
|
*/
|
2017-05-12 15:42:39 +03:00
|
|
|
async initializePublicDownloadList(list) {
|
2016-04-18 16:21:55 +03:00
|
|
|
try {
|
2017-05-12 15:42:39 +03:00
|
|
|
await this.loadPublicDownloadListFromStore(list);
|
2016-04-18 16:21:55 +03:00
|
|
|
} catch (ex) {
|
|
|
|
Cu.reportError(ex);
|
|
|
|
}
|
|
|
|
|
2017-07-26 02:10:27 +03:00
|
|
|
if (AppConstants.MOZ_PLACES) {
|
|
|
|
// After the list of persistent downloads has been loaded, we can add the
|
|
|
|
// history observers, even if the load operation failed. This object is kept
|
|
|
|
// alive by the history service.
|
|
|
|
new DownloadHistoryObserver(list);
|
|
|
|
}
|
2017-05-12 15:42:39 +03:00
|
|
|
},
|
2016-04-18 16:21:55 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Called by initializePublicDownloadList to load the list of persistent
|
|
|
|
* downloads, before its first use by the host application. This function may
|
|
|
|
* be called only once during the entire lifetime of the application.
|
|
|
|
*
|
|
|
|
* @param list
|
2013-06-26 16:21:06 +04:00
|
|
|
* DownloadList object to be populated with the download objects
|
|
|
|
* serialized from the previous session. This list will be persisted
|
2013-08-16 13:02:18 +04:00
|
|
|
* to disk during the session lifetime.
|
2013-06-26 16:21:06 +04:00
|
|
|
*
|
|
|
|
* @return {Promise}
|
|
|
|
* @resolves When the list has been populated.
|
|
|
|
* @rejects JavaScript exception.
|
|
|
|
*/
|
2017-05-12 15:42:39 +03:00
|
|
|
async loadPublicDownloadListFromStore(list) {
|
2016-04-18 16:21:55 +03:00
|
|
|
if (this._store) {
|
|
|
|
throw new Error("Initialization may be performed only once.");
|
|
|
|
}
|
2013-08-16 22:35:42 +04:00
|
|
|
|
2016-04-18 16:21:55 +03:00
|
|
|
this._store = new DownloadStore(list, OS.Path.join(
|
|
|
|
OS.Constants.Path.profileDir,
|
|
|
|
"downloads.json"));
|
|
|
|
this._store.onsaveitem = this.shouldPersistDownload.bind(this);
|
2013-06-26 16:21:06 +04:00
|
|
|
|
2016-04-18 16:21:55 +03:00
|
|
|
try {
|
2017-05-15 12:48:04 +03:00
|
|
|
await this._store.load();
|
2016-04-18 16:21:55 +03:00
|
|
|
} catch (ex) {
|
|
|
|
Cu.reportError(ex);
|
|
|
|
}
|
2013-08-16 22:35:42 +04:00
|
|
|
|
2016-04-18 16:21:55 +03:00
|
|
|
// Add the view used for detecting changes to downloads to be persisted.
|
|
|
|
// We must do this after the list of persistent downloads has been loaded,
|
|
|
|
// even if the load operation failed. We wait for a complete initialization
|
|
|
|
// so other callers cannot modify the list without being detected. The
|
|
|
|
// DownloadAutoSaveView is kept alive by the underlying DownloadList.
|
2017-05-12 15:42:39 +03:00
|
|
|
await new DownloadAutoSaveView(list, this._store).initialize();
|
|
|
|
},
|
2013-08-16 13:02:18 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines if a Download object from the list of persistent downloads
|
|
|
|
* should be saved into a file, so that it can be restored across sessions.
|
|
|
|
*
|
|
|
|
* This function allows filtering out downloads that the host application is
|
|
|
|
* not interested in persisting across sessions, for example downloads that
|
|
|
|
* finished successfully.
|
|
|
|
*
|
|
|
|
* @param aDownload
|
|
|
|
* The Download object to be inspected. This is originally taken from
|
|
|
|
* the global DownloadList object for downloads that were not started
|
|
|
|
* from a private browsing window. The item may have been removed
|
|
|
|
* from the list since the save operation started, though in this case
|
|
|
|
* the save operation will be repeated later.
|
|
|
|
*
|
|
|
|
* @return True to save the download, false otherwise.
|
|
|
|
*/
|
2016-04-18 16:21:55 +03:00
|
|
|
shouldPersistDownload(aDownload) {
|
2016-03-20 17:25:42 +03:00
|
|
|
// On all platforms, we save all the downloads currently in progress, as
|
|
|
|
// well as stopped downloads for which we retained partially downloaded
|
|
|
|
// data or we have blocked data.
|
2017-07-26 02:10:27 +03:00
|
|
|
// On Android we store all history; on Desktop, stopped downloads for which
|
|
|
|
// we don't need to track the presence of a ".part" file are only retained
|
|
|
|
// in the browser history.
|
|
|
|
return !aDownload.stopped || aDownload.hasPartialData ||
|
|
|
|
aDownload.hasBlockedData || AppConstants.platform == "android";
|
2013-06-26 16:21:06 +04:00
|
|
|
},
|
2013-06-03 07:43:06 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the system downloads directory asynchronously.
|
|
|
|
*
|
|
|
|
* @return {Promise}
|
2013-10-23 06:04:55 +04:00
|
|
|
* @resolves The downloads directory string path.
|
2013-06-03 07:43:06 +04:00
|
|
|
*/
|
2017-05-12 15:42:39 +03:00
|
|
|
async getSystemDownloadsDirectory() {
|
2016-04-18 16:21:55 +03:00
|
|
|
if (this._downloadsDirectory) {
|
|
|
|
return this._downloadsDirectory;
|
|
|
|
}
|
2013-06-03 07:43:06 +04:00
|
|
|
|
2017-07-26 02:10:27 +03:00
|
|
|
if (AppConstants.platform == "android") {
|
|
|
|
// Android doesn't have a $HOME directory, and by default we only have
|
|
|
|
// write access to /data/data/org.mozilla.{$APP} and /sdcard
|
|
|
|
this._downloadsDirectory = gEnvironment.get("DOWNLOADS_DIRECTORY");
|
|
|
|
if (!this._downloadsDirectory) {
|
|
|
|
throw new Components.Exception("DOWNLOADS_DIRECTORY is not set.",
|
|
|
|
Cr.NS_ERROR_FILE_UNRECOGNIZED_PATH);
|
|
|
|
}
|
2016-04-18 16:21:55 +03:00
|
|
|
} else {
|
2017-07-26 02:10:27 +03:00
|
|
|
try {
|
|
|
|
this._downloadsDirectory = this._getDirectory("DfltDwnld");
|
2017-08-29 20:57:11 +03:00
|
|
|
} catch (e) {
|
2017-07-26 02:10:27 +03:00
|
|
|
this._downloadsDirectory = await this._createDownloadsDirectory("Home");
|
|
|
|
}
|
2016-04-18 16:21:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return this._downloadsDirectory;
|
2017-05-12 15:42:39 +03:00
|
|
|
},
|
2013-06-03 07:43:06 +04:00
|
|
|
_downloadsDirectory: null,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the user downloads directory asynchronously.
|
|
|
|
*
|
|
|
|
* @return {Promise}
|
2013-10-23 06:04:55 +04:00
|
|
|
* @resolves The downloads directory string path.
|
2013-06-03 07:43:06 +04:00
|
|
|
*/
|
2017-05-12 15:42:39 +03:00
|
|
|
async getPreferredDownloadsDirectory() {
|
2016-04-18 16:21:55 +03:00
|
|
|
let directoryPath = null;
|
2017-03-07 17:29:48 +03:00
|
|
|
let prefValue = Services.prefs.getIntPref("browser.download.folderList", 1);
|
2016-04-18 16:21:55 +03:00
|
|
|
|
2017-08-29 20:57:11 +03:00
|
|
|
switch (prefValue) {
|
2016-04-18 16:21:55 +03:00
|
|
|
case 0: // Desktop
|
|
|
|
directoryPath = this._getDirectory("Desk");
|
|
|
|
break;
|
|
|
|
case 1: // Downloads
|
2017-05-12 15:42:39 +03:00
|
|
|
directoryPath = await this.getSystemDownloadsDirectory();
|
2016-04-18 16:21:55 +03:00
|
|
|
break;
|
|
|
|
case 2: // Custom
|
|
|
|
try {
|
|
|
|
let directory = Services.prefs.getComplexValue("browser.download.dir",
|
|
|
|
Ci.nsIFile);
|
|
|
|
directoryPath = directory.path;
|
2017-05-12 15:42:39 +03:00
|
|
|
await OS.File.makeDir(directoryPath, { ignoreExisting: true });
|
2017-08-29 20:57:11 +03:00
|
|
|
} catch (ex) {
|
2016-04-18 16:21:55 +03:00
|
|
|
// Either the preference isn't set or the directory cannot be created.
|
2017-05-12 15:42:39 +03:00
|
|
|
directoryPath = await this.getSystemDownloadsDirectory();
|
2016-04-18 16:21:55 +03:00
|
|
|
}
|
|
|
|
break;
|
2017-07-27 20:14:17 +03:00
|
|
|
case 3: // Cloud Storage
|
|
|
|
try {
|
|
|
|
directoryPath = await CloudStorage.getDownloadFolder();
|
2017-08-29 20:57:11 +03:00
|
|
|
} catch (ex) {}
|
2017-07-31 00:52:26 +03:00
|
|
|
if (!directoryPath) {
|
2017-07-27 20:14:17 +03:00
|
|
|
directoryPath = await this.getSystemDownloadsDirectory();
|
|
|
|
}
|
|
|
|
break;
|
2016-04-18 16:21:55 +03:00
|
|
|
default:
|
2017-05-12 15:42:39 +03:00
|
|
|
directoryPath = await this.getSystemDownloadsDirectory();
|
2016-04-18 16:21:55 +03:00
|
|
|
}
|
|
|
|
return directoryPath;
|
2017-05-12 15:42:39 +03:00
|
|
|
},
|
2013-06-03 07:43:06 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the temporary downloads directory asynchronously.
|
|
|
|
*
|
|
|
|
* @return {Promise}
|
2013-10-23 06:04:55 +04:00
|
|
|
* @resolves The downloads directory string path.
|
2013-06-03 07:43:06 +04:00
|
|
|
*/
|
2017-05-12 15:42:39 +03:00
|
|
|
async getTemporaryDownloadsDirectory() {
|
2016-04-18 16:21:55 +03:00
|
|
|
let directoryPath = null;
|
2017-07-26 02:10:27 +03:00
|
|
|
if (AppConstants.platform == "macosx") {
|
|
|
|
directoryPath = await this.getPreferredDownloadsDirectory();
|
|
|
|
} else if (AppConstants.platform == "android") {
|
|
|
|
directoryPath = await this.getSystemDownloadsDirectory();
|
|
|
|
} else {
|
|
|
|
directoryPath = this._getDirectory("TmpD");
|
|
|
|
}
|
2016-04-18 16:21:55 +03:00
|
|
|
return directoryPath;
|
2017-05-12 15:42:39 +03:00
|
|
|
},
|
2013-06-03 07:43:06 +04:00
|
|
|
|
2013-06-28 23:49:31 +04:00
|
|
|
/**
|
|
|
|
* Checks to determine whether to block downloads for parental controls.
|
|
|
|
*
|
|
|
|
* aParam aDownload
|
|
|
|
* The download object.
|
|
|
|
*
|
|
|
|
* @return {Promise}
|
|
|
|
* @resolves The boolean indicates to block downloads or not.
|
|
|
|
*/
|
2016-04-18 16:21:55 +03:00
|
|
|
shouldBlockForParentalControls(aDownload) {
|
2013-06-28 23:49:31 +04:00
|
|
|
let isEnabled = gParentalControlsService &&
|
|
|
|
gParentalControlsService.parentalControlsEnabled;
|
|
|
|
let shouldBlock = isEnabled &&
|
|
|
|
gParentalControlsService.blockFileDownloadsEnabled;
|
|
|
|
|
|
|
|
// Log the event if required by parental controls settings.
|
|
|
|
if (isEnabled && gParentalControlsService.loggingEnabled) {
|
|
|
|
gParentalControlsService.log(gParentalControlsService.ePCLog_FileDownload,
|
2013-07-26 17:30:35 +04:00
|
|
|
shouldBlock,
|
|
|
|
NetUtil.newURI(aDownload.source.url), null);
|
2013-06-28 23:49:31 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return Promise.resolve(shouldBlock);
|
|
|
|
},
|
|
|
|
|
2016-01-15 21:38:18 +03:00
|
|
|
/**
|
|
|
|
* Checks to determine whether to block downloads for not granted runtime permissions.
|
|
|
|
*
|
|
|
|
* @return {Promise}
|
|
|
|
* @resolves The boolean indicates to block downloads or not.
|
|
|
|
*/
|
2017-07-26 02:10:27 +03:00
|
|
|
async shouldBlockForRuntimePermissions() {
|
|
|
|
return AppConstants.platform == "android" &&
|
|
|
|
!(await RuntimePermissions.waitForPermissions(
|
|
|
|
RuntimePermissions.WRITE_EXTERNAL_STORAGE));
|
2016-01-15 21:38:18 +03:00
|
|
|
},
|
|
|
|
|
2013-11-11 22:54:09 +04:00
|
|
|
/**
|
|
|
|
* Checks to determine whether to block downloads because they might be
|
|
|
|
* malware, based on application reputation checks.
|
|
|
|
*
|
|
|
|
* aParam aDownload
|
|
|
|
* The download object.
|
|
|
|
*
|
|
|
|
* @return {Promise}
|
2016-03-16 17:29:23 +03:00
|
|
|
* @resolves Object with the following properties:
|
|
|
|
* {
|
|
|
|
* shouldBlock: Whether the download should be blocked.
|
|
|
|
* verdict: Detailed reason for the block, according to the
|
|
|
|
* "Downloads.Error.BLOCK_VERDICT_" constants, or empty
|
|
|
|
* string if the reason is unknown.
|
|
|
|
* }
|
2013-11-11 22:54:09 +04:00
|
|
|
*/
|
2016-04-18 16:21:55 +03:00
|
|
|
shouldBlockForReputationCheck(aDownload) {
|
2013-11-11 22:54:09 +04:00
|
|
|
let hash;
|
2014-01-28 01:39:00 +04:00
|
|
|
let sigInfo;
|
2014-05-29 03:06:21 +04:00
|
|
|
let channelRedirects;
|
2013-11-11 22:54:09 +04:00
|
|
|
try {
|
|
|
|
hash = aDownload.saver.getSha256Hash();
|
2014-03-13 07:31:51 +04:00
|
|
|
sigInfo = aDownload.saver.getSignatureInfo();
|
2014-05-29 03:06:21 +04:00
|
|
|
channelRedirects = aDownload.saver.getRedirects();
|
2013-11-11 22:54:09 +04:00
|
|
|
} catch (ex) {
|
2014-05-29 03:06:21 +04:00
|
|
|
// Bail if DownloadSaver doesn't have a hash or signature info.
|
2016-03-16 17:29:23 +03:00
|
|
|
return Promise.resolve({
|
|
|
|
shouldBlock: false,
|
|
|
|
verdict: "",
|
|
|
|
});
|
2013-11-11 22:54:09 +04:00
|
|
|
}
|
2014-01-28 01:39:00 +04:00
|
|
|
if (!hash || !sigInfo) {
|
2016-03-16 17:29:23 +03:00
|
|
|
return Promise.resolve({
|
|
|
|
shouldBlock: false,
|
|
|
|
verdict: "",
|
|
|
|
});
|
2013-11-11 22:54:09 +04:00
|
|
|
}
|
2017-05-12 15:55:27 +03:00
|
|
|
return new Promise(resolve => {
|
|
|
|
let aReferrer = null;
|
|
|
|
if (aDownload.source.referrer) {
|
|
|
|
aReferrer = NetUtil.newURI(aDownload.source.referrer);
|
|
|
|
}
|
|
|
|
gApplicationReputationService.queryReputation({
|
|
|
|
sourceURI: NetUtil.newURI(aDownload.source.url),
|
|
|
|
referrerURI: aReferrer,
|
|
|
|
fileSize: aDownload.currentBytes,
|
|
|
|
sha256Hash: hash,
|
|
|
|
suggestedFileName: OS.Path.basename(aDownload.target.path),
|
|
|
|
signatureInfo: sigInfo,
|
|
|
|
redirects: channelRedirects },
|
|
|
|
function onComplete(aShouldBlock, aRv, aVerdict) {
|
|
|
|
resolve({
|
|
|
|
shouldBlock: aShouldBlock,
|
|
|
|
verdict: (aShouldBlock && kVerdictMap[aVerdict]) || "",
|
|
|
|
});
|
2016-03-16 17:29:23 +03:00
|
|
|
});
|
2017-05-12 15:55:27 +03:00
|
|
|
});
|
2013-11-11 22:54:09 +04:00
|
|
|
},
|
|
|
|
|
2014-01-12 12:05:28 +04:00
|
|
|
/**
|
|
|
|
* Checks whether downloaded files should be marked as coming from
|
|
|
|
* Internet Zone.
|
|
|
|
*
|
|
|
|
* @return true if files should be marked
|
|
|
|
*/
|
2016-04-18 16:21:55 +03:00
|
|
|
_shouldSaveZoneInformation() {
|
2014-01-12 12:05:28 +04:00
|
|
|
let key = Cc["@mozilla.org/windows-registry-key;1"]
|
|
|
|
.createInstance(Ci.nsIWindowsRegKey);
|
|
|
|
try {
|
|
|
|
key.open(Ci.nsIWindowsRegKey.ROOT_KEY_CURRENT_USER,
|
|
|
|
"Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Attachments",
|
|
|
|
Ci.nsIWindowsRegKey.ACCESS_QUERY_VALUE);
|
|
|
|
try {
|
|
|
|
return key.readIntValue("SaveZoneInformation") != 1;
|
|
|
|
} finally {
|
|
|
|
key.close();
|
|
|
|
}
|
|
|
|
} catch (ex) {
|
|
|
|
// If the key is not present, files should be marked by default.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-09-03 21:23:37 +04:00
|
|
|
/**
|
|
|
|
* Performs platform-specific operations when a download is done.
|
|
|
|
*
|
|
|
|
* aParam aDownload
|
|
|
|
* The Download object.
|
|
|
|
*
|
|
|
|
* @return {Promise}
|
|
|
|
* @resolves When all the operations completed successfully.
|
|
|
|
* @rejects JavaScript exception if any of the operations failed.
|
|
|
|
*/
|
2017-05-12 15:42:39 +03:00
|
|
|
async downloadDone(aDownload) {
|
2016-04-18 16:21:55 +03:00
|
|
|
// On Windows, we mark any file saved to the NTFS file system as coming
|
|
|
|
// from the Internet security zone unless Group Policy disables the
|
|
|
|
// feature. We do this by writing to the "Zone.Identifier" Alternate
|
|
|
|
// Data Stream directly, because the Save method of the
|
|
|
|
// IAttachmentExecute interface would trigger operations that may cause
|
|
|
|
// the application to hang, or other performance issues.
|
|
|
|
// The stream created in this way is forward-compatible with all the
|
|
|
|
// current and future versions of Windows.
|
2017-07-26 02:10:27 +03:00
|
|
|
if (AppConstants.platform == "win" && this._shouldSaveZoneInformation()) {
|
2016-04-18 16:21:55 +03:00
|
|
|
let zone;
|
|
|
|
try {
|
|
|
|
zone = gDownloadPlatform.mapUrlToZone(aDownload.source.url);
|
|
|
|
} catch (e) {
|
|
|
|
// Default to Internet Zone if mapUrlToZone failed for
|
|
|
|
// whatever reason.
|
|
|
|
zone = Ci.mozIDownloadPlatform.ZONE_INTERNET;
|
2013-09-10 14:40:39 +04:00
|
|
|
}
|
2014-06-21 03:54:06 +04:00
|
|
|
try {
|
2016-04-18 16:21:55 +03:00
|
|
|
// Don't write zone IDs for Local, Intranet, or Trusted sites
|
|
|
|
// to match Windows behavior.
|
|
|
|
if (zone >= Ci.mozIDownloadPlatform.ZONE_INTERNET) {
|
|
|
|
let streamPath = aDownload.target.path + ":Zone.Identifier";
|
2017-05-12 15:42:39 +03:00
|
|
|
let stream = await OS.File.open(
|
2017-04-06 11:08:06 +03:00
|
|
|
streamPath,
|
|
|
|
{ create: true },
|
|
|
|
{ winAllowLengthBeyondMaxPathWithCaveats: true }
|
|
|
|
);
|
2016-04-18 16:21:55 +03:00
|
|
|
try {
|
2017-05-12 15:42:39 +03:00
|
|
|
await stream.write(new TextEncoder().encode("[ZoneTransfer]\r\nZoneId=" + zone + "\r\n"));
|
2016-04-18 16:21:55 +03:00
|
|
|
} finally {
|
2017-05-12 15:42:39 +03:00
|
|
|
await stream.close();
|
2016-04-18 16:21:55 +03:00
|
|
|
}
|
2015-04-01 21:13:28 +03:00
|
|
|
}
|
2014-06-21 03:54:06 +04:00
|
|
|
} catch (ex) {
|
2016-04-18 16:21:55 +03:00
|
|
|
// If writing to the stream fails, we ignore the error and continue.
|
|
|
|
// The Windows API error 123 (ERROR_INVALID_NAME) is expected to
|
|
|
|
// occur when working on a file system that does not support
|
|
|
|
// Alternate Data Streams, like FAT32, thus we don't report this
|
|
|
|
// specific error.
|
|
|
|
if (!(ex instanceof OS.File.Error) || ex.winLastError != 123) {
|
2015-01-15 00:06:00 +03:00
|
|
|
Cu.reportError(ex);
|
|
|
|
}
|
2014-06-21 03:54:06 +04:00
|
|
|
}
|
2016-04-18 16:21:55 +03:00
|
|
|
}
|
2014-06-21 03:54:06 +04:00
|
|
|
|
2016-04-18 16:21:55 +03:00
|
|
|
// The file with the partially downloaded data has restrictive permissions
|
|
|
|
// that don't allow other users on the system to access it. Now that the
|
|
|
|
// download is completed, we need to adjust permissions based on whether
|
|
|
|
// this is a permanently downloaded file or a temporary download to be
|
|
|
|
// opened read-only with an external application.
|
|
|
|
try {
|
|
|
|
// The following logic to determine whether this is a temporary download
|
|
|
|
// is due to the fact that "deleteTempFileOnExit" is false on Mac, where
|
|
|
|
// downloads to be opened with external applications are preserved in
|
|
|
|
// the "Downloads" folder like normal downloads.
|
|
|
|
let isTemporaryDownload =
|
|
|
|
aDownload.launchWhenSucceeded && (aDownload.source.isPrivate ||
|
|
|
|
Services.prefs.getBoolPref("browser.helperApps.deleteTempFileOnExit"));
|
|
|
|
// Permanently downloaded files are made accessible by other users on
|
|
|
|
// this system, while temporary downloads are marked as read-only.
|
|
|
|
let options = {};
|
|
|
|
if (isTemporaryDownload) {
|
|
|
|
options.unixMode = 0o400;
|
|
|
|
options.winAttributes = {readOnly: true};
|
|
|
|
} else {
|
|
|
|
options.unixMode = 0o666;
|
|
|
|
}
|
|
|
|
// On Unix, the umask of the process is respected.
|
2017-05-12 15:42:39 +03:00
|
|
|
await OS.File.setPermissions(aDownload.target.path, options);
|
2016-04-18 16:21:55 +03:00
|
|
|
} catch (ex) {
|
|
|
|
// We should report errors with making the permissions less restrictive
|
|
|
|
// or marking the file as read-only on Unix and Mac, but this should not
|
|
|
|
// prevent the download from completing.
|
|
|
|
// The setPermissions API error EPERM is expected to occur when working
|
|
|
|
// on a file system that does not support file permissions, like FAT32,
|
|
|
|
// thus we don't report this error.
|
|
|
|
if (!(ex instanceof OS.File.Error) || ex.unixErrno != OS.Constants.libc.EPERM) {
|
|
|
|
Cu.reportError(ex);
|
|
|
|
}
|
|
|
|
}
|
2013-09-03 21:23:37 +04:00
|
|
|
|
2016-09-13 10:25:36 +03:00
|
|
|
let aReferrer = null;
|
|
|
|
if (aDownload.source.referrer) {
|
2016-09-15 15:58:51 +03:00
|
|
|
aReferrer = NetUtil.newURI(aDownload.source.referrer);
|
2016-09-13 10:25:36 +03:00
|
|
|
}
|
|
|
|
|
2016-04-18 16:21:55 +03:00
|
|
|
gDownloadPlatform.downloadDone(NetUtil.newURI(aDownload.source.url),
|
2016-09-13 10:25:36 +03:00
|
|
|
aReferrer,
|
2016-04-18 16:21:55 +03:00
|
|
|
new FileUtils.File(aDownload.target.path),
|
|
|
|
aDownload.contentType,
|
|
|
|
aDownload.source.isPrivate);
|
2017-05-12 15:42:39 +03:00
|
|
|
},
|
2016-04-18 16:21:55 +03:00
|
|
|
|
|
|
|
/**
|
2013-07-30 22:59:16 +04:00
|
|
|
* Launches a file represented by the target of a download. This can
|
|
|
|
* open the file with the default application for the target MIME type
|
|
|
|
* or file extension, or with a custom application if
|
|
|
|
* aDownload.launcherPath is set.
|
|
|
|
*
|
|
|
|
* @param aDownload
|
|
|
|
* A Download object that contains the necessary information
|
|
|
|
* to launch the file. The relevant properties are: the target
|
|
|
|
* file, the contentType and the custom application chosen
|
|
|
|
* to launch it.
|
|
|
|
*
|
|
|
|
* @return {Promise}
|
|
|
|
* @resolves When the instruction to launch the file has been
|
|
|
|
* successfully given to the operating system. Note that
|
|
|
|
* the OS might still take a while until the file is actually
|
|
|
|
* launched.
|
|
|
|
* @rejects JavaScript exception if there was an error trying to launch
|
|
|
|
* the file.
|
|
|
|
*/
|
2017-05-12 15:42:39 +03:00
|
|
|
async launchDownload(aDownload) {
|
2016-04-18 16:21:55 +03:00
|
|
|
let file = new FileUtils.File(aDownload.target.path);
|
2013-07-30 22:59:16 +04:00
|
|
|
|
2016-04-18 16:21:55 +03:00
|
|
|
// In case of a double extension, like ".tar.gz", we only
|
|
|
|
// consider the last one, because the MIME service cannot
|
|
|
|
// handle multiple extensions.
|
|
|
|
let fileExtension = null, mimeInfo = null;
|
|
|
|
let match = file.leafName.match(/\.([^.]+)$/);
|
|
|
|
if (match) {
|
|
|
|
fileExtension = match[1];
|
|
|
|
}
|
2013-07-30 22:59:16 +04:00
|
|
|
|
2017-05-30 00:38:54 +03:00
|
|
|
let isWindowsExe = AppConstants.platform == "win" &&
|
2018-06-26 17:27:36 +03:00
|
|
|
fileExtension && fileExtension.toLowerCase() == "exe";
|
2017-05-30 00:38:54 +03:00
|
|
|
|
|
|
|
// Ask for confirmation if the file is executable, except for .exe on
|
|
|
|
// Windows where the operating system will show the prompt based on the
|
|
|
|
// security zone. We do this here, instead of letting the caller handle
|
|
|
|
// the prompt separately in the user interface layer, for two reasons. The
|
|
|
|
// first is because of its security nature, so that add-ons cannot forget
|
|
|
|
// to do this check. The second is that the system-level security prompt
|
|
|
|
// would be displayed at launch time in any case.
|
|
|
|
if (file.isExecutable() && !isWindowsExe &&
|
|
|
|
!(await this.confirmLaunchExecutable(file.path))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-04-18 16:21:55 +03:00
|
|
|
try {
|
|
|
|
// The MIME service might throw if contentType == "" and it can't find
|
|
|
|
// a MIME type for the given extension, so we'll treat this case as
|
|
|
|
// an unknown mimetype.
|
|
|
|
mimeInfo = gMIMEService.getFromTypeAndExtension(aDownload.contentType,
|
|
|
|
fileExtension);
|
|
|
|
} catch (e) { }
|
|
|
|
|
|
|
|
if (aDownload.launcherPath) {
|
|
|
|
if (!mimeInfo) {
|
|
|
|
// This should not happen on normal circumstances because launcherPath
|
|
|
|
// is only set when we had an instance of nsIMIMEInfo to retrieve
|
|
|
|
// the custom application chosen by the user.
|
|
|
|
throw new Error(
|
|
|
|
"Unable to create nsIMIMEInfo to launch a custom application");
|
2013-07-30 22:59:16 +04:00
|
|
|
}
|
|
|
|
|
2016-04-18 16:21:55 +03:00
|
|
|
// Custom application chosen
|
|
|
|
let localHandlerApp = Cc["@mozilla.org/uriloader/local-handler-app;1"]
|
|
|
|
.createInstance(Ci.nsILocalHandlerApp);
|
|
|
|
localHandlerApp.executable = new FileUtils.File(aDownload.launcherPath);
|
2013-07-30 22:59:16 +04:00
|
|
|
|
2016-04-18 16:21:55 +03:00
|
|
|
mimeInfo.preferredApplicationHandler = localHandlerApp;
|
|
|
|
mimeInfo.preferredAction = Ci.nsIMIMEInfo.useHelperApp;
|
2013-07-30 22:59:16 +04:00
|
|
|
|
2016-04-18 16:21:55 +03:00
|
|
|
this.launchFile(file, mimeInfo);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// No custom application chosen, let's launch the file with the default
|
|
|
|
// handler. First, let's try to launch it through the MIME service.
|
|
|
|
if (mimeInfo) {
|
|
|
|
mimeInfo.preferredAction = Ci.nsIMIMEInfo.useSystemDefault;
|
2013-07-30 22:59:16 +04:00
|
|
|
|
|
|
|
try {
|
2016-04-18 16:21:55 +03:00
|
|
|
this.launchFile(file, mimeInfo);
|
2013-07-30 22:59:16 +04:00
|
|
|
return;
|
|
|
|
} catch (ex) { }
|
2016-04-18 16:21:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// If it didn't work or if there was no MIME info available,
|
|
|
|
// let's try to directly launch the file.
|
|
|
|
try {
|
|
|
|
this.launchFile(file);
|
|
|
|
return;
|
|
|
|
} catch (ex) { }
|
2013-07-30 22:59:16 +04:00
|
|
|
|
2016-04-18 16:21:55 +03:00
|
|
|
// If our previous attempts failed, try sending it through
|
|
|
|
// the system's external "file:" URL handler.
|
2017-08-06 05:25:42 +03:00
|
|
|
gExternalProtocolService.loadURI(NetUtil.newURI(file));
|
2017-05-12 15:42:39 +03:00
|
|
|
},
|
2013-07-30 22:59:16 +04:00
|
|
|
|
2016-04-18 16:21:55 +03:00
|
|
|
/**
|
|
|
|
* Asks for confirmation for launching the specified executable file. This
|
|
|
|
* can be overridden by regression tests to avoid the interactive prompt.
|
|
|
|
*/
|
2017-05-12 15:42:39 +03:00
|
|
|
async confirmLaunchExecutable(path) {
|
2016-04-18 16:21:55 +03:00
|
|
|
// We don't anchor the prompt to a specific window intentionally, not
|
|
|
|
// only because this is the same behavior as the system-level prompt,
|
|
|
|
// but also because the most recently active window is the right choice
|
|
|
|
// in basically all cases.
|
2017-08-29 20:57:11 +03:00
|
|
|
return DownloadUIHelper.getPrompter().confirmLaunchExecutable(path);
|
2017-05-12 15:42:39 +03:00
|
|
|
},
|
2013-07-30 22:59:16 +04:00
|
|
|
|
2016-04-18 16:21:55 +03:00
|
|
|
/**
|
|
|
|
* Launches the specified file, unless overridden by regression tests.
|
|
|
|
*/
|
|
|
|
launchFile(file, mimeInfo) {
|
|
|
|
if (mimeInfo) {
|
|
|
|
mimeInfo.launchWithFile(file);
|
|
|
|
} else {
|
|
|
|
file.launch();
|
|
|
|
}
|
2013-07-30 22:59:16 +04:00
|
|
|
},
|
|
|
|
|
2016-04-18 16:21:55 +03:00
|
|
|
/**
|
2013-07-30 22:59:16 +04:00
|
|
|
* Shows the containing folder of a file.
|
|
|
|
*
|
2016-04-18 16:21:55 +03:00
|
|
|
* @param aFilePath
|
|
|
|
* The path to the file.
|
2013-07-30 22:59:16 +04:00
|
|
|
*
|
|
|
|
* @return {Promise}
|
|
|
|
* @resolves When the instruction to open the containing folder has been
|
|
|
|
* successfully given to the operating system. Note that
|
|
|
|
* the OS might still take a while until the folder is actually
|
|
|
|
* opened.
|
|
|
|
* @rejects JavaScript exception if there was an error trying to open
|
|
|
|
* the containing folder.
|
|
|
|
*/
|
2017-05-12 15:42:39 +03:00
|
|
|
async showContainingDirectory(aFilePath) {
|
2016-04-18 16:21:55 +03:00
|
|
|
let file = new FileUtils.File(aFilePath);
|
2013-07-30 22:59:16 +04:00
|
|
|
|
2016-04-18 16:21:55 +03:00
|
|
|
try {
|
|
|
|
// Show the directory containing the file and select the file.
|
|
|
|
file.reveal();
|
|
|
|
return;
|
|
|
|
} catch (ex) { }
|
|
|
|
|
|
|
|
// If reveal fails for some reason (e.g., it's not implemented on unix
|
|
|
|
// or the file doesn't exist), try using the parent if we have it.
|
|
|
|
let parent = file.parent;
|
|
|
|
if (!parent) {
|
|
|
|
throw new Error(
|
|
|
|
"Unexpected reference to a top-level directory instead of a file");
|
2013-07-30 22:59:16 +04:00
|
|
|
}
|
|
|
|
|
2016-04-18 16:21:55 +03:00
|
|
|
try {
|
|
|
|
// Open the parent directory to show where the file should be.
|
|
|
|
parent.launch();
|
|
|
|
return;
|
|
|
|
} catch (ex) { }
|
|
|
|
|
|
|
|
// If launch also fails (probably because it's not implemented), let
|
|
|
|
// the OS handler try to open the parent.
|
2017-08-06 05:25:42 +03:00
|
|
|
gExternalProtocolService.loadURI(NetUtil.newURI(parent));
|
2017-05-12 15:42:39 +03:00
|
|
|
},
|
2013-07-30 22:59:16 +04:00
|
|
|
|
2013-06-03 07:43:06 +04:00
|
|
|
/**
|
|
|
|
* Calls the directory service, create a downloads directory and returns an
|
|
|
|
* nsIFile for the downloads directory.
|
|
|
|
*
|
|
|
|
* @return {Promise}
|
2013-10-23 06:04:55 +04:00
|
|
|
* @resolves The directory string path.
|
2013-06-03 07:43:06 +04:00
|
|
|
*/
|
2016-04-18 16:21:55 +03:00
|
|
|
_createDownloadsDirectory(aName) {
|
2013-08-17 17:13:10 +04:00
|
|
|
// We read the name of the directory from the list of translated strings
|
|
|
|
// that is kept by the UI helper module, even if this string is not strictly
|
|
|
|
// displayed in the user interface.
|
2013-10-23 06:04:55 +04:00
|
|
|
let directoryPath = OS.Path.join(this._getDirectory(aName),
|
|
|
|
DownloadUIHelper.strings.downloadsFolder);
|
2013-06-03 07:43:06 +04:00
|
|
|
|
|
|
|
// Create the Downloads folder and ignore if it already exists.
|
2016-04-18 16:21:55 +03:00
|
|
|
return OS.File.makeDir(directoryPath, { ignoreExisting: true })
|
|
|
|
.then(() => directoryPath);
|
2013-06-03 07:43:06 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2016-04-18 16:21:55 +03:00
|
|
|
* Returns the string path for the given directory service location name. This
|
|
|
|
* can be overridden by regression tests to return the path of the system
|
|
|
|
* temporary directory in all cases.
|
2013-06-03 07:43:06 +04:00
|
|
|
*/
|
2016-04-18 16:21:55 +03:00
|
|
|
_getDirectory(name) {
|
|
|
|
return Services.dirsvc.get(name, Ci.nsIFile).path;
|
2013-08-09 08:40:53 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register the downloads interruption observers.
|
|
|
|
*
|
|
|
|
* @param aList
|
|
|
|
* The public or private downloads list.
|
|
|
|
* @param aIsPrivate
|
|
|
|
* True if the list is private, false otherwise.
|
|
|
|
*
|
|
|
|
* @return {Promise}
|
|
|
|
* @resolves When the views and observers are added.
|
|
|
|
*/
|
2016-04-18 16:21:55 +03:00
|
|
|
addListObservers(aList, aIsPrivate) {
|
2013-08-09 08:40:53 +04:00
|
|
|
DownloadObserver.registerView(aList, aIsPrivate);
|
|
|
|
if (!DownloadObserver.observersAdded) {
|
|
|
|
DownloadObserver.observersAdded = true;
|
2014-02-17 20:00:36 +04:00
|
|
|
for (let topic of kObserverTopics) {
|
2017-04-14 22:51:38 +03:00
|
|
|
Services.obs.addObserver(DownloadObserver, topic);
|
2014-02-17 20:00:36 +04:00
|
|
|
}
|
2013-08-09 08:40:53 +04:00
|
|
|
}
|
|
|
|
return Promise.resolve();
|
2013-08-16 22:35:42 +04:00
|
|
|
},
|
|
|
|
|
2015-08-11 23:33:57 +03:00
|
|
|
/**
|
|
|
|
* Force a save on _store if it exists. Used to ensure downloads do not
|
|
|
|
* persist after being sanitized on Android.
|
|
|
|
*
|
|
|
|
* @return {Promise}
|
|
|
|
* @resolves When _store.save() completes.
|
|
|
|
*/
|
2016-04-18 16:21:55 +03:00
|
|
|
forceSave() {
|
2015-08-11 23:33:57 +03:00
|
|
|
if (this._store) {
|
|
|
|
return this._store.save();
|
|
|
|
}
|
|
|
|
return Promise.resolve();
|
|
|
|
},
|
2013-01-29 18:24:17 +04:00
|
|
|
};
|
2013-08-09 08:40:53 +04:00
|
|
|
|
2018-02-23 22:50:01 +03:00
|
|
|
var DownloadObserver = {
|
2013-08-09 08:40:53 +04:00
|
|
|
/**
|
|
|
|
* Flag to determine if the observers have been added previously.
|
|
|
|
*/
|
|
|
|
observersAdded: false,
|
|
|
|
|
2013-09-17 17:23:22 +04:00
|
|
|
/**
|
|
|
|
* Timer used to delay restarting canceled downloads upon waking and returning
|
|
|
|
* online.
|
|
|
|
*/
|
|
|
|
_wakeTimer: null,
|
|
|
|
|
2013-08-09 08:40:53 +04:00
|
|
|
/**
|
|
|
|
* Set that contains the in progress publics downloads.
|
2013-09-17 17:23:22 +04:00
|
|
|
* It's kept updated when a public download is added, removed or changes its
|
2013-08-09 08:40:53 +04:00
|
|
|
* properties.
|
|
|
|
*/
|
|
|
|
_publicInProgressDownloads: new Set(),
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set that contains the in progress private downloads.
|
2013-09-17 17:23:22 +04:00
|
|
|
* It's kept updated when a private download is added, removed or changes its
|
2013-08-09 08:40:53 +04:00
|
|
|
* properties.
|
|
|
|
*/
|
|
|
|
_privateInProgressDownloads: new Set(),
|
|
|
|
|
2013-09-17 17:23:22 +04:00
|
|
|
/**
|
|
|
|
* Set that contains the downloads that have been canceled when going offline
|
|
|
|
* or to sleep. These are started again when returning online or waking. This
|
|
|
|
* list is not persisted so when exiting and restarting, the downloads will not
|
|
|
|
* be started again.
|
|
|
|
*/
|
|
|
|
_canceledOfflineDownloads: new Set(),
|
|
|
|
|
2013-08-09 08:40:53 +04:00
|
|
|
/**
|
|
|
|
* Registers a view that updates the corresponding downloads state set, based
|
|
|
|
* on the aIsPrivate argument. The set is updated when a download is added,
|
2013-09-17 17:23:22 +04:00
|
|
|
* removed or changes its properties.
|
2013-08-09 08:40:53 +04:00
|
|
|
*
|
|
|
|
* @param aList
|
|
|
|
* The public or private downloads list.
|
|
|
|
* @param aIsPrivate
|
|
|
|
* True if the list is private, false otherwise.
|
|
|
|
*/
|
|
|
|
registerView: function DO_registerView(aList, aIsPrivate) {
|
|
|
|
let downloadsSet = aIsPrivate ? this._privateInProgressDownloads
|
|
|
|
: this._publicInProgressDownloads;
|
|
|
|
let downloadsView = {
|
2013-09-17 17:23:22 +04:00
|
|
|
onDownloadAdded: aDownload => {
|
2013-08-09 08:40:53 +04:00
|
|
|
if (!aDownload.stopped) {
|
|
|
|
downloadsSet.add(aDownload);
|
|
|
|
}
|
|
|
|
},
|
2013-09-17 17:23:22 +04:00
|
|
|
onDownloadChanged: aDownload => {
|
2013-08-09 08:40:53 +04:00
|
|
|
if (aDownload.stopped) {
|
|
|
|
downloadsSet.delete(aDownload);
|
|
|
|
} else {
|
|
|
|
downloadsSet.add(aDownload);
|
|
|
|
}
|
|
|
|
},
|
2013-09-17 17:23:22 +04:00
|
|
|
onDownloadRemoved: aDownload => {
|
2013-08-09 08:40:53 +04:00
|
|
|
downloadsSet.delete(aDownload);
|
2013-09-17 17:23:22 +04:00
|
|
|
// The download must also be removed from the canceled when offline set.
|
|
|
|
this._canceledOfflineDownloads.delete(aDownload);
|
2013-08-09 08:40:53 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-09-17 20:02:21 +04:00
|
|
|
// We register the view asynchronously.
|
2017-06-19 13:32:37 +03:00
|
|
|
aList.addView(downloadsView).catch(Cu.reportError);
|
2013-08-09 08:40:53 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2013-09-04 08:01:42 +04:00
|
|
|
* Wrapper that handles the test mode before calling the prompt that display
|
|
|
|
* a warning message box that informs that there are active downloads,
|
|
|
|
* and asks whether the user wants to cancel them or not.
|
2013-08-09 08:40:53 +04:00
|
|
|
*
|
|
|
|
* @param aCancel
|
|
|
|
* The observer notification subject.
|
|
|
|
* @param aDownloadsCount
|
|
|
|
* The current downloads count.
|
2013-09-04 08:01:42 +04:00
|
|
|
* @param aPrompter
|
|
|
|
* The prompter object that shows the confirm dialog.
|
|
|
|
* @param aPromptType
|
|
|
|
* The type of prompt notification depending on the observer.
|
2013-08-09 08:40:53 +04:00
|
|
|
*/
|
|
|
|
_confirmCancelDownloads: function DO_confirmCancelDownload(
|
2013-09-04 08:01:42 +04:00
|
|
|
aCancel, aDownloadsCount, aPrompter, aPromptType) {
|
2013-08-09 08:40:53 +04:00
|
|
|
// If user has already dismissed the request, then do nothing.
|
|
|
|
if ((aCancel instanceof Ci.nsISupportsPRBool) && aCancel.data) {
|
|
|
|
return;
|
|
|
|
}
|
2013-08-24 02:33:52 +04:00
|
|
|
// Handle test mode
|
2016-04-18 16:21:55 +03:00
|
|
|
if (gCombinedDownloadIntegration._testPromptDownloads) {
|
|
|
|
gCombinedDownloadIntegration._testPromptDownloads = aDownloadsCount;
|
2013-08-24 02:33:52 +04:00
|
|
|
return;
|
|
|
|
}
|
2013-08-09 08:40:53 +04:00
|
|
|
|
2013-09-04 08:01:42 +04:00
|
|
|
aCancel.data = aPrompter.confirmCancelDownloads(aDownloadsCount, aPromptType);
|
2013-08-09 08:40:53 +04:00
|
|
|
},
|
|
|
|
|
2013-09-17 17:23:22 +04:00
|
|
|
/**
|
|
|
|
* Resume all downloads that were paused when going offline, used when waking
|
|
|
|
* from sleep or returning from being offline.
|
|
|
|
*/
|
|
|
|
_resumeOfflineDownloads: function DO_resumeOfflineDownloads() {
|
|
|
|
this._wakeTimer = null;
|
|
|
|
|
|
|
|
for (let download of this._canceledOfflineDownloads) {
|
2016-02-03 15:58:03 +03:00
|
|
|
download.start().catch(() => {});
|
2013-09-17 17:23:22 +04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-07-24 14:16:01 +03:00
|
|
|
// nsIObserver
|
2013-08-09 08:40:53 +04:00
|
|
|
observe: function DO_observe(aSubject, aTopic, aData) {
|
|
|
|
let downloadsCount;
|
2013-09-04 08:01:42 +04:00
|
|
|
let p = DownloadUIHelper.getPrompter();
|
2013-08-09 08:40:53 +04:00
|
|
|
switch (aTopic) {
|
|
|
|
case "quit-application-requested":
|
|
|
|
downloadsCount = this._publicInProgressDownloads.size +
|
|
|
|
this._privateInProgressDownloads.size;
|
2013-09-04 08:01:42 +04:00
|
|
|
this._confirmCancelDownloads(aSubject, downloadsCount, p, p.ON_QUIT);
|
2013-08-09 08:40:53 +04:00
|
|
|
break;
|
|
|
|
case "offline-requested":
|
|
|
|
downloadsCount = this._publicInProgressDownloads.size +
|
|
|
|
this._privateInProgressDownloads.size;
|
2013-09-04 08:01:42 +04:00
|
|
|
this._confirmCancelDownloads(aSubject, downloadsCount, p, p.ON_OFFLINE);
|
2013-08-09 08:40:53 +04:00
|
|
|
break;
|
|
|
|
case "last-pb-context-exiting":
|
2013-09-04 08:01:42 +04:00
|
|
|
downloadsCount = this._privateInProgressDownloads.size;
|
|
|
|
this._confirmCancelDownloads(aSubject, downloadsCount, p,
|
|
|
|
p.ON_LEAVE_PRIVATE_BROWSING);
|
2013-08-09 08:40:53 +04:00
|
|
|
break;
|
2013-09-12 16:03:04 +04:00
|
|
|
case "last-pb-context-exited":
|
2017-05-12 15:42:39 +03:00
|
|
|
let promise = (async function() {
|
|
|
|
let list = await Downloads.getList(Downloads.PRIVATE);
|
|
|
|
let downloads = await list.getAll();
|
2013-09-12 16:03:04 +04:00
|
|
|
|
2013-09-17 20:02:21 +04:00
|
|
|
// We can remove the downloads and finalize them in parallel.
|
2013-09-12 16:03:04 +04:00
|
|
|
for (let download of downloads) {
|
2017-06-19 13:32:37 +03:00
|
|
|
list.remove(download).catch(Cu.reportError);
|
|
|
|
download.finalize(true).catch(Cu.reportError);
|
2013-09-12 16:03:04 +04:00
|
|
|
}
|
2017-05-12 15:42:39 +03:00
|
|
|
})();
|
2013-09-12 16:03:04 +04:00
|
|
|
// Handle test mode
|
2016-04-18 16:21:55 +03:00
|
|
|
if (gCombinedDownloadIntegration._testResolveClearPrivateList) {
|
|
|
|
gCombinedDownloadIntegration._testResolveClearPrivateList(promise);
|
|
|
|
} else {
|
|
|
|
promise.catch(ex => Cu.reportError(ex));
|
2013-09-12 16:03:04 +04:00
|
|
|
}
|
|
|
|
break;
|
2013-09-17 17:23:22 +04:00
|
|
|
case "sleep_notification":
|
|
|
|
case "suspend_process_notification":
|
|
|
|
case "network:offline-about-to-go-offline":
|
|
|
|
for (let download of this._publicInProgressDownloads) {
|
|
|
|
download.cancel();
|
|
|
|
this._canceledOfflineDownloads.add(download);
|
|
|
|
}
|
|
|
|
for (let download of this._privateInProgressDownloads) {
|
|
|
|
download.cancel();
|
|
|
|
this._canceledOfflineDownloads.add(download);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "wake_notification":
|
|
|
|
case "resume_process_notification":
|
2017-08-29 20:57:11 +03:00
|
|
|
let wakeDelay =
|
|
|
|
Services.prefs.getIntPref("browser.download.manager.resumeOnWakeDelay", 10000);
|
2013-09-17 17:23:22 +04:00
|
|
|
|
|
|
|
if (wakeDelay >= 0) {
|
|
|
|
this._wakeTimer = new Timer(this._resumeOfflineDownloads.bind(this), wakeDelay,
|
|
|
|
Ci.nsITimer.TYPE_ONE_SHOT);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "network:offline-status-changed":
|
|
|
|
if (aData == "online") {
|
|
|
|
this._resumeOfflineDownloads();
|
2013-09-12 16:03:04 +04:00
|
|
|
}
|
|
|
|
break;
|
2014-02-17 20:00:36 +04:00
|
|
|
// We need to unregister observers explicitly before we reach the
|
|
|
|
// "xpcom-shutdown" phase, otherwise observers may be notified when some
|
|
|
|
// required services are not available anymore. We can't unregister
|
|
|
|
// observers on "quit-application", because this module is also loaded
|
|
|
|
// during "make package" automation, and the quit notification is not sent
|
|
|
|
// in that execution environment (bug 973637).
|
|
|
|
case "xpcom-will-shutdown":
|
|
|
|
for (let topic of kObserverTopics) {
|
|
|
|
Services.obs.removeObserver(this, topic);
|
|
|
|
}
|
|
|
|
break;
|
2013-08-09 08:40:53 +04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-04-23 06:55:06 +03:00
|
|
|
QueryInterface: ChromeUtils.generateQI([Ci.nsIObserver])
|
2013-08-09 08:40:53 +04:00
|
|
|
};
|
2013-08-16 13:02:18 +04:00
|
|
|
|
2013-09-12 23:20:30 +04:00
|
|
|
/**
|
|
|
|
* Registers a Places observer so that operations on download history are
|
|
|
|
* reflected on the provided list of downloads.
|
|
|
|
*
|
|
|
|
* You do not need to keep a reference to this object in order to keep it alive,
|
|
|
|
* because the history service already keeps a strong reference to it.
|
|
|
|
*
|
|
|
|
* @param aList
|
|
|
|
* DownloadList object linked to this observer.
|
|
|
|
*/
|
2018-02-23 22:50:01 +03:00
|
|
|
var DownloadHistoryObserver = function(aList) {
|
2013-09-12 23:20:30 +04:00
|
|
|
this._list = aList;
|
2017-04-14 22:51:38 +03:00
|
|
|
PlacesUtils.history.addObserver(this);
|
2017-10-15 21:50:30 +03:00
|
|
|
};
|
2013-09-12 23:20:30 +04:00
|
|
|
|
2013-11-13 01:17:28 +04:00
|
|
|
this.DownloadHistoryObserver.prototype = {
|
2013-09-12 23:20:30 +04:00
|
|
|
/**
|
|
|
|
* DownloadList object linked to this observer.
|
|
|
|
*/
|
|
|
|
_list: null,
|
|
|
|
|
2018-04-23 06:55:06 +03:00
|
|
|
QueryInterface: ChromeUtils.generateQI([Ci.nsINavHistoryObserver]),
|
2013-09-12 23:20:30 +04:00
|
|
|
|
2017-07-24 14:16:01 +03:00
|
|
|
// nsINavHistoryObserver
|
2013-09-12 23:20:30 +04:00
|
|
|
onDeleteURI: function DL_onDeleteURI(aURI, aGUID) {
|
|
|
|
this._list.removeFinished(download => aURI.equals(NetUtil.newURI(
|
|
|
|
download.source.url)));
|
|
|
|
},
|
|
|
|
|
2017-07-24 14:16:01 +03:00
|
|
|
// nsINavHistoryObserver
|
2013-09-12 23:20:30 +04:00
|
|
|
onClearHistory: function DL_onClearHistory() {
|
|
|
|
this._list.removeFinished();
|
|
|
|
},
|
|
|
|
|
2017-08-29 20:57:11 +03:00
|
|
|
onTitleChanged() {},
|
|
|
|
onBeginUpdateBatch() {},
|
|
|
|
onEndUpdateBatch() {},
|
|
|
|
onPageChanged() {},
|
|
|
|
onDeleteVisits() {},
|
2013-09-12 23:20:30 +04:00
|
|
|
};
|
|
|
|
|
2013-08-16 13:02:18 +04:00
|
|
|
/**
|
|
|
|
* This view can be added to a DownloadList object to trigger a save operation
|
2013-09-17 20:02:21 +04:00
|
|
|
* in the given DownloadStore object when a relevant change occurs. You should
|
|
|
|
* call the "initialize" method in order to register the view and load the
|
|
|
|
* current state from disk.
|
2013-08-16 13:02:18 +04:00
|
|
|
*
|
2013-09-12 23:20:30 +04:00
|
|
|
* You do not need to keep a reference to this object in order to keep it alive,
|
|
|
|
* because the DownloadList object already keeps a strong reference to it.
|
|
|
|
*
|
|
|
|
* @param aList
|
|
|
|
* The DownloadList object on which the view should be registered.
|
2013-08-16 13:02:18 +04:00
|
|
|
* @param aStore
|
|
|
|
* The DownloadStore object used for saving.
|
|
|
|
*/
|
2018-02-23 22:50:01 +03:00
|
|
|
var DownloadAutoSaveView = function(aList, aStore) {
|
2013-09-17 20:02:21 +04:00
|
|
|
this._list = aList;
|
2013-08-16 13:02:18 +04:00
|
|
|
this._store = aStore;
|
|
|
|
this._downloadsMap = new Map();
|
2013-12-03 16:21:08 +04:00
|
|
|
this._writer = new DeferredTask(() => this._store.save(), kSaveDelayMs);
|
2015-03-17 00:40:00 +03:00
|
|
|
AsyncShutdown.profileBeforeChange.addBlocker("DownloadAutoSaveView: writing data",
|
|
|
|
() => this._writer.finalize());
|
2017-10-15 21:50:30 +03:00
|
|
|
};
|
2013-08-16 13:02:18 +04:00
|
|
|
|
2013-11-13 01:17:28 +04:00
|
|
|
this.DownloadAutoSaveView.prototype = {
|
2013-08-16 13:02:18 +04:00
|
|
|
/**
|
2013-09-17 20:02:21 +04:00
|
|
|
* DownloadList object linked to this view.
|
2013-08-16 13:02:18 +04:00
|
|
|
*/
|
2013-09-17 20:02:21 +04:00
|
|
|
_list: null,
|
2013-08-16 13:02:18 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The DownloadStore object used for saving.
|
|
|
|
*/
|
|
|
|
_store: null,
|
|
|
|
|
2013-09-17 20:02:21 +04:00
|
|
|
/**
|
|
|
|
* True when the initial state of the downloads has been loaded.
|
|
|
|
*/
|
|
|
|
_initialized: false,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Registers the view and loads the current state from disk.
|
|
|
|
*
|
|
|
|
* @return {Promise}
|
|
|
|
* @resolves When the view has been registered.
|
|
|
|
* @rejects JavaScript exception.
|
|
|
|
*/
|
2017-08-29 20:57:11 +03:00
|
|
|
initialize() {
|
2013-09-17 20:02:21 +04:00
|
|
|
// We set _initialized to true after adding the view, so that
|
|
|
|
// onDownloadAdded doesn't cause a save to occur.
|
|
|
|
return this._list.addView(this).then(() => this._initialized = true);
|
|
|
|
},
|
|
|
|
|
2013-08-16 13:02:18 +04:00
|
|
|
/**
|
|
|
|
* This map contains only Download objects that should be saved to disk, and
|
|
|
|
* associates them with the result of their getSerializationHash function, for
|
|
|
|
* the purpose of detecting changes to the relevant properties.
|
|
|
|
*/
|
|
|
|
_downloadsMap: null,
|
|
|
|
|
|
|
|
/**
|
2013-12-03 16:21:08 +04:00
|
|
|
* DeferredTask for the save operation.
|
2013-08-16 13:02:18 +04:00
|
|
|
*/
|
2013-12-03 16:21:08 +04:00
|
|
|
_writer: null,
|
2013-08-16 13:02:18 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when the list of downloads changed, this triggers the asynchronous
|
|
|
|
* serialization of the list of downloads.
|
|
|
|
*/
|
2017-08-29 20:57:11 +03:00
|
|
|
saveSoon() {
|
2013-12-03 16:21:08 +04:00
|
|
|
this._writer.arm();
|
2013-08-16 13:02:18 +04:00
|
|
|
},
|
|
|
|
|
2017-07-24 14:16:01 +03:00
|
|
|
// DownloadList callback
|
2017-08-29 20:57:11 +03:00
|
|
|
onDownloadAdded(aDownload) {
|
2016-04-18 16:21:55 +03:00
|
|
|
if (gCombinedDownloadIntegration.shouldPersistDownload(aDownload)) {
|
2013-08-16 13:02:18 +04:00
|
|
|
this._downloadsMap.set(aDownload, aDownload.getSerializationHash());
|
|
|
|
if (this._initialized) {
|
|
|
|
this.saveSoon();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-07-24 14:16:01 +03:00
|
|
|
// DownloadList callback
|
2017-08-29 20:57:11 +03:00
|
|
|
onDownloadChanged(aDownload) {
|
2016-04-18 16:21:55 +03:00
|
|
|
if (!gCombinedDownloadIntegration.shouldPersistDownload(aDownload)) {
|
2013-08-16 13:02:18 +04:00
|
|
|
if (this._downloadsMap.has(aDownload)) {
|
|
|
|
this._downloadsMap.delete(aDownload);
|
|
|
|
this.saveSoon();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let hash = aDownload.getSerializationHash();
|
|
|
|
if (this._downloadsMap.get(aDownload) != hash) {
|
|
|
|
this._downloadsMap.set(aDownload, hash);
|
|
|
|
this.saveSoon();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-07-24 14:16:01 +03:00
|
|
|
// DownloadList callback
|
2017-08-29 20:57:11 +03:00
|
|
|
onDownloadRemoved(aDownload) {
|
2013-08-16 13:02:18 +04:00
|
|
|
if (this._downloadsMap.has(aDownload)) {
|
|
|
|
this._downloadsMap.delete(aDownload);
|
|
|
|
this.saveSoon();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|