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.
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.
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.
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.
25 #include "CachedResource.h"
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"
35 #include "DocumentLoader.h"
36 #include "FrameLoader.h"
37 #include "FrameLoaderClient.h"
38 #include "HTTPHeaderNames.h"
39 #include "InspectorInstrumentation.h"
41 #include "LoaderStrategy.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>
58 #include "QuickLook.h"
63 #define RELEASE_LOG_IF_ALLOWED(fmt, ...) RELEASE_LOG_IF(cachedResourceLoader.isAlwaysOnLoggingAllowed(), Network, "%p - CachedResource::" fmt, this, ##__VA_ARGS__)
67 ResourceLoadPriority CachedResource::defaultPriorityForResourceType(Type type)
70 case CachedResource::MainResource:
71 return ResourceLoadPriority::VeryHigh;
72 case CachedResource::CSSStyleSheet:
73 case CachedResource::Script:
74 return ResourceLoadPriority::High;
76 case CachedResource::SVGFontResource:
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;
86 case CachedResource::XSLStyleSheet:
87 return ResourceLoadPriority::High;
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;
99 #if ENABLE(VIDEO_TRACK)
100 case CachedResource::TextTrackResource:
101 return ResourceLoadPriority::Low;
104 ASSERT_NOT_REACHED();
105 return ResourceLoadPriority::Low;
108 static Seconds deadDecodedDataDeletionIntervalForResourceType(CachedResource::Type type)
110 if (type == CachedResource::Script)
113 return MemoryCache::singleton().deadDecodedDataDeletionInterval();
116 DEFINE_DEBUG_ONLY_GLOBAL(RefCountedLeakCounter, cachedResourceLeakCounter, ("CachedResource"));
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())
131 , m_ignoreForRequestCount(request.ignoreForRequestCount())
133 ASSERT(sessionID.isValid());
135 setLoadPriority(request.priority());
137 cachedResourceLeakCounter.increment();
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);
143 if (isRequestCrossOrigin(m_origin.get(), m_resourceRequest.url(), m_options))
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))
157 ASSERT(sessionID.isValid());
159 cachedResourceLeakCounter.increment();
163 CachedResource::~CachedResource()
165 ASSERT(!m_resourceToRevalidate); // Should be true because canDelete() checks this.
169 ASSERT(url().isNull() || !allowsCaching() || MemoryCache::singleton().resourceForRequest(resourceRequest(), sessionID()) != this);
173 cachedResourceLeakCounter.decrement();
176 if (m_owningCachedResourceLoader)
177 m_owningCachedResourceLoader->removeCachedResource(*this);
180 void CachedResource::failBeforeStarting()
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);
189 void CachedResource::load(CachedResourceLoader& cachedResourceLoader)
191 if (!cachedResourceLoader.frame()) {
192 RELEASE_LOG_IF_ALLOWED("load: No associated frame");
193 failBeforeStarting();
196 Frame& frame = *cachedResourceLoader.frame();
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
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();
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();
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);
237 m_resourceRequest.setHTTPHeaderField(HTTPHeaderName::IfNoneMatch, eTag);
241 #if ENABLE(LINK_PREFETCH)
242 if (type() == CachedResource::LinkPrefetch || type() == CachedResource::LinkSubresource)
243 m_resourceRequest.setHTTPHeaderField(HTTPHeaderName::Purpose, "prefetch");
245 m_resourceRequest.setPriority(loadPriority());
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);
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);
260 m_fragmentIdentifierForRequest = String();
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();
269 // FIXME: We should not special-case Beacon here.
270 if (shouldUsePingLoad(type())) {
272 // Beacon is not exposed to workers so it is safe to rely on the document here.
273 auto* document = cachedResourceLoader.document();
274 auto* contentSecurityPolicy = document && !document->shouldBypassMainWorldContentSecurityPolicy() ? document->contentSecurityPolicy() : nullptr;
275 ASSERT(m_originalRequestHeaders);
276 platformStrategies()->loaderStrategy()->createPingHandle(frame.loader().networkingContext(), request, *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);
283 m_loader = platformStrategies()->loaderStrategy()->loadResource(frame, *this, request, m_options);
285 RELEASE_LOG_IF_ALLOWED("load: Unable to create SubresourceLoader (frame = %p)", &frame);
286 failBeforeStarting();
293 void CachedResource::loadFrom(const CachedResource& resource)
295 ASSERT(url() == resource.url());
296 ASSERT(type() == resource.type());
297 ASSERT(resource.status() == Status::Cached);
299 if (isCrossOrigin() && m_options.mode == FetchOptions::Mode::Cors) {
302 if (!WebCore::passesAccessControlCheck(resource.response(), m_options.allowCredentials, *m_origin, errorMessage)) {
303 setResourceError(ResourceError(String(), 0, url(), errorMessage, ResourceError::Type::AccessControl));
308 setBodyDataFrom(resource);
309 setStatus(Status::Cached);
313 void CachedResource::setBodyDataFrom(const CachedResource& resource)
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());
322 void CachedResource::checkNotify()
324 if (isLoading() || stillNeedsLoad())
327 CachedResourceClientWalker<CachedResourceClient> walker(m_clients);
328 while (CachedResourceClient* client = walker.next())
329 client->notifyFinished(*this);
332 void CachedResource::addDataBuffer(SharedBuffer&)
334 ASSERT(dataBufferingPolicy() == BufferData);
337 void CachedResource::addData(const char*, unsigned)
339 ASSERT(dataBufferingPolicy() == DoNotBufferData);
342 void CachedResource::finishLoading(SharedBuffer*)
348 void CachedResource::error(CachedResource::Status status)
351 ASSERT(errorOccurred());
358 void CachedResource::cancelLoad()
360 if (!isLoading() && !stillNeedsLoad())
363 setStatus(LoadError);
368 void CachedResource::finish()
370 if (!errorOccurred())
374 void CachedResource::setCrossOrigin()
376 ASSERT(m_options.mode != FetchOptions::Mode::SameOrigin);
377 m_responseTainting = (m_options.mode == FetchOptions::Mode::Cors) ? ResourceResponse::Tainting::Cors : ResourceResponse::Tainting::Opaque;
380 bool CachedResource::isCrossOrigin() const
382 return m_responseTainting != ResourceResponse::Tainting::Basic;
385 bool CachedResource::isCORSSameOrigin() const
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);
393 ASSERT(type() != CachedResource::XSLStyleSheet);
396 // https://html.spec.whatwg.org/multipage/infrastructure.html#cors-same-origin
397 return !loadFailedOrCanceled() && m_responseTainting != ResourceResponse::Tainting::Opaque;
400 bool CachedResource::isExpired() const
402 if (m_response.isNull())
405 return computeCurrentAge(m_response, m_responseTimestamp) > freshnessLifetime(m_response);
408 static inline bool shouldCacheSchemeIndefinitely(StringView scheme)
411 if (equalLettersIgnoringASCIICase(scheme, "applewebdata"))
415 if (equalLettersIgnoringASCIICase(scheme, "resource"))
418 return equalLettersIgnoringASCIICase(scheme, "data");
421 std::chrono::microseconds CachedResource::freshnessLifetime(const ResourceResponse& response) const
423 using namespace std::literals::chrono_literals;
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()))
435 return std::chrono::microseconds::max();
438 return computeFreshnessLifetimeForHTTPFamily(response, m_responseTimestamp);
441 void CachedResource::redirectReceived(ResourceRequest&, const ResourceResponse& response)
443 m_requestedFromNetworkingLayer = true;
444 if (response.isNull())
447 updateRedirectChainStatus(m_redirectChainCacheStatus, response);
450 void CachedResource::setResponse(const ResourceResponse& response)
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);
458 m_varyingHeaderValues = collectVaryingRequestHeaders(m_resourceRequest, m_response, m_sessionID);
461 void CachedResource::responseReceived(const ResourceResponse& response)
463 setResponse(response);
464 m_responseTimestamp = std::chrono::system_clock::now();
465 String encoding = response.textEncodingName();
466 if (!encoding.isNull())
467 setEncoding(encoding);
470 void CachedResource::clearLoader()
473 m_identifierForLoadWithoutResourceLoader = m_loader->identifier();
478 void CachedResource::addClient(CachedResourceClient& client)
480 if (addClientToSet(client))
481 didAddClient(client);
484 void CachedResource::didAddClient(CachedResourceClient& client)
486 if (m_decodedDataDeletionTimer.isActive())
487 m_decodedDataDeletionTimer.stop();
489 if (m_clientsAwaitingCallback.remove(&client))
490 m_clients.add(&client);
491 if (!isLoading() && !stillNeedsLoad())
492 client.notifyFinished(*this);
495 bool CachedResource::addClientToSet(CachedResourceClient& client)
497 if (m_preloadResult == PreloadNotReferenced && client.shouldMarkAsReferenced()) {
499 m_preloadResult = PreloadReferencedWhileComplete;
500 else if (m_requestedFromNetworkingLayer)
501 m_preloadResult = PreloadReferencedWhileLoading;
503 m_preloadResult = PreloadReferenced;
505 if (allowsCaching() && !hasClients() && inCache())
506 MemoryCache::singleton().addToLiveResourcesSize(*this);
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));
518 m_clients.add(&client);
522 void CachedResource::removeClient(CachedResourceClient& client)
524 auto callback = m_clientsAwaitingCallback.take(&client);
526 ASSERT(!m_clients.contains(&client));
530 ASSERT(m_clients.contains(&client));
531 m_clients.remove(&client);
532 didRemoveClient(client);
535 if (deleteIfPossible()) {
536 // `this` object is dead here.
543 auto& memoryCache = MemoryCache::singleton();
544 if (allowsCaching() && inCache()) {
545 memoryCache.removeFromLiveResourcesSize(*this);
546 memoryCache.removeFromLiveDecodedResourcesList(*this);
548 if (!m_switchingClientsToRevalidatedResource)
550 destroyDecodedDataIfNeeded();
552 if (!allowsCaching())
555 if (response().cacheControlContainsNoStore() && url().protocolIs("https")) {
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);
562 memoryCache.pruneSoon();
565 void CachedResource::destroyDecodedDataIfNeeded()
569 if (!MemoryCache::singleton().deadDecodedDataDeletionInterval())
571 m_decodedDataDeletionTimer.restart();
574 void CachedResource::decodedDataDeletionTimerFired()
576 destroyDecodedData();
579 bool CachedResource::deleteIfPossible()
583 InspectorInstrumentation::willDestroyCachedResource(*this);
588 m_data->hintMemoryNotNeededSoon();
593 void CachedResource::setDecodedSize(unsigned size)
595 if (size == m_decodedSize)
598 long long delta = static_cast<long long>(size) - m_decodedSize;
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);
605 m_decodedSize = size;
607 if (allowsCaching() && inCache()) {
608 auto& memoryCache = MemoryCache::singleton();
609 // Now insert into the new LRU list.
610 memoryCache.insertInLRUList(*this);
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);
625 // Update the cache's size totals.
626 memoryCache.adjustSize(hasClients(), delta);
630 void CachedResource::setEncodedSize(unsigned size)
632 if (size == m_encodedSize)
635 long long delta = static_cast<long long>(size) - m_encodedSize;
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);
642 m_encodedSize = size;
644 if (allowsCaching() && inCache()) {
645 auto& memoryCache = MemoryCache::singleton();
646 memoryCache.insertInLRUList(*this);
647 memoryCache.adjustSize(hasClients(), delta);
651 void CachedResource::didAccessDecodedData(double timeStamp)
653 m_lastDecodedAccessTime = timeStamp;
655 if (allowsCaching() && inCache()) {
656 auto& memoryCache = MemoryCache::singleton();
657 if (memoryCache.inLiveDecodedResourcesList(*this)) {
658 memoryCache.removeFromLiveDecodedResourcesList(*this);
659 memoryCache.insertInLiveDecodedResourcesList(*this);
661 memoryCache.pruneSoon();
665 void CachedResource::setResourceToRevalidate(CachedResource* resource)
668 ASSERT(!m_resourceToRevalidate);
669 ASSERT(resource != this);
670 ASSERT(m_handlesToRevalidate.isEmpty());
671 ASSERT(resource->type() == type());
672 ASSERT(!resource->m_proxyResource);
674 LOG(ResourceLoading, "CachedResource %p setResourceToRevalidate %p", this, resource);
676 resource->m_proxyResource = this;
677 m_resourceToRevalidate = resource;
680 void CachedResource::clearResourceToRevalidate()
682 ASSERT(m_resourceToRevalidate);
683 ASSERT(m_resourceToRevalidate->m_proxyResource == this);
685 if (m_switchingClientsToRevalidatedResource)
688 m_resourceToRevalidate->m_proxyResource = nullptr;
689 m_resourceToRevalidate->deleteIfPossible();
691 m_handlesToRevalidate.clear();
692 m_resourceToRevalidate = nullptr;
696 void CachedResource::switchClientsToRevalidatedResource()
698 ASSERT(m_resourceToRevalidate);
699 ASSERT(m_resourceToRevalidate->inCache());
702 LOG(ResourceLoading, "CachedResource %p switchClientsToRevalidatedResource %p", this, m_resourceToRevalidate);
704 m_switchingClientsToRevalidatedResource = true;
705 for (auto& handle : m_handlesToRevalidate) {
706 handle->m_resource = m_resourceToRevalidate;
707 m_resourceToRevalidate->registerHandle(handle);
710 ASSERT(!m_handleCount);
711 m_handlesToRevalidate.clear();
713 Vector<CachedResourceClient*> clientsToMove;
714 for (auto& entry : m_clients) {
715 CachedResourceClient* client = entry.key;
716 unsigned count = entry.value;
718 clientsToMove.append(client);
723 for (auto& client : clientsToMove)
724 removeClient(*client);
725 ASSERT(m_clients.isEmpty());
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);
737 m_switchingClientsToRevalidatedResource = false;
740 void CachedResource::updateResponseAfterRevalidation(const ResourceResponse& validatingResponse)
742 m_responseTimestamp = std::chrono::system_clock::now();
744 updateResponseHeadersAfterRevalidation(m_response, validatingResponse);
747 void CachedResource::registerHandle(CachedResourceHandleBase* h)
750 if (m_resourceToRevalidate)
751 m_handlesToRevalidate.add(h);
754 void CachedResource::unregisterHandle(CachedResourceHandleBase* h)
756 ASSERT(m_handleCount > 0);
759 if (m_resourceToRevalidate)
760 m_handlesToRevalidate.remove(h);
766 bool CachedResource::canUseCacheValidator() const
768 if (m_loading || errorOccurred())
771 if (m_response.cacheControlContainsNoStore())
773 return m_response.hasCacheValidatorFields();
776 CachedResource::RevalidationDecision CachedResource::makeRevalidationDecision(CachePolicy cachePolicy) const
778 switch (cachePolicy) {
779 case CachePolicyHistoryBuffer:
780 return RevalidationDecision::No;
782 case CachePolicyReload:
783 return RevalidationDecision::YesDueToCachePolicy;
785 case CachePolicyRevalidate:
786 if (m_response.cacheControlContainsImmutable() && m_response.url().protocolIs("https")) {
788 return RevalidationDecision::YesDueToExpired;
789 return RevalidationDecision::No;
791 return RevalidationDecision::YesDueToCachePolicy;
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;
801 return RevalidationDecision::YesDueToExpired;
803 return RevalidationDecision::No;
805 ASSERT_NOT_REACHED();
806 return RevalidationDecision::No;
809 bool CachedResource::redirectChainAllowsReuse(ReuseExpiredRedirectionOrNot reuseExpiredRedirection) const
811 return WebCore::redirectChainAllowsReuse(m_redirectChainCacheStatus, reuseExpiredRedirection);
814 bool CachedResource::varyHeaderValuesMatch(const ResourceRequest& request)
816 if (m_varyingHeaderValues.isEmpty())
819 return verifyVaryingRequestHeaders(m_varyingHeaderValues, request, m_sessionID);
822 unsigned CachedResource::overheadSize() const
824 static const int kAverageClientsHashMapSize = 384;
825 return sizeof(CachedResource) + m_response.memoryUsage() + kAverageClientsHashMapSize + m_resourceRequest.url().string().length() * 2;
828 bool CachedResource::areAllClientsXMLHttpRequests() const
830 if (type() != RawResource)
833 for (auto& client : m_clients) {
834 if (!client.key->isXMLHttpRequest())
840 void CachedResource::setLoadPriority(const std::optional<ResourceLoadPriority>& loadPriority)
843 m_loadPriority = loadPriority.value();
845 m_loadPriority = defaultPriorityForResourceType(type());
848 inline CachedResource::Callback::Callback(CachedResource& resource, CachedResourceClient& client)
849 : m_resource(resource)
851 , m_timer(*this, &Callback::timerFired)
853 m_timer.startOneShot(0_s);
856 inline void CachedResource::Callback::cancel()
858 if (m_timer.isActive())
862 void CachedResource::Callback::timerFired()
864 m_resource.didAddClient(m_client);
867 #if USE(FOUNDATION) || USE(SOUP)
869 void CachedResource::tryReplaceEncodedData(SharedBuffer& newBuffer)
874 if (!mayTryReplaceEncodedData())
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()))
884 m_data->append(newBuffer);
885 didReplaceSharedBufferContents();