Bug 836483 - Track download referrers. r=paolo

This commit is contained in:
Raymond Lee 2013-05-27 19:14:18 +08:00
parent f34ec3b8c6
commit 90fd51f4ba
4 changed files with 68 additions and 1 deletions

View File

@ -434,6 +434,12 @@ DownloadSource.prototype = {
* resource. * resource.
*/ */
isPrivate: false, isPrivate: false,
/**
* The nsIURI for the referrer of the download source, or null if no referrer
* should be sent or the download source is not HTTP.
*/
referrer: null,
}; };
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
@ -607,6 +613,9 @@ DownloadCopySaver.prototype = {
if (channel instanceof Ci.nsIPrivateBrowsingChannel) { if (channel instanceof Ci.nsIPrivateBrowsingChannel) {
channel.setPrivate(download.source.isPrivate); channel.setPrivate(download.source.isPrivate);
} }
if (channel instanceof Ci.nsIHttpChannel) {
channel.referrer = download.source.referrer;
}
channel.notificationCallbacks = { channel.notificationCallbacks = {
QueryInterface: XPCOMUtils.generateQI([Ci.nsIInterfaceRequestor]), QueryInterface: XPCOMUtils.generateQI([Ci.nsIInterfaceRequestor]),

View File

@ -79,7 +79,12 @@ this.Downloads = {
download.source = new DownloadSource(); download.source = new DownloadSource();
download.source.uri = aProperties.source.uri; download.source.uri = aProperties.source.uri;
download.source.isPrivate = aProperties.source.isPrivate; if ("isPrivate" in aProperties.source) {
download.source.isPrivate = aProperties.source.isPrivate;
}
if ("referrer" in aProperties.source) {
download.source.referrer = aProperties.source.referrer;
}
download.target = new DownloadTarget(); download.target = new DownloadTarget();
download.target.file = aProperties.target.file; download.target.file = aProperties.target.file;

View File

@ -47,6 +47,7 @@ const HTTP_BASE = "http://localhost:" + HTTP_SERVER_PORT;
const FAKE_SERVER_PORT = 4445; const FAKE_SERVER_PORT = 4445;
const FAKE_BASE = "http://localhost:" + FAKE_SERVER_PORT; const FAKE_BASE = "http://localhost:" + FAKE_SERVER_PORT;
const TEST_REFERRER_URI = NetUtil.newURI(HTTP_BASE + "/referrer.html");
const TEST_SOURCE_URI = NetUtil.newURI(HTTP_BASE + "/source.txt"); const TEST_SOURCE_URI = NetUtil.newURI(HTTP_BASE + "/source.txt");
const TEST_EMPTY_URI = NetUtil.newURI(HTTP_BASE + "/empty.txt"); const TEST_EMPTY_URI = NetUtil.newURI(HTTP_BASE + "/empty.txt");
const TEST_FAKE_SOURCE_URI = NetUtil.newURI(FAKE_BASE + "/source.txt"); const TEST_FAKE_SOURCE_URI = NetUtil.newURI(FAKE_BASE + "/source.txt");

View File

@ -28,6 +28,7 @@ add_task(function test_download_construction()
// Checks the generated DownloadSource and DownloadTarget properties. // Checks the generated DownloadSource and DownloadTarget properties.
do_check_true(download.source.uri.equals(TEST_SOURCE_URI)); do_check_true(download.source.uri.equals(TEST_SOURCE_URI));
do_check_eq(download.target.file, targetFile); do_check_eq(download.target.file, targetFile);
do_check_true(download.source.referrer === null);
// Starts the download and waits for completion. // Starts the download and waits for completion.
yield download.start(); yield download.start();
@ -35,6 +36,56 @@ add_task(function test_download_construction()
yield promiseVerifyContents(targetFile, TEST_DATA_SHORT); yield promiseVerifyContents(targetFile, TEST_DATA_SHORT);
}); });
/**
* Checks the referrer for downloads.
*/
add_task(function test_download_referrer()
{
let source_path = "/test_download_referrer.txt";
let source_uri = NetUtil.newURI(HTTP_BASE + source_path);
let target_uri = getTempFile(TEST_TARGET_FILE_NAME);
function cleanup() {
gHttpServer.registerPathHandler(source_path, null);
}
do_register_cleanup(cleanup);
gHttpServer.registerPathHandler(source_path, function (aRequest, aResponse) {
aResponse.setHeader("Content-Type", "text/plain", false);
do_check_true(aRequest.hasHeader("Referer"));
do_check_eq(aRequest.getHeader("Referer"), TEST_REFERRER_URI.spec);
});
let download = yield Downloads.createDownload({
source: { uri: source_uri, referrer: TEST_REFERRER_URI },
target: { file: target_uri },
saver: { type: "copy" },
});
do_check_true(download.source.referrer.equals(TEST_REFERRER_URI));
yield download.start();
download = yield Downloads.createDownload({
source: { uri: source_uri, referrer: TEST_REFERRER_URI, isPrivate: true },
target: { file: target_uri },
saver: { type: "copy" },
});
do_check_true(download.source.referrer.equals(TEST_REFERRER_URI));
yield download.start();
// Test the download still works for non-HTTP channel with referrer.
source_uri = NetUtil.newURI("data:text/html,<html><body></body></html>");
download = yield Downloads.createDownload({
source: { uri: source_uri, referrer: TEST_REFERRER_URI },
target: { file: target_uri },
saver: { type: "copy" },
});
do_check_true(download.source.referrer.equals(TEST_REFERRER_URI));
yield download.start();
cleanup();
});
/** /**
* Checks initial and final state and progress for a successful download. * Checks initial and final state and progress for a successful download.
*/ */
@ -750,3 +801,4 @@ add_task(function test_download_cancel_immediately_restart_and_check_startTime()
yield download.start(); yield download.start();
do_check_true(download.startTime.getTime() > startTime.getTime()); do_check_true(download.startTime.getTime() > startTime.getTime());
}); });