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