59390394bf3f8a46d451711ac77321c589e56f8e
[WebKit-https.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 "PlatformStrategies.h"
46 #include "ResourceHandle.h"
47 #include "SchemeRegistry.h"
48 #include "SecurityOrigin.h"
49 #include "SubresourceLoader.h"
50 #include <wtf/CurrentTime.h>
51 #include <wtf/MathExtras.h>
52 #include <wtf/RefCountedLeakCounter.h>
53 #include <wtf/StdLibExtras.h>
54 #include <wtf/text/CString.h>
55 #include <wtf/Vector.h>
56
57 #if USE(QUICK_LOOK)
58 #include "QuickLook.h"
59 #endif
60
61 using namespace WTF;
62
63 #define RELEASE_LOG_IF_ALLOWED(fmt, ...) RELEASE_LOG_IF(cachedResourceLoader.isAlwaysOnLoggingAllowed(), Network, "%p - CachedResource::" fmt, this, ##__VA_ARGS__)
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     case CachedResource::Script:
74         return ResourceLoadPriority::High;
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(CachedResourceRequest&& request, Type type, SessionID sessionID)
116     : m_resourceRequest(request.releaseResourceRequest())
117     , m_options(request.options())
118     , m_decodedDataDeletionTimer(*this, &CachedResource::destroyDecodedData, deadDecodedDataDeletionIntervalForResourceType(type))
119     , m_sessionID(sessionID)
120     , m_loadPriority(defaultPriorityForResourceType(type))
121     , m_responseTimestamp(std::chrono::system_clock::now())
122     , m_fragmentIdentifierForRequest(request.releaseFragmentIdentifier())
123     , m_origin(request.releaseOrigin())
124     , m_initiatorName(request.initiatorName())
125     , m_isLinkPreload(request.isLinkPreload())
126     , m_hasUnknownEncoding(request.isLinkPreload())
127     , m_type(type)
128 {
129     ASSERT(sessionID.isValid());
130
131     setLoadPriority(request.priority());
132 #ifndef NDEBUG
133     cachedResourceLeakCounter.increment();
134 #endif
135
136     // FIXME: We should have a better way of checking for Navigation loads, maybe FetchMode::Options::Navigate.
137     ASSERT(m_origin || m_type == CachedResource::MainResource);
138
139     if (isRequestCrossOrigin(m_origin.get(), m_resourceRequest.url(), m_options))
140         setCrossOrigin();
141 }
142
143 // FIXME: For this constructor, we should probably mandate that the URL has no fragment identifier.
144 CachedResource::CachedResource(const URL& url, Type type, SessionID sessionID)
145     : m_resourceRequest(url)
146     , m_decodedDataDeletionTimer(*this, &CachedResource::destroyDecodedData, deadDecodedDataDeletionIntervalForResourceType(type))
147     , m_sessionID(sessionID)
148     , m_responseTimestamp(std::chrono::system_clock::now())
149     , m_fragmentIdentifierForRequest(CachedResourceRequest::splitFragmentIdentifierFromRequestURL(m_resourceRequest))
150     , m_type(type)
151     , m_status(Cached)
152 {
153     ASSERT(sessionID.isValid());
154 #ifndef NDEBUG
155     cachedResourceLeakCounter.increment();
156 #endif
157 }
158
159 CachedResource::~CachedResource()
160 {
161     ASSERT(!m_resourceToRevalidate); // Should be true because canDelete() checks this.
162     ASSERT(canDelete());
163     ASSERT(!inCache());
164     ASSERT(!m_deleted);
165     ASSERT(url().isNull() || !allowsCaching() || MemoryCache::singleton().resourceForRequest(resourceRequest(), sessionID()) != this);
166
167 #ifndef NDEBUG
168     m_deleted = true;
169     cachedResourceLeakCounter.decrement();
170 #endif
171
172     if (m_owningCachedResourceLoader)
173         m_owningCachedResourceLoader->removeCachedResource(*this);
174 }
175
176 void CachedResource::failBeforeStarting()
177 {
178     // FIXME: What if resources in other frames were waiting for this revalidation?
179     LOG(ResourceLoading, "Cannot start loading '%s'", url().string().latin1().data());
180     if (allowsCaching() && m_resourceToRevalidate)
181         MemoryCache::singleton().revalidationFailed(*this);
182     error(CachedResource::LoadError);
183 }
184
185 void CachedResource::load(CachedResourceLoader& cachedResourceLoader)
186 {
187     if (!cachedResourceLoader.frame()) {
188         RELEASE_LOG_IF_ALLOWED("load: No associated frame");
189         failBeforeStarting();
190         return;
191     }
192     Frame& frame = *cachedResourceLoader.frame();
193
194     // Prevent new loads if we are in the PageCache or being added to the PageCache.
195     // We query the top document because new frames may be created in pagehide event handlers
196     // and their pageCacheState will not reflect the fact that they are about to enter page
197     // cache.
198     if (auto* topDocument = frame.mainFrame().document()) {
199         if (topDocument->pageCacheState() != Document::NotInPageCache) {
200             RELEASE_LOG_IF_ALLOWED("load: Already in page cache or being added to it (frame = %p)", &frame);
201             failBeforeStarting();
202             return;
203         }
204     }
205
206     FrameLoader& frameLoader = frame.loader();
207     if (m_options.securityCheck == DoSecurityCheck && (frameLoader.state() == FrameStateProvisional || !frameLoader.activeDocumentLoader() || frameLoader.activeDocumentLoader()->isStopping())) {
208         if (frameLoader.state() == FrameStateProvisional)
209             RELEASE_LOG_IF_ALLOWED("load: Failed security check -- state is provisional (frame = %p)", &frame);
210         else if (!frameLoader.activeDocumentLoader())
211             RELEASE_LOG_IF_ALLOWED("load: Failed security check -- not active document (frame = %p)", &frame);
212         else if (frameLoader.activeDocumentLoader()->isStopping())
213             RELEASE_LOG_IF_ALLOWED("load: Failed security check -- active loader is stopping (frame = %p)", &frame);
214         failBeforeStarting();
215         return;
216     }
217
218     m_loading = true;
219
220     if (isCacheValidator()) {
221         CachedResource* resourceToRevalidate = m_resourceToRevalidate;
222         ASSERT(resourceToRevalidate->canUseCacheValidator());
223         ASSERT(resourceToRevalidate->isLoaded());
224         const String& lastModified = resourceToRevalidate->response().httpHeaderField(HTTPHeaderName::LastModified);
225         const String& eTag = resourceToRevalidate->response().httpHeaderField(HTTPHeaderName::ETag);
226         if (!lastModified.isEmpty() || !eTag.isEmpty()) {
227             ASSERT(cachedResourceLoader.cachePolicy(type()) != CachePolicyReload);
228             if (cachedResourceLoader.cachePolicy(type()) == CachePolicyRevalidate)
229                 m_resourceRequest.setHTTPHeaderField(HTTPHeaderName::CacheControl, "max-age=0");
230             if (!lastModified.isEmpty())
231                 m_resourceRequest.setHTTPHeaderField(HTTPHeaderName::IfModifiedSince, lastModified);
232             if (!eTag.isEmpty())
233                 m_resourceRequest.setHTTPHeaderField(HTTPHeaderName::IfNoneMatch, eTag);
234         }
235     }
236
237 #if ENABLE(LINK_PREFETCH)
238     if (type() == CachedResource::LinkPrefetch || type() == CachedResource::LinkSubresource)
239         m_resourceRequest.setHTTPHeaderField(HTTPHeaderName::Purpose, "prefetch");
240 #endif
241     m_resourceRequest.setPriority(loadPriority());
242
243     // Navigation algorithm is setting up the request before sending it to CachedResourceLoader?CachedResource.
244     // So no need for extra fields for MainResource.
245     if (type() != CachedResource::MainResource)
246         frameLoader.addExtraFieldsToSubresourceRequest(m_resourceRequest);
247
248
249     // FIXME: It's unfortunate that the cache layer and below get to know anything about fragment identifiers.
250     // We should look into removing the expectation of that knowledge from the platform network stacks.
251     ResourceRequest request(m_resourceRequest);
252     if (!m_fragmentIdentifierForRequest.isNull()) {
253         URL url = request.url();
254         url.setFragmentIdentifier(m_fragmentIdentifierForRequest);
255         request.setURL(url);
256         m_fragmentIdentifierForRequest = String();
257     }
258
259     m_loader = platformStrategies()->loaderStrategy()->loadResource(frame, *this, request, m_options);
260     if (!m_loader) {
261         RELEASE_LOG_IF_ALLOWED("load: Unable to create SubresourceLoader (frame = %p)", &frame);
262         failBeforeStarting();
263         return;
264     }
265
266     m_status = Pending;
267 }
268
269 void CachedResource::loadFrom(const CachedResource& resource)
270 {
271     ASSERT(url() == resource.url());
272     ASSERT(type() == resource.type());
273     ASSERT(resource.status() == Status::Cached);
274
275     if (isCrossOrigin() && m_options.mode == FetchOptions::Mode::Cors) {
276         ASSERT(m_origin);
277         String errorMessage;
278         if (!WebCore::passesAccessControlCheck(resource.response(), m_options.allowCredentials, *m_origin, errorMessage)) {
279             setResourceError(ResourceError(String(), 0, url(), errorMessage, ResourceError::Type::AccessControl));
280             return;
281         }
282     }
283
284     setBodyDataFrom(resource);
285     setStatus(Status::Cached);
286     setLoading(false);
287 }
288
289 void CachedResource::setBodyDataFrom(const CachedResource& resource)
290 {
291     m_data = resource.m_data;
292     m_response = resource.m_response;
293     setDecodedSize(resource.decodedSize());
294     setEncodedSize(resource.encodedSize());
295 }
296
297 void CachedResource::checkNotify()
298 {
299     if (isLoading() || stillNeedsLoad())
300         return;
301
302     CachedResourceClientWalker<CachedResourceClient> walker(m_clients);
303     while (CachedResourceClient* client = walker.next())
304         client->notifyFinished(*this);
305 }
306
307 void CachedResource::addDataBuffer(SharedBuffer&)
308 {
309     ASSERT(dataBufferingPolicy() == BufferData);
310 }
311
312 void CachedResource::addData(const char*, unsigned)
313 {
314     ASSERT(dataBufferingPolicy() == DoNotBufferData);
315 }
316
317 void CachedResource::finishLoading(SharedBuffer*)
318 {
319     setLoading(false);
320     checkNotify();
321 }
322
323 void CachedResource::error(CachedResource::Status status)
324 {
325     setStatus(status);
326     ASSERT(errorOccurred());
327     m_data = nullptr;
328
329     setLoading(false);
330     checkNotify();
331 }
332     
333 void CachedResource::cancelLoad()
334 {
335     if (!isLoading() && !stillNeedsLoad())
336         return;
337
338     setStatus(LoadError);
339     setLoading(false);
340     checkNotify();
341 }
342
343 void CachedResource::finish()
344 {
345     if (!errorOccurred())
346         m_status = Cached;
347 }
348
349 void CachedResource::setCrossOrigin()
350 {
351     ASSERT(m_options.mode != FetchOptions::Mode::SameOrigin);
352     m_responseTainting = (m_options.mode == FetchOptions::Mode::Cors) ? ResourceResponse::Tainting::Cors : ResourceResponse::Tainting::Opaque;
353 }
354
355 bool CachedResource::isCrossOrigin() const
356 {
357     return m_responseTainting != ResourceResponse::Tainting::Basic;
358 }
359
360 bool CachedResource::isCORSSameOrigin() const
361 {
362     // Following resource types do not use CORS
363     ASSERT(type() != CachedResource::Type::FontResource);
364 #if ENABLE(SVG_FONTS)
365     ASSERT(type() != CachedResource::Type::SVGFontResource);
366 #endif
367 #if ENABLE(XSLT)
368     ASSERT(type() != CachedResource::XSLStyleSheet);
369 #endif
370
371     // https://html.spec.whatwg.org/multipage/infrastructure.html#cors-same-origin
372     return !loadFailedOrCanceled() && m_responseTainting != ResourceResponse::Tainting::Opaque;
373 }
374
375 bool CachedResource::isExpired() const
376 {
377     if (m_response.isNull())
378         return false;
379
380     return computeCurrentAge(m_response, m_responseTimestamp) > freshnessLifetime(m_response);
381 }
382
383 static inline bool shouldCacheSchemeIndefinitely(StringView scheme)
384 {
385 #if PLATFORM(COCOA)
386     if (equalLettersIgnoringASCIICase(scheme, "applewebdata"))
387         return true;
388 #endif
389 #if USE(SOUP)
390     if (equalLettersIgnoringASCIICase(scheme, "resource"))
391         return true;
392 #endif
393     return equalLettersIgnoringASCIICase(scheme, "data");
394 }
395
396 std::chrono::microseconds CachedResource::freshnessLifetime(const ResourceResponse& response) const
397 {
398     if (!response.url().protocolIsInHTTPFamily()) {
399         StringView protocol = response.url().protocol();
400         if (!shouldCacheSchemeIndefinitely(protocol)) {
401             // Don't cache non-HTTP main resources since we can't check for freshness.
402             // FIXME: We should not cache subresources either, but when we tried this
403             // it caused performance and flakiness issues in our test infrastructure.
404             if (m_type == MainResource || SchemeRegistry::shouldAlwaysRevalidateURLScheme(protocol.toStringWithoutCopying()))
405                 return 0us;
406         }
407
408         return std::chrono::microseconds::max();
409     }
410
411     return computeFreshnessLifetimeForHTTPFamily(response, m_responseTimestamp);
412 }
413
414 void CachedResource::redirectReceived(ResourceRequest&, const ResourceResponse& response)
415 {
416     m_requestedFromNetworkingLayer = true;
417     if (response.isNull())
418         return;
419
420     updateRedirectChainStatus(m_redirectChainCacheStatus, response);
421 }
422
423 void CachedResource::setResponse(const ResourceResponse& response)
424 {
425     ASSERT(m_response.type() == ResourceResponse::Type::Default);
426     m_response = response;
427     m_response.setRedirected(m_redirectChainCacheStatus.status != RedirectChainCacheStatus::NoRedirection);
428
429     m_varyingHeaderValues = collectVaryingRequestHeaders(m_resourceRequest, m_response, m_sessionID);
430 }
431
432 void CachedResource::responseReceived(const ResourceResponse& response)
433 {
434     setResponse(response);
435     m_responseTimestamp = std::chrono::system_clock::now();
436     String encoding = response.textEncodingName();
437     if (!encoding.isNull())
438         setEncoding(encoding);
439 }
440
441 void CachedResource::clearLoader()
442 {
443     ASSERT(m_loader);
444     m_identifierForLoadWithoutResourceLoader = m_loader->identifier();
445     m_loader = nullptr;
446     deleteIfPossible();
447 }
448
449 void CachedResource::addClient(CachedResourceClient& client)
450 {
451     if (addClientToSet(client))
452         didAddClient(client);
453 }
454
455 void CachedResource::didAddClient(CachedResourceClient& client)
456 {
457     if (m_decodedDataDeletionTimer.isActive())
458         m_decodedDataDeletionTimer.stop();
459
460     if (m_clientsAwaitingCallback.remove(&client))
461         m_clients.add(&client);
462     if (!isLoading() && !stillNeedsLoad())
463         client.notifyFinished(*this);
464 }
465
466 bool CachedResource::addClientToSet(CachedResourceClient& client)
467 {
468     if (m_preloadResult == PreloadNotReferenced && client.shouldMarkAsReferenced()) {
469         if (isLoaded())
470             m_preloadResult = PreloadReferencedWhileComplete;
471         else if (m_requestedFromNetworkingLayer)
472             m_preloadResult = PreloadReferencedWhileLoading;
473         else
474             m_preloadResult = PreloadReferenced;
475     }
476     if (allowsCaching() && !hasClients() && inCache())
477         MemoryCache::singleton().addToLiveResourcesSize(*this);
478
479     if ((m_type == RawResource || m_type == MainResource) && !m_response.isNull() && !m_proxyResource) {
480         // Certain resources (especially XHRs and main resources) do crazy things if an asynchronous load returns
481         // synchronously (e.g., scripts may not have set all the state they need to handle the load).
482         // Therefore, rather than immediately sending callbacks on a cache hit like other CachedResources,
483         // we schedule the callbacks and ensure we never finish synchronously.
484         ASSERT(!m_clientsAwaitingCallback.contains(&client));
485         m_clientsAwaitingCallback.add(&client, std::make_unique<Callback>(*this, client));
486         return false;
487     }
488
489     m_clients.add(&client);
490     return true;
491 }
492
493 void CachedResource::removeClient(CachedResourceClient& client)
494 {
495     auto callback = m_clientsAwaitingCallback.take(&client);
496     if (callback) {
497         ASSERT(!m_clients.contains(&client));
498         callback->cancel();
499         callback = nullptr;
500     } else {
501         ASSERT(m_clients.contains(&client));
502         m_clients.remove(&client);
503         didRemoveClient(client);
504     }
505
506     if (deleteIfPossible()) {
507         // `this` object is dead here.
508         return;
509     }
510
511     if (hasClients())
512         return;
513
514     auto& memoryCache = MemoryCache::singleton();
515     if (allowsCaching() && inCache()) {
516         memoryCache.removeFromLiveResourcesSize(*this);
517         memoryCache.removeFromLiveDecodedResourcesList(*this);
518     }
519     if (!m_switchingClientsToRevalidatedResource)
520         allClientsRemoved();
521     destroyDecodedDataIfNeeded();
522
523     if (!allowsCaching())
524         return;
525
526     if (response().cacheControlContainsNoStore() && url().protocolIs("https")) {
527         // RFC2616 14.9.2:
528         // "no-store: ... MUST make a best-effort attempt to remove the information from volatile storage as promptly as possible"
529         // "... History buffers MAY store such responses as part of their normal operation."
530         // We allow non-secure content to be reused in history, but we do not allow secure content to be reused.
531         memoryCache.remove(*this);
532     }
533     memoryCache.pruneSoon();
534 }
535
536 void CachedResource::destroyDecodedDataIfNeeded()
537 {
538     if (!m_decodedSize)
539         return;
540     if (!MemoryCache::singleton().deadDecodedDataDeletionInterval().count())
541         return;
542     m_decodedDataDeletionTimer.restart();
543 }
544
545 void CachedResource::decodedDataDeletionTimerFired()
546 {
547     destroyDecodedData();
548 }
549
550 bool CachedResource::deleteIfPossible()
551 {
552     if (canDelete()) {
553         if (!inCache()) {
554             InspectorInstrumentation::willDestroyCachedResource(*this);
555             delete this;
556             return true;
557         }
558         if (m_data)
559             m_data->hintMemoryNotNeededSoon();
560     }
561     return false;
562 }
563
564 void CachedResource::setDecodedSize(unsigned size)
565 {
566     if (size == m_decodedSize)
567         return;
568
569     long long delta = static_cast<long long>(size) - m_decodedSize;
570
571     // The object must be moved to a different queue, since its size has been changed.
572     // Remove before updating m_decodedSize, so we find the resource in the correct LRU list.
573     if (allowsCaching() && inCache())
574         MemoryCache::singleton().removeFromLRUList(*this);
575
576     m_decodedSize = size;
577    
578     if (allowsCaching() && inCache()) {
579         auto& memoryCache = MemoryCache::singleton();
580         // Now insert into the new LRU list.
581         memoryCache.insertInLRUList(*this);
582         
583         // Insert into or remove from the live decoded list if necessary.
584         // When inserting into the LiveDecodedResourcesList it is possible
585         // that the m_lastDecodedAccessTime is still zero or smaller than
586         // the m_lastDecodedAccessTime of the current list head. This is a
587         // violation of the invariant that the list is to be kept sorted
588         // by access time. The weakening of the invariant does not pose
589         // a problem. For more details please see: https://bugs.webkit.org/show_bug.cgi?id=30209
590         bool inLiveDecodedResourcesList = memoryCache.inLiveDecodedResourcesList(*this);
591         if (m_decodedSize && !inLiveDecodedResourcesList && hasClients())
592             memoryCache.insertInLiveDecodedResourcesList(*this);
593         else if (!m_decodedSize && inLiveDecodedResourcesList)
594             memoryCache.removeFromLiveDecodedResourcesList(*this);
595
596         // Update the cache's size totals.
597         memoryCache.adjustSize(hasClients(), delta);
598     }
599 }
600
601 void CachedResource::setEncodedSize(unsigned size)
602 {
603     if (size == m_encodedSize)
604         return;
605
606     long long delta = static_cast<long long>(size) - m_encodedSize;
607
608     // The object must be moved to a different queue, since its size has been changed.
609     // Remove before updating m_encodedSize, so we find the resource in the correct LRU list.
610     if (allowsCaching() && inCache())
611         MemoryCache::singleton().removeFromLRUList(*this);
612
613     m_encodedSize = size;
614
615     if (allowsCaching() && inCache()) {
616         auto& memoryCache = MemoryCache::singleton();
617         memoryCache.insertInLRUList(*this);
618         memoryCache.adjustSize(hasClients(), delta);
619     }
620 }
621
622 void CachedResource::didAccessDecodedData(double timeStamp)
623 {
624     m_lastDecodedAccessTime = timeStamp;
625     
626     if (allowsCaching() && inCache()) {
627         auto& memoryCache = MemoryCache::singleton();
628         if (memoryCache.inLiveDecodedResourcesList(*this)) {
629             memoryCache.removeFromLiveDecodedResourcesList(*this);
630             memoryCache.insertInLiveDecodedResourcesList(*this);
631         }
632         memoryCache.pruneSoon();
633     }
634 }
635     
636 void CachedResource::setResourceToRevalidate(CachedResource* resource) 
637
638     ASSERT(resource);
639     ASSERT(!m_resourceToRevalidate);
640     ASSERT(resource != this);
641     ASSERT(m_handlesToRevalidate.isEmpty());
642     ASSERT(resource->type() == type());
643     ASSERT(!resource->m_proxyResource);
644
645     LOG(ResourceLoading, "CachedResource %p setResourceToRevalidate %p", this, resource);
646
647     resource->m_proxyResource = this;
648     m_resourceToRevalidate = resource;
649 }
650
651 void CachedResource::clearResourceToRevalidate() 
652 {
653     ASSERT(m_resourceToRevalidate);
654     ASSERT(m_resourceToRevalidate->m_proxyResource == this);
655
656     if (m_switchingClientsToRevalidatedResource)
657         return;
658
659     m_resourceToRevalidate->m_proxyResource = nullptr;
660     m_resourceToRevalidate->deleteIfPossible();
661
662     m_handlesToRevalidate.clear();
663     m_resourceToRevalidate = nullptr;
664     deleteIfPossible();
665 }
666     
667 void CachedResource::switchClientsToRevalidatedResource()
668 {
669     ASSERT(m_resourceToRevalidate);
670     ASSERT(m_resourceToRevalidate->inCache());
671     ASSERT(!inCache());
672
673     LOG(ResourceLoading, "CachedResource %p switchClientsToRevalidatedResource %p", this, m_resourceToRevalidate);
674
675     m_switchingClientsToRevalidatedResource = true;
676     for (auto& handle : m_handlesToRevalidate) {
677         handle->m_resource = m_resourceToRevalidate;
678         m_resourceToRevalidate->registerHandle(handle);
679         --m_handleCount;
680     }
681     ASSERT(!m_handleCount);
682     m_handlesToRevalidate.clear();
683
684     Vector<CachedResourceClient*> clientsToMove;
685     for (auto& entry : m_clients) {
686         CachedResourceClient* client = entry.key;
687         unsigned count = entry.value;
688         while (count) {
689             clientsToMove.append(client);
690             --count;
691         }
692     }
693
694     for (auto& client : clientsToMove)
695         removeClient(*client);
696     ASSERT(m_clients.isEmpty());
697
698     for (auto& client : clientsToMove)
699         m_resourceToRevalidate->addClientToSet(*client);
700     for (auto& client : clientsToMove) {
701         // Calling didAddClient may do anything, including trying to cancel revalidation.
702         // Assert that it didn't succeed.
703         ASSERT(m_resourceToRevalidate);
704         // Calling didAddClient for a client may end up removing another client. In that case it won't be in the set anymore.
705         if (m_resourceToRevalidate->m_clients.contains(client))
706             m_resourceToRevalidate->didAddClient(*client);
707     }
708     m_switchingClientsToRevalidatedResource = false;
709 }
710
711 void CachedResource::updateResponseAfterRevalidation(const ResourceResponse& validatingResponse)
712 {
713     m_responseTimestamp = std::chrono::system_clock::now();
714
715     updateResponseHeadersAfterRevalidation(m_response, validatingResponse);
716 }
717
718 void CachedResource::registerHandle(CachedResourceHandleBase* h)
719 {
720     ++m_handleCount;
721     if (m_resourceToRevalidate)
722         m_handlesToRevalidate.add(h);
723 }
724
725 void CachedResource::unregisterHandle(CachedResourceHandleBase* h)
726 {
727     ASSERT(m_handleCount > 0);
728     --m_handleCount;
729
730     if (m_resourceToRevalidate)
731          m_handlesToRevalidate.remove(h);
732
733     if (!m_handleCount)
734         deleteIfPossible();
735 }
736
737 bool CachedResource::canUseCacheValidator() const
738 {
739     if (m_loading || errorOccurred())
740         return false;
741
742     if (m_response.cacheControlContainsNoStore())
743         return false;
744     return m_response.hasCacheValidatorFields();
745 }
746
747 CachedResource::RevalidationDecision CachedResource::makeRevalidationDecision(CachePolicy cachePolicy) const
748 {    
749     switch (cachePolicy) {
750     case CachePolicyHistoryBuffer:
751         return RevalidationDecision::No;
752
753     case CachePolicyReload:
754         return RevalidationDecision::YesDueToCachePolicy;
755
756     case CachePolicyRevalidate:
757         if (m_response.cacheControlContainsImmutable() && m_response.url().protocolIs("https")) {
758             if (isExpired())
759                 return RevalidationDecision::YesDueToExpired;
760             return RevalidationDecision::No;
761         }
762         return RevalidationDecision::YesDueToCachePolicy;
763
764     case CachePolicyVerify:
765         if (m_response.cacheControlContainsNoCache())
766             return RevalidationDecision::YesDueToNoCache;
767         // FIXME: Cache-Control:no-store should prevent storing, not reuse.
768         if (m_response.cacheControlContainsNoStore())
769             return RevalidationDecision::YesDueToNoStore;
770
771         if (isExpired())
772             return RevalidationDecision::YesDueToExpired;
773
774         return RevalidationDecision::No;
775     };
776     ASSERT_NOT_REACHED();
777     return RevalidationDecision::No;
778 }
779
780 bool CachedResource::redirectChainAllowsReuse(ReuseExpiredRedirectionOrNot reuseExpiredRedirection) const
781 {
782     return WebCore::redirectChainAllowsReuse(m_redirectChainCacheStatus, reuseExpiredRedirection);
783 }
784
785 bool CachedResource::varyHeaderValuesMatch(const ResourceRequest& request)
786 {
787     if (m_varyingHeaderValues.isEmpty())
788         return true;
789
790     return verifyVaryingRequestHeaders(m_varyingHeaderValues, request, m_sessionID);
791 }
792
793 unsigned CachedResource::overheadSize() const
794 {
795     static const int kAverageClientsHashMapSize = 384;
796     return sizeof(CachedResource) + m_response.memoryUsage() + kAverageClientsHashMapSize + m_resourceRequest.url().string().length() * 2;
797 }
798
799 bool CachedResource::areAllClientsXMLHttpRequests() const
800 {
801     if (type() != RawResource)
802         return false;
803
804     for (auto& client : m_clients) {
805         if (!client.key->isXMLHttpRequest())
806             return false;
807     }
808     return true;
809 }
810
811 void CachedResource::setLoadPriority(const std::optional<ResourceLoadPriority>& loadPriority)
812 {
813     if (loadPriority)
814         m_loadPriority = loadPriority.value();
815     else
816         m_loadPriority = defaultPriorityForResourceType(type());
817 }
818
819 inline CachedResource::Callback::Callback(CachedResource& resource, CachedResourceClient& client)
820     : m_resource(resource)
821     , m_client(client)
822     , m_timer(*this, &Callback::timerFired)
823 {
824     m_timer.startOneShot(0);
825 }
826
827 inline void CachedResource::Callback::cancel()
828 {
829     if (m_timer.isActive())
830         m_timer.stop();
831 }
832
833 void CachedResource::Callback::timerFired()
834 {
835     m_resource.didAddClient(m_client);
836 }
837
838 #if USE(FOUNDATION) || USE(SOUP)
839
840 void CachedResource::tryReplaceEncodedData(SharedBuffer& newBuffer)
841 {
842     if (!m_data)
843         return;
844     
845     if (!mayTryReplaceEncodedData())
846         return;
847
848     // We have to do the memcmp because we can't tell if the replacement file backed data is for the
849     // same resource or if we made a second request with the same URL which gave us a different
850     // resource. We have seen this happen for cached POST resources.
851     if (m_data->size() != newBuffer.size() || memcmp(m_data->data(), newBuffer.data(), m_data->size()))
852         return;
853
854     if (m_data->tryReplaceContentsWithPlatformBuffer(newBuffer))
855         didReplaceSharedBufferContents();
856 }
857
858 #endif
859
860 }