Bug 1418930 Part 1: Define and use a method nsContentUtils::CORSModeToLoadImageFlags to standardize conversion of CORS modes to load image flags. r=bz

MozReview-Commit-ID: AqsEUhFYxH6

--HG--
extra : rebase_source : 60a367491f4e1d80f434a1f2afe3b09b773fc8d1
This commit is contained in:
Brad Werth 2018-01-31 11:10:01 -05:00
parent 9c2a68ffc5
commit e7b31713ef
4 changed files with 29 additions and 21 deletions

View File

@ -3701,6 +3701,20 @@ nsContentUtils::IsImageInCache(nsIURI* aURI, nsIDocument* aDocument)
return (NS_SUCCEEDED(rv) && props);
}
// static
int32_t
nsContentUtils::CORSModeToLoadImageFlags(mozilla::CORSMode aMode)
{
switch (aMode) {
case CORS_ANONYMOUS:
return imgILoader::LOAD_CORS_ANONYMOUS;
case CORS_USE_CREDENTIALS:
return imgILoader::LOAD_CORS_USE_CREDENTIALS;
default:
return 0;
}
}
// static
nsresult
nsContentUtils::LoadImage(nsIURI* aURI, nsINode* aContext,

View File

@ -22,6 +22,7 @@
#include "js/RootingAPI.h"
#include "mozilla/dom/FromParser.h"
#include "mozilla/BasicEvents.h"
#include "mozilla/CORSMode.h"
#include "mozilla/EventForwards.h"
#include "mozilla/GuardObjects.h"
#include "mozilla/TaskCategory.h"
@ -825,6 +826,14 @@ public:
*/
static bool DocumentInactiveForImageLoads(nsIDocument* aDocument);
/**
* Convert a CORSMode into the corresponding imgILoader flags for
* passing to LoadImage.
* @param aMode CORS mode to convert
* @return a bitfield suitable to bitwise OR with other nsIRequest flags
*/
static int32_t CORSModeToLoadImageFlags(mozilla::CORSMode aMode);
/**
* Method to start an image load. This does not do any security checks.
* This method will attempt to make aURI immutable; a caller that wants to

View File

@ -9256,20 +9256,9 @@ nsDocument::MaybePreLoadImage(nsIURI* uri, const nsAString &aCrossOriginAttr,
return;
}
nsLoadFlags loadFlags = nsIRequest::LOAD_NORMAL;
switch (Element::StringToCORSMode(aCrossOriginAttr)) {
case CORS_NONE:
// Nothing to do
break;
case CORS_ANONYMOUS:
loadFlags |= imgILoader::LOAD_CORS_ANONYMOUS;
break;
case CORS_USE_CREDENTIALS:
loadFlags |= imgILoader::LOAD_CORS_USE_CREDENTIALS;
break;
default:
MOZ_CRASH("Unknown CORS mode!");
}
nsLoadFlags loadFlags = nsIRequest::LOAD_NORMAL |
nsContentUtils::CORSModeToLoadImageFlags(
Element::StringToCORSMode(aCrossOriginAttr));
nsContentPolicyType policyType =
aIsImgSet ? nsIContentPolicy::TYPE_IMAGESET :

View File

@ -963,13 +963,9 @@ nsImageLoadingContent::LoadImage(nsIURI* aNewURI,
"Principal mismatch?");
#endif
nsLoadFlags loadFlags = aLoadFlags;
int32_t corsmode = GetCORSMode();
if (corsmode == CORS_ANONYMOUS) {
loadFlags |= imgILoader::LOAD_CORS_ANONYMOUS;
} else if (corsmode == CORS_USE_CREDENTIALS) {
loadFlags |= imgILoader::LOAD_CORS_USE_CREDENTIALS;
}
nsLoadFlags loadFlags = aLoadFlags |
nsContentUtils::CORSModeToLoadImageFlags(
GetCORSMode());
// get document wide referrer policy
// if referrer attributes are enabled in preferences, load img referrer attribute