Bug 564387 - Let "Save Video As..." respect the filename set in the Content-Disposition header. r=gavin

--HG--
rename : embedding/test/320x240.ogv => browser/base/content/test/bug564387_video1.ogv
This commit is contained in:
Kailas 2011-07-10 13:53:22 -07:00
parent 0e326605e1
commit 846f5869f6
8 changed files with 156 additions and 18 deletions

View File

@ -45,6 +45,7 @@
# Justin Dolske <dolske@mozilla.com>
# Kathleen Brade <brade@pearlcrescent.com>
# Mark Smith <mcs@pearlcrescent.com>
# Kailas Patil <patilkr24@gmail.com>
#
# Alternatively, the contents of this file may be used under the terms of
# either the GNU General Public License Version 2 or later (the "GPL"), or
@ -879,17 +880,12 @@ nsContextMenu.prototype = {
saveDocument(this.target.ownerDocument);
},
// Save URL of clicked-on link.
saveLink: function() {
// Helper function to wait for appropriate MIME-type headers and
// then prompt the user with a file picker
saveHelper: function(linkURL, linkText, dialogTitle, bypassCache, doc) {
// canonical def in nsURILoader.h
const NS_ERROR_SAVE_LINK_AS_TIMEOUT = 0x805d0020;
var doc = this.target.ownerDocument;
urlSecurityCheck(this.linkURL, doc.nodePrincipal);
var linkText = this.linkText();
var linkURL = this.linkURL;
// an object to proxy the data through to
// nsIExternalHelperAppService.doContent, which will wait for the
// appropriate MIME-type headers and then prompt the user with a
@ -941,7 +937,7 @@ nsContextMenu.prototype = {
if (aStatusCode == NS_ERROR_SAVE_LINK_AS_TIMEOUT) {
// do it the old fashioned way, which will pick the best filename
// it can without waiting.
saveURL(linkURL, linkText, null, true, false, doc.documentURIObject);
saveURL(linkURL, linkText, dialogTitle, bypassCache, false, doc.documentURIObject);
}
if (this.extListener)
this.extListener.onStopRequest(aRequest, aContext, aStatusCode);
@ -985,10 +981,19 @@ nsContextMenu.prototype = {
// set up a channel to do the saving
var ioService = Cc["@mozilla.org/network/io-service;1"].
getService(Ci.nsIIOService);
var channel = ioService.newChannelFromURI(this.getLinkURI());
var channel = ioService.newChannelFromURI(makeURI(linkURL));
channel.notificationCallbacks = new callbacks();
channel.loadFlags |= Ci.nsIRequest.LOAD_BYPASS_CACHE |
Ci.nsIChannel.LOAD_CALL_CONTENT_SNIFFERS;
let flags = Ci.nsIChannel.LOAD_CALL_CONTENT_SNIFFERS;
if (bypassCache)
flags |= Ci.nsIRequest.LOAD_BYPASS_CACHE;
if (channel instanceof Ci.nsICachingChannel)
flags |= Ci.nsICachingChannel.LOAD_BYPASS_LOCAL_CACHE_IF_BUSY;
channel.loadFlags |= flags;
if (channel instanceof Ci.nsIHttpChannel) {
channel.referrer = doc.documentURIObject;
if (channel instanceof Ci.nsIHttpChannelInternal)
@ -1006,6 +1011,14 @@ nsContextMenu.prototype = {
channel.asyncOpen(new saveAsListener(), null);
},
// Save URL of clicked-on link.
saveLink: function() {
var doc = this.target.ownerDocument;
urlSecurityCheck(this.linkURL, doc.nodePrincipal);
this.saveHelper(this.linkURL, this.linkText(), null, true, doc);
},
sendLink: function() {
// we don't know the title of the link so pass in an empty string
MailIntegration.sendMessage( this.linkURL, "" );
@ -1033,8 +1046,7 @@ nsContextMenu.prototype = {
else if (this.onVideo || this.onAudio) {
urlSecurityCheck(this.mediaURL, doc.nodePrincipal);
var dialogTitle = this.onVideo ? "SaveVideoTitle" : "SaveAudioTitle";
saveURL(this.mediaURL, null, dialogTitle, false,
false, doc.documentURIObject);
this.saveHelper(this.mediaURL, null, dialogTitle, false, doc);
}
},

View File

@ -212,6 +212,10 @@ _BROWSER_FILES = \
browser_clearplugindata_noage.html \
browser_popupUI.js \
browser_sanitizeDialog.js \
browser_save_video.js \
bug564387.html \
bug564387_video1.ogv \
bug564387_video1.ogv^headers^ \
browser_scope.js \
browser_selectTabAtIndex.js \
browser_tab_dragdrop.js \

View File

@ -0,0 +1,110 @@
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
/**
* TestCase for bug 564387
* <https://bugzilla.mozilla.org/show_bug.cgi?id=564387>
*/
function test() {
// --- Testing support library ---
// Import the toolkit test support library in the scope of the current test.
// This operation also defines the common constants Cc, Ci, Cu, Cr and Cm.
Components.classes["@mozilla.org/moz/jssubscript-loader;1"].
getService(Components.interfaces.mozIJSSubScriptLoader).loadSubScript(
"chrome://mochitests/content/browser/toolkit/content/tests/browser/common/_loadAll.js",
this);
// --- Test implementation ---
const kBaseUrl =
"http://mochi.test:8888/browser/browser/base/content/test/";
function pageShown(event) {
if (event.target.location != "about:blank")
testRunner.continueTest();
}
function saveVideoAs_TestGenerator() {
// Load Test page
gBrowser.addEventListener("pageshow", pageShown, false);
gBrowser.loadURI(kBaseUrl + "bug564387.html");
yield;
gBrowser.removeEventListener("pageshow", pageShown, false);
// Ensure that the window is focused.
SimpleTest.waitForFocus(testRunner.continueTest);
yield;
try {
// get the video element
var video1 = gBrowser.contentDocument.getElementById("video1");
// Synthesize the right click on the context menu, and
// wait for it to be shown
document.addEventListener("popupshown", testRunner.continueTest, false);
EventUtils.synthesizeMouseAtCenter(video1,
{ type: "contextmenu", button: 2 },
gBrowser.contentWindow);
yield;
// Create the folder the video will be saved into.
var destDir = createTemporarySaveDirectory();
try {
// Call the appropriate save function defined in contentAreaUtils.js.
mockFilePickerSettings.destDir = destDir;
mockFilePickerSettings.filterIndex = 1; // kSaveAsType_URL
// register mock file picker object
mockFilePickerRegisterer.register();
try {
// register mock download progress listener
mockTransferForContinuingRegisterer.register();
try {
// Select "Save Video As" option from context menu
var saveVideoCommand = document.getElementById("context-savevideo");
saveVideoCommand.doCommand();
// Unregister the popupshown listener
document.removeEventListener("popupshown",
testRunner.continueTest, false);
// Close the context menu
document.getElementById("placesContext").hidePopup();
// Wait for the download to finish, and exit if it wasn't successful.
var downloadSuccess = yield;
if (!downloadSuccess)
throw "Unexpected failure in downloading Video file!";
}
finally {
// unregister download progress listener
mockTransferForContinuingRegisterer.unregister();
}
}
finally {
// unregister mock file picker object
mockFilePickerRegisterer.unregister();
}
// Read the name of the saved file.
var fileName = mockFilePickerResults.selectedFile.leafName;
is(fileName, "Bug564387-expectedName.ogv",
"Video File Name is correctly retrieved from Content-Disposition http header");
}
finally {
// Clean up the saved file.
destDir.remove(true);
}
}
finally {
// Replace the current tab with a clean one.
gBrowser.addTab().linkedBrowser.stop();
gBrowser.removeCurrentTab();
}
}
// --- Run the test ---
testRunner.runTest(saveVideoAs_TestGenerator);
}

View File

@ -0,0 +1,11 @@
<html>
<!-- https://bugzilla.mozilla.org/show_bug.cgi?id=564387 -->
<head>
<title> Bug 564387 test</title>
</head>
<body>
Testing for Mozilla Bug: 564387
<br>
<video src="bug564387_video1.ogv" id="video1"> </video>
</body>
</html>

Binary file not shown.

View File

@ -0,0 +1,3 @@
Content-Disposition: filename="Bug564387-expectedName.ogv"
Content-Type: video/ogg

View File

@ -49,10 +49,9 @@ function test() {
// Import the toolkit test support library in the scope of the current test.
// This operation also defines the common constants Cc, Ci, Cu, Cr and Cm.
var rootDir = getRootDirectory(gTestPath);
Components.classes["@mozilla.org/moz/jssubscript-loader;1"].
getService(Components.interfaces.mozIJSSubScriptLoader).loadSubScript(
rootDir + "common/_loadAll.js",
"chrome://mochitests/content/browser/toolkit/content/tests/browser/common/_loadAll.js",
this);
// --- Test implementation ---

View File

@ -47,8 +47,7 @@ const Cm = Components.manager;
// Execute the following code while keeping the current scope clean.
void(function (scriptScope) {
var rootDir = getRootDirectory(gTestPath);
const kBaseUrl = rootDir + "common/";
const kBaseUrl = "chrome://mochitests/content/browser/toolkit/content/tests/browser/common/";
// If you add files here, add them to "Makefile.in" too.
var scriptNames = [