mirror of
https://gitlab.winehq.org/wine/wine-gecko.git
synced 2024-09-13 09:24:08 -07:00
Bug 908256, stop downloads when suspending or going offline, r=paolo
This commit is contained in:
parent
28694095a5
commit
8b8cf4e5fc
@ -696,6 +696,13 @@ this.DownloadIntegration = {
|
||||
Services.obs.addObserver(DownloadObserver, "offline-requested", true);
|
||||
Services.obs.addObserver(DownloadObserver, "last-pb-context-exiting", true);
|
||||
Services.obs.addObserver(DownloadObserver, "last-pb-context-exited", true);
|
||||
|
||||
Services.obs.addObserver(DownloadObserver, "sleep_notification", true);
|
||||
Services.obs.addObserver(DownloadObserver, "suspend_process_notification", true);
|
||||
Services.obs.addObserver(DownloadObserver, "wake_notification", true);
|
||||
Services.obs.addObserver(DownloadObserver, "resume_process_notification", true);
|
||||
Services.obs.addObserver(DownloadObserver, "network:offline-about-to-go-offline", true);
|
||||
Services.obs.addObserver(DownloadObserver, "network:offline-status-changed", true);
|
||||
}
|
||||
return Promise.resolve();
|
||||
},
|
||||
@ -724,24 +731,38 @@ this.DownloadObserver = {
|
||||
*/
|
||||
observersAdded: false,
|
||||
|
||||
/**
|
||||
* Timer used to delay restarting canceled downloads upon waking and returning
|
||||
* online.
|
||||
*/
|
||||
_wakeTimer: null,
|
||||
|
||||
/**
|
||||
* Set that contains the in progress publics downloads.
|
||||
* It's keep updated when a public download is added, removed or change its
|
||||
* It's kept updated when a public download is added, removed or changes its
|
||||
* properties.
|
||||
*/
|
||||
_publicInProgressDownloads: new Set(),
|
||||
|
||||
/**
|
||||
* Set that contains the in progress private downloads.
|
||||
* It's keep updated when a private download is added, removed or change its
|
||||
* It's kept updated when a private download is added, removed or changes its
|
||||
* properties.
|
||||
*/
|
||||
_privateInProgressDownloads: new Set(),
|
||||
|
||||
/**
|
||||
* 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(),
|
||||
|
||||
/**
|
||||
* Registers a view that updates the corresponding downloads state set, based
|
||||
* on the aIsPrivate argument. The set is updated when a download is added,
|
||||
* removed or changed its properties.
|
||||
* removed or changes its properties.
|
||||
*
|
||||
* @param aList
|
||||
* The public or private downloads list.
|
||||
@ -752,20 +773,22 @@ this.DownloadObserver = {
|
||||
let downloadsSet = aIsPrivate ? this._privateInProgressDownloads
|
||||
: this._publicInProgressDownloads;
|
||||
let downloadsView = {
|
||||
onDownloadAdded: function DO_V_onDownloadAdded(aDownload) {
|
||||
onDownloadAdded: aDownload => {
|
||||
if (!aDownload.stopped) {
|
||||
downloadsSet.add(aDownload);
|
||||
}
|
||||
},
|
||||
onDownloadChanged: function DO_V_onDownloadChanged(aDownload) {
|
||||
onDownloadChanged: aDownload => {
|
||||
if (aDownload.stopped) {
|
||||
downloadsSet.delete(aDownload);
|
||||
} else {
|
||||
downloadsSet.add(aDownload);
|
||||
}
|
||||
},
|
||||
onDownloadRemoved: function DO_V_onDownloadRemoved(aDownload) {
|
||||
onDownloadRemoved: aDownload => {
|
||||
downloadsSet.delete(aDownload);
|
||||
// The download must also be removed from the canceled when offline set.
|
||||
this._canceledOfflineDownloads.delete(aDownload);
|
||||
}
|
||||
};
|
||||
|
||||
@ -801,6 +824,18 @@ this.DownloadObserver = {
|
||||
aCancel.data = aPrompter.confirmCancelDownloads(aDownloadsCount, aPromptType);
|
||||
},
|
||||
|
||||
/**
|
||||
* 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) {
|
||||
download.start();
|
||||
}
|
||||
},
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////
|
||||
//// nsIObserver
|
||||
|
||||
@ -839,6 +874,35 @@ this.DownloadObserver = {
|
||||
(error) => { DownloadIntegration._deferTestClearPrivateList.reject(error); });
|
||||
}
|
||||
break;
|
||||
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":
|
||||
let wakeDelay = 10000;
|
||||
try {
|
||||
wakeDelay = Services.prefs.getIntPref("browser.download.manager.resumeOnWakeDelay");
|
||||
} catch(e) {}
|
||||
|
||||
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();
|
||||
}
|
||||
break;
|
||||
}
|
||||
},
|
||||
|
||||
|
@ -300,6 +300,87 @@ add_task(function test_mix_notifications()
|
||||
privateList.remove(download2);
|
||||
});
|
||||
|
||||
/**
|
||||
* Tests suspending and resuming as well as going offline and then online again.
|
||||
* The downloads should stop when suspending and start again when resuming.
|
||||
*/
|
||||
add_task(function test_suspend_resume()
|
||||
{
|
||||
enableObserversTestMode();
|
||||
|
||||
// The default wake delay is 10 seconds, so set the wake delay to be much
|
||||
// faster for these tests.
|
||||
Services.prefs.setIntPref("browser.download.manager.resumeOnWakeDelay", 5);
|
||||
|
||||
let addDownload = function(list)
|
||||
{
|
||||
return Task.spawn(function () {
|
||||
let download = yield promiseNewDownload(httpUrl("interruptible.txt"));
|
||||
download.start();
|
||||
list.add(download);
|
||||
throw new Task.Result(download);
|
||||
});
|
||||
}
|
||||
|
||||
let publicList = yield promiseNewList();
|
||||
let privateList = yield promiseNewList(true);
|
||||
|
||||
let download1 = yield addDownload(publicList);
|
||||
let download2 = yield addDownload(publicList);
|
||||
let download3 = yield addDownload(privateList);
|
||||
let download4 = yield addDownload(privateList);
|
||||
let download5 = yield addDownload(publicList);
|
||||
|
||||
// First, check that the downloads are all canceled when going to sleep.
|
||||
Services.obs.notifyObservers(null, "sleep_notification", null);
|
||||
do_check_true(download1.canceled);
|
||||
do_check_true(download2.canceled);
|
||||
do_check_true(download3.canceled);
|
||||
do_check_true(download4.canceled);
|
||||
do_check_true(download5.canceled);
|
||||
|
||||
// Remove a download. It should not be started again.
|
||||
publicList.remove(download5);
|
||||
do_check_true(download5.canceled);
|
||||
|
||||
// When waking up again, the downloads start again after the wake delay. To be
|
||||
// more robust, don't check after a delay but instead just wait for the
|
||||
// downloads to finish.
|
||||
Services.obs.notifyObservers(null, "wake_notification", null);
|
||||
yield download1.whenSucceeded();
|
||||
yield download2.whenSucceeded();
|
||||
yield download3.whenSucceeded();
|
||||
yield download4.whenSucceeded();
|
||||
|
||||
// Downloads should no longer be canceled. However, as download5 was removed
|
||||
// from the public list, it will not be restarted.
|
||||
do_check_false(download1.canceled);
|
||||
do_check_true(download5.canceled);
|
||||
|
||||
// Create four new downloads and check for going offline and then online again.
|
||||
|
||||
download1 = yield addDownload(publicList);
|
||||
download2 = yield addDownload(publicList);
|
||||
download3 = yield addDownload(privateList);
|
||||
download4 = yield addDownload(privateList);
|
||||
|
||||
// Going offline should cancel the downloads.
|
||||
Services.obs.notifyObservers(null, "network:offline-about-to-go-offline", null);
|
||||
do_check_true(download1.canceled);
|
||||
do_check_true(download2.canceled);
|
||||
do_check_true(download3.canceled);
|
||||
do_check_true(download4.canceled);
|
||||
|
||||
// Going back online should start the downloads again.
|
||||
Services.obs.notifyObservers(null, "network:offline-status-changed", "online");
|
||||
yield download1.whenSucceeded();
|
||||
yield download2.whenSucceeded();
|
||||
yield download3.whenSucceeded();
|
||||
yield download4.whenSucceeded();
|
||||
|
||||
Services.prefs.clearUserPref("browser.download.manager.resumeOnWakeDelay");
|
||||
});
|
||||
|
||||
/**
|
||||
* Tests both the downloads list and the in-progress downloads are clear when
|
||||
* private browsing observer is notified.
|
||||
|
Loading…
Reference in New Issue
Block a user