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