Commit 7d479f6a authored by Emilio Cobos Álvarez's avatar Emilio Cobos Álvarez
Browse files

Bug 1832528 - Make cors mode part of the image cache key. r=tnikkel

So that we can hold entries loaded with different CORS modes, such as
masks and backgrounds.

Differential Revision: https://phabricator.services.mozilla.com/D177759
parent 628ffa8e
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -30,6 +30,9 @@ enum CORSMode : uint8_t {
  CORS_USE_CREDENTIALS
};

constexpr auto kFirstCORSMode = CORS_NONE;
constexpr auto kLastCORSMode = CORS_USE_CREDENTIALS;

}  // namespace mozilla

#endif /* CORSMode_h_ */
+4 −4
Original line number Diff line number Diff line
@@ -12379,12 +12379,12 @@ void Document::MaybePreLoadImage(nsIURI* aUri,
                                 ReferrerPolicyEnum aReferrerPolicy,
                                 bool aIsImgSet, bool aLinkPreload,
                                 const TimeStamp& aInitTimestamp) {
  const CORSMode corsMode = dom::Element::StringToCORSMode(aCrossOriginAttr);
  if (aLinkPreload) {
    // Check if the image was already preloaded in this document to avoid
    // duplicate preloading.
    PreloadHashKey key = PreloadHashKey::CreateAsImage(
        aUri, NodePrincipal(),
        dom::Element::StringToCORSMode(aCrossOriginAttr));
    PreloadHashKey key =
        PreloadHashKey::CreateAsImage(aUri, NodePrincipal(), corsMode);
    if (!mPreloadService.PreloadExists(key)) {
      PreLoadImage(aUri, aCrossOriginAttr, aReferrerPolicy, aIsImgSet,
                   aLinkPreload, 0);
@@ -12395,7 +12395,7 @@ void Document::MaybePreLoadImage(nsIURI* aUri,
  // 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(aUri, this)) {
  if (nsContentUtils::IsImageAvailable(aUri, NodePrincipal(), corsMode, this)) {
    return;
  }
+9 −15
Original line number Diff line number Diff line
@@ -3844,19 +3844,6 @@ imgLoader* nsContentUtils::GetImgLoaderForChannel(nsIChannel* aChannel,
             : imgLoader::NormalLoader();
}

// static
bool nsContentUtils::IsImageInCache(nsIURI* aURI, Document* aDocument) {
  imgILoader* loader = GetImgLoaderForDocument(aDocument);
  nsCOMPtr<imgICache> cache = do_QueryInterface(loader);

  // If something unexpected happened we return false, otherwise if props
  // is set, the image is cached and we return true
  nsCOMPtr<nsIProperties> props;
  nsresult rv =
      cache->FindEntryProperties(aURI, aDocument, getter_AddRefs(props));
  return (NS_SUCCEEDED(rv) && props);
}

// static
int32_t nsContentUtils::CORSModeToLoadImageFlags(mozilla::CORSMode aMode) {
  switch (aMode) {
@@ -10026,8 +10013,15 @@ bool nsContentUtils::IsImageAvailable(nsIContent* aLoadingNode, nsIURI* aURI,
  MOZ_ASSERT(triggeringPrincipal);

  Document* doc = aLoadingNode->OwnerDoc();
  imgLoader* imgLoader = GetImgLoaderForDocument(doc);
  return imgLoader->IsImageAvailable(aURI, triggeringPrincipal, aCORSMode, doc);
  return IsImageAvailable(aURI, triggeringPrincipal, aCORSMode, doc);
}

bool nsContentUtils::IsImageAvailable(nsIURI* aURI,
                                      nsIPrincipal* aTriggeringPrincipal,
                                      CORSMode aCORSMode, Document* aDoc) {
  imgLoader* imgLoader = GetImgLoaderForDocument(aDoc);
  return imgLoader->IsImageAvailable(aURI, aTriggeringPrincipal, aCORSMode,
                                     aDoc);
}

/* static */
+2 −5
Original line number Diff line number Diff line
@@ -1051,11 +1051,6 @@ class nsContentUtils {
  static imgLoader* GetImgLoaderForChannel(nsIChannel* aChannel,
                                           Document* aContext);

  /**
   * Returns whether the given URI is in the image cache.
   */
  static bool IsImageInCache(nsIURI* aURI, Document* aDocument);

  /**
   * Method to get an imgIContainer from an image loading content
   *
@@ -3099,6 +3094,8 @@ class nsContentUtils {
  static bool IsImageAvailable(nsIContent*, nsIURI*,
                               nsIPrincipal* aDefaultTriggeringPrincipal,
                               mozilla::CORSMode);
  static bool IsImageAvailable(nsIURI*, nsIPrincipal* aTriggeringPrincipal,
                               mozilla::CORSMode, Document*);

  /**
   * Returns the content policy type that should be used for loading images
+12 −4
Original line number Diff line number Diff line
@@ -29,26 +29,30 @@ using namespace dom;

namespace image {

ImageCacheKey::ImageCacheKey(nsIURI* aURI, const OriginAttributes& aAttrs,
ImageCacheKey::ImageCacheKey(nsIURI* aURI, CORSMode aCORSMode,
                             const OriginAttributes& aAttrs,
                             Document* aDocument)
    : mURI(aURI),
      mOriginAttributes(aAttrs),
      mControlledDocument(GetSpecialCaseDocumentToken(aDocument)),
      mIsolationKey(GetIsolationKey(aDocument, aURI)) {}
      mIsolationKey(GetIsolationKey(aDocument, aURI)),
      mCORSMode(aCORSMode) {}

ImageCacheKey::ImageCacheKey(const ImageCacheKey& aOther)
    : mURI(aOther.mURI),
      mOriginAttributes(aOther.mOriginAttributes),
      mControlledDocument(aOther.mControlledDocument),
      mIsolationKey(aOther.mIsolationKey),
      mHash(aOther.mHash) {}
      mHash(aOther.mHash),
      mCORSMode(aOther.mCORSMode) {}

ImageCacheKey::ImageCacheKey(ImageCacheKey&& aOther)
    : mURI(std::move(aOther.mURI)),
      mOriginAttributes(aOther.mOriginAttributes),
      mControlledDocument(aOther.mControlledDocument),
      mIsolationKey(aOther.mIsolationKey),
      mHash(aOther.mHash) {}
      mHash(aOther.mHash),
      mCORSMode(aOther.mCORSMode) {}

bool ImageCacheKey::operator==(const ImageCacheKey& aOther) const {
  // Don't share the image cache between a controlled document and anything
@@ -67,6 +71,10 @@ bool ImageCacheKey::operator==(const ImageCacheKey& aOther) const {
    return false;
  }

  if (mCORSMode != aOther.mCORSMode) {
    return false;
  }

  // For non-blob URIs, compare the URIs.
  bool equals = false;
  nsresult rv = mURI->Equals(aOther.mURI, &equals);
Loading