8be377a87f6693139b6c997dae39b20ad79eb2d9
[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 "Frame.h"
37 #include "FrameLoader.h"
38 #include "FrameLoaderClient.h"
39 #include "HTTPHeaderNames.h"
40 #include "InspectorInstrumentation.h"
41 #include "LoaderStrategy.h"
42 #include "Logging.h"
43 #include "MemoryCache.h"
44 #include "PlatformStrategies.h"
45 #include "ProgressTracker.h"
46 #include "ResourceHandle.h"
47 #include "SchemeRegistry.h"
48 #include "SecurityOrigin.h"
49 #include "SubresourceLoader.h"
50 #include "URL.h"
51 #include <wtf/CompletionHandler.h>
52 #include <wtf/MathExtras.h>
53 #include <wtf/RefCountedLeakCounter.h>
54 #include <wtf/StdLibExtras.h>
55 #include <wtf/Vector.h>
56 #include <wtf/text/CString.h>
57
58 #if USE(QUICK_LOOK)
59 #include "QuickLook.h"
60 #endif
61
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 using namespace WTF;
67
68 ResourceLoadPriority CachedResource::defaultPriorityForResourceType(Type type)
69 {
70     switch (type) {
71     case CachedResource::MainResource:
72         return ResourceLoadPriority::VeryHigh;
73     case CachedResource::CSSStyleSheet:
74     case CachedResource::Script:
75         return ResourceLoadPriority::High;
76 #if ENABLE(SVG_FONTS)
77     case CachedResource::SVGFontResource:
78 #endif
79     case CachedResource::MediaResource:
80     case CachedResource::FontResource:
81     case CachedResource::RawResource:
82     case CachedResource::Icon:
83         return ResourceLoadPriority::Medium;
84     case CachedResource::ImageResource:
85         return ResourceLoadPriority::Low;
86 #if ENABLE(XSLT)
87     case CachedResource::XSLStyleSheet:
88         return ResourceLoadPriority::High;
89 #endif
90     case CachedResource::SVGDocumentResource:
91         return ResourceLoadPriority::Low;
92     case CachedResource::Beacon:
93         return ResourceLoadPriority::VeryLow;
94     case CachedResource::LinkPrefetch:
95         return ResourceLoadPriority::VeryLow;
96 #if ENABLE(VIDEO_TRACK)
97     case CachedResource::TextTrackResource:
98         return ResourceLoadPriority::Low;
99 #endif
100 #if ENABLE(APPLICATION_MANIFEST)
101     case CachedResource::ApplicationManifest:
102         return ResourceLoadPriority::Low;
103 #endif
104     }
105     ASSERT_NOT_REACHED();
106     return ResourceLoadPriority::Low;
107 }
108
109 static Seconds deadDecodedDataDeletionIntervalForResourceType(CachedResource::Type type)
110 {
111     if (type == CachedResource::Script)
112         return 0_s;
113
114     return MemoryCache::singleton().deadDecodedDataDeletionInterval();
115 }
116
117 DEFINE_DEBUG_ONLY_GLOBAL(RefCountedLeakCounter, cachedResourceLeakCounter, ("CachedResource"));
118
119 CachedResource::CachedResource(CachedResourceRequest&& request, Type type, PAL::SessionID sessionID)
120     : m_resourceRequest(request.releaseResourceRequest())
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(WallTime::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(WallTime::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 (type() == CachedResource::LinkPrefetch)
243         m_resourceRequest.setHTTPHeaderField(HTTPHeaderName::Purpose, "prefetch");
244     m_resourceRequest.setPriority(loadPriority());
245
246     // Navigation algorithm is setting up the request before sending it to CachedResourceLoader?CachedResource.
247     // So no need for extra fields for MainResource.
248     if (type() != CachedResource::MainResource)
249         frameLoader.addExtraFieldsToSubresourceRequest(m_resourceRequest);
250
251
252     // FIXME: It's unfortunate that the cache layer and below get to know anything about fragment identifiers.
253     // We should look into removing the expectation of that knowledge from the platform network stacks.
254     ResourceRequest request(m_resourceRequest);
255     if (!m_fragmentIdentifierForRequest.isNull()) {
256         URL url = request.url();
257         url.setFragmentIdentifier(m_fragmentIdentifierForRequest);
258         request.setURL(url);
259         m_fragmentIdentifierForRequest = String();
260     }
261
262     if (m_options.keepAlive) {
263         if (!cachedResourceLoader.keepaliveRequestTracker().tryRegisterRequest(*this)) {
264             setResourceError({ errorDomainWebKitInternal, 0, request.url(), "Reached maximum amount of queued data of 64Kb for keepalive requests"_s, ResourceError::Type::AccessControl });
265             failBeforeStarting();
266             return;
267         }
268         // FIXME: We should not special-case Beacon here.
269         if (shouldUsePingLoad(type())) {
270             ASSERT(m_originalRequest);
271             CachedResourceHandle<CachedResource> protectedThis(this);
272
273             // FIXME: Move beacon loads to normal subresource loading to get normal inspector request instrumentation hooks.
274             unsigned long identifier = frame.page()->progress().createUniqueIdentifier();
275             InspectorInstrumentation::willSendRequestOfType(&frame, identifier, frameLoader.activeDocumentLoader(), request, InspectorInstrumentation::LoadType::Beacon);
276
277             platformStrategies()->loaderStrategy()->startPingLoad(frame, request, m_originalRequest->httpHeaderFields(), m_options, [this, protectedThis = WTFMove(protectedThis), protectedFrame = makeRef(frame), identifier] (const ResourceError& error, const ResourceResponse& response) {
278                 if (!response.isNull())
279                     InspectorInstrumentation::didReceiveResourceResponse(protectedFrame, identifier, protectedFrame->loader().activeDocumentLoader(), response, nullptr);
280                 if (error.isNull()) {
281                     finishLoading(nullptr);
282                     NetworkLoadMetrics emptyMetrics;
283                     InspectorInstrumentation::didFinishLoading(protectedFrame.ptr(), protectedFrame->loader().activeDocumentLoader(), identifier, emptyMetrics, nullptr);
284                 } else {
285                     setResourceError(error);
286                     this->error(LoadError);
287                     InspectorInstrumentation::didFailLoading(protectedFrame.ptr(), protectedFrame->loader().activeDocumentLoader(), identifier, error);
288                 }
289             });
290             return;
291         }
292     }
293
294     platformStrategies()->loaderStrategy()->loadResource(frame, *this, WTFMove(request), m_options, [this, protectedThis = CachedResourceHandle<CachedResource>(this), frame = makeRef(frame), loggingAllowed = cachedResourceLoader.isAlwaysOnLoggingAllowed()] (RefPtr<SubresourceLoader>&& loader) {
295         m_loader = WTFMove(loader);
296         if (!m_loader) {
297             RELEASE_LOG_IF(loggingAllowed, Network, "%p - CachedResource::load: Unable to create SubresourceLoader (frame = %p)", this, frame.ptr());
298             failBeforeStarting();
299             return;
300         }
301         m_status = Pending;
302     });
303 }
304
305 void CachedResource::loadFrom(const CachedResource& resource)
306 {
307     ASSERT(url() == resource.url());
308     ASSERT(type() == resource.type());
309     ASSERT(resource.status() == Status::Cached);
310
311     if (isCrossOrigin() && m_options.mode == FetchOptions::Mode::Cors) {
312         ASSERT(m_origin);
313         String errorMessage;
314         if (!WebCore::passesAccessControlCheck(resource.response(), m_options.storedCredentialsPolicy, *m_origin, errorMessage)) {
315             setResourceError(ResourceError(String(), 0, url(), errorMessage, ResourceError::Type::AccessControl));
316             return;
317         }
318     }
319
320     setBodyDataFrom(resource);
321     setStatus(Status::Cached);
322     setLoading(false);
323 }
324
325 void CachedResource::setBodyDataFrom(const CachedResource& resource)
326 {
327     m_data = resource.m_data;
328     m_response = resource.m_response;
329     m_response.setTainting(m_responseTainting);
330     setDecodedSize(resource.decodedSize());
331     setEncodedSize(resource.encodedSize());
332 }
333
334 void CachedResource::checkNotify()
335 {
336     if (isLoading() || stillNeedsLoad())
337         return;
338
339     CachedResourceClientWalker<CachedResourceClient> walker(m_clients);
340     while (CachedResourceClient* client = walker.next())
341         client->notifyFinished(*this);
342 }
343
344 void CachedResource::updateBuffer(SharedBuffer&)
345 {
346     ASSERT(dataBufferingPolicy() == BufferData);
347 }
348
349 void CachedResource::updateData(const char*, unsigned)
350 {
351     ASSERT(dataBufferingPolicy() == DoNotBufferData);
352 }
353
354 void CachedResource::finishLoading(SharedBuffer*)
355 {
356     setLoading(false);
357     checkNotify();
358 }
359
360 void CachedResource::error(CachedResource::Status status)
361 {
362     setStatus(status);
363     ASSERT(errorOccurred());
364     m_data = nullptr;
365
366     setLoading(false);
367     checkNotify();
368 }
369     
370 void CachedResource::cancelLoad()
371 {
372     if (!isLoading() && !stillNeedsLoad())
373         return;
374
375     setStatus(LoadError);
376     setLoading(false);
377     checkNotify();
378 }
379
380 void CachedResource::finish()
381 {
382     if (!errorOccurred())
383         m_status = Cached;
384 }
385
386 void CachedResource::setCrossOrigin()
387 {
388     ASSERT(m_options.mode != FetchOptions::Mode::SameOrigin);
389     m_responseTainting = (m_options.mode == FetchOptions::Mode::Cors) ? ResourceResponse::Tainting::Cors : ResourceResponse::Tainting::Opaque;
390 }
391
392 bool CachedResource::isCrossOrigin() const
393 {
394     return m_responseTainting != ResourceResponse::Tainting::Basic;
395 }
396
397 bool CachedResource::isCORSSameOrigin() const
398 {
399     // Following resource types do not use CORS
400     ASSERT(type() != CachedResource::Type::FontResource);
401 #if ENABLE(SVG_FONTS)
402     ASSERT(type() != CachedResource::Type::SVGFontResource);
403 #endif
404 #if ENABLE(XSLT)
405     ASSERT(type() != CachedResource::XSLStyleSheet);
406 #endif
407
408     // https://html.spec.whatwg.org/multipage/infrastructure.html#cors-same-origin
409     return !loadFailedOrCanceled() && m_responseTainting != ResourceResponse::Tainting::Opaque;
410 }
411
412 bool CachedResource::isExpired() const
413 {
414     if (m_response.isNull())
415         return false;
416
417     return computeCurrentAge(m_response, m_responseTimestamp) > freshnessLifetime(m_response);
418 }
419
420 static inline bool shouldCacheSchemeIndefinitely(StringView scheme)
421 {
422 #if PLATFORM(COCOA)
423     if (equalLettersIgnoringASCIICase(scheme, "applewebdata"))
424         return true;
425 #endif
426 #if USE(SOUP)
427     if (equalLettersIgnoringASCIICase(scheme, "resource"))
428         return true;
429 #endif
430     return equalLettersIgnoringASCIICase(scheme, "data");
431 }
432
433 Seconds CachedResource::freshnessLifetime(const ResourceResponse& response) const
434 {
435     if (!response.url().protocolIsInHTTPFamily()) {
436         StringView protocol = response.url().protocol();
437         if (!shouldCacheSchemeIndefinitely(protocol)) {
438             // Don't cache non-HTTP main resources since we can't check for freshness.
439             // FIXME: We should not cache subresources either, but when we tried this
440             // it caused performance and flakiness issues in our test infrastructure.
441             if (m_type == MainResource || SchemeRegistry::shouldAlwaysRevalidateURLScheme(protocol.toStringWithoutCopying()))
442                 return 0_us;
443         }
444
445         return Seconds::infinity();
446     }
447
448     return computeFreshnessLifetimeForHTTPFamily(response, m_responseTimestamp);
449 }
450
451 void CachedResource::redirectReceived(ResourceRequest&& request, const ResourceResponse& response, CompletionHandler<void(ResourceRequest&&)>&& completionHandler)
452 {
453     m_requestedFromNetworkingLayer = true;
454     if (response.isNull())
455         return completionHandler(WTFMove(request));
456
457     updateRedirectChainStatus(m_redirectChainCacheStatus, response);
458     completionHandler(WTFMove(request));
459 }
460
461 void CachedResource::setResponse(const ResourceResponse& response)
462 {
463     ASSERT(m_response.type() == ResourceResponse::Type::Default);
464     m_response = response;
465     m_varyingHeaderValues = collectVaryingRequestHeaders(m_resourceRequest, m_response, m_sessionID);
466
467 #if ENABLE(SERVICE_WORKER)
468     if (m_response.source() == ResourceResponse::Source::ServiceWorker) {
469         m_responseTainting = m_response.tainting();
470         return;
471     }
472 #endif
473     m_response.setRedirected(m_redirectChainCacheStatus.status != RedirectChainCacheStatus::NoRedirection);
474     if (m_response.tainting() == ResourceResponse::Tainting::Basic || m_response.tainting() == ResourceResponse::Tainting::Cors)
475         m_response.setTainting(m_responseTainting);
476 }
477
478 void CachedResource::responseReceived(const ResourceResponse& response)
479 {
480     setResponse(response);
481     m_responseTimestamp = WallTime::now();
482     String encoding = response.textEncodingName();
483     if (!encoding.isNull())
484         setEncoding(encoding);
485 }
486
487 void CachedResource::clearLoader()
488 {
489     ASSERT(m_loader);
490     m_identifierForLoadWithoutResourceLoader = m_loader->identifier();
491     m_loader = nullptr;
492     deleteIfPossible();
493 }
494
495 void CachedResource::addClient(CachedResourceClient& client)
496 {
497     if (addClientToSet(client))
498         didAddClient(client);
499 }
500
501 void CachedResource::didAddClient(CachedResourceClient& client)
502 {
503     if (m_decodedDataDeletionTimer.isActive())
504         m_decodedDataDeletionTimer.stop();
505
506     if (m_clientsAwaitingCallback.remove(&client))
507         m_clients.add(&client);
508     if (!isLoading() && !stillNeedsLoad())
509         client.notifyFinished(*this);
510 }
511
512 bool CachedResource::addClientToSet(CachedResourceClient& client)
513 {
514     if (m_preloadResult == PreloadNotReferenced && client.shouldMarkAsReferenced()) {
515         if (isLoaded())
516             m_preloadResult = PreloadReferencedWhileComplete;
517         else if (m_requestedFromNetworkingLayer)
518             m_preloadResult = PreloadReferencedWhileLoading;
519         else
520             m_preloadResult = PreloadReferenced;
521     }
522     if (allowsCaching() && !hasClients() && inCache())
523         MemoryCache::singleton().addToLiveResourcesSize(*this);
524
525     if ((m_type == RawResource || m_type == MainResource) && !m_response.isNull() && !m_proxyResource) {
526         // Certain resources (especially XHRs and main resources) do crazy things if an asynchronous load returns
527         // synchronously (e.g., scripts may not have set all the state they need to handle the load).
528         // Therefore, rather than immediately sending callbacks on a cache hit like other CachedResources,
529         // we schedule the callbacks and ensure we never finish synchronously.
530         ASSERT(!m_clientsAwaitingCallback.contains(&client));
531         m_clientsAwaitingCallback.add(&client, std::make_unique<Callback>(*this, client));
532         return false;
533     }
534
535     m_clients.add(&client);
536     return true;
537 }
538
539 void CachedResource::removeClient(CachedResourceClient& client)
540 {
541     auto callback = m_clientsAwaitingCallback.take(&client);
542     if (callback) {
543         ASSERT(!m_clients.contains(&client));
544         callback->cancel();
545         callback = nullptr;
546     } else {
547         ASSERT(m_clients.contains(&client));
548         m_clients.remove(&client);
549         didRemoveClient(client);
550     }
551
552     if (deleteIfPossible()) {
553         // `this` object is dead here.
554         return;
555     }
556
557     if (hasClients())
558         return;
559
560     auto& memoryCache = MemoryCache::singleton();
561     if (allowsCaching() && inCache()) {
562         memoryCache.removeFromLiveResourcesSize(*this);
563         memoryCache.removeFromLiveDecodedResourcesList(*this);
564     }
565     if (!m_switchingClientsToRevalidatedResource)
566         allClientsRemoved();
567     destroyDecodedDataIfNeeded();
568
569     if (!allowsCaching())
570         return;
571
572     if (response().cacheControlContainsNoStore() && url().protocolIs("https")) {
573         // RFC2616 14.9.2:
574         // "no-store: ... MUST make a best-effort attempt to remove the information from volatile storage as promptly as possible"
575         // "... History buffers MAY store such responses as part of their normal operation."
576         // We allow non-secure content to be reused in history, but we do not allow secure content to be reused.
577         memoryCache.remove(*this);
578     }
579     memoryCache.pruneSoon();
580 }
581
582 void CachedResource::allClientsRemoved()
583 {
584     if (isLinkPreload() && m_loader)
585         m_loader->cancelIfNotFinishing();
586 }
587
588 void CachedResource::destroyDecodedDataIfNeeded()
589 {
590     if (!m_decodedSize)
591         return;
592     if (!MemoryCache::singleton().deadDecodedDataDeletionInterval())
593         return;
594     m_decodedDataDeletionTimer.restart();
595 }
596
597 void CachedResource::decodedDataDeletionTimerFired()
598 {
599     destroyDecodedData();
600 }
601
602 bool CachedResource::deleteIfPossible()
603 {
604     if (canDelete()) {
605         if (!inCache()) {
606             InspectorInstrumentation::willDestroyCachedResource(*this);
607             delete this;
608             return true;
609         }
610         if (m_data)
611             m_data->hintMemoryNotNeededSoon();
612     }
613     return false;
614 }
615
616 void CachedResource::setDecodedSize(unsigned size)
617 {
618     if (size == m_decodedSize)
619         return;
620
621     long long delta = static_cast<long long>(size) - m_decodedSize;
622
623     // The object must be moved to a different queue, since its size has been changed.
624     // Remove before updating m_decodedSize, so we find the resource in the correct LRU list.
625     if (allowsCaching() && inCache())
626         MemoryCache::singleton().removeFromLRUList(*this);
627
628     m_decodedSize = size;
629    
630     if (allowsCaching() && inCache()) {
631         auto& memoryCache = MemoryCache::singleton();
632         // Now insert into the new LRU list.
633         memoryCache.insertInLRUList(*this);
634         
635         // Insert into or remove from the live decoded list if necessary.
636         // When inserting into the LiveDecodedResourcesList it is possible
637         // that the m_lastDecodedAccessTime is still zero or smaller than
638         // the m_lastDecodedAccessTime of the current list head. This is a
639         // violation of the invariant that the list is to be kept sorted
640         // by access time. The weakening of the invariant does not pose
641         // a problem. For more details please see: https://bugs.webkit.org/show_bug.cgi?id=30209
642         bool inLiveDecodedResourcesList = memoryCache.inLiveDecodedResourcesList(*this);
643         if (m_decodedSize && !inLiveDecodedResourcesList && hasClients())
644             memoryCache.insertInLiveDecodedResourcesList(*this);
645         else if (!m_decodedSize && inLiveDecodedResourcesList)
646             memoryCache.removeFromLiveDecodedResourcesList(*this);
647
648         // Update the cache's size totals.
649         memoryCache.adjustSize(hasClients(), delta);
650     }
651 }
652
653 void CachedResource::setEncodedSize(unsigned size)
654 {
655     if (size == m_encodedSize)
656         return;
657
658     long long delta = static_cast<long long>(size) - m_encodedSize;
659
660     // The object must be moved to a different queue, since its size has been changed.
661     // Remove before updating m_encodedSize, so we find the resource in the correct LRU list.
662     if (allowsCaching() && inCache())
663         MemoryCache::singleton().removeFromLRUList(*this);
664
665     m_encodedSize = size;
666
667     if (allowsCaching() && inCache()) {
668         auto& memoryCache = MemoryCache::singleton();
669         memoryCache.insertInLRUList(*this);
670         memoryCache.adjustSize(hasClients(), delta);
671     }
672 }
673
674 void CachedResource::didAccessDecodedData(MonotonicTime timeStamp)
675 {
676     m_lastDecodedAccessTime = timeStamp;
677     
678     if (allowsCaching() && inCache()) {
679         auto& memoryCache = MemoryCache::singleton();
680         if (memoryCache.inLiveDecodedResourcesList(*this)) {
681             memoryCache.removeFromLiveDecodedResourcesList(*this);
682             memoryCache.insertInLiveDecodedResourcesList(*this);
683         }
684         memoryCache.pruneSoon();
685     }
686 }
687     
688 void CachedResource::setResourceToRevalidate(CachedResource* resource) 
689
690     ASSERT(resource);
691     ASSERT(!m_resourceToRevalidate);
692     ASSERT(resource != this);
693     ASSERT(m_handlesToRevalidate.isEmpty());
694     ASSERT(resource->type() == type());
695     ASSERT(!resource->m_proxyResource);
696
697     LOG(ResourceLoading, "CachedResource %p setResourceToRevalidate %p", this, resource);
698
699     resource->m_proxyResource = this;
700     m_resourceToRevalidate = resource;
701 }
702
703 void CachedResource::clearResourceToRevalidate() 
704 {
705     ASSERT(m_resourceToRevalidate);
706     ASSERT(m_resourceToRevalidate->m_proxyResource == this);
707
708     if (m_switchingClientsToRevalidatedResource)
709         return;
710
711     m_resourceToRevalidate->m_proxyResource = nullptr;
712     m_resourceToRevalidate->deleteIfPossible();
713
714     m_handlesToRevalidate.clear();
715     m_resourceToRevalidate = nullptr;
716     deleteIfPossible();
717 }
718     
719 void CachedResource::switchClientsToRevalidatedResource()
720 {
721     ASSERT(m_resourceToRevalidate);
722     ASSERT(m_resourceToRevalidate->inCache());
723     ASSERT(!inCache());
724
725     LOG(ResourceLoading, "CachedResource %p switchClientsToRevalidatedResource %p", this, m_resourceToRevalidate);
726
727     m_switchingClientsToRevalidatedResource = true;
728     for (auto& handle : m_handlesToRevalidate) {
729         handle->m_resource = m_resourceToRevalidate;
730         m_resourceToRevalidate->registerHandle(handle);
731         --m_handleCount;
732     }
733     ASSERT(!m_handleCount);
734     m_handlesToRevalidate.clear();
735
736     Vector<CachedResourceClient*> clientsToMove;
737     for (auto& entry : m_clients) {
738         CachedResourceClient* client = entry.key;
739         unsigned count = entry.value;
740         while (count) {
741             clientsToMove.append(client);
742             --count;
743         }
744     }
745
746     for (auto& client : clientsToMove)
747         removeClient(*client);
748     ASSERT(m_clients.isEmpty());
749
750     for (auto& client : clientsToMove)
751         m_resourceToRevalidate->addClientToSet(*client);
752     for (auto& client : clientsToMove) {
753         // Calling didAddClient may do anything, including trying to cancel revalidation.
754         // Assert that it didn't succeed.
755         ASSERT(m_resourceToRevalidate);
756         // Calling didAddClient for a client may end up removing another client. In that case it won't be in the set anymore.
757         if (m_resourceToRevalidate->m_clients.contains(client))
758             m_resourceToRevalidate->didAddClient(*client);
759     }
760     m_switchingClientsToRevalidatedResource = false;
761 }
762
763 void CachedResource::updateResponseAfterRevalidation(const ResourceResponse& validatingResponse)
764 {
765     m_responseTimestamp = WallTime::now();
766
767     updateResponseHeadersAfterRevalidation(m_response, validatingResponse);
768 }
769
770 void CachedResource::registerHandle(CachedResourceHandleBase* h)
771 {
772     ++m_handleCount;
773     if (m_resourceToRevalidate)
774         m_handlesToRevalidate.add(h);
775 }
776
777 void CachedResource::unregisterHandle(CachedResourceHandleBase* h)
778 {
779     ASSERT(m_handleCount > 0);
780     --m_handleCount;
781
782     if (m_resourceToRevalidate)
783          m_handlesToRevalidate.remove(h);
784
785     if (!m_handleCount)
786         deleteIfPossible();
787 }
788
789 bool CachedResource::canUseCacheValidator() const
790 {
791     if (m_loading || errorOccurred())
792         return false;
793
794     if (m_response.cacheControlContainsNoStore())
795         return false;
796     return m_response.hasCacheValidatorFields();
797 }
798
799 CachedResource::RevalidationDecision CachedResource::makeRevalidationDecision(CachePolicy cachePolicy) const
800 {    
801     switch (cachePolicy) {
802     case CachePolicyHistoryBuffer:
803         return RevalidationDecision::No;
804
805     case CachePolicyReload:
806         return RevalidationDecision::YesDueToCachePolicy;
807
808     case CachePolicyRevalidate:
809         if (m_response.cacheControlContainsImmutable() && m_response.url().protocolIs("https")) {
810             if (isExpired())
811                 return RevalidationDecision::YesDueToExpired;
812             return RevalidationDecision::No;
813         }
814         return RevalidationDecision::YesDueToCachePolicy;
815
816     case CachePolicyVerify:
817         if (m_response.cacheControlContainsNoCache())
818             return RevalidationDecision::YesDueToNoCache;
819         // FIXME: Cache-Control:no-store should prevent storing, not reuse.
820         if (m_response.cacheControlContainsNoStore())
821             return RevalidationDecision::YesDueToNoStore;
822
823         if (isExpired())
824             return RevalidationDecision::YesDueToExpired;
825
826         return RevalidationDecision::No;
827     };
828     ASSERT_NOT_REACHED();
829     return RevalidationDecision::No;
830 }
831
832 bool CachedResource::redirectChainAllowsReuse(ReuseExpiredRedirectionOrNot reuseExpiredRedirection) const
833 {
834     return WebCore::redirectChainAllowsReuse(m_redirectChainCacheStatus, reuseExpiredRedirection);
835 }
836
837 bool CachedResource::varyHeaderValuesMatch(const ResourceRequest& request)
838 {
839     if (m_varyingHeaderValues.isEmpty())
840         return true;
841
842     return verifyVaryingRequestHeaders(m_varyingHeaderValues, request, m_sessionID);
843 }
844
845 unsigned CachedResource::overheadSize() const
846 {
847     static const int kAverageClientsHashMapSize = 384;
848     return sizeof(CachedResource) + m_response.memoryUsage() + kAverageClientsHashMapSize + m_resourceRequest.url().string().length() * 2;
849 }
850
851 bool CachedResource::areAllClientsXMLHttpRequests() const
852 {
853     if (type() != RawResource)
854         return false;
855
856     for (auto& client : m_clients) {
857         if (!client.key->isXMLHttpRequest())
858             return false;
859     }
860     return true;
861 }
862
863 void CachedResource::setLoadPriority(const std::optional<ResourceLoadPriority>& loadPriority)
864 {
865     if (loadPriority)
866         m_loadPriority = loadPriority.value();
867     else
868         m_loadPriority = defaultPriorityForResourceType(type());
869 }
870
871 inline CachedResource::Callback::Callback(CachedResource& resource, CachedResourceClient& client)
872     : m_resource(resource)
873     , m_client(client)
874     , m_timer(*this, &Callback::timerFired)
875 {
876     m_timer.startOneShot(0_s);
877 }
878
879 inline void CachedResource::Callback::cancel()
880 {
881     if (m_timer.isActive())
882         m_timer.stop();
883 }
884
885 void CachedResource::Callback::timerFired()
886 {
887     m_resource.didAddClient(m_client);
888 }
889
890 #if USE(FOUNDATION) || USE(SOUP)
891
892 void CachedResource::tryReplaceEncodedData(SharedBuffer& newBuffer)
893 {
894     if (!m_data)
895         return;
896     
897     if (!mayTryReplaceEncodedData())
898         return;
899
900     // We have to do the memcmp because we can't tell if the replacement file backed data is for the
901     // same resource or if we made a second request with the same URL which gave us a different
902     // resource. We have seen this happen for cached POST resources.
903     if (m_data->size() != newBuffer.size() || memcmp(m_data->data(), newBuffer.data(), m_data->size()))
904         return;
905
906     m_data->clear();
907     m_data->append(newBuffer);
908     didReplaceSharedBufferContents();
909 }
910
911 #endif
912
913 }