Bug 496292 - Support changing the Accept header via a preference; r=joedrew sr=bzbarsky

This commit is contained in:
Ehsan Akhgari 2009-07-15 11:52:40 +04:30
parent 9fb2c80e84
commit 7155374c25
11 changed files with 298 additions and 7 deletions

View File

@ -99,7 +99,7 @@
// objects that just require generic constructors
NS_GENERIC_FACTORY_CONSTRUCTOR(imgContainer)
NS_GENERIC_FACTORY_CONSTRUCTOR(imgLoader)
NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(imgLoader, Init)
NS_GENERIC_FACTORY_CONSTRUCTOR(imgRequestProxy)
NS_GENERIC_FACTORY_CONSTRUCTOR(imgTools)

View File

@ -22,6 +22,7 @@
*
* Contributor(s):
* Stuart Parmenter <pavlov@netscape.com>
* Ehsan Akhgari <ehsan.akhgari@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
@ -44,8 +45,7 @@
#include "nsNetUtil.h"
#include "nsIHttpChannel.h"
#include "nsICachingChannel.h"
#include "nsIObserverService.h"
#include "nsIPrefBranch.h"
#include "nsIPrefBranch2.h"
#include "nsIPrefService.h"
#include "nsIProxyObjectManager.h"
#include "nsIServiceManager.h"
@ -175,6 +175,7 @@ static nsresult NewImageChannel(nsIChannel **aResult,
nsIURI *aInitialDocumentURI,
nsIURI *aReferringURI,
nsILoadGroup *aLoadGroup,
const nsCString& aAcceptHeader,
nsLoadFlags aLoadFlags)
{
nsresult rv;
@ -213,7 +214,7 @@ static nsresult NewImageChannel(nsIChannel **aResult,
newHttpChannel = do_QueryInterface(*aResult);
if (newHttpChannel) {
newHttpChannel->SetRequestHeader(NS_LITERAL_CSTRING("Accept"),
NS_LITERAL_CSTRING("image/png,image/*;q=0.8,*/*;q=0.5"),
aAcceptHeader,
PR_FALSE);
nsCOMPtr<nsIHttpChannelInternal> httpChannelInternal = do_QueryInterface(newHttpChannel);
@ -510,7 +511,7 @@ imgCacheQueue imgLoader::sChromeCacheQueue;
PRFloat64 imgLoader::sCacheTimeWeight;
PRUint32 imgLoader::sCacheMaxSize;
NS_IMPL_ISUPPORTS4(imgLoader, imgILoader, nsIContentSniffer, imgICache, nsISupportsWeakReference)
NS_IMPL_ISUPPORTS5(imgLoader, imgILoader, nsIContentSniffer, imgICache, nsISupportsWeakReference, nsIObserver)
imgLoader::imgLoader()
{
@ -598,10 +599,49 @@ nsresult imgLoader::InitCache()
sCacheMaxSize = cachesize;
else
sCacheMaxSize = 5 * 1024 * 1024;
return NS_OK;
}
nsresult imgLoader::Init()
{
nsresult rv;
nsCOMPtr<nsIPrefBranch2> prefs = do_GetService(NS_PREFSERVICE_CONTRACTID, &rv);
if (NS_FAILED(rv))
return rv;
ReadAcceptHeaderPref(prefs);
prefs->AddObserver("image.http.accept", this, PR_TRUE);
return NS_OK;
}
NS_IMETHODIMP
imgLoader::Observe(nsISupports* aSubject, const char* aTopic, const PRUnichar* aData)
{
NS_ASSERTION(strcmp(aTopic, NS_PREFBRANCH_PREFCHANGE_TOPIC_ID) == 0,
"invalid topic received");
if (strcmp(NS_ConvertUTF16toUTF8(aData).get(), "image.http.accept") == 0) {
nsCOMPtr<nsIPrefBranch> prefs = do_QueryInterface(aSubject);
ReadAcceptHeaderPref(prefs);
}
return NS_OK;
}
void imgLoader::ReadAcceptHeaderPref(nsIPrefBranch *aBranch)
{
NS_ASSERTION(aBranch, "Pref branch is null");
nsXPIDLCString accept;
nsresult rv = aBranch->GetCharPref("image.http.accept", getter_Copies(accept));
if (NS_SUCCEEDED(rv))
mAcceptHeader = accept;
else
mAcceptHeader = "image/png,image/*;q=0.8,*/*;q=0.5";
}
/* void clearCache (in boolean chrome); */
NS_IMETHODIMP imgLoader::ClearCache(PRBool chrome)
{
@ -860,6 +900,7 @@ PRBool imgLoader::ValidateRequestWithNewChannel(imgRequest *request,
aInitialDocumentURI,
aReferrerURI,
aLoadGroup,
mAcceptHeader,
aLoadFlags);
if (NS_FAILED(rv)) {
return PR_FALSE;
@ -1263,6 +1304,7 @@ NS_IMETHODIMP imgLoader::LoadImage(nsIURI *aURI,
aInitialDocumentURI,
aReferrerURI,
aLoadGroup,
mAcceptHeader,
requestFlags);
if (NS_FAILED(rv))
return NS_ERROR_FAILURE;

View File

@ -22,6 +22,7 @@
*
* Contributor(s):
* Stuart Parmenter <pavlov@netscape.com>
* Ehsan Akhgari <ehsan.akhgari@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
@ -46,6 +47,7 @@
#include "nsAutoPtr.h"
#include "prtypes.h"
#include "imgRequest.h"
#include "nsIObserverService.h"
#ifdef LOADER_THREADSAFE
#include "prlock.h"
@ -56,6 +58,7 @@ class imgRequestProxy;
class imgIRequest;
class imgIDecoderObserver;
class nsILoadGroup;
class nsIPrefBranch;
class imgCacheEntry
{
@ -218,17 +221,21 @@ private:
class imgLoader : public imgILoader,
public nsIContentSniffer,
public imgICache,
public nsSupportsWeakReference
public nsSupportsWeakReference,
public nsIObserver
{
public:
NS_DECL_ISUPPORTS
NS_DECL_IMGILOADER
NS_DECL_NSICONTENTSNIFFER
NS_DECL_IMGICACHE
NS_DECL_NSIOBSERVER
imgLoader();
virtual ~imgLoader();
nsresult Init();
static nsresult GetMimeTypeFromContent(const char* aContents, PRUint32 aLength, nsACString& aContentType);
static void Shutdown(); // for use by the factory
@ -308,6 +315,8 @@ private: // methods
nsLoadFlags aLoadFlags, imgIRequest *aRequestProxy,
imgIRequest **_retval);
void ReadAcceptHeaderPref(nsIPrefBranch *aBranch);
typedef nsRefPtrHashtable<nsCStringHashKey, imgCacheEntry> imgCacheTable;
@ -329,6 +338,8 @@ private: // data
static imgCacheQueue sChromeCacheQueue;
static PRFloat64 sCacheTimeWeight;
static PRUint32 sCacheMaxSize;
nsCString mAcceptHeader;
};

View File

@ -55,6 +55,12 @@ _TEST_FILES = test_bug399925.html \
test_bug490949.html \
bug490949-iframe.html \
bug490949.sjs \
test_bug496292.html \
bug496292-iframe-ref.html \
bug496292-iframe-1.html \
bug496292-iframe-2.html \
bug496292-1.sjs \
bug496292-2.sjs \
$(NULL)
libs:: $(_TEST_FILES)

View File

@ -0,0 +1,33 @@
function handleRequest(request, response)
{
var file = Components.classes["@mozilla.org/file/directory_service;1"]
.getService(Components.interfaces.nsIProperties)
.get("CurWorkD", Components.interfaces.nsIFile);
file.append("tests");
file.append("modules");
file.append("libpr0n");
file.append("test");
file.append("mochitest");
if (request.getHeader("Accept") == "image/png,image/*;q=0.8,*/*;q=0.5") {
file.append('blue.png');
} else {
file.append('red.png');
}
response.setStatusLine(request.httpVersion, 200, "OK");
response.setHeader("Content-Type", "image/png", false);
response.setHeader("Cache-Control", "no-cache", false);
var fileStream = Components.classes['@mozilla.org/network/file-input-stream;1']
.createInstance(Components.interfaces.nsIFileInputStream);
fileStream.init(file, 1, 0, false);
var binaryStream = Components.classes['@mozilla.org/binaryinputstream;1']
.createInstance(Components.interfaces.nsIBinaryInputStream);
binaryStream.setInputStream(fileStream);
response.bodyOutputStream.writeFrom(binaryStream, binaryStream.available());
binaryStream.close();
fileStream.close();
}

View File

@ -0,0 +1,33 @@
function handleRequest(request, response)
{
var file = Components.classes["@mozilla.org/file/directory_service;1"]
.getService(Components.interfaces.nsIProperties)
.get("CurWorkD", Components.interfaces.nsIFile);
file.append("tests");
file.append("modules");
file.append("libpr0n");
file.append("test");
file.append("mochitest");
if (request.getHeader("Accept") == "image/png") {
file.append('blue.png');
} else {
file.append('red.png');
}
response.setStatusLine(request.httpVersion, 200, "OK");
response.setHeader("Content-Type", "image/png", false);
response.setHeader("Cache-Control", "no-cache", false);
var fileStream = Components.classes['@mozilla.org/network/file-input-stream;1']
.createInstance(Components.interfaces.nsIFileInputStream);
fileStream.init(file, 1, 0, false);
var binaryStream = Components.classes['@mozilla.org/binaryinputstream;1']
.createInstance(Components.interfaces.nsIBinaryInputStream);
binaryStream.setInputStream(fileStream);
response.bodyOutputStream.writeFrom(binaryStream, binaryStream.available());
binaryStream.close();
fileStream.close();
}

View File

@ -0,0 +1,7 @@
<html>
<head>
<title>Bug 496292 iframe 1</title>
<body>
<img src="bug496292-1.sjs" width="100" height="100" id="image" />
</body>
</html>

View File

@ -0,0 +1,7 @@
<html>
<head>
<title>Bug 496292 iframe 2</title>
<body>
<img src="bug496292-2.sjs" width="100" height="100" id="image" />
</body>
</html>

View File

@ -0,0 +1,7 @@
<html>
<head>
<title>Bug 496292 reference iframe</title>
<body>
<img src="blue.png" width="100" height="100" id="image" />
</body>
</html>

View File

@ -0,0 +1,142 @@
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=496292
-->
<head>
<title>Test for Bug 496292</title>
<script type="application/javascript" src="/MochiKit/MochiKit.js"></script>
<script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=496292">Mozilla Bug 496292</a>
<p id="display"></p>
<div id="content" style="display: none">
<canvas id="canvas" width="100" height="100"> </canvas>
</div>
<pre id="test">
<script type="application/javascript">
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
const Ci = Components.interfaces;
const Cc = Components.classes;
SimpleTest.waitForExplicitFinish();
var canvas = document.getElementById('canvas');
var first, second, third, ref;
RemoteCanvas = function(url) {
this.url = url;
};
RemoteCanvas.CANVAS_WIDTH = 100;
RemoteCanvas.CANVAS_HEIGHT = 100;
RemoteCanvas.prototype.load = function(cb) {
this.cb = cb;
var windowWidth = window.innerWidth - 25;
var iframe;
iframe = document.createElement("iframe");
iframe.id = "test-iframe-" + this.url;
iframe.height = "10px";
iframe.width = windowWidth + "px";
iframe.style.visibility = "hidden";
iframe.src = this.url;
// Here is where the magic happens... add a listener to the
// frame's onload event - it will call handleEvent
iframe.addEventListener("load", this, true);
//append to the end of the page
window.document.body.appendChild(iframe);
};
RemoteCanvas.prototype.reload = function(cb, force) {
this.cb = cb;
window.frames[0].location.reload(force);
}
RemoteCanvas.prototype.handleEvent = function() {
netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
// Look back up the iframe by id
var ldrFrame = document.getElementById("test-iframe-" + this.url);
// Get a reference to the window object you need for the canvas
// drawWindow method
var remoteWindow = ldrFrame.contentWindow;
//Draw canvas
canvas.style.width = RemoteCanvas.CANVAS_WIDTH + "px";
canvas.style.height = RemoteCanvas.CANVAS_HEIGHT + "px";
canvas.width = RemoteCanvas.CANVAS_WIDTH;
canvas.height = RemoteCanvas.CANVAS_HEIGHT;
var windowWidth = window.innerWidth - 25;
var windowHeight = window.innerHeight;
var ctx = canvas.getContext("2d");
ctx.clearRect(0, 0,
RemoteCanvas.CANVAS_WIDTH,
RemoteCanvas.CANVAS_HEIGHT);
ctx.save();
ctx.scale(RemoteCanvas.CANVAS_WIDTH / windowWidth,
RemoteCanvas.CANVAS_HEIGHT / windowHeight);
ctx.drawWindow(remoteWindow,
0, 0,
windowWidth, windowHeight,
"rgb(0,0,0)");
ctx.restore();
this.cb();
};
function loadFirst()
{
ref = canvas.toDataURL();
var remoteCanvas = new RemoteCanvas("bug496292-iframe-1.html");
remoteCanvas.load(checkFirst);
}
function checkFirst()
{
first = canvas.toDataURL();
is(first, ref, "The default Accept header used by image loader is correct");
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
Cc["@mozilla.org/preferences-service;1"].
getService(Ci.nsIPrefBranch).
setCharPref("image.http.accept", "image/png");
var remoteCanvas = new RemoteCanvas("bug496292-iframe-2.html");
remoteCanvas.load(checkSecond);
}
function checkSecond()
{
second = canvas.toDataURL();
is(second, ref, "The modified Accept header used by image loader is correct");
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
Cc["@mozilla.org/preferences-service;1"].
getService(Ci.nsIPrefBranch).
clearUserPref("image.http.accept");
var remoteCanvas = new RemoteCanvas("bug496292-iframe-1.html");
remoteCanvas.load(checkThird);
}
function checkThird() {
third = canvas.toDataURL();
is(third, ref, "The Accept header used by image loader should be correctly reset");
SimpleTest.finish();
}
var refCanvas = new RemoteCanvas("bug496292-iframe-ref.html");
refCanvas.load(loadFirst);
</script>
</pre>
</body>
</html>

View File

@ -2737,6 +2737,9 @@ pref("image.cache.size", 5242880);
// Size is given a weight of 1000 - timeweight.
pref("image.cache.timeweight", 500);
// The default Accept header sent for images loaded over HTTP(S)
pref("image.http.accept", "image/png,image/*;q=0.8,*/*;q=0.5");
#ifdef XP_WIN
#ifndef WINCE
// The default TCP send window on Windows is too small, and autotuning only occurs on receive