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