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