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