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