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