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