f13ae62a13ad0cd8fb1129a3be2ce2cb8ffa35a6
[WebKit.git] / Source / WebCore / loader / cache / CachedResource.cpp
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) 2006 Samuel Weinig (sam.weinig@gmail.com)
6     Copyright (C) 2004-2011, 2014 Apple Inc. All rights reserved.
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
24 #include "config.h"
25 #include "CachedResource.h"
26
27 #include "CachedResourceClient.h"
28 #include "CachedResourceClientWalker.h"
29 #include "CachedResourceHandle.h"
30 #include "CachedResourceLoader.h"
31 #include "CrossOriginAccessControl.h"
32 #include "DiagnosticLoggingClient.h"
33 #include "DiagnosticLoggingKeys.h"
34 #include "Document.h"
35 #include "DocumentLoader.h"
36 #include "FrameLoader.h"
37 #include "FrameLoaderClient.h"
38 #include "HTTPHeaderNames.h"
39 #include "InspectorInstrumentation.h"
40 #include "URL.h"
41 #include "LoaderStrategy.h"
42 #include "Logging.h"
43 #include "MainFrame.h"
44 #include "MemoryCache.h"
45 #include "Page.h"
46 #include "PlatformStrategies.h"
47 #include "ResourceHandle.h"
48 #include "SchemeRegistry.h"
49 #include "SecurityOrigin.h"
50 #include "SecurityPolicy.h"
51 #include "SubresourceLoader.h"
52 #include <wtf/CurrentTime.h>
53 #include <wtf/MathExtras.h>
54 #include <wtf/RefCountedLeakCounter.h>
55 #include <wtf/StdLibExtras.h>
56 #include <wtf/text/CString.h>
57 #include <wtf/Vector.h>
58
59 #if USE(QUICK_LOOK)
60 #include "QuickLook.h"
61 #endif
62
63 using namespace WTF;
64
65 namespace WebCore {
66
67 ResourceLoadPriority CachedResource::defaultPriorityForResourceType(Type type)
68 {
69     switch (type) {
70     case CachedResource::MainResource:
71         return ResourceLoadPriority::VeryHigh;
72     case CachedResource::CSSStyleSheet:
73         return ResourceLoadPriority::High;
74     case CachedResource::Script:
75 #if ENABLE(SVG_FONTS)
76     case CachedResource::SVGFontResource:
77 #endif
78     case CachedResource::MediaResource:
79     case CachedResource::FontResource:
80     case CachedResource::RawResource:
81         return ResourceLoadPriority::Medium;
82     case CachedResource::ImageResource:
83         return ResourceLoadPriority::Low;
84 #if ENABLE(XSLT)
85     case CachedResource::XSLStyleSheet:
86         return ResourceLoadPriority::High;
87 #endif
88     case CachedResource::SVGDocumentResource:
89         return ResourceLoadPriority::Low;
90 #if ENABLE(LINK_PREFETCH)
91     case CachedResource::LinkPrefetch:
92         return ResourceLoadPriority::VeryLow;
93     case CachedResource::LinkSubresource:
94         return ResourceLoadPriority::VeryLow;
95 #endif
96 #if ENABLE(VIDEO_TRACK)
97     case CachedResource::TextTrackResource:
98         return ResourceLoadPriority::Low;
99 #endif
100     }
101     ASSERT_NOT_REACHED();
102     return ResourceLoadPriority::Low;
103 }
104
105 static std::chrono::milliseconds deadDecodedDataDeletionIntervalForResourceType(CachedResource::Type type)
106 {
107     if (type == CachedResource::Script)
108         return std::chrono::milliseconds { 0 };
109
110     return MemoryCache::singleton().deadDecodedDataDeletionInterval();
111 }
112
113 DEFINE_DEBUG_ONLY_GLOBAL(RefCountedLeakCounter, cachedResourceLeakCounter, ("CachedResource"));
114
115 CachedResource::CachedResource(const ResourceRequest& request, Type type, SessionID sessionID)
116     : m_resourceRequest(request)
117     , m_decodedDataDeletionTimer(*this, &CachedResource::destroyDecodedData, deadDecodedDataDeletionIntervalForResourceType(type))
118     , m_sessionID(sessionID)
119     , m_loadPriority(defaultPriorityForResourceType(type))
120     , m_responseTimestamp(std::chrono::system_clock::now())
121     , m_lastDecodedAccessTime(0)
122     , m_loadFinishTime(0)
123     , m_encodedSize(0)
124     , m_decodedSize(0)
125     , m_accessCount(0)
126     , m_handleCount(0)
127     , m_preloadCount(0)
128     , m_preloadResult(PreloadNotReferenced)
129     , m_requestedFromNetworkingLayer(false)
130     , m_inCache(false)
131     , m_loading(false)
132     , m_switchingClientsToRevalidatedResource(false)
133     , m_type(type)
134     , m_status(Pending)
135 #ifndef NDEBUG
136     , m_deleted(false)
137     , m_lruIndex(0)
138 #endif
139     , m_owningCachedResourceLoader(nullptr)
140     , m_resourceToRevalidate(nullptr)
141     , m_proxyResource(nullptr)
142 {
143     ASSERT(m_type == unsigned(type)); // m_type is a bitfield, so this tests careless updates of the enum.
144     ASSERT(sessionID.isValid());
145 #ifndef NDEBUG
146     cachedResourceLeakCounter.increment();
147 #endif
148
149     if (!m_resourceRequest.url().hasFragmentIdentifier())
150         return;
151     URL urlForCache = MemoryCache::removeFragmentIdentifierIfNeeded(m_resourceRequest.url());
152     if (urlForCache.hasFragmentIdentifier())
153         return;
154     m_fragmentIdentifierForRequest = m_resourceRequest.url().fragmentIdentifier();
155     m_resourceRequest.setURL(urlForCache);
156 }
157
158 CachedResource::~CachedResource()
159 {
160     ASSERT(!m_resourceToRevalidate); // Should be true because canDelete() checks this.
161     ASSERT(canDelete());
162     ASSERT(!inCache());
163     ASSERT(!m_deleted);
164     ASSERT(url().isNull() || !allowsCaching() || MemoryCache::singleton().resourceForRequest(resourceRequest(), sessionID()) != this);
165
166 #ifndef NDEBUG
167     m_deleted = true;
168     cachedResourceLeakCounter.decrement();
169 #endif
170
171     if (m_owningCachedResourceLoader)
172         m_owningCachedResourceLoader->removeCachedResource(*this);
173 }
174
175 void CachedResource::failBeforeStarting()
176 {
177     // FIXME: What if resources in other frames were waiting for this revalidation?
178     LOG(ResourceLoading, "Cannot start loading '%s'", url().string().latin1().data());
179     if (allowsCaching() && m_resourceToRevalidate)
180         MemoryCache::singleton().revalidationFailed(*this);
181     error(CachedResource::LoadError);
182 }
183
184 static void addAdditionalRequestHeadersToRequest(ResourceRequest& request, const CachedResourceLoader& cachedResourceLoader, CachedResource& resource)
185 {
186     if (resource.type() == CachedResource::MainResource)
187         return;
188     // In some cases we may try to load resources in frameless documents. Such loads always fail.
189     // FIXME: We shouldn't get this far.
190     if (!cachedResourceLoader.frame())
191         return;
192
193     // Note: We skip the Content-Security-Policy check here because we check
194     // the Content-Security-Policy at the CachedResourceLoader layer so we can
195     // handle different resource types differently.
196     FrameLoader& frameLoader = cachedResourceLoader.frame()->loader();
197     String outgoingReferrer;
198     String outgoingOrigin;
199     if (request.httpReferrer().isNull()) {
200         outgoingReferrer = frameLoader.outgoingReferrer();
201         outgoingOrigin = frameLoader.outgoingOrigin();
202     } else {
203         outgoingReferrer = request.httpReferrer();
204         outgoingOrigin = SecurityOrigin::createFromString(outgoingReferrer)->toString();
205     }
206
207     // FIXME: Refactor SecurityPolicy::generateReferrerHeader to align with new terminology used in https://w3c.github.io/webappsec-referrer-policy.
208     switch (resource.options().referrerPolicy) {
209     case FetchOptions::ReferrerPolicy::EmptyString: {
210         ReferrerPolicy referrerPolicy = cachedResourceLoader.document() ? cachedResourceLoader.document()->referrerPolicy() : ReferrerPolicy::Default;
211         outgoingReferrer = SecurityPolicy::generateReferrerHeader(referrerPolicy, request.url(), outgoingReferrer);
212         break; }
213     case FetchOptions::ReferrerPolicy::NoReferrerWhenDowngrade:
214         outgoingReferrer = SecurityPolicy::generateReferrerHeader(ReferrerPolicy::Default, request.url(), outgoingReferrer);
215         break;
216     case FetchOptions::ReferrerPolicy::NoReferrer:
217         outgoingReferrer = String();
218         break;
219     case FetchOptions::ReferrerPolicy::Origin:
220         outgoingReferrer = SecurityPolicy::generateReferrerHeader(ReferrerPolicy::Origin, request.url(), outgoingReferrer);
221         break;
222     case FetchOptions::ReferrerPolicy::OriginWhenCrossOrigin:
223         if (resource.isCrossOrigin())
224             outgoingReferrer = SecurityPolicy::generateReferrerHeader(ReferrerPolicy::Origin, request.url(), outgoingReferrer);
225         break;
226     case FetchOptions::ReferrerPolicy::UnsafeUrl:
227         break;
228     };
229
230     if (outgoingReferrer.isEmpty())
231         request.clearHTTPReferrer();
232     else
233         request.setHTTPReferrer(outgoingReferrer);
234     FrameLoader::addHTTPOriginIfNeeded(request, outgoingOrigin);
235
236     frameLoader.addExtraFieldsToSubresourceRequest(request);
237 }
238
239 void CachedResource::addAdditionalRequestHeaders(CachedResourceLoader& loader)
240 {
241     addAdditionalRequestHeadersToRequest(m_resourceRequest, loader, *this);
242 }
243
244 void CachedResource::computeOrigin(CachedResourceLoader& loader)
245 {
246     if (type() == MainResource)
247         return;
248
249     ASSERT(loader.document());
250     if (m_resourceRequest.hasHTTPOrigin())
251         m_origin = SecurityOrigin::createFromString(m_resourceRequest.httpOrigin());
252     else
253         m_origin = loader.document()->securityOrigin();
254     ASSERT(m_origin);
255
256     if (!(m_resourceRequest.url().protocolIsData() && m_options.sameOriginDataURLFlag == SameOriginDataURLFlag::Set) && !m_origin->canRequest(m_resourceRequest.url()))
257         setCrossOrigin();
258
259     addAdditionalRequestHeaders(loader);
260 }
261
262 void CachedResource::load(CachedResourceLoader& cachedResourceLoader, const ResourceLoaderOptions& options)
263 {
264     if (!cachedResourceLoader.frame()) {
265         failBeforeStarting();
266         return;
267     }
268     Frame& frame = *cachedResourceLoader.frame();
269
270     // Prevent new loads if we are in the PageCache or being added to the PageCache.
271     if (frame.page() && frame.page()->inPageCache()) {
272         failBeforeStarting();
273         return;
274     }
275
276     FrameLoader& frameLoader = frame.loader();
277     if (options.securityCheck == DoSecurityCheck && (frameLoader.state() == FrameStateProvisional || !frameLoader.activeDocumentLoader() || frameLoader.activeDocumentLoader()->isStopping())) {
278         failBeforeStarting();
279         return;
280     }
281
282     m_options = options;
283     m_loading = true;
284
285 #if USE(QUICK_LOOK)
286     if (!m_resourceRequest.isNull() && m_resourceRequest.url().protocolIs(QLPreviewProtocol())) {
287         // When QuickLook is invoked to convert a document, it returns a unique URL in the
288         // NSURLReponse for the main document. To make safeQLURLForDocumentURLAndResourceURL()
289         // work, we need to use the QL URL not the original URL.
290         const URL& documentURL = frameLoader.documentLoader()->response().url();
291         m_resourceRequest.setURL(safeQLURLForDocumentURLAndResourceURL(documentURL, url()));
292     }
293 #endif
294
295     if (!accept().isEmpty())
296         m_resourceRequest.setHTTPAccept(accept());
297
298     if (isCacheValidator()) {
299         CachedResource* resourceToRevalidate = m_resourceToRevalidate;
300         ASSERT(resourceToRevalidate->canUseCacheValidator());
301         ASSERT(resourceToRevalidate->isLoaded());
302         const String& lastModified = resourceToRevalidate->response().httpHeaderField(HTTPHeaderName::LastModified);
303         const String& eTag = resourceToRevalidate->response().httpHeaderField(HTTPHeaderName::ETag);
304         if (!lastModified.isEmpty() || !eTag.isEmpty()) {
305             ASSERT(cachedResourceLoader.cachePolicy(type()) != CachePolicyReload);
306             if (cachedResourceLoader.cachePolicy(type()) == CachePolicyRevalidate)
307                 m_resourceRequest.setHTTPHeaderField(HTTPHeaderName::CacheControl, "max-age=0");
308             if (!lastModified.isEmpty())
309                 m_resourceRequest.setHTTPHeaderField(HTTPHeaderName::IfModifiedSince, lastModified);
310             if (!eTag.isEmpty())
311                 m_resourceRequest.setHTTPHeaderField(HTTPHeaderName::IfNoneMatch, eTag);
312         }
313     }
314
315 #if ENABLE(LINK_PREFETCH)
316     if (type() == CachedResource::LinkPrefetch || type() == CachedResource::LinkSubresource)
317         m_resourceRequest.setHTTPHeaderField(HTTPHeaderName::Purpose, "prefetch");
318 #endif
319     m_resourceRequest.setPriority(loadPriority());
320
321     computeOrigin(cachedResourceLoader);
322
323     // FIXME: It's unfortunate that the cache layer and below get to know anything about fragment identifiers.
324     // We should look into removing the expectation of that knowledge from the platform network stacks.
325     ResourceRequest request(m_resourceRequest);
326     if (!m_fragmentIdentifierForRequest.isNull()) {
327         URL url = request.url();
328         url.setFragmentIdentifier(m_fragmentIdentifierForRequest);
329         request.setURL(url);
330         m_fragmentIdentifierForRequest = String();
331     }
332
333     m_loader = platformStrategies()->loaderStrategy()->loadResource(frame, *this, request, options);
334     if (!m_loader) {
335         failBeforeStarting();
336         return;
337     }
338
339     m_status = Pending;
340 }
341
342 void CachedResource::loadFrom(const CachedResource& resource, const ResourceLoaderOptions& options, CachedResourceLoader& cachedResourceLoader)
343 {
344     ASSERT(url() == resource.url());
345     ASSERT(type() == resource.type());
346     ASSERT(resource.status() == Status::Cached);
347
348     m_options = options;
349     computeOrigin(cachedResourceLoader);
350
351     if (isCrossOrigin() && options.mode == FetchOptions::Mode::Cors) {
352         ASSERT(m_origin);
353         String errorMessage;
354         if (!WebCore::passesAccessControlCheck(resource.response(), m_options.allowCredentials, *m_origin, errorMessage)) {
355             setResourceError(ResourceError(String(), 0, url(), errorMessage, ResourceError::Type::AccessControl));
356             return;
357         }
358     }
359
360     setBodyDataFrom(resource);
361 }
362
363 void CachedResource::checkNotify()
364 {
365     if (isLoading() || stillNeedsLoad())
366         return;
367
368     CachedResourceClientWalker<CachedResourceClient> walker(m_clients);
369     while (CachedResourceClient* client = walker.next())
370         client->notifyFinished(this);
371 }
372
373 void CachedResource::addDataBuffer(SharedBuffer&)
374 {
375     ASSERT(dataBufferingPolicy() == BufferData);
376 }
377
378 void CachedResource::addData(const char*, unsigned)
379 {
380     ASSERT(dataBufferingPolicy() == DoNotBufferData);
381 }
382
383 void CachedResource::finishLoading(SharedBuffer*)
384 {
385     setLoading(false);
386     checkNotify();
387 }
388
389 void CachedResource::error(CachedResource::Status status)
390 {
391     setStatus(status);
392     ASSERT(errorOccurred());
393     m_data = nullptr;
394
395     setLoading(false);
396     checkNotify();
397 }
398     
399 void CachedResource::cancelLoad()
400 {
401     if (!isLoading() && !stillNeedsLoad())
402         return;
403
404     setStatus(LoadError);
405     setLoading(false);
406     checkNotify();
407 }
408
409 void CachedResource::finish()
410 {
411     if (!errorOccurred())
412         m_status = Cached;
413 }
414
415 bool CachedResource::passesAccessControlCheck(SecurityOrigin& securityOrigin)
416 {
417     String errorDescription;
418     return WebCore::passesAccessControlCheck(response(), resourceRequest().allowCookies() ? AllowStoredCredentials : DoNotAllowStoredCredentials, securityOrigin, errorDescription);
419 }
420
421 bool CachedResource::passesSameOriginPolicyCheck(SecurityOrigin& securityOrigin)
422 {
423     if (securityOrigin.canRequest(responseForSameOriginPolicyChecks().url()))
424         return true;
425     return passesAccessControlCheck(securityOrigin);
426 }
427
428 void CachedResource::setCrossOrigin()
429 {
430     ASSERT(m_options.mode != FetchOptions::Mode::SameOrigin);
431     m_responseTainting = (m_options.mode == FetchOptions::Mode::Cors) ? ResourceResponse::Tainting::Cors : ResourceResponse::Tainting::Opaque;
432 }
433
434 bool CachedResource::isCrossOrigin() const
435 {
436     return m_responseTainting != ResourceResponse::Tainting::Basic;
437 }
438
439 bool CachedResource::isClean() const
440 {
441     // https://html.spec.whatwg.org/multipage/infrastructure.html#cors-same-origin
442     return !loadFailedOrCanceled() && m_responseTainting != ResourceResponse::Tainting::Opaque;
443 }
444
445 bool CachedResource::isExpired() const
446 {
447     if (m_response.isNull())
448         return false;
449
450     return computeCurrentAge(m_response, m_responseTimestamp) > freshnessLifetime(m_response);
451 }
452
453 static inline bool shouldCacheSchemeIndefinitely(const String& scheme)
454 {
455 #if PLATFORM(COCOA)
456     if (equalLettersIgnoringASCIICase(scheme, "applewebdata"))
457         return true;
458 #endif
459 #if USE(SOUP)
460     if (equalLettersIgnoringASCIICase(scheme, "resource"))
461         return true;
462 #endif
463     return equalLettersIgnoringASCIICase(scheme, "data");
464 }
465
466 std::chrono::microseconds CachedResource::freshnessLifetime(const ResourceResponse& response) const
467 {
468     if (!response.url().protocolIsInHTTPFamily()) {
469         String protocol = response.url().protocol();
470         if (!shouldCacheSchemeIndefinitely(protocol)) {
471             // Don't cache non-HTTP main resources since we can't check for freshness.
472             // FIXME: We should not cache subresources either, but when we tried this
473             // it caused performance and flakiness issues in our test infrastructure.
474             if (m_type == MainResource || SchemeRegistry::shouldAlwaysRevalidateURLScheme(protocol))
475                 return 0us;
476         }
477
478         return std::chrono::microseconds::max();
479     }
480
481     return computeFreshnessLifetimeForHTTPFamily(response, m_responseTimestamp);
482 }
483
484 void CachedResource::redirectReceived(ResourceRequest& request, const ResourceResponse& response)
485 {
486     m_requestedFromNetworkingLayer = true;
487     if (response.isNull())
488         return;
489
490     // Redirect to data: URL uses the last HTTP response for SOP.
491     if (response.isHTTP() && request.url().protocolIsData())
492         m_redirectResponseForSameOriginPolicyChecks = response;
493
494     updateRedirectChainStatus(m_redirectChainCacheStatus, response);
495 }
496
497 const ResourceResponse& CachedResource::responseForSameOriginPolicyChecks() const
498 {
499     return m_redirectResponseForSameOriginPolicyChecks.isNull() ? m_response : m_redirectResponseForSameOriginPolicyChecks;
500 }
501
502 void CachedResource::setResponse(const ResourceResponse& response)
503 {
504     ASSERT(m_response.type() == ResourceResponse::Type::Default);
505     m_response = response;
506     m_response.setRedirected(m_redirectChainCacheStatus.status != RedirectChainCacheStatus::NoRedirection);
507
508     m_varyingHeaderValues = collectVaryingRequestHeaders(m_resourceRequest, m_response, m_sessionID);
509 }
510
511 void CachedResource::responseReceived(const ResourceResponse& response)
512 {
513     setResponse(response);
514     m_responseTimestamp = std::chrono::system_clock::now();
515     String encoding = response.textEncodingName();
516     if (!encoding.isNull())
517         setEncoding(encoding);
518 }
519
520 void CachedResource::clearLoader()
521 {
522     ASSERT(m_loader);
523     m_identifierForLoadWithoutResourceLoader = m_loader->identifier();
524     m_loader = nullptr;
525     deleteIfPossible();
526 }
527
528 void CachedResource::addClient(CachedResourceClient* client)
529 {
530     if (addClientToSet(client))
531         didAddClient(client);
532 }
533
534 void CachedResource::didAddClient(CachedResourceClient* client)
535 {
536     if (m_decodedDataDeletionTimer.isActive())
537         m_decodedDataDeletionTimer.stop();
538
539     if (m_clientsAwaitingCallback.remove(client))
540         m_clients.add(client);
541     if (!isLoading() && !stillNeedsLoad())
542         client->notifyFinished(this);
543 }
544
545 bool CachedResource::addClientToSet(CachedResourceClient* client)
546 {
547     if (m_preloadResult == PreloadNotReferenced) {
548         if (isLoaded())
549             m_preloadResult = PreloadReferencedWhileComplete;
550         else if (m_requestedFromNetworkingLayer)
551             m_preloadResult = PreloadReferencedWhileLoading;
552         else
553             m_preloadResult = PreloadReferenced;
554     }
555     if (allowsCaching() && !hasClients() && inCache())
556         MemoryCache::singleton().addToLiveResourcesSize(*this);
557
558     if ((m_type == RawResource || m_type == MainResource) && !m_response.isNull() && !m_proxyResource) {
559         // Certain resources (especially XHRs and main resources) do crazy things if an asynchronous load returns
560         // synchronously (e.g., scripts may not have set all the state they need to handle the load).
561         // Therefore, rather than immediately sending callbacks on a cache hit like other CachedResources,
562         // we schedule the callbacks and ensure we never finish synchronously.
563         ASSERT(!m_clientsAwaitingCallback.contains(client));
564         m_clientsAwaitingCallback.add(client, std::make_unique<Callback>(*this, *client));
565         return false;
566     }
567
568     m_clients.add(client);
569     return true;
570 }
571
572 void CachedResource::removeClient(CachedResourceClient* client)
573 {
574     auto callback = m_clientsAwaitingCallback.take(client);
575     if (callback) {
576         ASSERT(!m_clients.contains(client));
577         callback->cancel();
578         callback = nullptr;
579     } else {
580         ASSERT(m_clients.contains(client));
581         m_clients.remove(client);
582         didRemoveClient(client);
583     }
584
585     if (deleteIfPossible()) {
586         // `this` object is dead here.
587         return;
588     }
589
590     if (hasClients())
591         return;
592
593     auto& memoryCache = MemoryCache::singleton();
594     if (allowsCaching() && inCache()) {
595         memoryCache.removeFromLiveResourcesSize(*this);
596         memoryCache.removeFromLiveDecodedResourcesList(*this);
597     }
598     if (!m_switchingClientsToRevalidatedResource)
599         allClientsRemoved();
600     destroyDecodedDataIfNeeded();
601
602     if (!allowsCaching())
603         return;
604
605     if (response().cacheControlContainsNoStore() && url().protocolIs("https")) {
606         // RFC2616 14.9.2:
607         // "no-store: ... MUST make a best-effort attempt to remove the information from volatile storage as promptly as possible"
608         // "... History buffers MAY store such responses as part of their normal operation."
609         // We allow non-secure content to be reused in history, but we do not allow secure content to be reused.
610         memoryCache.remove(*this);
611     }
612     memoryCache.pruneSoon();
613 }
614
615 void CachedResource::destroyDecodedDataIfNeeded()
616 {
617     if (!m_decodedSize)
618         return;
619     if (!MemoryCache::singleton().deadDecodedDataDeletionInterval().count())
620         return;
621     m_decodedDataDeletionTimer.restart();
622 }
623
624 void CachedResource::decodedDataDeletionTimerFired()
625 {
626     destroyDecodedData();
627 }
628
629 bool CachedResource::deleteIfPossible()
630 {
631     if (canDelete()) {
632         if (!inCache()) {
633             InspectorInstrumentation::willDestroyCachedResource(*this);
634             delete this;
635             return true;
636         }
637         if (m_data)
638             m_data->hintMemoryNotNeededSoon();
639     }
640     return false;
641 }
642
643 void CachedResource::setDecodedSize(unsigned size)
644 {
645     if (size == m_decodedSize)
646         return;
647
648     int delta = size - m_decodedSize;
649
650     // The object must be moved to a different queue, since its size has been changed.
651     // Remove before updating m_decodedSize, so we find the resource in the correct LRU list.
652     if (allowsCaching() && inCache())
653         MemoryCache::singleton().removeFromLRUList(*this);
654     
655     m_decodedSize = size;
656    
657     if (allowsCaching() && inCache()) {
658         auto& memoryCache = MemoryCache::singleton();
659         // Now insert into the new LRU list.
660         memoryCache.insertInLRUList(*this);
661         
662         // Insert into or remove from the live decoded list if necessary.
663         // When inserting into the LiveDecodedResourcesList it is possible
664         // that the m_lastDecodedAccessTime is still zero or smaller than
665         // the m_lastDecodedAccessTime of the current list head. This is a
666         // violation of the invariant that the list is to be kept sorted
667         // by access time. The weakening of the invariant does not pose
668         // a problem. For more details please see: https://bugs.webkit.org/show_bug.cgi?id=30209
669         bool inLiveDecodedResourcesList = memoryCache.inLiveDecodedResourcesList(*this);
670         if (m_decodedSize && !inLiveDecodedResourcesList && hasClients())
671             memoryCache.insertInLiveDecodedResourcesList(*this);
672         else if (!m_decodedSize && inLiveDecodedResourcesList)
673             memoryCache.removeFromLiveDecodedResourcesList(*this);
674
675         // Update the cache's size totals.
676         memoryCache.adjustSize(hasClients(), delta);
677     }
678 }
679
680 void CachedResource::setEncodedSize(unsigned size)
681 {
682     if (size == m_encodedSize)
683         return;
684
685     int delta = size - m_encodedSize;
686
687     // The object must be moved to a different queue, since its size has been changed.
688     // Remove before updating m_encodedSize, so we find the resource in the correct LRU list.
689     if (allowsCaching() && inCache())
690         MemoryCache::singleton().removeFromLRUList(*this);
691
692     m_encodedSize = size;
693
694     if (allowsCaching() && inCache()) {
695         auto& memoryCache = MemoryCache::singleton();
696         memoryCache.insertInLRUList(*this);
697         memoryCache.adjustSize(hasClients(), delta);
698     }
699 }
700
701 void CachedResource::didAccessDecodedData(double timeStamp)
702 {
703     m_lastDecodedAccessTime = timeStamp;
704     
705     if (allowsCaching() && inCache()) {
706         auto& memoryCache = MemoryCache::singleton();
707         if (memoryCache.inLiveDecodedResourcesList(*this)) {
708             memoryCache.removeFromLiveDecodedResourcesList(*this);
709             memoryCache.insertInLiveDecodedResourcesList(*this);
710         }
711         memoryCache.pruneSoon();
712     }
713 }
714     
715 void CachedResource::setResourceToRevalidate(CachedResource* resource) 
716
717     ASSERT(resource);
718     ASSERT(!m_resourceToRevalidate);
719     ASSERT(resource != this);
720     ASSERT(m_handlesToRevalidate.isEmpty());
721     ASSERT(resource->type() == type());
722     ASSERT(!resource->m_proxyResource);
723
724     LOG(ResourceLoading, "CachedResource %p setResourceToRevalidate %p", this, resource);
725
726     resource->m_proxyResource = this;
727     m_resourceToRevalidate = resource;
728 }
729
730 void CachedResource::clearResourceToRevalidate() 
731 {
732     ASSERT(m_resourceToRevalidate);
733     ASSERT(m_resourceToRevalidate->m_proxyResource == this);
734
735     if (m_switchingClientsToRevalidatedResource)
736         return;
737
738     m_resourceToRevalidate->m_proxyResource = nullptr;
739     m_resourceToRevalidate->deleteIfPossible();
740
741     m_handlesToRevalidate.clear();
742     m_resourceToRevalidate = nullptr;
743     deleteIfPossible();
744 }
745     
746 void CachedResource::switchClientsToRevalidatedResource()
747 {
748     ASSERT(m_resourceToRevalidate);
749     ASSERT(m_resourceToRevalidate->inCache());
750     ASSERT(!inCache());
751
752     LOG(ResourceLoading, "CachedResource %p switchClientsToRevalidatedResource %p", this, m_resourceToRevalidate);
753
754     m_switchingClientsToRevalidatedResource = true;
755     for (auto& handle : m_handlesToRevalidate) {
756         handle->m_resource = m_resourceToRevalidate;
757         m_resourceToRevalidate->registerHandle(handle);
758         --m_handleCount;
759     }
760     ASSERT(!m_handleCount);
761     m_handlesToRevalidate.clear();
762
763     Vector<CachedResourceClient*> clientsToMove;
764     for (auto& entry : m_clients) {
765         CachedResourceClient* client = entry.key;
766         unsigned count = entry.value;
767         while (count) {
768             clientsToMove.append(client);
769             --count;
770         }
771     }
772
773     for (auto& client : clientsToMove)
774         removeClient(client);
775     ASSERT(m_clients.isEmpty());
776
777     for (auto& client : clientsToMove)
778         m_resourceToRevalidate->addClientToSet(client);
779     for (auto& client : clientsToMove) {
780         // Calling didAddClient may do anything, including trying to cancel revalidation.
781         // Assert that it didn't succeed.
782         ASSERT(m_resourceToRevalidate);
783         // Calling didAddClient for a client may end up removing another client. In that case it won't be in the set anymore.
784         if (m_resourceToRevalidate->m_clients.contains(client))
785             m_resourceToRevalidate->didAddClient(client);
786     }
787     m_switchingClientsToRevalidatedResource = false;
788 }
789
790 void CachedResource::updateResponseAfterRevalidation(const ResourceResponse& validatingResponse)
791 {
792     m_responseTimestamp = std::chrono::system_clock::now();
793
794     updateResponseHeadersAfterRevalidation(m_response, validatingResponse);
795 }
796
797 void CachedResource::registerHandle(CachedResourceHandleBase* h)
798 {
799     ++m_handleCount;
800     if (m_resourceToRevalidate)
801         m_handlesToRevalidate.add(h);
802 }
803
804 void CachedResource::unregisterHandle(CachedResourceHandleBase* h)
805 {
806     ASSERT(m_handleCount > 0);
807     --m_handleCount;
808
809     if (m_resourceToRevalidate)
810          m_handlesToRevalidate.remove(h);
811
812     if (!m_handleCount)
813         deleteIfPossible();
814 }
815
816 bool CachedResource::canUseCacheValidator() const
817 {
818     if (m_loading || errorOccurred())
819         return false;
820
821     if (m_response.cacheControlContainsNoStore())
822         return false;
823     return m_response.hasCacheValidatorFields();
824 }
825
826 CachedResource::RevalidationDecision CachedResource::makeRevalidationDecision(CachePolicy cachePolicy) const
827 {    
828     switch (cachePolicy) {
829     case CachePolicyHistoryBuffer:
830         return RevalidationDecision::No;
831
832     case CachePolicyReload:
833     case CachePolicyRevalidate:
834         return RevalidationDecision::YesDueToCachePolicy;
835
836     case CachePolicyVerify:
837         if (m_response.cacheControlContainsNoCache())
838             return RevalidationDecision::YesDueToNoCache;
839         // FIXME: Cache-Control:no-store should prevent storing, not reuse.
840         if (m_response.cacheControlContainsNoStore())
841             return RevalidationDecision::YesDueToNoStore;
842
843         if (isExpired())
844             return RevalidationDecision::YesDueToExpired;
845
846         return RevalidationDecision::No;
847     };
848     ASSERT_NOT_REACHED();
849     return RevalidationDecision::No;
850 }
851
852 bool CachedResource::redirectChainAllowsReuse(ReuseExpiredRedirectionOrNot reuseExpiredRedirection) const
853 {
854     return WebCore::redirectChainAllowsReuse(m_redirectChainCacheStatus, reuseExpiredRedirection);
855 }
856
857 bool CachedResource::varyHeaderValuesMatch(const ResourceRequest& request, const CachedResourceLoader& cachedResourceLoader)
858 {
859     if (m_varyingHeaderValues.isEmpty())
860         return true;
861
862     ResourceRequest requestWithFullHeaders(request);
863     addAdditionalRequestHeadersToRequest(requestWithFullHeaders, cachedResourceLoader, *this);
864
865     return verifyVaryingRequestHeaders(m_varyingHeaderValues, requestWithFullHeaders, m_sessionID);
866 }
867
868 unsigned CachedResource::overheadSize() const
869 {
870     static const int kAverageClientsHashMapSize = 384;
871     return sizeof(CachedResource) + m_response.memoryUsage() + kAverageClientsHashMapSize + m_resourceRequest.url().string().length() * 2;
872 }
873
874 bool CachedResource::areAllClientsXMLHttpRequests() const
875 {
876     if (type() != RawResource)
877         return false;
878
879     for (auto& client : m_clients) {
880         if (!client.key->isXMLHttpRequest())
881             return false;
882     }
883     return true;
884 }
885
886 void CachedResource::setLoadPriority(const Optional<ResourceLoadPriority>& loadPriority)
887 {
888     if (loadPriority)
889         m_loadPriority = loadPriority.value();
890     else
891         m_loadPriority = defaultPriorityForResourceType(type());
892 }
893
894 inline CachedResource::Callback::Callback(CachedResource& resource, CachedResourceClient& client)
895     : m_resource(resource)
896     , m_client(client)
897     , m_timer(*this, &Callback::timerFired)
898 {
899     m_timer.startOneShot(0);
900 }
901
902 inline void CachedResource::Callback::cancel()
903 {
904     if (m_timer.isActive())
905         m_timer.stop();
906 }
907
908 void CachedResource::Callback::timerFired()
909 {
910     m_resource.didAddClient(&m_client);
911 }
912
913 #if USE(FOUNDATION) || USE(SOUP)
914
915 void CachedResource::tryReplaceEncodedData(SharedBuffer& newBuffer)
916 {
917     if (!m_data)
918         return;
919     
920     if (!mayTryReplaceEncodedData())
921         return;
922
923     // We have to do the memcmp because we can't tell if the replacement file backed data is for the
924     // same resource or if we made a second request with the same URL which gave us a different
925     // resource. We have seen this happen for cached POST resources.
926     if (m_data->size() != newBuffer.size() || memcmp(m_data->data(), newBuffer.data(), m_data->size()))
927         return;
928
929     if (m_data->tryReplaceContentsWithPlatformBuffer(newBuffer))
930         didReplaceSharedBufferContents();
931 }
932
933 #endif
934
935 }