Drop ResourceLoadPriorityUnresolved resource load priority and use Optional<> instead
[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 "PlatformStrategies.h"
46 #include "ResourceHandle.h"
47 #include "ResourceLoadScheduler.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 namespace WebCore {
66
67 static ResourceLoadPriority defaultPriorityForResourceType(CachedResource::Type type)
68 {
69     switch (type) {
70     case CachedResource::MainResource:
71         return ResourceLoadPriorityVeryHigh;
72     case CachedResource::CSSStyleSheet:
73         return ResourceLoadPriorityHigh;
74     case CachedResource::Script:
75 #if ENABLE(SVG_FONTS)
76     case CachedResource::SVGFontResource:
77 #endif
78     case CachedResource::FontResource:
79     case CachedResource::RawResource:
80         return ResourceLoadPriorityMedium;
81     case CachedResource::ImageResource:
82         return ResourceLoadPriorityLow;
83 #if ENABLE(XSLT)
84     case CachedResource::XSLStyleSheet:
85         return ResourceLoadPriorityHigh;
86 #endif
87     case CachedResource::SVGDocumentResource:
88         return ResourceLoadPriorityLow;
89 #if ENABLE(LINK_PREFETCH)
90     case CachedResource::LinkPrefetch:
91         return ResourceLoadPriorityVeryLow;
92     case CachedResource::LinkSubresource:
93         return ResourceLoadPriorityVeryLow;
94 #endif
95 #if ENABLE(VIDEO_TRACK)
96     case CachedResource::TextTrackResource:
97         return ResourceLoadPriorityLow;
98 #endif
99     }
100     ASSERT_NOT_REACHED();
101     return ResourceLoadPriorityLow;
102 }
103
104 static std::chrono::milliseconds deadDecodedDataDeletionIntervalForResourceType(CachedResource::Type type)
105 {
106     if (type == CachedResource::Script)
107         return std::chrono::milliseconds { 0 };
108
109     return MemoryCache::singleton().deadDecodedDataDeletionInterval();
110 }
111
112 DEFINE_DEBUG_ONLY_GLOBAL(RefCountedLeakCounter, cachedResourceLeakCounter, ("CachedResource"));
113
114 CachedResource::CachedResource(const ResourceRequest& request, Type type, SessionID sessionID)
115     : m_resourceRequest(request)
116     , m_decodedDataDeletionTimer(*this, &CachedResource::decodedDataDeletionTimerFired, deadDecodedDataDeletionIntervalForResourceType(type))
117     , m_sessionID(sessionID)
118     , m_loadPriority(defaultPriorityForResourceType(type))
119     , m_responseTimestamp(currentTime())
120     , m_lastDecodedAccessTime(0)
121     , m_loadFinishTime(0)
122     , m_encodedSize(0)
123     , m_decodedSize(0)
124     , m_accessCount(0)
125     , m_handleCount(0)
126     , m_preloadCount(0)
127     , m_preloadResult(PreloadNotReferenced)
128     , m_requestedFromNetworkingLayer(false)
129     , m_inCache(false)
130     , m_loading(false)
131     , m_switchingClientsToRevalidatedResource(false)
132     , m_type(type)
133     , m_status(Pending)
134 #ifndef NDEBUG
135     , m_deleted(false)
136     , m_lruIndex(0)
137 #endif
138     , m_nextInAllResourcesList(0)
139     , m_prevInAllResourcesList(0)
140     , m_owningCachedResourceLoader(0)
141     , m_resourceToRevalidate(0)
142     , m_proxyResource(0)
143 {
144     ASSERT(m_type == unsigned(type)); // m_type is a bitfield, so this tests careless updates of the enum.
145     ASSERT(sessionID.isValid());
146 #ifndef NDEBUG
147     cachedResourceLeakCounter.increment();
148 #endif
149
150     if (!m_resourceRequest.url().hasFragmentIdentifier())
151         return;
152     URL urlForCache = MemoryCache::removeFragmentIdentifierIfNeeded(m_resourceRequest.url());
153     if (urlForCache.hasFragmentIdentifier())
154         return;
155     m_fragmentIdentifierForRequest = m_resourceRequest.url().fragmentIdentifier();
156     m_resourceRequest.setURL(urlForCache);
157 }
158
159 CachedResource::~CachedResource()
160 {
161     ASSERT(!m_resourceToRevalidate); // Should be true because canDelete() checks this.
162     ASSERT(canDelete());
163     ASSERT(!inCache());
164     ASSERT(!m_deleted);
165     ASSERT(url().isNull() || MemoryCache::singleton().resourceForRequest(resourceRequest(), sessionID()) != this);
166
167 #ifndef NDEBUG
168     m_deleted = true;
169     cachedResourceLeakCounter.decrement();
170 #endif
171
172     if (m_owningCachedResourceLoader)
173         m_owningCachedResourceLoader->removeCachedResource(*this);
174 }
175
176 void CachedResource::failBeforeStarting()
177 {
178     // FIXME: What if resources in other frames were waiting for this revalidation?
179     LOG(ResourceLoading, "Cannot start loading '%s'", url().string().latin1().data());
180     if (m_resourceToRevalidate) 
181         MemoryCache::singleton().revalidationFailed(*this);
182     error(CachedResource::LoadError);
183 }
184
185 void CachedResource::addAdditionalRequestHeaders(CachedResourceLoader& cachedResourceLoader)
186 {
187     // Note: We skip the Content-Security-Policy check here because we check
188     // the Content-Security-Policy at the CachedResourceLoader layer so we can
189     // handle different resource types differently.
190
191     FrameLoader& frameLoader = cachedResourceLoader.frame()->loader();
192     String outgoingReferrer;
193     String outgoingOrigin;
194     if (m_resourceRequest.httpReferrer().isNull()) {
195         outgoingReferrer = frameLoader.outgoingReferrer();
196         outgoingOrigin = frameLoader.outgoingOrigin();
197     } else {
198         outgoingReferrer = m_resourceRequest.httpReferrer();
199         outgoingOrigin = SecurityOrigin::createFromString(outgoingReferrer)->toString();
200     }
201
202     outgoingReferrer = SecurityPolicy::generateReferrerHeader(cachedResourceLoader.document()->referrerPolicy(), m_resourceRequest.url(), outgoingReferrer);
203     if (outgoingReferrer.isEmpty())
204         m_resourceRequest.clearHTTPReferrer();
205     else
206         m_resourceRequest.setHTTPReferrer(outgoingReferrer);
207     FrameLoader::addHTTPOriginIfNeeded(m_resourceRequest, outgoingOrigin);
208
209     frameLoader.addExtraFieldsToSubresourceRequest(m_resourceRequest);
210 }
211
212 void CachedResource::load(CachedResourceLoader& cachedResourceLoader, const ResourceLoaderOptions& options)
213 {
214     if (!cachedResourceLoader.frame()) {
215         failBeforeStarting();
216         return;
217     }
218
219     FrameLoader& frameLoader = cachedResourceLoader.frame()->loader();
220     if (options.securityCheck() == DoSecurityCheck && (frameLoader.state() == FrameStateProvisional || !frameLoader.activeDocumentLoader() || frameLoader.activeDocumentLoader()->isStopping())) {
221         failBeforeStarting();
222         return;
223     }
224
225     m_options = options;
226     m_loading = true;
227
228 #if USE(QUICK_LOOK)
229     if (!m_resourceRequest.isNull() && m_resourceRequest.url().protocolIs(QLPreviewProtocol())) {
230         // When QuickLook is invoked to convert a document, it returns a unique URL in the
231         // NSURLReponse for the main document. To make safeQLURLForDocumentURLAndResourceURL()
232         // work, we need to use the QL URL not the original URL.
233         const URL& documentURL = cachedResourceLoader.frame() ? cachedResourceLoader.frame()->loader().documentLoader()->response().url() : cachedResourceLoader.document()->url();
234         m_resourceRequest.setURL(safeQLURLForDocumentURLAndResourceURL(documentURL, url()));
235     }
236 #endif
237
238     if (!accept().isEmpty())
239         m_resourceRequest.setHTTPAccept(accept());
240
241     if (isCacheValidator()) {
242         CachedResource* resourceToRevalidate = m_resourceToRevalidate;
243         ASSERT(resourceToRevalidate->canUseCacheValidator());
244         ASSERT(resourceToRevalidate->isLoaded());
245         const String& lastModified = resourceToRevalidate->response().httpHeaderField(HTTPHeaderName::LastModified);
246         const String& eTag = resourceToRevalidate->response().httpHeaderField(HTTPHeaderName::ETag);
247         if (!lastModified.isEmpty() || !eTag.isEmpty()) {
248             ASSERT(cachedResourceLoader.cachePolicy(type()) != CachePolicyReload);
249             if (cachedResourceLoader.cachePolicy(type()) == CachePolicyRevalidate)
250                 m_resourceRequest.setHTTPHeaderField(HTTPHeaderName::CacheControl, "max-age=0");
251             if (!lastModified.isEmpty())
252                 m_resourceRequest.setHTTPHeaderField(HTTPHeaderName::IfModifiedSince, lastModified);
253             if (!eTag.isEmpty())
254                 m_resourceRequest.setHTTPHeaderField(HTTPHeaderName::IfNoneMatch, eTag);
255         }
256     }
257
258 #if ENABLE(LINK_PREFETCH)
259     if (type() == CachedResource::LinkPrefetch || type() == CachedResource::LinkSubresource)
260         m_resourceRequest.setHTTPHeaderField(HTTPHeaderName::Purpose, "prefetch");
261 #endif
262     m_resourceRequest.setPriority(loadPriority());
263
264     if (type() != MainResource)
265         addAdditionalRequestHeaders(cachedResourceLoader);
266
267     // FIXME: It's unfortunate that the cache layer and below get to know anything about fragment identifiers.
268     // We should look into removing the expectation of that knowledge from the platform network stacks.
269     ResourceRequest request(m_resourceRequest);
270     if (!m_fragmentIdentifierForRequest.isNull()) {
271         URL url = request.url();
272         url.setFragmentIdentifier(m_fragmentIdentifierForRequest);
273         request.setURL(url);
274         m_fragmentIdentifierForRequest = String();
275     }
276
277     m_loader = platformStrategies()->loaderStrategy()->resourceLoadScheduler()->scheduleSubresourceLoad(cachedResourceLoader.frame(), this, request, options);
278     if (!m_loader) {
279         failBeforeStarting();
280         return;
281     }
282
283     m_status = Pending;
284 }
285
286 void CachedResource::checkNotify()
287 {
288     if (isLoading() || stillNeedsLoad())
289         return;
290
291     CachedResourceClientWalker<CachedResourceClient> walker(m_clients);
292     while (CachedResourceClient* client = walker.next())
293         client->notifyFinished(this);
294 }
295
296 void CachedResource::addDataBuffer(SharedBuffer&)
297 {
298     ASSERT(dataBufferingPolicy() == BufferData);
299 }
300
301 void CachedResource::addData(const char*, unsigned)
302 {
303     ASSERT(dataBufferingPolicy() == DoNotBufferData);
304 }
305
306 void CachedResource::finishLoading(SharedBuffer*)
307 {
308     setLoading(false);
309     checkNotify();
310 }
311
312 void CachedResource::error(CachedResource::Status status)
313 {
314     setStatus(status);
315     ASSERT(errorOccurred());
316     m_data.clear();
317
318     setLoading(false);
319     checkNotify();
320 }
321     
322 void CachedResource::cancelLoad()
323 {
324     if (!isLoading() && !stillNeedsLoad())
325         return;
326
327     setStatus(LoadError);
328     setLoading(false);
329     checkNotify();
330 }
331
332 void CachedResource::finish()
333 {
334     if (!errorOccurred())
335         m_status = Cached;
336 }
337
338 bool CachedResource::passesAccessControlCheck(SecurityOrigin* securityOrigin)
339 {
340     String errorDescription;
341     return WebCore::passesAccessControlCheck(m_response, resourceRequest().allowCookies() ? AllowStoredCredentials : DoNotAllowStoredCredentials, securityOrigin, errorDescription);
342 }
343
344 bool CachedResource::isExpired() const
345 {
346     if (m_response.isNull())
347         return false;
348
349     return computeCurrentAge(m_response, m_responseTimestamp) > freshnessLifetime(m_response);
350 }
351
352 double CachedResource::freshnessLifetime(const ResourceResponse& response) const
353 {
354     if (!response.url().protocolIsInHTTPFamily()) {
355         // Don't cache non-HTTP main resources since we can't check for freshness.
356         // FIXME: We should not cache subresources either, but when we tried this
357         // it caused performance and flakiness issues in our test infrastructure.
358         if (m_type == MainResource && !SchemeRegistry::shouldCacheResponsesFromURLSchemeIndefinitely(response.url().protocol()))
359             return 0;
360
361         return std::numeric_limits<double>::max();
362     }
363
364     return computeFreshnessLifetimeForHTTPFamily(response, m_responseTimestamp);
365 }
366
367 void CachedResource::willSendRequest(ResourceRequest&, const ResourceResponse& response)
368 {
369     m_requestedFromNetworkingLayer = true;
370     if (response.isNull())
371         return;
372     updateRedirectChainStatus(m_redirectChainCacheStatus, response);
373 }
374
375 void CachedResource::responseReceived(const ResourceResponse& response)
376 {
377     setResponse(response);
378     m_responseTimestamp = currentTime();
379     String encoding = response.textEncodingName();
380     if (!encoding.isNull())
381         setEncoding(encoding);
382 }
383
384 void CachedResource::clearLoader()
385 {
386     ASSERT(m_loader);
387     m_loader = 0;
388 }
389
390 void CachedResource::addClient(CachedResourceClient* client)
391 {
392     if (addClientToSet(client))
393         didAddClient(client);
394 }
395
396 void CachedResource::didAddClient(CachedResourceClient* client)
397 {
398     if (m_decodedDataDeletionTimer.isActive())
399         m_decodedDataDeletionTimer.stop();
400
401     if (m_clientsAwaitingCallback.remove(client))
402         m_clients.add(client);
403     if (!isLoading() && !stillNeedsLoad())
404         client->notifyFinished(this);
405 }
406
407 bool CachedResource::addClientToSet(CachedResourceClient* client)
408 {
409     if (m_preloadResult == PreloadNotReferenced) {
410         if (isLoaded())
411             m_preloadResult = PreloadReferencedWhileComplete;
412         else if (m_requestedFromNetworkingLayer)
413             m_preloadResult = PreloadReferencedWhileLoading;
414         else
415             m_preloadResult = PreloadReferenced;
416     }
417     if (!hasClients() && inCache())
418         MemoryCache::singleton().addToLiveResourcesSize(*this);
419
420     if ((m_type == RawResource || m_type == MainResource) && !m_response.isNull() && !m_proxyResource) {
421         // Certain resources (especially XHRs and main resources) do crazy things if an asynchronous load returns
422         // synchronously (e.g., scripts may not have set all the state they need to handle the load).
423         // Therefore, rather than immediately sending callbacks on a cache hit like other CachedResources,
424         // we schedule the callbacks and ensure we never finish synchronously.
425         ASSERT(!m_clientsAwaitingCallback.contains(client));
426         m_clientsAwaitingCallback.add(client, std::make_unique<Callback>(*this, *client));
427         return false;
428     }
429
430     m_clients.add(client);
431     return true;
432 }
433
434 void CachedResource::removeClient(CachedResourceClient* client)
435 {
436     auto callback = m_clientsAwaitingCallback.take(client);
437     if (callback) {
438         ASSERT(!m_clients.contains(client));
439         callback->cancel();
440         callback = nullptr;
441     } else {
442         ASSERT(m_clients.contains(client));
443         m_clients.remove(client);
444         didRemoveClient(client);
445     }
446
447     bool deleted = deleteIfPossible();
448     if (!deleted && !hasClients()) {
449         auto& memoryCache = MemoryCache::singleton();
450         if (inCache()) {
451             memoryCache.removeFromLiveResourcesSize(*this);
452             memoryCache.removeFromLiveDecodedResourcesList(*this);
453         }
454         if (!m_switchingClientsToRevalidatedResource)
455             allClientsRemoved();
456         destroyDecodedDataIfNeeded();
457         if (response().cacheControlContainsNoStore() && url().protocolIs("https")) {
458             // RFC2616 14.9.2:
459             // "no-store: ... MUST make a best-effort attempt to remove the information from volatile storage as promptly as possible"
460             // "... History buffers MAY store such responses as part of their normal operation."
461             // We allow non-secure content to be reused in history, but we do not allow secure content to be reused.
462             memoryCache.remove(*this);
463         }
464         memoryCache.prune();
465     }
466     // This object may be dead here.
467 }
468
469 void CachedResource::destroyDecodedDataIfNeeded()
470 {
471     if (!m_decodedSize)
472         return;
473     if (!MemoryCache::singleton().deadDecodedDataDeletionInterval().count())
474         return;
475     m_decodedDataDeletionTimer.restart();
476 }
477
478 void CachedResource::decodedDataDeletionTimerFired()
479 {
480     destroyDecodedData();
481 }
482
483 bool CachedResource::deleteIfPossible()
484 {
485     if (canDelete() && !inCache()) {
486         InspectorInstrumentation::willDestroyCachedResource(*this);
487         delete this;
488         return true;
489     }
490     return false;
491 }
492
493 void CachedResource::setDecodedSize(unsigned size)
494 {
495     if (size == m_decodedSize)
496         return;
497
498     int delta = size - m_decodedSize;
499
500     // The object must be moved to a different queue, since its size has been changed.
501     // Remove before updating m_decodedSize, so we find the resource in the correct LRU list.
502     if (inCache())
503         MemoryCache::singleton().removeFromLRUList(*this);
504     
505     m_decodedSize = size;
506    
507     if (inCache()) {
508         auto& memoryCache = MemoryCache::singleton();
509         // Now insert into the new LRU list.
510         memoryCache.insertInLRUList(*this);
511         
512         // Insert into or remove from the live decoded list if necessary.
513         // When inserting into the LiveDecodedResourcesList it is possible
514         // that the m_lastDecodedAccessTime is still zero or smaller than
515         // the m_lastDecodedAccessTime of the current list head. This is a
516         // violation of the invariant that the list is to be kept sorted
517         // by access time. The weakening of the invariant does not pose
518         // a problem. For more details please see: https://bugs.webkit.org/show_bug.cgi?id=30209
519         bool inLiveDecodedResourcesList = memoryCache.inLiveDecodedResourcesList(*this);
520         if (m_decodedSize && !inLiveDecodedResourcesList && hasClients())
521             memoryCache.insertInLiveDecodedResourcesList(*this);
522         else if (!m_decodedSize && inLiveDecodedResourcesList)
523             memoryCache.removeFromLiveDecodedResourcesList(*this);
524
525         // Update the cache's size totals.
526         memoryCache.adjustSize(hasClients(), delta);
527     }
528 }
529
530 void CachedResource::setEncodedSize(unsigned size)
531 {
532     if (size == m_encodedSize)
533         return;
534
535     int delta = size - m_encodedSize;
536
537     // The object must be moved to a different queue, since its size has been changed.
538     // Remove before updating m_encodedSize, so we find the resource in the correct LRU list.
539     if (inCache())
540         MemoryCache::singleton().removeFromLRUList(*this);
541
542     m_encodedSize = size;
543
544     if (inCache()) {
545         auto& memoryCache = MemoryCache::singleton();
546         memoryCache.insertInLRUList(*this);
547         memoryCache.adjustSize(hasClients(), delta);
548     }
549 }
550
551 void CachedResource::didAccessDecodedData(double timeStamp)
552 {
553     m_lastDecodedAccessTime = timeStamp;
554     
555     if (inCache()) {
556         auto& memoryCache = MemoryCache::singleton();
557         if (memoryCache.inLiveDecodedResourcesList(*this)) {
558             memoryCache.removeFromLiveDecodedResourcesList(*this);
559             memoryCache.insertInLiveDecodedResourcesList(*this);
560         }
561         memoryCache.prune();
562     }
563 }
564     
565 void CachedResource::setResourceToRevalidate(CachedResource* resource) 
566
567     ASSERT(resource);
568     ASSERT(!m_resourceToRevalidate);
569     ASSERT(resource != this);
570     ASSERT(m_handlesToRevalidate.isEmpty());
571     ASSERT(resource->type() == type());
572
573     LOG(ResourceLoading, "CachedResource %p setResourceToRevalidate %p", this, resource);
574
575     // The following assert should be investigated whenever it occurs. Although it should never fire, it currently does in rare circumstances.
576     // https://bugs.webkit.org/show_bug.cgi?id=28604.
577     // So the code needs to be robust to this assert failing thus the "if (m_resourceToRevalidate->m_proxyResource == this)" in CachedResource::clearResourceToRevalidate.
578     ASSERT(!resource->m_proxyResource);
579
580     resource->m_proxyResource = this;
581     m_resourceToRevalidate = resource;
582 }
583
584 void CachedResource::clearResourceToRevalidate() 
585
586     ASSERT(m_resourceToRevalidate);
587     if (m_switchingClientsToRevalidatedResource)
588         return;
589
590     // A resource may start revalidation before this method has been called, so check that this resource is still the proxy resource before clearing it out.
591     if (m_resourceToRevalidate->m_proxyResource == this) {
592         m_resourceToRevalidate->m_proxyResource = 0;
593         m_resourceToRevalidate->deleteIfPossible();
594     }
595     m_handlesToRevalidate.clear();
596     m_resourceToRevalidate = 0;
597     deleteIfPossible();
598 }
599     
600 void CachedResource::switchClientsToRevalidatedResource()
601 {
602     ASSERT(m_resourceToRevalidate);
603     ASSERT(m_resourceToRevalidate->inCache());
604     ASSERT(!inCache());
605
606     LOG(ResourceLoading, "CachedResource %p switchClientsToRevalidatedResource %p", this, m_resourceToRevalidate);
607
608     m_switchingClientsToRevalidatedResource = true;
609     HashSet<CachedResourceHandleBase*>::iterator end = m_handlesToRevalidate.end();
610     for (HashSet<CachedResourceHandleBase*>::iterator it = m_handlesToRevalidate.begin(); it != end; ++it) {
611         CachedResourceHandleBase* handle = *it;
612         handle->m_resource = m_resourceToRevalidate;
613         m_resourceToRevalidate->registerHandle(handle);
614         --m_handleCount;
615     }
616     ASSERT(!m_handleCount);
617     m_handlesToRevalidate.clear();
618
619     Vector<CachedResourceClient*> clientsToMove;
620     HashCountedSet<CachedResourceClient*>::iterator end2 = m_clients.end();
621     for (HashCountedSet<CachedResourceClient*>::iterator it = m_clients.begin(); it != end2; ++it) {
622         CachedResourceClient* client = it->key;
623         unsigned count = it->value;
624         while (count) {
625             clientsToMove.append(client);
626             --count;
627         }
628     }
629
630     unsigned moveCount = clientsToMove.size();
631     for (unsigned n = 0; n < moveCount; ++n)
632         removeClient(clientsToMove[n]);
633     ASSERT(m_clients.isEmpty());
634
635     for (unsigned n = 0; n < moveCount; ++n)
636         m_resourceToRevalidate->addClientToSet(clientsToMove[n]);
637     for (unsigned n = 0; n < moveCount; ++n) {
638         // Calling didAddClient may do anything, including trying to cancel revalidation.
639         // Assert that it didn't succeed.
640         ASSERT(m_resourceToRevalidate);
641         // Calling didAddClient for a client may end up removing another client. In that case it won't be in the set anymore.
642         if (m_resourceToRevalidate->m_clients.contains(clientsToMove[n]))
643             m_resourceToRevalidate->didAddClient(clientsToMove[n]);
644     }
645     m_switchingClientsToRevalidatedResource = false;
646 }
647
648 void CachedResource::updateResponseAfterRevalidation(const ResourceResponse& validatingResponse)
649 {
650     m_responseTimestamp = currentTime();
651
652     updateResponseHeadersAfterRevalidation(m_response, validatingResponse);
653 }
654
655 void CachedResource::registerHandle(CachedResourceHandleBase* h)
656 {
657     ++m_handleCount;
658     if (m_resourceToRevalidate)
659         m_handlesToRevalidate.add(h);
660 }
661
662 void CachedResource::unregisterHandle(CachedResourceHandleBase* h)
663 {
664     ASSERT(m_handleCount > 0);
665     --m_handleCount;
666
667     if (m_resourceToRevalidate)
668          m_handlesToRevalidate.remove(h);
669
670     if (!m_handleCount)
671         deleteIfPossible();
672 }
673
674 bool CachedResource::canUseCacheValidator() const
675 {
676     if (m_loading || errorOccurred())
677         return false;
678
679     if (m_response.cacheControlContainsNoStore())
680         return false;
681     return m_response.hasCacheValidatorFields();
682 }
683
684 static inline void logResourceRevalidationReason(Frame* frame, const String& reason)
685 {
686     if (frame)
687         frame->mainFrame().diagnosticLoggingClient().logDiagnosticMessageWithValue(DiagnosticLoggingKeys::cachedResourceRevalidationKey(), DiagnosticLoggingKeys::reasonKey(), reason);
688 }
689
690 bool CachedResource::mustRevalidateDueToCacheHeaders(const CachedResourceLoader& cachedResourceLoader, CachePolicy cachePolicy) const
691 {    
692     ASSERT(cachePolicy == CachePolicyRevalidate || cachePolicy == CachePolicyCache || cachePolicy == CachePolicyVerify);
693
694     if (cachePolicy == CachePolicyRevalidate) {
695         logResourceRevalidationReason(cachedResourceLoader.frame(), DiagnosticLoggingKeys::reloadKey());
696         return true;
697     }
698
699     if (m_response.cacheControlContainsNoCache() || m_response.cacheControlContainsNoStore()) {
700         LOG(ResourceLoading, "CachedResource %p mustRevalidate because of m_response.cacheControlContainsNoCache() || m_response.cacheControlContainsNoStore()\n", this);
701         if (m_response.cacheControlContainsNoStore())
702             logResourceRevalidationReason(cachedResourceLoader.frame(), DiagnosticLoggingKeys::noStoreKey());
703         else
704             logResourceRevalidationReason(cachedResourceLoader.frame(), DiagnosticLoggingKeys::noCacheKey());
705
706         return true;
707     }
708
709     if (cachePolicy == CachePolicyCache) {
710         if (m_response.cacheControlContainsMustRevalidate() && isExpired()) {
711             LOG(ResourceLoading, "CachedResource %p mustRevalidate because of cachePolicy == CachePolicyCache and m_response.cacheControlContainsMustRevalidate() && isExpired()\n", this);
712             logResourceRevalidationReason(cachedResourceLoader.frame(), DiagnosticLoggingKeys::mustRevalidateIsExpiredKey());
713             return true;
714         }
715         return false;
716     }
717
718     // CachePolicyVerify
719     if (isExpired()) {
720         LOG(ResourceLoading, "CachedResource %p mustRevalidate because of isExpired()\n", this);
721         logResourceRevalidationReason(cachedResourceLoader.frame(), DiagnosticLoggingKeys::isExpiredKey());
722         return true;
723     }
724
725     return false;
726 }
727
728 bool CachedResource::redirectChainAllowsReuse(ReuseExpiredRedirectionOrNot reuseExpiredRedirection) const
729 {
730     return WebCore::redirectChainAllowsReuse(m_redirectChainCacheStatus, reuseExpiredRedirection);
731 }
732
733 unsigned CachedResource::overheadSize() const
734 {
735     static const int kAverageClientsHashMapSize = 384;
736     return sizeof(CachedResource) + m_response.memoryUsage() + kAverageClientsHashMapSize + m_resourceRequest.url().string().length() * 2;
737 }
738
739 void CachedResource::setLoadPriority(const Optional<ResourceLoadPriority>& loadPriority)
740 {
741     if (loadPriority)
742         m_loadPriority = loadPriority.value();
743     else
744         m_loadPriority = defaultPriorityForResourceType(type());
745 }
746
747 inline CachedResource::Callback::Callback(CachedResource& resource, CachedResourceClient& client)
748     : m_resource(resource)
749     , m_client(client)
750     , m_timer(*this, &Callback::timerFired)
751 {
752     m_timer.startOneShot(0);
753 }
754
755 inline void CachedResource::Callback::cancel()
756 {
757     if (m_timer.isActive())
758         m_timer.stop();
759 }
760
761 void CachedResource::Callback::timerFired()
762 {
763     m_resource.didAddClient(&m_client);
764 }
765
766 #if USE(FOUNDATION)
767
768 void CachedResource::tryReplaceEncodedData(SharedBuffer& newBuffer)
769 {
770     if (!m_data)
771         return;
772     
773     if (!mayTryReplaceEncodedData())
774         return;
775
776     // We have to do the memcmp because we can't tell if the replacement file backed data is for the
777     // same resource or if we made a second request with the same URL which gave us a different
778     // resource. We have seen this happen for cached POST resources.
779     if (m_data->size() != newBuffer.size() || memcmp(m_data->data(), newBuffer.data(), m_data->size()))
780         return;
781
782     m_data->tryReplaceContentsWithPlatformBuffer(newBuffer);
783 }
784
785 #endif
786
787 }