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