Bug 1418930 Part 1: Define and use a method nsContentUtils::CORSModeToLoadImageFlags to standardize conversion of CORS modes to load image flags. draft
authorBrad Werth <bwerth@mozilla.com>
Wed, 31 Jan 2018 11:10:01 -0500
changeset 751633 f63a0e04d63da6863c78a7dac756cacab04a35b5
parent 751476 f1a4b64f19b0e93c49492735db30a5023e624ae7
child 751634 db09e6c2efb8b69eb3c85bf16d17e52d081271d6
push id98028
push userbwerth@mozilla.com
push dateTue, 06 Feb 2018 19:52:00 +0000
bugs1418930
milestone60.0a1
Bug 1418930 Part 1: Define and use a method nsContentUtils::CORSModeToLoadImageFlags to standardize conversion of CORS modes to load image flags. MozReview-Commit-ID: AqsEUhFYxH6
dom/base/nsContentUtils.cpp
dom/base/nsContentUtils.h
dom/base/nsDocument.cpp
dom/base/nsImageLoadingContent.cpp
--- a/dom/base/nsContentUtils.cpp
+++ b/dom/base/nsContentUtils.cpp
@@ -3697,16 +3697,30 @@ nsContentUtils::IsImageInCache(nsIURI* a
     // is set, the image is cached and we return true
     nsCOMPtr<nsIProperties> props;
     nsCOMPtr<nsIDOMDocument> domDoc = do_QueryInterface(aDocument);
     nsresult rv = cache->FindEntryProperties(aURI, domDoc, getter_AddRefs(props));
     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,
                           nsIDocument* aLoadingDocument,
                           nsIPrincipal* aLoadingPrincipal,
                           uint64_t aRequestContextID,
                           nsIURI* aReferrer,
                           net::ReferrerPolicy aReferrerPolicy,
                           imgINotificationObserver* aObserver, int32_t aLoadFlags,
--- a/dom/base/nsContentUtils.h
+++ b/dom/base/nsContentUtils.h
@@ -17,16 +17,17 @@
 #include <ieeefp.h>
 #endif
 
 #include "js/TypeDecls.h"
 #include "js/Value.h"
 #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"
 #include "mozilla/TimeStamp.h"
 #include "nsContentListDeclarations.h"
 #include "nsMathUtils.h"
 #include "nsTArrayForwardDeclare.h"
 #include "Units.h"
@@ -821,16 +822,24 @@ public:
                            uint32_t aContentPolicyType = nsIContentPolicy::TYPE_INTERNAL_IMAGE);
 
   /**
    * Returns true if objects in aDocument shouldn't initiate image loads.
    */
   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
    * keep a mutable version around should pass in a clone.
    *
    * @param aURI uri of the image to be loaded
    * @param aContext element of document where the result of this request
    *                 will be used.
    * @param aLoadingDocument the document we belong to
--- a/dom/base/nsDocument.cpp
+++ b/dom/base/nsDocument.cpp
@@ -9251,30 +9251,19 @@ nsDocument::MaybePreLoadImage(nsIURI* ur
 {
   // Early exit if the img is already present in the img-cache
   // which indicates that the "real" load has already started and
   // that we shouldn't preload it.
   if (nsContentUtils::IsImageInCache(uri, static_cast<nsIDocument *>(this))) {
     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 :
                 nsIContentPolicy::TYPE_INTERNAL_IMAGE_PRELOAD;
 
   // Image not in cache - trigger preload
   RefPtr<imgRequestProxy> request;
   nsresult rv =
--- a/dom/base/nsImageLoadingContent.cpp
+++ b/dom/base/nsImageLoadingContent.cpp
@@ -958,23 +958,19 @@ nsImageLoadingContent::LoadImage(nsIURI*
   // We use the principal of aDocument to avoid having to QI |this| an extra
   // time. It should always be the same as the principal of this node.
 #ifdef DEBUG
   nsIContent* thisContent = AsContent();
   MOZ_ASSERT(thisContent->NodePrincipal() == aDocument->NodePrincipal(),
              "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
   // if the image does not provide a referrer attribute, ignore this
   net::ReferrerPolicy referrerPolicy = aDocument->GetReferrerPolicy();
   net::ReferrerPolicy imgReferrerPolicy = GetImageReferrerPolicy();
   if (imgReferrerPolicy != net::RP_Unset) {
     referrerPolicy = imgReferrerPolicy;