Bug 1280006 - Backout "Bug 1270680 - Part 1: Double-key the image cache by origin attribute." r=tanvi

This reverts commit bb0482fe09fbfad9be89384ec5cb8b6518187379.
This commit is contained in:
Jonathan Hao 2016-06-14 12:43:21 +01:00
parent b5797b008c
commit a1a0a9bd7d
8 changed files with 22 additions and 101 deletions

View File

@ -270,8 +270,7 @@ nsContextMenuInfo::GetBackgroundImageRequestInternal(nsIDOMNode* aDOMNode,
nsAutoString bgStringValue;
nsCOMPtr<nsIDocument> doc(do_QueryInterface(document));
NS_ENSURE_TRUE(doc, NS_ERROR_FAILURE);
nsCOMPtr<nsIPrincipal> principal = doc->NodePrincipal();
nsCOMPtr<nsIPrincipal> principal = doc ? doc->NodePrincipal() : nullptr;
while (true) {
nsCOMPtr<Element> domElement(do_QueryInterface(domNode));

View File

@ -45,11 +45,8 @@ BlobSerial(ImageURL* aURI)
return Nothing();
}
ImageCacheKey::ImageCacheKey(nsIURI* aURI,
const PrincipalOriginAttributes& aAttrs,
nsIDocument* aDocument)
ImageCacheKey::ImageCacheKey(nsIURI* aURI, nsIDocument* aDocument)
: mURI(new ImageURL(aURI))
, mOriginAttributes(aAttrs)
, mControlledDocument(GetControlledDocumentToken(aDocument))
, mIsChrome(URISchemeIs(mURI, "chrome"))
{
@ -59,14 +56,11 @@ ImageCacheKey::ImageCacheKey(nsIURI* aURI,
mBlobSerial = BlobSerial(mURI);
}
mHash = ComputeHash(mURI, mBlobSerial, mOriginAttributes, mControlledDocument);
mHash = ComputeHash(mURI, mBlobSerial, mControlledDocument);
}
ImageCacheKey::ImageCacheKey(ImageURL* aURI,
const PrincipalOriginAttributes& aAttrs,
nsIDocument* aDocument)
ImageCacheKey::ImageCacheKey(ImageURL* aURI, nsIDocument* aDocument)
: mURI(aURI)
, mOriginAttributes(aAttrs)
, mControlledDocument(GetControlledDocumentToken(aDocument))
, mIsChrome(URISchemeIs(mURI, "chrome"))
{
@ -76,13 +70,12 @@ ImageCacheKey::ImageCacheKey(ImageURL* aURI,
mBlobSerial = BlobSerial(mURI);
}
mHash = ComputeHash(mURI, mBlobSerial, mOriginAttributes, mControlledDocument);
mHash = ComputeHash(mURI, mBlobSerial, mControlledDocument);
}
ImageCacheKey::ImageCacheKey(const ImageCacheKey& aOther)
: mURI(aOther.mURI)
, mBlobSerial(aOther.mBlobSerial)
, mOriginAttributes(aOther.mOriginAttributes)
, mControlledDocument(aOther.mControlledDocument)
, mHash(aOther.mHash)
, mIsChrome(aOther.mIsChrome)
@ -91,7 +84,6 @@ ImageCacheKey::ImageCacheKey(const ImageCacheKey& aOther)
ImageCacheKey::ImageCacheKey(ImageCacheKey&& aOther)
: mURI(Move(aOther.mURI))
, mBlobSerial(Move(aOther.mBlobSerial))
, mOriginAttributes(aOther.mOriginAttributes)
, mControlledDocument(aOther.mControlledDocument)
, mHash(aOther.mHash)
, mIsChrome(aOther.mIsChrome)
@ -104,10 +96,6 @@ ImageCacheKey::operator==(const ImageCacheKey& aOther) const
if (mControlledDocument != aOther.mControlledDocument) {
return false;
}
// The origin attributes always have to match.
if (mOriginAttributes != aOther.mOriginAttributes) {
return false;
}
if (mBlobSerial || aOther.mBlobSerial) {
// If at least one of us has a blob serial, just compare the blob serial and
// the ref portion of the URIs.
@ -128,16 +116,12 @@ ImageCacheKey::Spec() const
/* static */ uint32_t
ImageCacheKey::ComputeHash(ImageURL* aURI,
const Maybe<uint64_t>& aBlobSerial,
const PrincipalOriginAttributes& aAttrs,
void* aControlledDocument)
{
// Since we frequently call Hash() several times in a row on the same
// ImageCacheKey, as an optimization we compute our hash once and store it.
nsPrintfCString ptr("%p", aControlledDocument);
nsAutoCString suffix;
aAttrs.CreateSuffix(suffix);
if (aBlobSerial) {
// For blob URIs, we hash the serial number of the underlying blob, so that
// different blob URIs which point to the same blob share a cache entry. We
@ -146,13 +130,13 @@ ImageCacheKey::ComputeHash(ImageURL* aURI,
// the same.
nsAutoCString ref;
aURI->GetRef(ref);
return HashGeneric(*aBlobSerial, HashString(ref + suffix + ptr));
return HashGeneric(*aBlobSerial, HashString(ref + ptr));
}
// For non-blob URIs, we hash the URI spec.
nsAutoCString spec;
aURI->GetSpec(spec);
return HashString(spec + suffix + ptr);
return HashString(spec + ptr);
}
/* static */ void*

View File

@ -10,7 +10,6 @@
#ifndef mozilla_image_src_ImageCacheKey_h
#define mozilla_image_src_ImageCacheKey_h
#include "mozilla/BasePrincipal.h"
#include "mozilla/Maybe.h"
class nsIDocument;
@ -32,10 +31,8 @@ class ImageURL;
class ImageCacheKey final
{
public:
ImageCacheKey(nsIURI* aURI, const PrincipalOriginAttributes& aAttrs,
nsIDocument* aDocument);
ImageCacheKey(ImageURL* aURI, const PrincipalOriginAttributes& aAttrs,
nsIDocument* aDocument);
ImageCacheKey(nsIURI* aURI, nsIDocument* aDocument);
ImageCacheKey(ImageURL* aURI, nsIDocument* aDocument);
ImageCacheKey(const ImageCacheKey& aOther);
ImageCacheKey(ImageCacheKey&& aOther);
@ -56,13 +53,11 @@ public:
private:
static uint32_t ComputeHash(ImageURL* aURI,
const Maybe<uint64_t>& aBlobSerial,
const PrincipalOriginAttributes& aAttrs,
void* aControlledDocument);
static void* GetControlledDocumentToken(nsIDocument* aDocument);
RefPtr<ImageURL> mURI;
Maybe<uint64_t> mBlobSerial;
PrincipalOriginAttributes mOriginAttributes;
void* mControlledDocument;
uint32_t mHash;
bool mIsChrome;

View File

@ -1368,20 +1368,7 @@ imgLoader::FindEntryProperties(nsIURI* uri,
*_retval = nullptr;
nsCOMPtr<nsIDocument> doc = do_QueryInterface(aDOMDoc);
nsCOMPtr<nsIPrincipal> principal;
if (doc) {
principal = doc->NodePrincipal();
} else {
nsCOMPtr<nsIScriptSecurityManager> ssm = nsContentUtils::GetSecurityManager();
NS_ENSURE_TRUE(ssm, NS_ERROR_FAILURE);
ssm->GetSystemPrincipal(getter_AddRefs(principal));
}
NS_ENSURE_TRUE(principal, NS_ERROR_FAILURE);
const PrincipalOriginAttributes& attrs =
BasePrincipal::Cast(principal)->OriginAttributesRef();
ImageCacheKey key(uri, attrs, doc);
ImageCacheKey key(uri, doc);
imgCacheTable& cache = GetCache(key);
RefPtr<imgCacheEntry> entry;
@ -2138,11 +2125,7 @@ imgLoader::LoadImage(nsIURI* aURI,
// XXX For now ignore aCacheKey. We will need it in the future
// for correctly dealing with image load requests that are a result
// of post data.
NS_ENSURE_TRUE(aLoadingPrincipal, NS_ERROR_FAILURE);
const PrincipalOriginAttributes& attrs =
BasePrincipal::Cast(aLoadingPrincipal)->OriginAttributesRef();
ImageCacheKey key(aURI, attrs, aLoadingDocument);
ImageCacheKey key(aURI, aLoadingDocument);
imgCacheTable& cache = GetCache(key);
if (cache.Get(key, getter_AddRefs(entry)) && entry) {
@ -2346,19 +2329,7 @@ imgLoader::LoadImageWithChannel(nsIChannel* channel,
nsCOMPtr<nsIURI> uri;
channel->GetURI(getter_AddRefs(uri));
nsCOMPtr<nsIDocument> doc = do_QueryInterface(aCX);
NS_ENSURE_TRUE(channel, NS_ERROR_FAILURE);
nsCOMPtr<nsILoadInfo> loadInfo = channel->GetLoadInfo();
NS_ENSURE_TRUE(loadInfo, NS_ERROR_FAILURE);
nsCOMPtr<nsIPrincipal> principal;
loadInfo->GetLoadingPrincipal(getter_AddRefs(principal));
NS_ENSURE_TRUE(principal, NS_ERROR_FAILURE);
const PrincipalOriginAttributes& attrs =
BasePrincipal::Cast(principal)->OriginAttributesRef();
ImageCacheKey key(uri, attrs, doc);
ImageCacheKey key(uri, doc);
nsLoadFlags requestFlags = nsIRequest::LOAD_NORMAL;
channel->GetLoadFlags(&requestFlags);
@ -2460,7 +2431,7 @@ imgLoader::LoadImageWithChannel(nsIChannel* channel,
// constructed above with the *current URI* and not the *original URI*. I'm
// pretty sure this is a bug, and it's preventing us from ever getting a
// cache hit in LoadImageWithChannel when redirects are involved.
ImageCacheKey originalURIKey(originalURI, attrs, doc);
ImageCacheKey originalURIKey(originalURI, doc);
// Default to doing a principal check because we don't know who
// started that load and whether their principal ended up being

View File

@ -13,9 +13,6 @@ var Cr = Components.results;
Cu.import("resource://testing-common/httpd.js");
Cu.import("resource://gre/modules/NetUtil.jsm");
Cu.import("resource://gre/modules/Services.jsm");
var ssm = Services.scriptSecurityManager;
var server = new HttpServer();
server.registerDirectory("/", do_get_file(''));
@ -100,9 +97,7 @@ function checkSecondLoad()
var listener = new ImageListener(checkClone, secondLoadDone);
var outer = Cc["@mozilla.org/image/tools;1"].getService(Ci.imgITools)
.createScriptedObserver(listener);
var systemPrincipal = ssm.getSystemPrincipal();
requests.push(gCurrentLoader.loadImageXPCOM(uri, null, null, "default",
systemPrincipal, null, outer, null, 0, null));
requests.push(gCurrentLoader.loadImageXPCOM(uri, null, null, "default", null, null, outer, null, 0, null));
listener.synchronous = false;
}
@ -182,9 +177,7 @@ function startImageCallback(otherCb)
var listener2 = new ImageListener(null, function(foo, bar) { do_test_finished(); });
var outer = Cc["@mozilla.org/image/tools;1"].getService(Ci.imgITools)
.createScriptedObserver(listener2);
var systemPrincipal = ssm.getSystemPrincipal();
requests.push(gCurrentLoader.loadImageXPCOM(uri, null, null, "default",
systemPrincipal, null, outer, null, 0, null));
requests.push(gCurrentLoader.loadImageXPCOM(uri, null, null, "default", null, null, outer, null, 0, null));
listener2.synchronous = false;
// Now that we've started another load, chain to the callback.
@ -211,9 +204,7 @@ function run_test()
var listener = new ImageListener(startImageCallback(checkClone), firstLoadDone);
var outer = Cc["@mozilla.org/image/tools;1"].getService(Ci.imgITools)
.createScriptedObserver(listener);
var systemPrincipal = ssm.getSystemPrincipal();
var req = gCurrentLoader.loadImageXPCOM(uri, null, null, "default",
systemPrincipal, null, outer, null, 0, null);
var req = gCurrentLoader.loadImageXPCOM(uri, null, null, "default", null, null, outer, null, 0, null);
requests.push(req);
// Ensure that we don't cause any mayhem when we lock an image.

View File

@ -7,8 +7,6 @@ Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/NetUtil.jsm");
Cu.import("resource://testing-common/httpd.js");
var ssm = Services.scriptSecurityManager;
var server = new HttpServer();
server.registerPathHandler('/image.png', imageHandler);
server.start(-1);
@ -86,9 +84,7 @@ function loadImage(isPrivate, callback) {
var loadGroup = Cc["@mozilla.org/network/load-group;1"].createInstance(Ci.nsILoadGroup);
loadGroup.notificationCallbacks = new NotificationCallbacks(isPrivate);
var loader = isPrivate ? gPrivateLoader : gPublicLoader;
var systemPrincipal = ssm.getSystemPrincipal();
requests.push(loader.loadImageXPCOM(uri, null, null, "default",
systemPrincipal, loadGroup, outer, null, 0, null));
requests.push(loader.loadImageXPCOM(uri, null, null, "default", null, loadGroup, outer, null, 0, null));
listener.synchronous = false;
}
@ -108,9 +104,6 @@ function run_loadImage_tests() {
});
}
gPrivateLoader.QueryInterface(Ci.imgICache).clearCache(false);
gPublicLoader.QueryInterface(Ci.imgICache).clearCache(false);
Services.obs.addObserver(observer, "cacheservice:empty-cache", false);
let cs = Cc["@mozilla.org/netwerk/cache-storage-service;1"]
.getService(Ci.nsICacheStorageService);

View File

@ -2207,19 +2207,13 @@ nsImageFrame::LoadIcon(const nsAString& aSpec,
nsLoadFlags loadFlags = nsIRequest::LOAD_NORMAL;
nsContentPolicyType contentPolicyType = nsIContentPolicy::TYPE_INTERNAL_IMAGE;
nsCOMPtr<nsIScriptSecurityManager> ssm = nsContentUtils::GetSecurityManager();
NS_ENSURE_TRUE(ssm, NS_ERROR_FAILURE);
nsCOMPtr<nsIPrincipal> systemPrincipal;
ssm->GetSystemPrincipal(getter_AddRefs(systemPrincipal));
NS_ENSURE_TRUE(systemPrincipal, NS_ERROR_FAILURE);
return il->LoadImage(realURI, /* icon URI */
nullptr, /* initial document URI; this is only
relevant for cookies, so does not
apply to icons. */
nullptr, /* referrer (not relevant for icons) */
mozilla::net::RP_Default,
systemPrincipal, /* principal (not relevant for icons) */
nullptr, /* principal (not relevant for icons) */
loadGroup,
gIconLoad,
nullptr, /* No context */

View File

@ -6,7 +6,6 @@
#include "nsAlertsIconListener.h"
#include "imgIContainer.h"
#include "imgIRequest.h"
#include "nsContentUtils.h"
#include "imgLoader.h"
#include "nsNetUtil.h"
#include "nsServiceManagerUtils.h"
@ -249,19 +248,14 @@ nsAlertsIconListener::StartRequest(const nsAString & aImageUrl, bool aInPrivateB
if (!imageUri)
return ShowAlert(nullptr);
nsCOMPtr<imgILoader> il(do_GetService("@mozilla.org/image/loader;1"));
imgLoader* il = aInPrivateBrowsing ? imgLoader::PrivateBrowsingLoader()
: imgLoader::NormalLoader();
if (!il)
return ShowAlert(nullptr);
nsCOMPtr<nsIScriptSecurityManager> ssm = nsContentUtils::GetSecurityManager();
NS_ENSURE_TRUE(ssm, NS_ERROR_FAILURE);
nsCOMPtr<nsIPrincipal> systemPrincipal;
ssm->GetSystemPrincipal(getter_AddRefs(systemPrincipal));
NS_ENSURE_TRUE(systemPrincipal, NS_ERROR_FAILURE);
nsresult rv = il->LoadImageXPCOM(imageUri, nullptr, nullptr,
NS_LITERAL_STRING("default"),
systemPrincipal, nullptr, this, nullptr,
NS_LITERAL_STRING("default"), nullptr, nullptr,
this, nullptr,
aInPrivateBrowsing ? nsIRequest::LOAD_ANONYMOUS :
nsIRequest::LOAD_NORMAL,
nullptr, 0 /* use default */,