Skip to content

Commit

Permalink
Bug 1406253 - Part 3: use currentRequstFinalURI in context menu and a…
Browse files Browse the repository at this point in the history
…dd a test case. r=dao

If the image request gets redirect on loading, HTMLImageElement.currentURI
(which corresponds to nsIImageLoadingContent.currentURI) would return the
original URI before redirect, making "Save Image" in the context menu use
incorrect URI and filename. Use currentRequestFinalURI instead to get
redirected URI.

MozReview-Commit-ID: Bd7Q36sH93b

--HG--
extra : rebase_source : b88ccf98bc2a41aac007d79060424eaa2c2aca88
  • Loading branch information
freesamael committed Nov 14, 2017
1 parent 9a66d99 commit ef3c08c
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 11 deletions.
8 changes: 4 additions & 4 deletions browser/modules/ContextMenu.jsm
Original file line number Diff line number Diff line change
Expand Up @@ -541,13 +541,13 @@ class ContextMenu {
let contentDisposition = null;
if (aEvent.target.nodeType == Ci.nsIDOMNode.ELEMENT_NODE &&
aEvent.target instanceof Ci.nsIImageLoadingContent &&
aEvent.target.currentURI) {
aEvent.target.currentRequestFinalURI) {
disableSetDesktopBg = this._disableSetDesktopBackground(aEvent.target);

try {
let imageCache = Cc["@mozilla.org/image/tools;1"].getService(Ci.imgITools)
.getImgCacheForDocument(doc);
let props = imageCache.findEntryProperties(aEvent.target.currentURI, doc);
let props = imageCache.findEntryProperties(aEvent.target.currentRequestFinalURI, doc);

try {
contentType = props.get("type", Ci.nsISupportsCString).data;
Expand Down Expand Up @@ -810,7 +810,7 @@ class ContextMenu {
// nsDocumentViewer::GetInImage. Make sure to update both if this is
// changed.
if (context.target instanceof Ci.nsIImageLoadingContent &&
context.target.currentURI) {
context.target.currentRequestFinalURI) {
context.onImage = true;

context.imageInfo = {
Expand All @@ -832,7 +832,7 @@ class ContextMenu {
context.onCompletedImage = true;
}

context.mediaURL = context.target.currentURI.spec;
context.mediaURL = context.target.currentRequestFinalURI.spec;

const descURL = context.target.getAttribute("longdesc");

Expand Down
4 changes: 2 additions & 2 deletions dom/browser-element/BrowserElementChildPreload.js
Original file line number Diff line number Diff line change
Expand Up @@ -862,8 +862,8 @@ BrowserElementChild.prototype = {
documentURI: documentURI,
text: elem.textContent.substring(0, kLongestReturnedString)};
}
if (elem instanceof Ci.nsIImageLoadingContent && elem.currentURI) {
return {uri: elem.currentURI.spec, documentURI: documentURI};
if (elem instanceof Ci.nsIImageLoadingContent && elem.currentRequestFinalURI) {
return {uri: elem.currentRequestFinalURI.spec, documentURI: documentURI};
}
if (ChromeUtils.getClassName(elem) === "HTMLImageElement") {
return {uri: elem.src, documentURI: documentURI};
Expand Down
6 changes: 1 addition & 5 deletions mobile/android/chrome/content/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -848,10 +848,6 @@ var BrowserApp = {
selector: NativeWindow.contextmenus._disableRestricted("SHARE", NativeWindow.contextmenus.imageShareableContext),
order: NativeWindow.contextmenus.DEFAULT_HTML5_ORDER - 1, // Show above HTML5 menu items
showAsActions: function(aTarget) {
let doc = aTarget.ownerDocument;
let imageCache = Cc["@mozilla.org/image/tools;1"].getService(Ci.imgITools)
.getImgCacheForDocument(doc);
let props = imageCache.findEntryProperties(aTarget.currentURI, doc);
let src = aTarget.src;
return {
title: src,
Expand Down Expand Up @@ -881,7 +877,7 @@ var BrowserApp = {
return;
}

ContentAreaUtils.saveImageURL(aTarget.currentURI.spec, null, "SaveImageTitle",
ContentAreaUtils.saveImageURL(aTarget.currentRequestFinalURI.spec, null, "SaveImageTitle",
false, true, aTarget.ownerDocument.documentURIObject,
aTarget.ownerDocument);
});
Expand Down
3 changes: 3 additions & 0 deletions toolkit/content/tests/browser/browser.ini
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ support-files =
image.jpg
image_page.html
silentAudioTrack.webm
doggy.png
firebird.png

[browser_audioCompeting.js]
tags = audiochannel
Expand Down Expand Up @@ -62,6 +64,7 @@ skip-if = !e10s || !crashreporter
run-if = e10s && crashreporter
[browser_datetime_datepicker.js]
[browser_default_image_filename.js]
[browser_default_image_filename_redirect.js]
[browser_f7_caret_browsing.js]
[browser_findbar.js]
[browser_isSynthetic.js]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* TestCase for bug 1406253
* <https://bugzilla.mozilla.org/show_bug.cgi?id=1406253>
*
* Load firebird.png, redirect it to doggy.png, and verify the filename is
* doggy.png in file picker dialog.
*/

let {WebRequest} = Cu.import("resource://gre/modules/WebRequest.jsm", {});
let MockFilePicker = SpecialPowers.MockFilePicker;
MockFilePicker.init(window);
add_task(async function() {
const URL_FIREBIRD = "http://mochi.test:8888/browser/toolkit/content/tests/browser/firebird.png";
const URL_DOGGY = "http://mochi.test:8888/browser/toolkit/content/tests/browser/doggy.png";
function redirect(requestDetails) {
info("Redirecting: " + requestDetails.url);
return {
redirectUrl: URL_DOGGY
};
}
WebRequest.onBeforeRequest.addListener(redirect,
{urls: new MatchPatternSet(["http://*/*firebird.png"])},
["blocking"]
);

await BrowserTestUtils.withNewTab(URL_FIREBIRD,
async function(browser) {

// Click image to show context menu.
let popupShownPromise = BrowserTestUtils.waitForEvent(document, "popupshown");
await BrowserTestUtils.synthesizeMouseAtCenter("img",
{ type: "contextmenu", button: 2 },
browser);
await popupShownPromise;

// Prepare mock file picker.
let showFilePickerPromise = new Promise(resolve => {
MockFilePicker.showCallback = fp => resolve(fp.defaultString);
});
registerCleanupFunction(function() {
MockFilePicker.cleanup();
});

// Select "Save Image As" option from context menu
var saveImageAsCommand = document.getElementById("context-saveimage");
saveImageAsCommand.doCommand();

let filename = await showFilePickerPromise;
is(filename, "doggy.png", "Verify image filename.");

// Close context menu.
let contextMenu = document.getElementById("contentAreaContextMenu");
let popupHiddenPromise = BrowserTestUtils.waitForEvent(contextMenu, "popuphidden");
contextMenu.hidePopup();
await popupHiddenPromise;
});

WebRequest.onBeforeRequest.removeListener(redirect);
});
Binary file added toolkit/content/tests/browser/doggy.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added toolkit/content/tests/browser/firebird.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit ef3c08c

Please sign in to comment.