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     // Don't load for user cancel or stop
 294     if (m_documentLoader && !m_documentLoader->mainDocumentError().isNull() && m_documentLoader->mainDocumentError().isCancellation())
 295         return false;
 296 
 297     // FIXME: Convert this to check the isolated world's Content Security Policy once webkit.org/b/104520 is solved.
 298     bool shouldBypassMainWorldContentSecurityPolicy = (frame() && frame()->script().shouldBypassMainWorldContentSecurityPolicy());
 299 
 300     // Some types of resources can be loaded only from the same origin.  Other
 301     // types of resources, like Images, Scripts, and CSS, can be loaded from
 302     // any URL.
 303     switch (type) {
 304     case CachedResource::MainResource:
 305     case CachedResource::ImageResource:
 306     case CachedResource::CSSStyleSheet:
 307     case CachedResource::Script:
 308     case CachedResource::FontResource:
 309     case CachedResource::RawResource:
 310 #if ENABLE(LINK_PREFETCH)
 311     case CachedResource::LinkPrefetch:
 312     case CachedResource::LinkSubresource:
 313 #endif
 314 #if ENABLE(VIDEO_TRACK)
 315     case CachedResource::TextTrackResource:
 316 #endif
 317         if (options.requestOriginPolicy == RestrictToSameOrigin && !m_document->securityOrigin()->canRequest(url)) {
 318             printAccessDeniedMessage(url);
 319             return false;
 320         }
 321         break;
 322     case CachedResource::SVGDocumentResource:
 323 #if ENABLE(XSLT)
 324     case CachedResource::XSLStyleSheet:
 325         if (!m_document->securityOrigin()->canRequest(url)) {
 326             printAccessDeniedMessage(url);
 327             return false;
 328         }
 329 #endif
 330         break;
 331     }
 332 
 333     switch (type) {
 334 #if ENABLE(XSLT)
 335     case CachedResource::XSLStyleSheet:
 336         if (!shouldBypassMainWorldContentSecurityPolicy && !m_document->contentSecurityPolicy()->allowScriptFromSource(url))
 337             return false;
 338         break;
 339 #endif
 340     case CachedResource::Script:
 341 #if PLATFORM(JAVA)
 342         // m_document holds current active document loader
 343         // if the cached resource doesn't belong to active document i.e these resource being requested from a older page
 344         // which is about to be replaced by current active document loader
 345         if (!m_document)
 346             return false;
 347 #endif
 348         if (!shouldBypassMainWorldContentSecurityPolicy && !m_document->contentSecurityPolicy()->allowScriptFromSource(url))
 349             return false;
 350 
 351         if (frame()) {
 352             if (!frame()->loader().client().allowScriptFromSource(frame()->settings().isScriptEnabled(), url))
 353                 return false;
 354         }
 355         break;
 356     case CachedResource::CSSStyleSheet:
 357         if (!shouldBypassMainWorldContentSecurityPolicy && !m_document->contentSecurityPolicy()->allowStyleFromSource(url))
 358             return false;
 359         break;
 360     case CachedResource::SVGDocumentResource:
 361     case CachedResource::ImageResource:
 362 #if PLATFORM(JAVA)
 363         if (!m_document)
 364             return false;
 365 #endif
 366         if (!shouldBypassMainWorldContentSecurityPolicy && !m_document->contentSecurityPolicy()->allowImageFromSource(url))
 367             return false;
 368         break;
 369     case CachedResource::FontResource: {
 370         if (!shouldBypassMainWorldContentSecurityPolicy && !m_document->contentSecurityPolicy()->allowFontFromSource(url))
 371             return false;
 372         break;
 373     }
 374     case CachedResource::MainResource:
 375     case CachedResource::RawResource:
 376 #if ENABLE(LINK_PREFETCH)
 377     case CachedResource::LinkPrefetch:
 378     case CachedResource::LinkSubresource:
 379 #endif
 380         break;
 381 #if ENABLE(VIDEO_TRACK)
 382     case CachedResource::TextTrackResource:
 383         if (!shouldBypassMainWorldContentSecurityPolicy && !m_document->contentSecurityPolicy()->allowMediaFromSource(url))
 384             return false;
 385         break;
 386 #endif
 387     }
 388 
 389     // Last of all, check for insecure content. We do this last so that when
 390     // folks block insecure content with a CSP policy, they don't get a warning.
 391     // They'll still get a warning in the console about CSP blocking the load.
 392 
 393     // FIXME: Should we consider forPreload here?
 394     if (!checkInsecureContent(type, url))
 395         return false;
 396 
 397     return true;
 398 }
 399 
 400 bool CachedResourceLoader::shouldContinueAfterNotifyingLoadedFromMemoryCache(CachedResource* resource)
 401 {
 402     if (!resource || !frame() || resource->status() != CachedResource::Cached)
 403         return true;
 404 
 405     ResourceRequest newRequest;
 406     frame()->loader().loadedResourceFromMemoryCache(resource, newRequest);
 407 
 408     // FIXME <http://webkit.org/b/113251>: If the delegate modifies the request's
 409     // URL, it is no longer appropriate to use this CachedResource.
 410     return !newRequest.isNull();
 411 }
 412 
 413 CachedResourceHandle<CachedResource> CachedResourceLoader::requestResource(CachedResource::Type type, CachedResourceRequest& request)
 414 {
 415     URL url = request.resourceRequest().url();
 416 
 417     LOG(ResourceLoading, "CachedResourceLoader::requestResource '%s', charset '%s', priority=%d, forPreload=%u", url.stringCenterEllipsizedToLength().latin1().data(), request.charset().latin1().data(), request.priority(), request.forPreload());
 418 
 419     // If only the fragment identifiers differ, it is the same resource.
 420     url = MemoryCache::removeFragmentIdentifierIfNeeded(url);
 421 
 422     if (!url.isValid())
 423         return 0;
 424 
 425     if (!canRequest(type, url, request.options(), request.forPreload()))
 426         return 0;
 427 
 428     if (Frame* f = frame())
 429         f->loader().client().dispatchWillRequestResource(&request);
 430 
 431     if (memoryCache()->disabled()) {
 432         DocumentResourceMap::iterator it = m_documentResources.find(url.string());
 433         if (it != m_documentResources.end()) {
 434             it->value->setOwningCachedResourceLoader(0);
 435             m_documentResources.remove(it);
 436         }
 437     }
 438 
 439     // See if we can use an existing resource from the cache.
 440     CachedResourceHandle<CachedResource> resource;
 441 #if ENABLE(CACHE_PARTITIONING)
 442     if (document())
 443         request.mutableResourceRequest().setCachePartition(document()->topOrigin()->cachePartition());
 444 #endif
 445 
 446     resource = memoryCache()->resourceForRequest(request.resourceRequest());
 447 
 448     const RevalidationPolicy policy = determineRevalidationPolicy(type, request.mutableResourceRequest(), request.forPreload(), resource.get(), request.defer());
 449     switch (policy) {
 450     case Reload:
 451         memoryCache()->remove(resource.get());
 452         FALLTHROUGH;
 453     case Load:
 454         resource = loadResource(type, request, request.charset());
 455         break;
 456     case Revalidate:
 457         resource = revalidateResource(request, resource.get());
 458         break;
 459     case Use:
 460         if (!shouldContinueAfterNotifyingLoadedFromMemoryCache(resource.get()))
 461             return 0;
 462         memoryCache()->resourceAccessed(resource.get());
 463         break;
 464     }
 465 
 466     if (!resource)
 467         return 0;
 468 
 469     if (!request.forPreload() || policy != Use)
 470         resource->setLoadPriority(request.priority());
 471 
 472     if ((policy != Use || resource->stillNeedsLoad()) && CachedResourceRequest::NoDefer == request.defer()) {
 473         resource->load(this, request.options());
 474 
 475         // We don't support immediate loads, but we do support immediate failure.
 476         if (resource->errorOccurred()) {
 477             if (resource->inCache())
 478                 memoryCache()->remove(resource.get());
 479             return 0;
 480         }
 481     }
 482 
 483     if (!request.resourceRequest().url().protocolIsData())
 484         m_validatedURLs.add(request.resourceRequest().url());
 485 
 486     ASSERT(resource->url() == url.string());
 487     m_documentResources.set(resource->url(), resource);
 488     return resource;
 489 }
 490 
 491 CachedResourceHandle<CachedResource> CachedResourceLoader::revalidateResource(const CachedResourceRequest& request, CachedResource* resource)
 492 {
 493     ASSERT(resource);
 494     ASSERT(resource->inCache());
 495     ASSERT(!memoryCache()->disabled());
 496     ASSERT(resource->canUseCacheValidator());
 497     ASSERT(!resource->resourceToRevalidate());
 498 
 499     // Copy the URL out of the resource to be revalidated in case it gets deleted by the remove() call below.
 500     String url = resource->url();
 501     CachedResourceHandle<CachedResource> newResource = createResource(resource->type(), resource->resourceRequest(), resource->encoding());
 502 
 503     LOG(ResourceLoading, "Resource %p created to revalidate %p", newResource.get(), resource);
 504     newResource->setResourceToRevalidate(resource);
 505 
 506     memoryCache()->remove(resource);
 507     memoryCache()->add(newResource.get());
 508 #if ENABLE(RESOURCE_TIMING)
 509     storeResourceTimingInitiatorInformation(resource, request);
 510 #else
 511     UNUSED_PARAM(request);
 512 #endif
 513     return newResource;
 514 }
 515 
 516 CachedResourceHandle<CachedResource> CachedResourceLoader::loadResource(CachedResource::Type type, CachedResourceRequest& request, const String& charset)
 517 {
 518     ASSERT(!memoryCache()->resourceForRequest(request.resourceRequest()));
 519 
 520     LOG(ResourceLoading, "Loading CachedResource for '%s'.", request.resourceRequest().url().stringCenterEllipsizedToLength().latin1().data());
 521 
 522     CachedResourceHandle<CachedResource> resource = createResource(type, request.mutableResourceRequest(), charset);
 523 
 524     if (!memoryCache()->add(resource.get()))
 525         resource->setOwningCachedResourceLoader(this);
 526 #if ENABLE(RESOURCE_TIMING)
 527     storeResourceTimingInitiatorInformation(resource, request);
 528 #endif
 529     return resource;
 530 }
 531 
 532 #if ENABLE(RESOURCE_TIMING)
 533 void CachedResourceLoader::storeResourceTimingInitiatorInformation(const CachedResourceHandle<CachedResource>& resource, const CachedResourceRequest& request)
 534 {
 535     if (resource->type() == CachedResource::MainResource) {
 536         // <iframe>s should report the initial navigation requested by the parent document, but not subsequent navigations.
 537         if (frame()->ownerElement() && m_documentLoader->frameLoader()->stateMachine().committingFirstRealLoad()) {
 538             InitiatorInfo info = { frame()->ownerElement()->localName(), monotonicallyIncreasingTime() };
 539             m_initiatorMap.add(resource.get(), info);
 540         }
 541     } else {
 542         InitiatorInfo info = { request.initiatorName(), monotonicallyIncreasingTime() };
 543         m_initiatorMap.add(resource.get(), info);
 544     }
 545 }
 546 #endif // ENABLE(RESOURCE_TIMING)
 547 
 548 CachedResourceLoader::RevalidationPolicy CachedResourceLoader::determineRevalidationPolicy(CachedResource::Type type, ResourceRequest& request, bool forPreload, CachedResource* existingResource, CachedResourceRequest::DeferOption defer) const
 549 {
 550     if (!existingResource)
 551         return Load;
 552 
 553     // We already have a preload going for this URL.
 554     if (forPreload && existingResource->isPreloaded())
 555         return Use;
 556 
 557     // If the same URL has been loaded as a different type, we need to reload.
 558     if (existingResource->type() != type) {
 559         LOG(ResourceLoading, "CachedResourceLoader::determineRevalidationPolicy reloading due to type mismatch.");
 560         return Reload;
 561     }
 562 
 563     if (!existingResource->canReuse(request))
 564         return Reload;
 565 
 566     // Conditional requests should have failed canReuse check.
 567     ASSERT(!request.isConditional());
 568 
 569     // Do not load from cache if images are not enabled. The load for this image will be blocked
 570     // in CachedImage::load.
 571     if (CachedResourceRequest::DeferredByClient == defer)
 572         return Reload;
 573 
 574     // Don't reload resources while pasting.
 575     if (m_allowStaleResources)
 576         return Use;
 577 
 578     // Alwaus use preloads.
 579     if (existingResource->isPreloaded())
 580         return Use;
 581 
 582     // CachePolicyHistoryBuffer uses the cache no matter what.
 583     if (cachePolicy(type) == CachePolicyHistoryBuffer)
 584         return Use;
 585 
 586     // Don't reuse resources with Cache-control: no-store.
 587     if (existingResource->response().cacheControlContainsNoStore()) {
 588         LOG(ResourceLoading, "CachedResourceLoader::determineRevalidationPolicy reloading due to Cache-control: no-store.");
 589         return Reload;
 590     }
 591 
 592     // If credentials were sent with the previous request and won't be
 593     // with this one, or vice versa, re-fetch the resource.
 594     //
 595     // This helps with the case where the server sends back
 596     // "Access-Control-Allow-Origin: *" all the time, but some of the
 597     // client's requests are made without CORS and some with.
 598     if (existingResource->resourceRequest().allowCookies() != request.allowCookies()) {
 599         LOG(ResourceLoading, "CachedResourceLoader::determineRevalidationPolicy reloading due to difference in credentials settings.");
 600         return Reload;
 601     }
 602 
 603     // During the initial load, avoid loading the same resource multiple times for a single document, even if the cache policies would tell us to.
 604     if (document() && !document()->loadEventFinished() && m_validatedURLs.contains(existingResource->url()))
 605         return Use;
 606 
 607     // CachePolicyReload always reloads
 608     if (cachePolicy(type) == CachePolicyReload) {
 609         LOG(ResourceLoading, "CachedResourceLoader::determineRevalidationPolicy reloading due to CachePolicyReload.");
 610         return Reload;
 611     }
 612 
 613     // We'll try to reload the resource if it failed last time.
 614     if (existingResource->errorOccurred()) {
 615         LOG(ResourceLoading, "CachedResourceLoader::determineRevalidationPolicye reloading due to resource being in the error state");
 616         return Reload;
 617     }
 618 
 619     // For resources that are not yet loaded we ignore the cache policy.
 620     if (existingResource->isLoading())
 621         return Use;
 622 
 623     // Check if the cache headers requires us to revalidate (cache expiration for example).
 624     if (existingResource->mustRevalidateDueToCacheHeaders(cachePolicy(type))) {
 625         // See if the resource has usable ETag or Last-modified headers.
 626         if (existingResource->canUseCacheValidator())
 627             return Revalidate;
 628 
 629         // No, must reload.
 630         LOG(ResourceLoading, "CachedResourceLoader::determineRevalidationPolicy reloading due to missing cache validators.");
 631         return Reload;
 632     }
 633 
 634     return Use;
 635 }
 636 
 637 void CachedResourceLoader::printAccessDeniedMessage(const URL& url) const
 638 {
 639     if (url.isNull())
 640         return;
 641 
 642     if (!frame())
 643         return;
 644 
 645     String message;
 646     if (!m_document || m_document->url().isNull())
 647         message = "Unsafe attempt to load URL " + url.stringCenterEllipsizedToLength() + '.';
 648     else
 649         message = "Unsafe attempt to load URL " + url.stringCenterEllipsizedToLength() + " from frame with URL " + m_document->url().stringCenterEllipsizedToLength() + ". Domains, protocols and ports must match.\n";
 650 
 651     frame()->document()->addConsoleMessage(MessageSource::Security, MessageLevel::Error, message);
 652 }
 653 
 654 void CachedResourceLoader::setAutoLoadImages(bool enable)
 655 {
 656     if (enable == m_autoLoadImages)
 657         return;
 658 
 659     m_autoLoadImages = enable;
 660 
 661     if (!m_autoLoadImages)
 662         return;
 663 
 664     reloadImagesIfNotDeferred();
 665 }
 666 
 667 void CachedResourceLoader::setImagesEnabled(bool enable)
 668 {
 669     if (enable == m_imagesEnabled)
 670         return;
 671 
 672     m_imagesEnabled = enable;
 673 
 674     if (!m_imagesEnabled)
 675         return;
 676 
 677     reloadImagesIfNotDeferred();
 678 }
 679 
 680 bool CachedResourceLoader::clientDefersImage(const URL& url) const
 681 {
 682     return frame() && !frame()->loader().client().allowImage(m_imagesEnabled, url);
 683 }
 684 
 685 bool CachedResourceLoader::shouldDeferImageLoad(const URL& url) const
 686 {
 687     return clientDefersImage(url) || !m_autoLoadImages;
 688 }
 689 
 690 void CachedResourceLoader::reloadImagesIfNotDeferred()
 691 {
 692     DocumentResourceMap::iterator end = m_documentResources.end();
 693     for (DocumentResourceMap::iterator it = m_documentResources.begin(); it != end; ++it) {
 694         CachedResource* resource = it->value.get();
 695         if (resource->isImage() && resource->stillNeedsLoad() && !clientDefersImage(resource->url()))
 696             const_cast<CachedResource*>(resource)->load(this, defaultCachedResourceOptions());
 697     }
 698 }
 699 
 700 CachePolicy CachedResourceLoader::cachePolicy(CachedResource::Type type) const
 701 {
 702     if (!frame())
 703         return CachePolicyVerify;
 704 
 705     if (type != CachedResource::MainResource)
 706         return frame()->loader().subresourceCachePolicy();
 707 
 708     if (frame()->loader().loadType() == FrameLoadTypeReloadFromOrigin || frame()->loader().loadType() == FrameLoadTypeReload)
 709         return CachePolicyReload;
 710     return CachePolicyVerify;
 711 }
 712 
 713 void CachedResourceLoader::removeCachedResource(CachedResource* resource) const
 714 {
 715 #ifndef NDEBUG
 716     DocumentResourceMap::iterator it = m_documentResources.find(resource->url());
 717     if (it != m_documentResources.end())
 718         ASSERT(it->value.get() == resource);
 719 #endif
 720     m_documentResources.remove(resource->url());
 721 }
 722 
 723 void CachedResourceLoader::loadDone(CachedResource* resource, bool shouldPerformPostLoadActions)
 724 {
 725     RefPtr<DocumentLoader> protectDocumentLoader(m_documentLoader);
 726     RefPtr<Document> protectDocument(m_document);
 727 
 728 #if ENABLE(RESOURCE_TIMING)
 729     if (resource && resource->response().isHTTP() && ((!resource->errorOccurred() && !resource->wasCanceled()) || resource->response().httpStatusCode() == 304)) {
 730         HashMap<CachedResource*, InitiatorInfo>::iterator initiatorIt = m_initiatorMap.find(resource);
 731         if (initiatorIt != m_initiatorMap.end()) {
 732             ASSERT(document());
 733             Document* initiatorDocument = document();
 734             if (resource->type() == CachedResource::MainResource)
 735                 initiatorDocument = document()->parentDocument();
 736             ASSERT(initiatorDocument);
 737             const InitiatorInfo& info = initiatorIt->value;
 738             initiatorDocument->domWindow()->performance()->addResourceTiming(info.name, initiatorDocument, resource->resourceRequest(), resource->response(), info.startTime, resource->loadFinishTime());
 739             m_initiatorMap.remove(initiatorIt);
 740         }
 741     }
 742 #else
 743     UNUSED_PARAM(resource);
 744 #endif // ENABLE(RESOURCE_TIMING)
 745 
 746     if (frame())
 747         frame()->loader().loadDone();
 748 
 749     if (shouldPerformPostLoadActions)
 750         performPostLoadActions();
 751 
 752     if (!m_garbageCollectDocumentResourcesTimer.isActive())
 753         m_garbageCollectDocumentResourcesTimer.startOneShot(0);
 754 }
 755 
 756 // Garbage collecting m_documentResources is a workaround for the
 757 // CachedResourceHandles on the RHS being strong references. Ideally this
 758 // would be a weak map, however CachedResourceHandles perform additional
 759 // bookkeeping on CachedResources, so instead pseudo-GC them -- when the
 760 // reference count reaches 1, m_documentResources is the only reference, so
 761 // remove it from the map.
 762 void CachedResourceLoader::garbageCollectDocumentResourcesTimerFired(Timer<CachedResourceLoader>& timer)
 763 {
 764     ASSERT_UNUSED(timer, &timer == &m_garbageCollectDocumentResourcesTimer);
 765     garbageCollectDocumentResources();
 766 }
 767 
 768 void CachedResourceLoader::garbageCollectDocumentResources()
 769 {
 770     typedef Vector<String, 10> StringVector;
 771     StringVector resourcesToDelete;
 772 
 773     for (DocumentResourceMap::iterator it = m_documentResources.begin(); it != m_documentResources.end(); ++it) {
 774         if (it->value->hasOneHandle()) {
 775             resourcesToDelete.append(it->key);
 776             it->value->setOwningCachedResourceLoader(0);
 777         }
 778     }
 779 
 780     for (StringVector::const_iterator it = resourcesToDelete.begin(); it != resourcesToDelete.end(); ++it)
 781         m_documentResources.remove(*it);
 782 }
 783 
 784 void CachedResourceLoader::performPostLoadActions()
 785 {
 786     checkForPendingPreloads();
 787 
 788     // PostLoad might trigger async resource request which is not required for Canceled Main Document
 789     // caused by user cancel or stop
 790     if (m_documentLoader && !m_documentLoader->mainDocumentError().isNull() && m_documentLoader->mainDocumentError().isCancellation())
 791         return;
 792 
 793     platformStrategies()->loaderStrategy()->resourceLoadScheduler()->servePendingRequests();
 794 }
 795 
 796 void CachedResourceLoader::incrementRequestCount(const CachedResource* res)
 797 {
 798     if (res->ignoreForRequestCount())
 799         return;
 800 
 801     ++m_requestCount;
 802 }
 803 
 804 void CachedResourceLoader::decrementRequestCount(const CachedResource* res)
 805 {
 806     if (res->ignoreForRequestCount())
 807         return;
 808 
 809     --m_requestCount;
 810     ASSERT(m_requestCount > -1);
 811 }
 812 
 813 void CachedResourceLoader::preload(CachedResource::Type type, CachedResourceRequest& request, const String& charset)
 814 {
 815     // We always preload resources on iOS. See <https://bugs.webkit.org/show_bug.cgi?id=91276>.
 816     // FIXME: We should consider adding a setting to toggle aggressive preloading behavior as opposed
 817     // to making this behavior specific to iOS.
 818 #if !PLATFORM(IOS)
 819     bool hasRendering = m_document->body() && m_document->renderView();
 820     bool canBlockParser = type == CachedResource::Script || type == CachedResource::CSSStyleSheet;
 821     if (!hasRendering && !canBlockParser) {
 822         // Don't preload subresources that can't block the parser before we have something to draw.
 823         // This helps prevent preloads from delaying first display when bandwidth is limited.
 824         PendingPreload pendingPreload = { type, request, charset };
 825         m_pendingPreloads.append(pendingPreload);
 826         return;
 827     }
 828 #endif
 829     requestPreload(type, request, charset);
 830 }
 831 
 832 void CachedResourceLoader::checkForPendingPreloads()
 833 {
 834     if (m_pendingPreloads.isEmpty() || !m_document || !m_document->body() || !m_document->body()->renderer())
 835         return;
 836 #if PLATFORM(IOS)
 837     // We always preload resources on iOS. See <https://bugs.webkit.org/show_bug.cgi?id=91276>.
 838     // So, we should never have any pending preloads.
 839     // FIXME: We should look to avoid compiling this code entirely when building for iOS.
 840     ASSERT_NOT_REACHED();
 841 #endif
 842     while (!m_pendingPreloads.isEmpty()) {
 843         PendingPreload preload = m_pendingPreloads.takeFirst();
 844         // 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).
 845         if (!cachedResource(preload.m_request.resourceRequest().url()))
 846             requestPreload(preload.m_type, preload.m_request, preload.m_charset);
 847     }
 848     m_pendingPreloads.clear();
 849 }
 850 
 851 void CachedResourceLoader::requestPreload(CachedResource::Type type, CachedResourceRequest& request, const String& charset)
 852 {
 853     String encoding;
 854     if (type == CachedResource::Script || type == CachedResource::CSSStyleSheet)
 855         encoding = charset.isEmpty() ? m_document->charset() : charset;
 856 
 857     request.setCharset(encoding);
 858     request.setForPreload(true);
 859 
 860     CachedResourceHandle<CachedResource> resource = requestResource(type, request);
 861     if (!resource || (m_preloads && m_preloads->contains(resource.get())))
 862         return;
 863     resource->increasePreloadCount();
 864 
 865     if (!m_preloads)
 866         m_preloads = adoptPtr(new ListHashSet<CachedResource*>);
 867     m_preloads->add(resource.get());
 868 
 869 #if PRELOAD_DEBUG
 870     printf("PRELOADING %s\n",  resource->url().latin1().data());
 871 #endif
 872 }
 873 
 874 bool CachedResourceLoader::isPreloaded(const String& urlString) const
 875 {
 876     const URL& url = m_document->completeURL(urlString);
 877 
 878     if (m_preloads) {
 879         ListHashSet<CachedResource*>::iterator end = m_preloads->end();
 880         for (ListHashSet<CachedResource*>::iterator it = m_preloads->begin(); it != end; ++it) {
 881             CachedResource* resource = *it;
 882             if (resource->url() == url)
 883                 return true;
 884         }
 885     }
 886 
 887     Deque<PendingPreload>::const_iterator dequeEnd = m_pendingPreloads.end();
 888     for (Deque<PendingPreload>::const_iterator it = m_pendingPreloads.begin(); it != dequeEnd; ++it) {
 889         PendingPreload pendingPreload = *it;
 890         if (pendingPreload.m_request.resourceRequest().url() == url)
 891             return true;
 892     }
 893     return false;
 894 }
 895 
 896 void CachedResourceLoader::clearPreloads()
 897 {
 898 #if PRELOAD_DEBUG
 899     printPreloadStats();
 900 #endif
 901     if (!m_preloads)
 902         return;
 903 
 904     ListHashSet<CachedResource*>::iterator end = m_preloads->end();
 905     for (ListHashSet<CachedResource*>::iterator it = m_preloads->begin(); it != end; ++it) {
 906         CachedResource* res = *it;
 907         res->decreasePreloadCount();
 908         bool deleted = res->deleteIfPossible();
 909         if (!deleted && res->preloadResult() == CachedResource::PreloadNotReferenced)
 910             memoryCache()->remove(res);
 911     }
 912     m_preloads.clear();
 913 }
 914 
 915 void CachedResourceLoader::clearPendingPreloads()
 916 {
 917     m_pendingPreloads.clear();
 918 }
 919 
 920 #if PRELOAD_DEBUG
 921 void CachedResourceLoader::printPreloadStats()
 922 {
 923     unsigned scripts = 0;
 924     unsigned scriptMisses = 0;
 925     unsigned stylesheets = 0;
 926     unsigned stylesheetMisses = 0;
 927     unsigned images = 0;
 928     unsigned imageMisses = 0;
 929     ListHashSet<CachedResource*>::iterator end = m_preloads.end();
 930     for (ListHashSet<CachedResource*>::iterator it = m_preloads.begin(); it != end; ++it) {
 931         CachedResource* res = *it;
 932         if (res->preloadResult() == CachedResource::PreloadNotReferenced)
 933             printf("!! UNREFERENCED PRELOAD %s\n", res->url().latin1().data());
 934         else if (res->preloadResult() == CachedResource::PreloadReferencedWhileComplete)
 935             printf("HIT COMPLETE PRELOAD %s\n", res->url().latin1().data());
 936         else if (res->preloadResult() == CachedResource::PreloadReferencedWhileLoading)
 937             printf("HIT LOADING PRELOAD %s\n", res->url().latin1().data());
 938 
 939         if (res->type() == CachedResource::Script) {
 940             scripts++;
 941             if (res->preloadResult() < CachedResource::PreloadReferencedWhileLoading)
 942                 scriptMisses++;
 943         } else if (res->type() == CachedResource::CSSStyleSheet) {
 944             stylesheets++;
 945             if (res->preloadResult() < CachedResource::PreloadReferencedWhileLoading)
 946                 stylesheetMisses++;
 947         } else {
 948             images++;
 949             if (res->preloadResult() < CachedResource::PreloadReferencedWhileLoading)
 950                 imageMisses++;
 951         }
 952 
 953         if (res->errorOccurred())
 954             memoryCache()->remove(res);
 955 
 956         res->decreasePreloadCount();
 957     }
 958     m_preloads.clear();
 959 
 960     if (scripts)
 961         printf("SCRIPTS: %d (%d hits, hit rate %d%%)\n", scripts, scripts - scriptMisses, (scripts - scriptMisses) * 100 / scripts);
 962     if (stylesheets)
 963         printf("STYLESHEETS: %d (%d hits, hit rate %d%%)\n", stylesheets, stylesheets - stylesheetMisses, (stylesheets - stylesheetMisses) * 100 / stylesheets);
 964     if (images)
 965         printf("IMAGES:  %d (%d hits, hit rate %d%%)\n", images, images - imageMisses, (images - imageMisses) * 100 / images);
 966 }
 967 #endif
 968 
 969 const ResourceLoaderOptions& CachedResourceLoader::defaultCachedResourceOptions()
 970 {
 971     static ResourceLoaderOptions options(SendCallbacks, SniffContent, BufferData, AllowStoredCredentials, AskClientForAllCredentials, DoSecurityCheck, UseDefaultOriginRestrictionsForType);
 972     return options;
 973 }
 974 
 975 }