Commit d84d324b authored by Andrew Osmond's avatar Andrew Osmond
Browse files

Bug 1419889 - Don't force the image cache to validate if it hasn't started yet. r=tnikkel

imgLoader::ValidateEntry would aggressively determine an entry has
expired, even when the request hasn't yet begun. This is because the
expiration time for the entry was not set unless it was for a channel
which supports caching. Now we set the expiration time for all
channels, and if it doesn't support caching, it just expires at the
current time when imgRequest::OnStartRequest is called. Additionally,
imgLoader::ValidateEntry will not consider the expiration time in the
entry until it is non-zero.
parent 02e10653
Loading
Loading
Loading
Loading
+9 −10
Original line number Diff line number Diff line
@@ -930,8 +930,8 @@ NewImageChannel(nsIChannel** aResult,
  return NS_OK;
}

static uint32_t
SecondsFromPRTime(PRTime prTime)
/* static */ uint32_t
imgCacheEntry::SecondsFromPRTime(PRTime prTime)
{
  return uint32_t(int64_t(prTime) / int64_t(PR_USEC_PER_SEC));
}
@@ -1881,13 +1881,11 @@ imgLoader::ValidateEntry(imgCacheEntry* aEntry,
{
  LOG_SCOPE(gImgLog, "imgLoader::ValidateEntry");

  bool hasExpired;
  uint32_t expirationTime = aEntry->GetExpiryTime();
  if (expirationTime <= SecondsFromPRTime(PR_Now())) {
    hasExpired = true;
  } else {
    hasExpired = false;
  }
  // If the expiration time is zero, then the request has not gotten far enough
  // to know when it will expire.
  uint32_t expiryTime = aEntry->GetExpiryTime();
  bool hasExpired = expiryTime != 0 &&
                    expiryTime <= imgCacheEntry::SecondsFromPRTime(PR_Now());

  nsresult rv;

@@ -1904,7 +1902,8 @@ imgLoader::ValidateEntry(imgCacheEntry* aEntry,
      if (NS_SUCCEEDED(rv)) {
        // nsIFile uses millisec, NSPR usec
        fileLastMod *= 1000;
        hasExpired = SecondsFromPRTime((PRTime)fileLastMod) > lastModTime;
        hasExpired =
          imgCacheEntry::SecondsFromPRTime((PRTime)fileLastMod) > lastModTime;
      }
    }
  }
+2 −0
Original line number Diff line number Diff line
@@ -41,6 +41,8 @@ class ImageURL;
class imgCacheEntry
{
public:
  static uint32_t SecondsFromPRTime(PRTime prTime);

  imgCacheEntry(imgLoader* loader, imgRequest* request,
                bool aForcePrincipalCheck);
  ~imgCacheEntry();
+14 −9
Original line number Diff line number Diff line
@@ -33,6 +33,7 @@
#include "nsContentUtils.h"

#include "plstr.h" // PL_strcasestr(...)
#include "prtime.h" // for PR_Now
#include "nsNetUtil.h"
#include "nsIProtocolHandler.h"
#include "imgIRequest.h"
@@ -637,17 +638,21 @@ imgRequest::SetCacheValidation(imgCacheEntry* aCacheEntry, nsIRequest* aRequest)
{
  /* get the expires info */
  if (aCacheEntry) {
    nsCOMPtr<nsICacheInfoChannel> cacheChannel(do_QueryInterface(aRequest));
    if (cacheChannel) {
      uint32_t expiration = 0;
      /* get the expiration time from the caching channel's token */
      if (NS_SUCCEEDED(cacheChannel->GetCacheTokenExpirationTime(&expiration))) {
    // Expiration time defaults to 0. We set the expiration time on our
    // entry if it hasn't been set yet.
    if (aCacheEntry->GetExpiryTime() == 0) {
          aCacheEntry->SetExpiryTime(expiration);
      uint32_t expiration = 0;
      nsCOMPtr<nsICacheInfoChannel> cacheChannel(do_QueryInterface(aRequest));
      if (cacheChannel) {
        /* get the expiration time from the caching channel's token */
        cacheChannel->GetCacheTokenExpirationTime(&expiration);
      }
      if (expiration == 0) {
        // If the channel doesn't support caching, then ensure this expires the
        // next time it is used.
        expiration = imgCacheEntry::SecondsFromPRTime(PR_Now()) - 1;
      }
      aCacheEntry->SetExpiryTime(expiration);
    }

    // Determine whether the cache entry must be revalidated when we try to use