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