Bug 1406253 - Part 3: use currentRequstFinalURI in context menu and add 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
This commit is contained in:
Samael Wang 2017-11-14 19:19:27 +08:00
parent 9a66d9924f
commit ef3c08ceb1
7 changed files with 69 additions and 11 deletions

View File

@ -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;
@ -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 = {
@ -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");

View File

@ -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};

View File

@ -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,
@ -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);
});

View File

@ -23,6 +23,8 @@ support-files =
image.jpg
image_page.html
silentAudioTrack.webm
doggy.png
firebird.png
[browser_audioCompeting.js]
tags = audiochannel
@ -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]

View File

@ -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 not shown.

After

Width:  |  Height:  |  Size: 46 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB