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