1 /*
   2     Copyright (C) 1998 Lars Knoll (knoll@mpi-hd.mpg.de)
   3     Copyright (C) 2001 Dirk Mueller (mueller@kde.org)
   4     Copyright (C) 2002 Waldo Bastian (bastian@kde.org)
   5     Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Apple Inc. All rights reserved.
   6     Copyright (C) 2009 Torch Mobile Inc. http://www.torchmobile.com/
   7 
   8     This library is free software; you can redistribute it and/or
   9     modify it under the terms of the GNU Library General Public
  10     License as published by the Free Software Foundation; either
  11     version 2 of the License, or (at your option) any later version.
  12 
  13     This library is distributed in the hope that it will be useful,
  14     but WITHOUT ANY WARRANTY; without even the implied warranty of
  15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16     Library General Public License for more details.
  17 
  18     You should have received a copy of the GNU Library General Public License
  19     along with this library; see the file COPYING.LIB.  If not, write to
  20     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  21     Boston, MA 02110-1301, USA.
  22 
  23     This class provides all functionality needed for loading images, style sheets and html
  24     pages from the web. It has a memory cache for these objects.
  25 */
  26 
  27 #include "config.h"
  28 #include "CachedResourceLoader.h"
  29 
  30 #include "CachedCSSStyleSheet.h"
  31 #include "CachedSVGDocument.h"
  32 #include "CachedFont.h"
  33 #include "CachedImage.h"
  34 #include "CachedRawResource.h"
  35 #include "CachedResourceRequest.h"
  36 #include "CachedScript.h"
  37 #include "CachedXSLStyleSheet.h"
  38 #include "Console.h"
  39 #include "ContentSecurityPolicy.h"
  40 #include "DOMWindow.h"
  41 #include "Document.h"
  42 #include "DocumentLoader.h"
  43 #include "Frame.h"
  44 #include "FrameLoader.h"
  45 #include "FrameLoaderClient.h"
  46 #include "HTMLElement.h"
  47 #include "HTMLFrameOwnerElement.h"
  48 #include "LoaderStrategy.h"
  49 #include "Logging.h"
  50 #include "MemoryCache.h"
  51 #include "PingLoader.h"
  52 #include "PlatformStrategies.h"
  53 #include "RenderElement.h"
  54 #include "ResourceLoadScheduler.h"
  55 #include "ScriptController.h"
  56 #include "SecurityOrigin.h"
  57 #include "Settings.h"
  58 #include <wtf/text/CString.h>
  59 #include <wtf/text/WTFString.h>
  60 
  61 #if ENABLE(VIDEO_TRACK)
  62 #include "CachedTextTrack.h"
  63 #endif
  64 
  65 #if ENABLE(RESOURCE_TIMING)
  66 #include "Performance.h"
  67 #endif
  68 
  69 #define PRELOAD_DEBUG 0
  70 
  71 namespace WebCore {
  72 
  73 static CachedResource* createResource(CachedResource::Type type, ResourceRequest& request, const String& charset)
  74 {
  75     switch (type) {
  76     case CachedResource::ImageResource:
  77         return new CachedImage(request);
  78     case CachedResource::CSSStyleSheet:
  79         return new CachedCSSStyleSheet(request, charset);
  80     case CachedResource::Script:
  81         return new CachedScript(request, charset);
  82     case CachedResource::SVGDocumentResource:
  83         return new CachedSVGDocument(request);
  84     case CachedResource::FontResource:
  85         return new CachedFont(request);
  86     case CachedResource::RawResource:
  87     case CachedResource::MainResource:
  88         return new CachedRawResource(request, type);
  89 #if ENABLE(XSLT)
  90     case CachedResource::XSLStyleSheet:
  91         return new CachedXSLStyleSheet(request);
  92 #endif
  93 #if ENABLE(LINK_PREFETCH)
  94     case CachedResource::LinkPrefetch:
  95         return new CachedResource(request, CachedResource::LinkPrefetch);
  96     case CachedResource::LinkSubresource:
  97         return new CachedResource(request, CachedResource::LinkSubresource);
  98 #endif
  99 #if ENABLE(VIDEO_TRACK)
 100     case CachedResource::TextTrackResource:
 101         return new CachedTextTrack(request);
 102 #endif
 103     }
 104     ASSERT_NOT_REACHED();
 105     return 0;
 106 }
 107 
 108 CachedResourceLoader::CachedResourceLoader(DocumentLoader* documentLoader)
 109     : m_document(0)
 110     , m_documentLoader(documentLoader)
 111     , m_requestCount(0)
 112     , m_garbageCollectDocumentResourcesTimer(this, &CachedResourceLoader::garbageCollectDocumentResourcesTimerFired)
 113     , m_autoLoadImages(true)
 114     , m_imagesEnabled(true)
 115     , m_allowStaleResources(false)
 116 {
 117 }
 118 
 119 CachedResourceLoader::~CachedResourceLoader()
 120 {
 121     m_documentLoader = 0;
 122     m_document = 0;
 123 
 124     clearPreloads();
 125     DocumentResourceMap::iterator end = m_documentResources.end();
 126     for (DocumentResourceMap::iterator it = m_documentResources.begin(); it != end; ++it)
 127         it->value->setOwningCachedResourceLoader(0);
 128 
 129     // Make sure no requests still point to this CachedResourceLoader
 130     ASSERT(m_requestCount == 0);
 131 }
 132 
 133 CachedResource* CachedResourceLoader::cachedResource(const String& resourceURL) const
 134 {
 135     URL url = m_document->completeURL(resourceURL);
 136     return cachedResource(url);
 137 }
 138 
 139 CachedResource* CachedResourceLoader::cachedResource(const URL& resourceURL) const
 140 {
 141     URL url = MemoryCache::removeFragmentIdentifierIfNeeded(resourceURL);
 142     return m_documentResources.get(url).get();
 143 }
 144 
 145 Frame* CachedResourceLoader::frame() const
 146 {
 147     return m_documentLoader ? m_documentLoader->frame() : 0;
 148 }
 149 
 150 CachedResourceHandle<CachedImage> CachedResourceLoader::requestImage(CachedResourceRequest& request)
 151 {
 152     if (Frame* frame = this->frame()) {
 153         if (frame->loader().pageDismissalEventBeingDispatched() != FrameLoader::NoDismissal) {
 154             URL requestURL = request.resourceRequest().url();
 155             if (requestURL.isValid() && canRequest(CachedResource::ImageResource, requestURL, request.options(), request.forPreload()))
 156                 PingLoader::loadImage(*frame, requestURL);
 157             return nullptr;
 158         }
 159     }
 160 
 161     request.setDefer(clientDefersImage(request.resourceRequest().url()) ? CachedResourceRequest::DeferredByClient : CachedResourceRequest::NoDefer);
 162     return toCachedImage(requestResource(CachedResource::ImageResource, request).get());
 163 }
 164 
 165 CachedResourceHandle<CachedFont> CachedResourceLoader::requestFont(CachedResourceRequest& request)
 166 {
 167     return toCachedFont(requestResource(CachedResource::FontResource, request).get());
 168 }
 169 
 170 #if ENABLE(VIDEO_TRACK)
 171 CachedResourceHandle<CachedTextTrack> CachedResourceLoader::requestTextTrack(CachedResourceRequest& request)
 172 {
 173     return toCachedTextTrack(requestResource(CachedResource::TextTrackResource, request).get());
 174 }
 175 #endif
 176 
 177 CachedResourceHandle<CachedCSSStyleSheet> CachedResourceLoader::requestCSSStyleSheet(CachedResourceRequest& request)
 178 {
 179     return toCachedCSSStyleSheet(requestResource(CachedResource::CSSStyleSheet, request).get());
 180 }
 181 
 182 CachedResourceHandle<CachedCSSStyleSheet> CachedResourceLoader::requestUserCSSStyleSheet(CachedResourceRequest& request)
 183 {
 184     URL url = MemoryCache::removeFragmentIdentifierIfNeeded(request.resourceRequest().url());
 185 
 186 #if ENABLE(CACHE_PARTITIONING)
 187     request.mutableResourceRequest().setCachePartition(document()->topOrigin()->cachePartition());
 188 #endif
 189 
 190     if (CachedResource* existing = memoryCache()->resourceForRequest(request.resourceRequest())) {
 191         if (existing->type() == CachedResource::CSSStyleSheet)
 192             return toCachedCSSStyleSheet(existing);
 193         memoryCache()->remove(existing);
 194     }
 195     if (url.string() != request.resourceRequest().url())
 196         request.mutableResourceRequest().setURL(url);
 197 
 198     CachedResourceHandle<CachedCSSStyleSheet> userSheet = new CachedCSSStyleSheet(request.resourceRequest(), request.charset());
 199 
 200     memoryCache()->add(userSheet.get());
 201     // FIXME: loadResource calls setOwningCachedResourceLoader() if the resource couldn't be added to cache. Does this function need to call it, too?
 202 
 203     userSheet->load(this, ResourceLoaderOptions(DoNotSendCallbacks, SniffContent, BufferData, AllowStoredCredentials, AskClientForAllCredentials, SkipSecurityCheck, UseDefaultOriginRestrictionsForType));
 204 
 205     return userSheet;
 206 }
 207 
 208 CachedResourceHandle<CachedScript> CachedResourceLoader::requestScript(CachedResourceRequest& request)
 209 {
 210     return toCachedScript(requestResource(CachedResource::Script, request).get());
 211 }
 212 
 213 #if ENABLE(XSLT)
 214 CachedResourceHandle<CachedXSLStyleSheet> CachedResourceLoader::requestXSLStyleSheet(CachedResourceRequest& request)
 215 {
 216     return toCachedXSLStyleSheet(requestResource(CachedResource::XSLStyleSheet, request).get());
 217 }
 218 #endif
 219 
 220 CachedResourceHandle<CachedSVGDocument> CachedResourceLoader::requestSVGDocument(CachedResourceRequest& request)
 221 {
 222     return toCachedSVGDocument(requestResource(CachedResource::SVGDocumentResource, request).get());
 223 }
 224 
 225 #if ENABLE(LINK_PREFETCH)
 226 CachedResourceHandle<CachedResource> CachedResourceLoader::requestLinkResource(CachedResource::Type type, CachedResourceRequest& request)
 227 {
 228     ASSERT(frame());
 229     ASSERT(type == CachedResource::LinkPrefetch || type == CachedResource::LinkSubresource);
 230     return requestResource(type, request);
 231 }
 232 #endif
 233 
 234 CachedResourceHandle<CachedRawResource> CachedResourceLoader::requestRawResource(CachedResourceRequest& request)
 235 {
 236     return toCachedRawResource(requestResource(CachedResource::RawResource, request).get());
 237 }
 238 
 239 CachedResourceHandle<CachedRawResource> CachedResourceLoader::requestMainResource(CachedResourceRequest& request)
 240 {
 241     return toCachedRawResource(requestResource(CachedResource::MainResource, request).get());
 242 }
 243 
 244 bool CachedResourceLoader::checkInsecureContent(CachedResource::Type type, const URL& url) const
 245 {
 246     switch (type) {
 247     case CachedResource::Script:
 248 #if ENABLE(XSLT)
 249     case CachedResource::XSLStyleSheet:
 250 #endif
 251     case CachedResource::SVGDocumentResource:
 252     case CachedResource::CSSStyleSheet:
 253         // These resource can inject script into the current document (Script,
 254         // XSL) or exfiltrate the content of the current document (CSS).
 255         if (Frame* f = frame())
 256             if (!f->loader().mixedContentChecker().canRunInsecureContent(m_document->securityOrigin(), url))
 257                 return false;
 258         break;
 259 #if ENABLE(VIDEO_TRACK)
 260     case CachedResource::TextTrackResource:
 261 #endif
 262     case CachedResource::RawResource:
 263     case CachedResource::ImageResource:
 264     case CachedResource::FontResource: {
 265         // These resources can corrupt only the frame's pixels.
 266         if (Frame* f = frame()) {
 267             Frame& topFrame = f->tree().top();
 268             if (!topFrame.loader().mixedContentChecker().canDisplayInsecureContent(topFrame.document()->securityOrigin(), url))
 269                 return false;
 270         }
 271         break;
 272     }
 273     case CachedResource::MainResource:
 274 #if ENABLE(LINK_PREFETCH)
 275     case CachedResource::LinkPrefetch:
 276     case CachedResource::LinkSubresource:
 277         // Prefetch cannot affect the current document.
 278 #endif
 279         break;
 280     }
 281     return true;
 282 }
 283 
 284 bool CachedResourceLoader::canRequest(CachedResource::Type type, const URL& url, const ResourceLoaderOptions& options, bool forPreload)
 285 {
 286     if (document() && !document()->securityOrigin()->canDisplay(url)) {
 287         if (!forPreload)
 288             FrameLoader::reportLocalLoadFailed(frame(), url.stringCenterEllipsizedToLength());
 289         LOG(ResourceLoading, "CachedResourceLoader::requestResource URL was not allowed by SecurityOrigin::canDisplay");
 290         return 0;
 291     }
 292 
 293     // FIXME: Convert this to check the isolated world's Content Security Policy once webkit.org/b/104520 is solved.
 294     bool shouldBypassMainWorldContentSecurityPolicy = (frame() && frame()->script().shouldBypassMainWorldContentSecurityPolicy());
 295 
 296     // Some types of resources can be loaded only from the same origin.  Other
 297     // types of resources, like Images, Scripts, and CSS, can be loaded from
 298     // any URL.
 299     switch (type) {
 300     case CachedResource::MainResource:
 301     case CachedResource::ImageResource:
 302     case CachedResource::CSSStyleSheet:
 303     case CachedResource::Script:
 304     case CachedResource::FontResource:
 305     case CachedResource::RawResource:
 306 #if ENABLE(LINK_PREFETCH)
 307     case CachedResource::LinkPrefetch:
 308     case CachedResource::LinkSubresource:
 309 #endif
 310 #if ENABLE(VIDEO_TRACK)
 311     case CachedResource::TextTrackResource:
 312 #endif
 313         if (options.requestOriginPolicy == RestrictToSameOrigin && !m_document->securityOrigin()->canRequest(url)) {
 314             printAccessDeniedMessage(url);
 315             return false;
 316         }
 317         break;
 318     case CachedResource::SVGDocumentResource:
 319 #if ENABLE(XSLT)
 320     case CachedResource::XSLStyleSheet:
 321         if (!m_document->securityOrigin()->canRequest(url)) {
 322             printAccessDeniedMessage(url);
 323             return false;
 324         }
 325 #endif
 326         break;
 327     }
 328 
 329     switch (type) {
 330 #if ENABLE(XSLT)
 331     case CachedResource::XSLStyleSheet:
 332         if (!shouldBypassMainWorldContentSecurityPolicy && !m_document->contentSecurityPolicy()->allowScriptFromSource(url))
 333             return false;
 334         break;
 335 #endif
 336     case CachedResource::Script:
 337 #if PLATFORM(JAVA)
 338         // m_document holds current active document loader
 339         // if the cached resource doesn't belong to active document i.e these resource being requested from a older page
 340         // which is about to be replaced by current active document loader
 341         if (!m_document)
 342             return false;
 343 #endif
 344         if (!shouldBypassMainWorldContentSecurityPolicy && !m_document->contentSecurityPolicy()->allowScriptFromSource(url))
 345             return false;
 346 
 347         if (frame()) {
 348             if (!frame()->loader().client().allowScriptFromSource(frame()->settings().isScriptEnabled(), url))
 349                 return false;
 350         }
 351         break;
 352     case CachedResource::CSSStyleSheet:
 353         if (!shouldBypassMainWorldContentSecurityPolicy && !m_document->contentSecurityPolicy()->allowStyleFromSource(url))
 354             return false;
 355         break;
 356     case CachedResource::SVGDocumentResource:
 357     case CachedResource::ImageResource:
 358 #if PLATFORM(JAVA)
 359         if (!m_document)
 360             return false;
 361 #endif
 362         if (!shouldBypassMainWorldContentSecurityPolicy && !m_document->contentSecurityPolicy()->allowImageFromSource(url))
 363             return false;
 364         break;
 365     case CachedResource::FontResource: {
 366         if (!shouldBypassMainWorldContentSecurityPolicy && !m_document->contentSecurityPolicy()->allowFontFromSource(url))
 367             return false;
 368         break;
 369     }
 370     case CachedResource::MainResource:
 371     case CachedResource::RawResource:
 372 #if ENABLE(LINK_PREFETCH)
 373     case CachedResource::LinkPrefetch:
 374     case CachedResource::LinkSubresource:
 375 #endif
 376         break;
 377 #if ENABLE(VIDEO_TRACK)
 378     case CachedResource::TextTrackResource:
 379         if (!shouldBypassMainWorldContentSecurityPolicy && !m_document->contentSecurityPolicy()->allowMediaFromSource(url))
 380             return false;
 381         break;
 382 #endif
 383     }
 384 
 385     // Last of all, check for insecure content. We do this last so that when
 386     // folks block insecure content with a CSP policy, they don't get a warning.
 387     // They'll still get a warning in the console about CSP blocking the load.
 388 
 389     // FIXME: Should we consider forPreload here?
 390     if (!checkInsecureContent(type, url))
 391         return false;
 392 
 393     return true;
 394 }
 395 
 396 bool CachedResourceLoader::shouldContinueAfterNotifyingLoadedFromMemoryCache(CachedResource* resource)
 397 {
 398     if (!resource || !frame() || resource->status() != CachedResource::Cached)
 399         return true;
 400 
 401     ResourceRequest newRequest;
 402     frame()->loader().loadedResourceFromMemoryCache(resource, newRequest);
 403 
 404     // FIXME <http://webkit.org/b/113251>: If the delegate modifies the request's
 405     // URL, it is no longer appropriate to use this CachedResource.
 406     return !newRequest.isNull();
 407 }
 408 
 409 CachedResourceHandle<CachedResource> CachedResourceLoader::requestResource(CachedResource::Type type, CachedResourceRequest& request)
 410 {
 411     URL url = request.resourceRequest().url();
 412 
 413     LOG(ResourceLoading, "CachedResourceLoader::requestResource '%s', charset '%s', priority=%d, forPreload=%u", url.stringCenterEllipsizedToLength().latin1().data(), request.charset().latin1().data(), request.priority(), request.forPreload());
 414 
 415     // If only the fragment identifiers differ, it is the same resource.
 416     url = MemoryCache::removeFragmentIdentifierIfNeeded(url);
 417 
 418     if (!url.isValid())
 419         return 0;
 420 
 421     if (!canRequest(type, url, request.options(), request.forPreload()))
 422         return 0;
 423 
 424     if (Frame* f = frame())
 425         f->loader().client().dispatchWillRequestResource(&request);
 426 
 427     if (memoryCache()->disabled()) {
 428         DocumentResourceMap::iterator it = m_documentResources.find(url.string());
 429         if (it != m_documentResources.end()) {
 430             it->value->setOwningCachedResourceLoader(0);
 431             m_documentResources.remove(it);
 432         }
 433     }
 434 
 435     // See if we can use an existing resource from the cache.
 436     CachedResourceHandle<CachedResource> resource;
 437 #if ENABLE(CACHE_PARTITIONING)
 438     if (document())
 439         request.mutableResourceRequest().setCachePartition(document()->topOrigin()->cachePartition());
 440 #endif
 441 
 442     resource = memoryCache()->resourceForRequest(request.resourceRequest());
 443 
 444     const RevalidationPolicy policy = determineRevalidationPolicy(type, request.mutableResourceRequest(), request.forPreload(), resource.get(), request.defer());
 445     switch (policy) {
 446     case Reload:
 447         memoryCache()->remove(resource.get());
 448         FALLTHROUGH;
 449     case Load:
 450         resource = loadResource(type, request, request.charset());
 451         break;
 452     case Revalidate:
 453         resource = revalidateResource(request, resource.get());
 454         break;
 455     case Use:
 456         if (!shouldContinueAfterNotifyingLoadedFromMemoryCache(resource.get()))
 457             return 0;
 458         memoryCache()->resourceAccessed(resource.get());
 459         break;
 460     }
 461 
 462     if (!resource)
 463         return 0;
 464 
 465     if (!request.forPreload() || policy != Use)
 466         resource->setLoadPriority(request.priority());
 467 
 468     if ((policy != Use || resource->stillNeedsLoad()) && CachedResourceRequest::NoDefer == request.defer()) {
 469         resource->load(this, request.options());
 470 
 471         // We don't support immediate loads, but we do support immediate failure.
 472         if (resource->errorOccurred()) {
 473             if (resource->inCache())
 474                 memoryCache()->remove(resource.get());
 475             return 0;
 476         }
 477     }
 478 
 479     if (!request.resourceRequest().url().protocolIsData())
 480         m_validatedURLs.add(request.resourceRequest().url());
 481 
 482     ASSERT(resource->url() == url.string());
 483     m_documentResources.set(resource->url(), resource);
 484     return resource;
 485 }
 486 
 487 CachedResourceHandle<CachedResource> CachedResourceLoader::revalidateResource(const CachedResourceRequest& request, CachedResource* resource)
 488 {
 489     ASSERT(resource);
 490     ASSERT(resource->inCache());
 491     ASSERT(!memoryCache()->disabled());
 492     ASSERT(resource->canUseCacheValidator());
 493     ASSERT(!resource->resourceToRevalidate());
 494 
 495     // Copy the URL out of the resource to be revalidated in case it gets deleted by the remove() call below.
 496     String url = resource->url();
 497     CachedResourceHandle<CachedResource> newResource = createResource(resource->type(), resource->resourceRequest(), resource->encoding());
 498 
 499     LOG(ResourceLoading, "Resource %p created to revalidate %p", newResource.get(), resource);
 500     newResource->setResourceToRevalidate(resource);
 501 
 502     memoryCache()->remove(resource);
 503     memoryCache()->add(newResource.get());
 504 #if ENABLE(RESOURCE_TIMING)
 505     storeResourceTimingInitiatorInformation(resource, request);
 506 #else
 507     UNUSED_PARAM(request);
 508 #endif
 509     return newResource;
 510 }
 511 
 512 CachedResourceHandle<CachedResource> CachedResourceLoader::loadResource(CachedResource::Type type, CachedResourceRequest& request, const String& charset)
 513 {
 514     ASSERT(!memoryCache()->resourceForRequest(request.resourceRequest()));
 515 
 516     LOG(ResourceLoading, "Loading CachedResource for '%s'.", request.resourceRequest().url().stringCenterEllipsizedToLength().latin1().data());
 517 
 518     CachedResourceHandle<CachedResource> resource = createResource(type, request.mutableResourceRequest(), charset);
 519 
 520     if (!memoryCache()->add(resource.get()))
 521         resource->setOwningCachedResourceLoader(this);
 522 #if ENABLE(RESOURCE_TIMING)
 523     storeResourceTimingInitiatorInformation(resource, request);
 524 #endif
 525     return resource;
 526 }
 527 
 528 #if ENABLE(RESOURCE_TIMING)
 529 void CachedResourceLoader::storeResourceTimingInitiatorInformation(const CachedResourceHandle<CachedResource>& resource, const CachedResourceRequest& request)
 530 {
 531     if (resource->type() == CachedResource::MainResource) {
 532         // <iframe>s should report the initial navigation requested by the parent document, but not subsequent navigations.
 533         if (frame()->ownerElement() && m_documentLoader->frameLoader()->stateMachine().committingFirstRealLoad()) {
 534             InitiatorInfo info = { frame()->ownerElement()->localName(), monotonicallyIncreasingTime() };
 535             m_initiatorMap.add(resource.get(), info);
 536         }
 537     } else {
 538         InitiatorInfo info = { request.initiatorName(), monotonicallyIncreasingTime() };
 539         m_initiatorMap.add(resource.get(), info);
 540     }
 541 }
 542 #endif // ENABLE(RESOURCE_TIMING)
 543 
 544 CachedResourceLoader::RevalidationPolicy CachedResourceLoader::determineRevalidationPolicy(CachedResource::Type type, ResourceRequest& request, bool forPreload, CachedResource* existingResource, CachedResourceRequest::DeferOption defer) const
 545 {
 546     if (!existingResource)
 547         return Load;
 548 
 549     // We already have a preload going for this URL.
 550     if (forPreload && existingResource->isPreloaded())
 551         return Use;
 552 
 553     // If the same URL has been loaded as a different type, we need to reload.
 554     if (existingResource->type() != type) {
 555         LOG(ResourceLoading, "CachedResourceLoader::determineRevalidationPolicy reloading due to type mismatch.");
 556         return Reload;
 557     }
 558 
 559     if (!existingResource->canReuse(request))
 560         return Reload;
 561 
 562     // Conditional requests should have failed canReuse check.
 563     ASSERT(!request.isConditional());
 564 
 565     // Do not load from cache if images are not enabled. The load for this image will be blocked
 566     // in CachedImage::load.
 567     if (CachedResourceRequest::DeferredByClient == defer)
 568         return Reload;
 569 
 570     // Don't reload resources while pasting.
 571     if (m_allowStaleResources)
 572         return Use;
 573 
 574     // Alwaus use preloads.
 575     if (existingResource->isPreloaded())
 576         return Use;
 577 
 578     // CachePolicyHistoryBuffer uses the cache no matter what.
 579     if (cachePolicy(type) == CachePolicyHistoryBuffer)
 580         return Use;
 581 
 582     // Don't reuse resources with Cache-control: no-store.
 583     if (existingResource->response().cacheControlContainsNoStore()) {
 584         LOG(ResourceLoading, "CachedResourceLoader::determineRevalidationPolicy reloading due to Cache-control: no-store.");
 585         return Reload;
 586     }
 587 
 588     // If credentials were sent with the previous request and won't be
 589     // with this one, or vice versa, re-fetch the resource.
 590     //
 591     // This helps with the case where the server sends back
 592     // "Access-Control-Allow-Origin: *" all the time, but some of the
 593     // client's requests are made without CORS and some with.
 594     if (existingResource->resourceRequest().allowCookies() != request.allowCookies()) {
 595         LOG(ResourceLoading, "CachedResourceLoader::determineRevalidationPolicy reloading due to difference in credentials settings.");
 596         return Reload;
 597     }
 598 
 599     // During the initial load, avoid loading the same resource multiple times for a single document, even if the cache policies would tell us to.
 600     if (document() && !document()->loadEventFinished() && m_validatedURLs.contains(existingResource->url()))
 601         return Use;
 602 
 603     // CachePolicyReload always reloads
 604     if (cachePolicy(type) == CachePolicyReload) {
 605         LOG(ResourceLoading, "CachedResourceLoader::determineRevalidationPolicy reloading due to CachePolicyReload.");
 606         return Reload;
 607     }
 608 
 609     // We'll try to reload the resource if it failed last time.
 610     if (existingResource->errorOccurred()) {
 611         LOG(ResourceLoading, "CachedResourceLoader::determineRevalidationPolicye reloading due to resource being in the error state");
 612         return Reload;
 613     }
 614 
 615     // For resources that are not yet loaded we ignore the cache policy.
 616     if (existingResource->isLoading())
 617         return Use;
 618 
 619     // Check if the cache headers requires us to revalidate (cache expiration for example).
 620     if (existingResource->mustRevalidateDueToCacheHeaders(cachePolicy(type))) {
 621         // See if the resource has usable ETag or Last-modified headers.
 622         if (existingResource->canUseCacheValidator())
 623             return Revalidate;
 624 
 625         // No, must reload.
 626         LOG(ResourceLoading, "CachedResourceLoader::determineRevalidationPolicy reloading due to missing cache validators.");
 627         return Reload;
 628     }
 629 
 630     return Use;
 631 }
 632 
 633 void CachedResourceLoader::printAccessDeniedMessage(const URL& url) const
 634 {
 635     if (url.isNull())
 636         return;
 637 
 638     if (!frame())
 639         return;
 640 
 641     String message;
 642     if (!m_document || m_document->url().isNull())
 643         message = "Unsafe attempt to load URL " + url.stringCenterEllipsizedToLength() + '.';
 644     else
 645         message = "Unsafe attempt to load URL " + url.stringCenterEllipsizedToLength() + " from frame with URL " + m_document->url().stringCenterEllipsizedToLength() + ". Domains, protocols and ports must match.\n";
 646 
 647     frame()->document()->addConsoleMessage(MessageSource::Security, MessageLevel::Error, message);
 648 }
 649 
 650 void CachedResourceLoader::setAutoLoadImages(bool enable)
 651 {
 652     if (enable == m_autoLoadImages)
 653         return;
 654 
 655     m_autoLoadImages = enable;
 656 
 657     if (!m_autoLoadImages)
 658         return;
 659 
 660     reloadImagesIfNotDeferred();
 661 }
 662 
 663 void CachedResourceLoader::setImagesEnabled(bool enable)
 664 {
 665     if (enable == m_imagesEnabled)
 666         return;
 667 
 668     m_imagesEnabled = enable;
 669 
 670     if (!m_imagesEnabled)
 671         return;
 672 
 673     reloadImagesIfNotDeferred();
 674 }
 675 
 676 bool CachedResourceLoader::clientDefersImage(const URL& url) const
 677 {
 678     return frame() && !frame()->loader().client().allowImage(m_imagesEnabled, url);
 679 }
 680 
 681 bool CachedResourceLoader::shouldDeferImageLoad(const URL& url) const
 682 {
 683     return clientDefersImage(url) || !m_autoLoadImages;
 684 }
 685 
 686 void CachedResourceLoader::reloadImagesIfNotDeferred()
 687 {
 688     DocumentResourceMap::iterator end = m_documentResources.end();
 689     for (DocumentResourceMap::iterator it = m_documentResources.begin(); it != end; ++it) {
 690         CachedResource* resource = it->value.get();
 691         if (resource->isImage() && resource->stillNeedsLoad() && !clientDefersImage(resource->url()))
 692             const_cast<CachedResource*>(resource)->load(this, defaultCachedResourceOptions());
 693     }
 694 }
 695 
 696 CachePolicy CachedResourceLoader::cachePolicy(CachedResource::Type type) const
 697 {
 698     if (!frame())
 699         return CachePolicyVerify;
 700 
 701     if (type != CachedResource::MainResource)
 702         return frame()->loader().subresourceCachePolicy();
 703 
 704     if (frame()->loader().loadType() == FrameLoadTypeReloadFromOrigin || frame()->loader().loadType() == FrameLoadTypeReload)
 705         return CachePolicyReload;
 706     return CachePolicyVerify;
 707 }
 708 
 709 void CachedResourceLoader::removeCachedResource(CachedResource* resource) const
 710 {
 711 #ifndef NDEBUG
 712     DocumentResourceMap::iterator it = m_documentResources.find(resource->url());
 713     if (it != m_documentResources.end())
 714         ASSERT(it->value.get() == resource);
 715 #endif
 716     m_documentResources.remove(resource->url());
 717 }
 718 
 719 void CachedResourceLoader::loadDone(CachedResource* resource, bool shouldPerformPostLoadActions)
 720 {
 721     RefPtr<DocumentLoader> protectDocumentLoader(m_documentLoader);
 722     RefPtr<Document> protectDocument(m_document);
 723 
 724 #if ENABLE(RESOURCE_TIMING)
 725     if (resource && resource->response().isHTTP() && ((!resource->errorOccurred() && !resource->wasCanceled()) || resource->response().httpStatusCode() == 304)) {
 726         HashMap<CachedResource*, InitiatorInfo>::iterator initiatorIt = m_initiatorMap.find(resource);
 727         if (initiatorIt != m_initiatorMap.end()) {
 728             ASSERT(document());
 729             Document* initiatorDocument = document();
 730             if (resource->type() == CachedResource::MainResource)
 731                 initiatorDocument = document()->parentDocument();
 732             ASSERT(initiatorDocument);
 733             const InitiatorInfo& info = initiatorIt->value;
 734             initiatorDocument->domWindow()->performance()->addResourceTiming(info.name, initiatorDocument, resource->resourceRequest(), resource->response(), info.startTime, resource->loadFinishTime());
 735             m_initiatorMap.remove(initiatorIt);
 736         }
 737     }
 738 #else
 739     UNUSED_PARAM(resource);
 740 #endif // ENABLE(RESOURCE_TIMING)
 741 
 742     if (frame())
 743         frame()->loader().loadDone();
 744 
 745     if (shouldPerformPostLoadActions)
 746         performPostLoadActions();
 747 
 748     if (!m_garbageCollectDocumentResourcesTimer.isActive())
 749         m_garbageCollectDocumentResourcesTimer.startOneShot(0);
 750 }
 751 
 752 // Garbage collecting m_documentResources is a workaround for the
 753 // CachedResourceHandles on the RHS being strong references. Ideally this
 754 // would be a weak map, however CachedResourceHandles perform additional
 755 // bookkeeping on CachedResources, so instead pseudo-GC them -- when the
 756 // reference count reaches 1, m_documentResources is the only reference, so
 757 // remove it from the map.
 758 void CachedResourceLoader::garbageCollectDocumentResourcesTimerFired(Timer<CachedResourceLoader>& timer)
 759 {
 760     ASSERT_UNUSED(timer, &timer == &m_garbageCollectDocumentResourcesTimer);
 761     garbageCollectDocumentResources();
 762 }
 763 
 764 void CachedResourceLoader::garbageCollectDocumentResources()
 765 {
 766     typedef Vector<String, 10> StringVector;
 767     StringVector resourcesToDelete;
 768 
 769     for (DocumentResourceMap::iterator it = m_documentResources.begin(); it != m_documentResources.end(); ++it) {
 770         if (it->value->hasOneHandle()) {
 771             resourcesToDelete.append(it->key);
 772             it->value->setOwningCachedResourceLoader(0);
 773         }
 774     }
 775 
 776     for (StringVector::const_iterator it = resourcesToDelete.begin(); it != resourcesToDelete.end(); ++it)
 777         m_documentResources.remove(*it);
 778 }
 779 
 780 void CachedResourceLoader::performPostLoadActions()
 781 {
 782     checkForPendingPreloads();
 783 
 784     platformStrategies()->loaderStrategy()->resourceLoadScheduler()->servePendingRequests();
 785 }
 786 
 787 void CachedResourceLoader::incrementRequestCount(const CachedResource* res)
 788 {
 789     if (res->ignoreForRequestCount())
 790         return;
 791 
 792     ++m_requestCount;
 793 }
 794 
 795 void CachedResourceLoader::decrementRequestCount(const CachedResource* res)
 796 {
 797     if (res->ignoreForRequestCount())
 798         return;
 799 
 800     --m_requestCount;
 801     ASSERT(m_requestCount > -1);
 802 }
 803 
 804 void CachedResourceLoader::preload(CachedResource::Type type, CachedResourceRequest& request, const String& charset)
 805 {
 806     // We always preload resources on iOS. See <https://bugs.webkit.org/show_bug.cgi?id=91276>.
 807     // FIXME: We should consider adding a setting to toggle aggressive preloading behavior as opposed
 808     // to making this behavior specific to iOS.
 809 #if !PLATFORM(IOS)
 810     bool hasRendering = m_document->body() && m_document->renderView();
 811     bool canBlockParser = type == CachedResource::Script || type == CachedResource::CSSStyleSheet;
 812     if (!hasRendering && !canBlockParser) {
 813         // Don't preload subresources that can't block the parser before we have something to draw.
 814         // This helps prevent preloads from delaying first display when bandwidth is limited.
 815         PendingPreload pendingPreload = { type, request, charset };
 816         m_pendingPreloads.append(pendingPreload);
 817         return;
 818     }
 819 #endif
 820     requestPreload(type, request, charset);
 821 }
 822 
 823 void CachedResourceLoader::checkForPendingPreloads()
 824 {
 825     if (m_pendingPreloads.isEmpty() || !m_document || !m_document->body() || !m_document->body()->renderer())
 826         return;
 827 #if PLATFORM(IOS)
 828     // We always preload resources on iOS. See <https://bugs.webkit.org/show_bug.cgi?id=91276>.
 829     // So, we should never have any pending preloads.
 830     // FIXME: We should look to avoid compiling this code entirely when building for iOS.
 831     ASSERT_NOT_REACHED();
 832 #endif
 833     while (!m_pendingPreloads.isEmpty()) {
 834         PendingPreload preload = m_pendingPreloads.takeFirst();
 835         // Don't request preload if the resource already loaded normally (this will result in double load if the page is being reloaded with cached results ignored).
 836         if (!cachedResource(preload.m_request.resourceRequest().url()))
 837             requestPreload(preload.m_type, preload.m_request, preload.m_charset);
 838     }
 839     m_pendingPreloads.clear();
 840 }
 841 
 842 void CachedResourceLoader::requestPreload(CachedResource::Type type, CachedResourceRequest& request, const String& charset)
 843 {
 844     String encoding;
 845     if (type == CachedResource::Script || type == CachedResource::CSSStyleSheet)
 846         encoding = charset.isEmpty() ? m_document->charset() : charset;
 847 
 848     request.setCharset(encoding);
 849     request.setForPreload(true);
 850 
 851     CachedResourceHandle<CachedResource> resource = requestResource(type, request);
 852     if (!resource || (m_preloads && m_preloads->contains(resource.get())))
 853         return;
 854     resource->increasePreloadCount();
 855 
 856     if (!m_preloads)
 857         m_preloads = adoptPtr(new ListHashSet<CachedResource*>);
 858     m_preloads->add(resource.get());
 859 
 860 #if PRELOAD_DEBUG
 861     printf("PRELOADING %s\n",  resource->url().latin1().data());
 862 #endif
 863 }
 864 
 865 bool CachedResourceLoader::isPreloaded(const String& urlString) const
 866 {
 867     const URL& url = m_document->completeURL(urlString);
 868 
 869     if (m_preloads) {
 870         ListHashSet<CachedResource*>::iterator end = m_preloads->end();
 871         for (ListHashSet<CachedResource*>::iterator it = m_preloads->begin(); it != end; ++it) {
 872             CachedResource* resource = *it;
 873             if (resource->url() == url)
 874                 return true;
 875         }
 876     }
 877 
 878     Deque<PendingPreload>::const_iterator dequeEnd = m_pendingPreloads.end();
 879     for (Deque<PendingPreload>::const_iterator it = m_pendingPreloads.begin(); it != dequeEnd; ++it) {
 880         PendingPreload pendingPreload = *it;
 881         if (pendingPreload.m_request.resourceRequest().url() == url)
 882             return true;
 883     }
 884     return false;
 885 }
 886 
 887 void CachedResourceLoader::clearPreloads()
 888 {
 889 #if PRELOAD_DEBUG
 890     printPreloadStats();
 891 #endif
 892     if (!m_preloads)
 893         return;
 894 
 895     ListHashSet<CachedResource*>::iterator end = m_preloads->end();
 896     for (ListHashSet<CachedResource*>::iterator it = m_preloads->begin(); it != end; ++it) {
 897         CachedResource* res = *it;
 898         res->decreasePreloadCount();
 899         bool deleted = res->deleteIfPossible();
 900         if (!deleted && res->preloadResult() == CachedResource::PreloadNotReferenced)
 901             memoryCache()->remove(res);
 902     }
 903     m_preloads.clear();
 904 }
 905 
 906 void CachedResourceLoader::clearPendingPreloads()
 907 {
 908     m_pendingPreloads.clear();
 909 }
 910 
 911 #if PRELOAD_DEBUG
 912 void CachedResourceLoader::printPreloadStats()
 913 {
 914     unsigned scripts = 0;
 915     unsigned scriptMisses = 0;
 916     unsigned stylesheets = 0;
 917     unsigned stylesheetMisses = 0;
 918     unsigned images = 0;
 919     unsigned imageMisses = 0;
 920     ListHashSet<CachedResource*>::iterator end = m_preloads.end();
 921     for (ListHashSet<CachedResource*>::iterator it = m_preloads.begin(); it != end; ++it) {
 922         CachedResource* res = *it;
 923         if (res->preloadResult() == CachedResource::PreloadNotReferenced)
 924             printf("!! UNREFERENCED PRELOAD %s\n", res->url().latin1().data());
 925         else if (res->preloadResult() == CachedResource::PreloadReferencedWhileComplete)
 926             printf("HIT COMPLETE PRELOAD %s\n", res->url().latin1().data());
 927         else if (res->preloadResult() == CachedResource::PreloadReferencedWhileLoading)
 928             printf("HIT LOADING PRELOAD %s\n", res->url().latin1().data());
 929 
 930         if (res->type() == CachedResource::Script) {
 931             scripts++;
 932             if (res->preloadResult() < CachedResource::PreloadReferencedWhileLoading)
 933                 scriptMisses++;
 934         } else if (res->type() == CachedResource::CSSStyleSheet) {
 935             stylesheets++;
 936             if (res->preloadResult() < CachedResource::PreloadReferencedWhileLoading)
 937                 stylesheetMisses++;
 938         } else {
 939             images++;
 940             if (res->preloadResult() < CachedResource::PreloadReferencedWhileLoading)
 941                 imageMisses++;
 942         }
 943 
 944         if (res->errorOccurred())
 945             memoryCache()->remove(res);
 946 
 947         res->decreasePreloadCount();
 948     }
 949     m_preloads.clear();
 950 
 951     if (scripts)
 952         printf("SCRIPTS: %d (%d hits, hit rate %d%%)\n", scripts, scripts - scriptMisses, (scripts - scriptMisses) * 100 / scripts);
 953     if (stylesheets)
 954         printf("STYLESHEETS: %d (%d hits, hit rate %d%%)\n", stylesheets, stylesheets - stylesheetMisses, (stylesheets - stylesheetMisses) * 100 / stylesheets);
 955     if (images)
 956         printf("IMAGES:  %d (%d hits, hit rate %d%%)\n", images, images - imageMisses, (images - imageMisses) * 100 / images);
 957 }
 958 #endif
 959 
 960 const ResourceLoaderOptions& CachedResourceLoader::defaultCachedResourceOptions()
 961 {
 962     static ResourceLoaderOptions options(SendCallbacks, SniffContent, BufferData, AllowStoredCredentials, AskClientForAllCredentials, DoSecurityCheck, UseDefaultOriginRestrictionsForType);
 963     return options;
 964 }
 965 
 966 }