Bug 794606 - Make resuming a download correctly share the privacy status of the original download. r=mak

This commit is contained in:
Josh Matthews 2012-10-04 15:08:28 -04:00
parent 58243b9b26
commit b4ec952733
4 changed files with 131 additions and 0 deletions

View File

@ -2989,6 +2989,11 @@ nsDownload::Resume()
rv = NS_NewChannel(getter_AddRefs(channel), mSource, nullptr, nullptr, ir);
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIPrivateBrowsingChannel> pbChannel = do_QueryInterface(channel);
if (pbChannel) {
pbChannel->SetPrivate(mDownloadManager->mInPrivateBrowsing);
}
// Make sure we can get a file, either the temporary or the real target, for
// both purposes of file size and a target to write to
nsCOMPtr<nsIFile> targetLocalFile(mTempFile);

View File

@ -111,6 +111,7 @@ function addDownload(aParams)
// it is part of the active downloads the moment addDownload is called
gDownloadCount++;
let dm = downloadUtils.downloadManager;
var dl = dm.addDownload(Ci.nsIDownloadManager.DOWNLOAD_TYPE_DOWNLOAD,
createURI(aParams.sourceURI),
createURI(aParams.targetFile), aParams.downloadName, null,

View File

@ -0,0 +1,124 @@
/* 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/. */
this.__defineGetter__("pb", function () {
delete this.pb;
try {
return this.pb = Cc["@mozilla.org/privatebrowsing;1"].
getService(Ci.nsIPrivateBrowsingService);
} catch (e) {}
return this.pb = null;
});
// Public request gets times=0 cookie, completes
// Private request gets times=1 cookie, canceled
// Private resumed request sends times=1 cookie, completes
function run_test() {
do_test_pending();
let httpserv = new HttpServer();
Services.prefs.setBoolPref("browser.privatebrowsing.keep_current_session", true);
let times = 0;
httpserv.registerPathHandler("/head_download_manager.js", function (meta, response) {
response.setHeader("Content-Type", "text/plain", false);
response.setStatusLine("1.1", !meta.hasHeader('range') ? 200 : 206);
// Set a cookie if none is sent with the request. Public and private requests
// should therefore receive different cookies, so we can tell if the resumed
// request is actually treated as private or not.
if (!meta.hasHeader('Cookie')) {
do_check_true(times == 0 || times == 1);
response.setHeader('Set-Cookie', 'times=' + times++);
} else {
do_check_eq(times, 2);
do_check_eq(meta.getHeader('Cookie'), 'times=1');
}
let full = "";
let body = "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"; //60
for (var i = 0; i < 1000; i++) {
full += body;
}
response.write(full);
});
httpserv.start(4444);
let state = 0;
let listener = {
onDownloadStateChange: function(aState, aDownload) {
switch (aDownload.state) {
case downloadUtils.downloadManager.DOWNLOAD_DOWNLOADING:
// We only care about the private download
if (state != 1)
break;
state++;
do_check_true(aDownload.resumable);
downloadUtils.downloadManager.pauseDownload(aDownload.id);
do_check_eq(aDownload.state, downloadUtils.downloadManager.DOWNLOAD_PAUSED);
do_execute_soon(function() {
downloadUtils.downloadManager.resumeDownload(aDownload.id);
});
break;
case downloadUtils.downloadManager.DOWNLOAD_FINISHED:
if (state == 0) {
do_execute_soon(function() {
// Perform an identical request but in private mode.
// It should receive a different cookie than the
// public request.
state++;
pb.privateBrowsingEnabled = true;
addDownload({
isPrivate: pb.privateBrowsingEnabled,
sourceURI: downloadCSource,
downloadName: downloadCName + "!!!",
runBeforeStart: function (aDownload) {
// Check that dl is retrievable
do_check_eq(downloadUtils.downloadManager.activeDownloadCount, 1);
}
});
});
} else if (state == 2) {
// We're done here.
do_execute_soon(function() {
pb.privateBrowsingEnabled = false;
Services.prefs.clearUserPref("browser.privatebrowsing.keep_current_session");
httpserv.stop(do_test_finished);
});
}
break;
default:
break;
}
},
onStateChange: function(a, b, c, d, e) { },
onProgressChange: function(a, b, c, d, e, f, g) { },
onSecurityChange: function(a, b, c, d) { }
};
downloadUtils.downloadManager.addListener(listener);
const downloadCSource = "http://localhost:4444/head_download_manager.js";
const downloadCName = "download-C";
// First a public download that completes without interruption.
let dl = addDownload({
isPrivate: pb.privateBrowsingEnabled,
sourceURI: downloadCSource,
downloadName: downloadCName,
runBeforeStart: function (aDownload) {
// Check that dl is retrievable
do_check_eq(downloadUtils.downloadManager.activeDownloadCount, 1);
}
});
}

View File

@ -21,6 +21,7 @@ skip-if = os == "android"
[test_memory_db_support.js]
[test_offline_support.js]
[test_old_download_files_removed.js]
[test_private_resume.js]
[test_privatebrowsing.js]
[test_privatebrowsing_cancel.js]
[test_removeDownloadsByTimeframe.js]