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