Skip to content

Commit

Permalink
Bug 1576347 - Correct referrerInfo when saving media and add a test o…
Browse files Browse the repository at this point in the history
…f "Go To Download Page" button r=Gijs

Differential Revision: https://phabricator.services.mozilla.com/D43749

--HG--
extra : moz-landing-system : lando
  • Loading branch information
Thomas Nguyen committed Aug 30, 2019
1 parent 9e00f53 commit c175f3d
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 25 deletions.
27 changes: 13 additions & 14 deletions browser/actors/ContextMenuChild.jsm
Original file line number Diff line number Diff line change
Expand Up @@ -623,11 +623,19 @@ class ContextMenuChild extends JSWindowActorChild {
let referrerInfo = Cc["@mozilla.org/referrer-info;1"].createInstance(
Ci.nsIReferrerInfo
);
referrerInfo.initWithNode(
context.onLink ? context.link : aEvent.composedTarget
);
referrerInfo.initWithNode(aEvent.composedTarget);
referrerInfo = E10SUtils.serializeReferrerInfo(referrerInfo);

// In the case "onLink" we may have to send link referrerInfo to use in
// _openLinkInParameters
let linkReferrerInfo = null;
if (context.onLink) {
linkReferrerInfo = Cc["@mozilla.org/referrer-info;1"].createInstance(
Ci.nsIReferrerInfo
);
linkReferrerInfo.initWithNode(context.link);
}

let target = context.target;
if (target) {
this._cleanContext();
Expand Down Expand Up @@ -681,17 +689,8 @@ class ContextMenuChild extends JSWindowActorChild {
);
}

// In the case "onLink" we may have to send target referrerInfo. This object
// may be used to in saveMedia function.
if (context.onLink) {
let targetReferrerInfo = Cc[
"@mozilla.org/referrer-info;1"
].createInstance(Ci.nsIReferrerInfo);

targetReferrerInfo.initWithNode(aEvent.composedTarget);
data.targetReferrerInfo = E10SUtils.serializeReferrerInfo(
targetReferrerInfo
);
if (linkReferrerInfo) {
data.linkReferrerInfo = E10SUtils.serializeReferrerInfo(linkReferrerInfo);
}

if (Services.appinfo.processType == Services.appinfo.PROCESS_TYPE_CONTENT) {
Expand Down
24 changes: 15 additions & 9 deletions browser/base/content/nsContextMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ function openContextMenu(aMessage, aBrowser, aActor) {
let actor = aActor;
let spellInfo = data.spellInfo;
let frameReferrerInfo = data.frameReferrerInfo;
let targetReferrerInfo = data.targetReferrerInfo;
let linkReferrerInfo = data.linkReferrerInfo;
let principal = data.principal;
let storagePrincipal = data.storagePrincipal;

Expand All @@ -61,8 +61,8 @@ function openContextMenu(aMessage, aBrowser, aActor) {
frameReferrerInfo = E10SUtils.deserializeReferrerInfo(frameReferrerInfo);
}

if (targetReferrerInfo) {
targetReferrerInfo = E10SUtils.deserializeReferrerInfo(targetReferrerInfo);
if (linkReferrerInfo) {
linkReferrerInfo = E10SUtils.deserializeReferrerInfo(linkReferrerInfo);
}

// For now, JS Window Actors don't deserialize Principals automatically, so we
Expand Down Expand Up @@ -100,7 +100,7 @@ function openContextMenu(aMessage, aBrowser, aActor) {
charSet: data.charSet,
referrerInfo: E10SUtils.deserializeReferrerInfo(data.referrerInfo),
frameReferrerInfo,
targetReferrerInfo,
linkReferrerInfo,
contentType: data.contentType,
contentDisposition: data.contentDisposition,
frameOuterWindowID: data.frameOuterWindowID,
Expand Down Expand Up @@ -1095,7 +1095,9 @@ nsContextMenu.prototype = {
params[p] = extra[p];
}

let referrerInfo = gContextMenuContentData.referrerInfo;
let referrerInfo = this.onLink
? gContextMenuContentData.linkReferrerInfo
: gContextMenuContentData.referrerInfo;
// If we want to change userContextId, we must be sure that we don't
// propagate the referrer.
if (
Expand Down Expand Up @@ -1342,7 +1344,7 @@ nsContextMenu.prototype = {
}

// Cache this because we fetch the data async
let { targetReferrerInfo } = gContextMenuContentData;
let referrerInfo = gContextMenuContentData.referrerInfo;

this.actor.saveVideoFrameAsImage(this.targetIdentifier).then(dataURL => {
// FIXME can we switch this to a blob URL?
Expand All @@ -1352,7 +1354,7 @@ nsContextMenu.prototype = {
"SaveImageTitle",
true, // bypass cache
false, // don't skip prompt for where to save
targetReferrerInfo, // referrer info
referrerInfo, // referrer info
null, // document
null, // content type
null, // content disposition
Expand Down Expand Up @@ -1620,14 +1622,18 @@ nsContextMenu.prototype = {

// Save URL of clicked-on link.
saveLink() {
let referrerInfo = this.onLink
? gContextMenuContentData.linkReferrerInfo
: gContextMenuContentData.referrerInfo;

let isContentWindowPrivate = this.ownerDoc.isPrivate;
this.saveHelper(
this.linkURL,
this.linkTextStr,
null,
true,
this.ownerDoc,
gContextMenuContentData.referrerInfo,
referrerInfo,
this.frameOuterWindowID,
this.linkDownload,
isContentWindowPrivate
Expand All @@ -1645,7 +1651,7 @@ nsContextMenu.prototype = {
saveMedia() {
let doc = this.ownerDoc;
let isContentWindowPrivate = this.ownerDoc.isPrivate;
let referrerInfo = gContextMenuContentData.targetReferrerInfo;
let referrerInfo = gContextMenuContentData.referrerInfo;
let isPrivate = PrivateBrowsingUtils.isBrowserPrivate(this.browser);
if (this.onCanvas) {
// Bypass cache, since it's a data: URL.
Expand Down
8 changes: 7 additions & 1 deletion browser/components/downloads/DownloadsViewUI.jsm
Original file line number Diff line number Diff line change
Expand Up @@ -327,8 +327,14 @@ this.DownloadsViewUI.DownloadElementShell.prototype = {
* Downloads View.
*/
showStatusWithDetails(stateLabel, hoverStatus) {
let referrer =
this.download.source.referrerInfo &&
this.download.source.referrerInfo.originalReferrer
? this.download.source.referrerInfo.originalReferrer.spec
: null;

let [displayHost] = DownloadUtils.getURIHost(
this.download.source.referrer || this.download.source.url
referrer || this.download.source.url
);
let [displayDate] = DownloadUtils.getReadableDates(
new Date(this.download.endTime)
Expand Down
1 change: 1 addition & 0 deletions browser/components/downloads/test/browser/browser.ini
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ skip-if = (os == 'win' && os_version == '10.0' && ccov) # Bug 1306510
skip-if = true # Bug 1352792
[browser_downloads_panel_height.js]
[browser_downloads_autohide.js]
[browser_go_to_download_page.js]
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */

const ReferrerInfo = Components.Constructor(
"@mozilla.org/referrer-info;1",
"nsIReferrerInfo",
"init"
);

const TEST_REFERRER = "https://example.com/";

registerCleanupFunction(async function() {
await task_resetState();
await PlacesUtils.history.clear();
});

async function addDownload(referrerInfo) {
let startTimeMs = Date.now();

let publicList = await Downloads.getList(Downloads.PUBLIC);
let downloadData = {
source: {
url: "http://www.example.com/test-download.txt",
referrerInfo,
},
target: {
path: gTestTargetFile.path,
},
startTime: new Date(startTimeMs++),
};
let download = await Downloads.createDownload(downloadData);
await publicList.add(download);
await download.start();
}

/**
* Make sure "Go To Download Page" is enabled and works as expected.
*/
add_task(async function test_go_to_download_page() {
let referrerInfo = new ReferrerInfo(
Ci.nsIReferrerInfo.NO_REFERRER,
true,
NetUtil.newURI(TEST_REFERRER)
);

let tabPromise = BrowserTestUtils.waitForNewTab(gBrowser, TEST_REFERRER);

// Wait for focus first
await promiseFocus();

// Ensure that state is reset in case previous tests didn't finish.
await task_resetState();

// Populate the downloads database with the data required by this test.
await addDownload(referrerInfo);

// Open the user interface and wait for data to be fully loaded.
await task_openPanel();

let win = await openLibrary("Downloads");
registerCleanupFunction(function() {
win.close();
});

let listbox = win.document.getElementById("downloadsRichListBox");
ok(listbox, "download list box present");

// Select one of the downloads.
listbox.itemChildren[0].click();

let contextMenu = win.document.getElementById("downloadsContextMenu");

let popupShownPromise = BrowserTestUtils.waitForEvent(
contextMenu,
"popupshown"
);
EventUtils.synthesizeMouseAtCenter(
listbox.itemChildren[0],
{ type: "contextmenu", button: 2 },
win
);
await popupShownPromise;

// Find and click "Go To Download Page"
let goToDownloadButton = [...contextMenu.children].find(
child => child.command == "downloadsCmd_openReferrer"
);
goToDownloadButton.click();

let newTab = await tabPromise;
ok(newTab, "Go To Download Page opened a new tab");
gBrowser.removeTab(newTab);
});
2 changes: 1 addition & 1 deletion toolkit/content/contentAreaUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -1396,7 +1396,7 @@ function getCharsetforSave(aDocument) {
* WARNING: Please note that openURL() does not perform any content security checks!!!
*/
function openURL(aURL) {
var uri = makeURI(aURL);
var uri = aURL instanceof Ci.nsIURI ? aURL : makeURI(aURL);

var protocolSvc = Cc[
"@mozilla.org/uriloader/external-protocol-service;1"
Expand Down

0 comments on commit c175f3d

Please sign in to comment.