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 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 "CachedResourceRequest.h"
34 #include "Frame.h"
35 #include "FrameLoaderClient.h"
36 #include "KURL.h"
37 #include "Logging.h"
38 #include "PurgeableBuffer.h"
39 #include "ResourceHandle.h"
40 #include "SharedBuffer.h"
41 #include <wtf/CurrentTime.h>
42 #include <wtf/MathExtras.h>
43 #include <wtf/RefCountedLeakCounter.h>
44 #include <wtf/StdLibExtras.h>
45 #include <wtf/Vector.h>
46 
47 using namespace WTF;
48 
49 namespace WebCore {
50 
defaultPriorityForResourceType(CachedResource::Type type)51 static ResourceLoadPriority defaultPriorityForResourceType(CachedResource::Type type)
52 {
53     switch (type) {
54         case CachedResource::CSSStyleSheet:
55 #if ENABLE(XSLT)
56         case CachedResource::XSLStyleSheet:
57 #endif
58             return ResourceLoadPriorityHigh;
59         case CachedResource::Script:
60         case CachedResource::FontResource:
61             return ResourceLoadPriorityMedium;
62         case CachedResource::ImageResource:
63             return ResourceLoadPriorityLow;
64 #if ENABLE(LINK_PREFETCH)
65         case CachedResource::LinkResource:
66             return ResourceLoadPriorityVeryLow;
67 #endif
68     }
69     ASSERT_NOT_REACHED();
70     return ResourceLoadPriorityLow;
71 }
72 
73 #ifndef NDEBUG
74 static RefCountedLeakCounter cachedResourceLeakCounter("CachedResource");
75 #endif
76 
CachedResource(const String & url,Type type)77 CachedResource::CachedResource(const String& url, Type type)
78     : m_url(url)
79     , m_request(0)
80     , m_loadPriority(defaultPriorityForResourceType(type))
81     , m_responseTimestamp(currentTime())
82     , m_lastDecodedAccessTime(0)
83     , m_encodedSize(0)
84     , m_decodedSize(0)
85     , m_accessCount(0)
86     , m_handleCount(0)
87     , m_preloadCount(0)
88     , m_preloadResult(PreloadNotReferenced)
89     , m_inLiveDecodedResourcesList(false)
90     , m_requestedFromNetworkingLayer(false)
91     , m_sendResourceLoadCallbacks(true)
92     , m_inCache(false)
93     , m_loading(false)
94     , m_type(type)
95     , m_status(Pending)
96 #ifndef NDEBUG
97     , m_deleted(false)
98     , m_lruIndex(0)
99 #endif
100     , m_nextInAllResourcesList(0)
101     , m_prevInAllResourcesList(0)
102     , m_nextInLiveResourcesList(0)
103     , m_prevInLiveResourcesList(0)
104     , m_owningCachedResourceLoader(0)
105     , m_resourceToRevalidate(0)
106     , m_proxyResource(0)
107 {
108 #ifndef NDEBUG
109     cachedResourceLeakCounter.increment();
110 #endif
111 }
112 
~CachedResource()113 CachedResource::~CachedResource()
114 {
115     ASSERT(!m_resourceToRevalidate); // Should be true because canDelete() checks this.
116     ASSERT(canDelete());
117     ASSERT(!inCache());
118     ASSERT(!m_deleted);
119     ASSERT(url().isNull() || memoryCache()->resourceForURL(KURL(ParsedURLString, url())) != this);
120 
121 #ifndef NDEBUG
122     m_deleted = true;
123     cachedResourceLeakCounter.decrement();
124 #endif
125 
126     if (m_owningCachedResourceLoader)
127         m_owningCachedResourceLoader->removeCachedResource(this);
128 }
129 
load(CachedResourceLoader * cachedResourceLoader,bool incremental,SecurityCheckPolicy securityCheck,bool sendResourceLoadCallbacks)130 void CachedResource::load(CachedResourceLoader* cachedResourceLoader, bool incremental, SecurityCheckPolicy securityCheck, bool sendResourceLoadCallbacks)
131 {
132     m_sendResourceLoadCallbacks = sendResourceLoadCallbacks;
133     cachedResourceLoader->load(this, incremental, securityCheck, sendResourceLoadCallbacks);
134     m_loading = true;
135 }
136 
checkNotify()137 void CachedResource::checkNotify()
138 {
139     if (isLoading())
140         return;
141 
142     CachedResourceClientWalker w(m_clients);
143     while (CachedResourceClient* c = w.next())
144         c->notifyFinished(this);
145 }
146 
data(PassRefPtr<SharedBuffer>,bool allDataReceived)147 void CachedResource::data(PassRefPtr<SharedBuffer>, bool allDataReceived)
148 {
149     if (!allDataReceived)
150         return;
151 
152     setLoading(false);
153     checkNotify();
154 }
155 
error(CachedResource::Status status)156 void CachedResource::error(CachedResource::Status status)
157 {
158     setStatus(status);
159     ASSERT(errorOccurred());
160     m_data.clear();
161 
162     setLoading(false);
163     checkNotify();
164 }
165 
finish()166 void CachedResource::finish()
167 {
168     m_status = Cached;
169 }
170 
isExpired() const171 bool CachedResource::isExpired() const
172 {
173     if (m_response.isNull())
174         return false;
175 
176     return currentAge() > freshnessLifetime();
177 }
178 
currentAge() const179 double CachedResource::currentAge() const
180 {
181     // RFC2616 13.2.3
182     // No compensation for latency as that is not terribly important in practice
183     double dateValue = m_response.date();
184     double apparentAge = isfinite(dateValue) ? max(0., m_responseTimestamp - dateValue) : 0;
185     double ageValue = m_response.age();
186     double correctedReceivedAge = isfinite(ageValue) ? max(apparentAge, ageValue) : apparentAge;
187     double residentTime = currentTime() - m_responseTimestamp;
188     return correctedReceivedAge + residentTime;
189 }
190 
freshnessLifetime() const191 double CachedResource::freshnessLifetime() const
192 {
193     // Cache non-http resources liberally
194     if (!m_response.url().protocolInHTTPFamily())
195         return std::numeric_limits<double>::max();
196 
197     // RFC2616 13.2.4
198     double maxAgeValue = m_response.cacheControlMaxAge();
199     if (isfinite(maxAgeValue))
200         return maxAgeValue;
201     double expiresValue = m_response.expires();
202     double dateValue = m_response.date();
203     double creationTime = isfinite(dateValue) ? dateValue : m_responseTimestamp;
204     if (isfinite(expiresValue))
205         return expiresValue - creationTime;
206     double lastModifiedValue = m_response.lastModified();
207     if (isfinite(lastModifiedValue))
208         return (creationTime - lastModifiedValue) * 0.1;
209     // If no cache headers are present, the specification leaves the decision to the UA. Other browsers seem to opt for 0.
210     return 0;
211 }
212 
setResponse(const ResourceResponse & response)213 void CachedResource::setResponse(const ResourceResponse& response)
214 {
215     m_response = response;
216     m_responseTimestamp = currentTime();
217 }
218 
setSerializedCachedMetadata(const char * data,size_t size)219 void CachedResource::setSerializedCachedMetadata(const char* data, size_t size)
220 {
221     // We only expect to receive cached metadata from the platform once.
222     // If this triggers, it indicates an efficiency problem which is most
223     // likely unexpected in code designed to improve performance.
224     ASSERT(!m_cachedMetadata);
225 
226     m_cachedMetadata = CachedMetadata::deserialize(data, size);
227 }
228 
setCachedMetadata(unsigned dataTypeID,const char * data,size_t size)229 void CachedResource::setCachedMetadata(unsigned dataTypeID, const char* data, size_t size)
230 {
231     // Currently, only one type of cached metadata per resource is supported.
232     // If the need arises for multiple types of metadata per resource this could
233     // be enhanced to store types of metadata in a map.
234     ASSERT(!m_cachedMetadata);
235 
236     m_cachedMetadata = CachedMetadata::create(dataTypeID, data, size);
237     ResourceHandle::cacheMetadata(m_response, m_cachedMetadata->serialize());
238 }
239 
cachedMetadata(unsigned dataTypeID) const240 CachedMetadata* CachedResource::cachedMetadata(unsigned dataTypeID) const
241 {
242     if (!m_cachedMetadata || m_cachedMetadata->dataTypeID() != dataTypeID)
243         return 0;
244     return m_cachedMetadata.get();
245 }
246 
setRequest(CachedResourceRequest * request)247 void CachedResource::setRequest(CachedResourceRequest* request)
248 {
249     if (request && !m_request)
250         m_status = Pending;
251     m_request = request;
252 
253     CachedResourceHandle<CachedResource> protect(this);
254 
255     // All loads finish with data(allDataReceived = true) or error(), except for
256     // canceled loads, which silently set our request to 0. Be sure to notify our
257     // client in that case, so we don't seem to continue loading forever.
258     if (!m_request && isLoading()) {
259         setLoading(false);
260         setStatus(Canceled);
261         checkNotify();
262     }
263 }
264 
addClient(CachedResourceClient * client)265 void CachedResource::addClient(CachedResourceClient* client)
266 {
267     addClientToSet(client);
268     didAddClient(client);
269 }
270 
didAddClient(CachedResourceClient * c)271 void CachedResource::didAddClient(CachedResourceClient* c)
272 {
273     if (!isLoading())
274         c->notifyFinished(this);
275 }
276 
addClientToSet(CachedResourceClient * client)277 void CachedResource::addClientToSet(CachedResourceClient* client)
278 {
279     ASSERT(!isPurgeable());
280 
281     if (m_preloadResult == PreloadNotReferenced) {
282         if (isLoaded())
283             m_preloadResult = PreloadReferencedWhileComplete;
284         else if (m_requestedFromNetworkingLayer)
285             m_preloadResult = PreloadReferencedWhileLoading;
286         else
287             m_preloadResult = PreloadReferenced;
288     }
289     if (!hasClients() && inCache())
290         memoryCache()->addToLiveResourcesSize(this);
291     m_clients.add(client);
292 }
293 
removeClient(CachedResourceClient * client)294 void CachedResource::removeClient(CachedResourceClient* client)
295 {
296     ASSERT(m_clients.contains(client));
297     m_clients.remove(client);
298 
299     if (canDelete() && !inCache())
300         delete this;
301     else if (!hasClients() && inCache()) {
302         memoryCache()->removeFromLiveResourcesSize(this);
303         memoryCache()->removeFromLiveDecodedResourcesList(this);
304         allClientsRemoved();
305         if (response().cacheControlContainsNoStore()) {
306             // RFC2616 14.9.2:
307             // "no-store: ... MUST make a best-effort attempt to remove the information from volatile storage as promptly as possible"
308             // "... History buffers MAY store such responses as part of their normal operation."
309             // We allow non-secure content to be reused in history, but we do not allow secure content to be reused.
310             if (protocolIs(url(), "https"))
311                 memoryCache()->remove(this);
312         } else
313             memoryCache()->prune();
314     }
315     // This object may be dead here.
316 }
317 
deleteIfPossible()318 void CachedResource::deleteIfPossible()
319 {
320     if (canDelete() && !inCache())
321         delete this;
322 }
323 
setDecodedSize(unsigned size)324 void CachedResource::setDecodedSize(unsigned size)
325 {
326     if (size == m_decodedSize)
327         return;
328 
329     int delta = size - m_decodedSize;
330 
331     // The object must now be moved to a different queue, since its size has been changed.
332     // We have to remove explicitly before updating m_decodedSize, so that we find the correct previous
333     // queue.
334     if (inCache())
335         memoryCache()->removeFromLRUList(this);
336 
337     m_decodedSize = size;
338 
339     if (inCache()) {
340         // Now insert into the new LRU list.
341         memoryCache()->insertInLRUList(this);
342 
343         // Insert into or remove from the live decoded list if necessary.
344         // When inserting into the LiveDecodedResourcesList it is possible
345         // that the m_lastDecodedAccessTime is still zero or smaller than
346         // the m_lastDecodedAccessTime of the current list head. This is a
347         // violation of the invariant that the list is to be kept sorted
348         // by access time. The weakening of the invariant does not pose
349         // a problem. For more details please see: https://bugs.webkit.org/show_bug.cgi?id=30209
350         if (m_decodedSize && !m_inLiveDecodedResourcesList && hasClients())
351             memoryCache()->insertInLiveDecodedResourcesList(this);
352         else if (!m_decodedSize && m_inLiveDecodedResourcesList)
353             memoryCache()->removeFromLiveDecodedResourcesList(this);
354 
355         // Update the cache's size totals.
356         memoryCache()->adjustSize(hasClients(), delta);
357     }
358 }
359 
setEncodedSize(unsigned size)360 void CachedResource::setEncodedSize(unsigned size)
361 {
362     if (size == m_encodedSize)
363         return;
364 
365     // The size cannot ever shrink (unless it is being nulled out because of an error).  If it ever does, assert.
366     ASSERT(size == 0 || size >= m_encodedSize);
367 
368     int delta = size - m_encodedSize;
369 
370     // The object must now be moved to a different queue, since its size has been changed.
371     // We have to remove explicitly before updating m_encodedSize, so that we find the correct previous
372     // queue.
373     if (inCache())
374         memoryCache()->removeFromLRUList(this);
375 
376     m_encodedSize = size;
377 
378     if (inCache()) {
379         // Now insert into the new LRU list.
380         memoryCache()->insertInLRUList(this);
381 
382         // Update the cache's size totals.
383         memoryCache()->adjustSize(hasClients(), delta);
384     }
385 }
386 
didAccessDecodedData(double timeStamp)387 void CachedResource::didAccessDecodedData(double timeStamp)
388 {
389     m_lastDecodedAccessTime = timeStamp;
390 
391     if (inCache()) {
392         if (m_inLiveDecodedResourcesList) {
393             memoryCache()->removeFromLiveDecodedResourcesList(this);
394             memoryCache()->insertInLiveDecodedResourcesList(this);
395         }
396         memoryCache()->prune();
397     }
398 }
399 
setResourceToRevalidate(CachedResource * resource)400 void CachedResource::setResourceToRevalidate(CachedResource* resource)
401 {
402     ASSERT(resource);
403     ASSERT(!m_resourceToRevalidate);
404     ASSERT(resource != this);
405     ASSERT(m_handlesToRevalidate.isEmpty());
406     ASSERT(resource->type() == type());
407 
408     LOG(ResourceLoading, "CachedResource %p setResourceToRevalidate %p", this, resource);
409 
410     // The following assert should be investigated whenever it occurs. Although it should never fire, it currently does in rare circumstances.
411     // https://bugs.webkit.org/show_bug.cgi?id=28604.
412     // So the code needs to be robust to this assert failing thus the "if (m_resourceToRevalidate->m_proxyResource == this)" in CachedResource::clearResourceToRevalidate.
413     ASSERT(!resource->m_proxyResource);
414 
415     resource->m_proxyResource = this;
416     m_resourceToRevalidate = resource;
417 }
418 
clearResourceToRevalidate()419 void CachedResource::clearResourceToRevalidate()
420 {
421     ASSERT(m_resourceToRevalidate);
422     // 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.
423     if (m_resourceToRevalidate->m_proxyResource == this) {
424         m_resourceToRevalidate->m_proxyResource = 0;
425         m_resourceToRevalidate->deleteIfPossible();
426     }
427     m_handlesToRevalidate.clear();
428     m_resourceToRevalidate = 0;
429     deleteIfPossible();
430 }
431 
switchClientsToRevalidatedResource()432 void CachedResource::switchClientsToRevalidatedResource()
433 {
434     ASSERT(m_resourceToRevalidate);
435     ASSERT(m_resourceToRevalidate->inCache());
436     ASSERT(!inCache());
437 
438     LOG(ResourceLoading, "CachedResource %p switchClientsToRevalidatedResource %p", this, m_resourceToRevalidate);
439 
440     HashSet<CachedResourceHandleBase*>::iterator end = m_handlesToRevalidate.end();
441     for (HashSet<CachedResourceHandleBase*>::iterator it = m_handlesToRevalidate.begin(); it != end; ++it) {
442         CachedResourceHandleBase* handle = *it;
443         handle->m_resource = m_resourceToRevalidate;
444         m_resourceToRevalidate->registerHandle(handle);
445         --m_handleCount;
446     }
447     ASSERT(!m_handleCount);
448     m_handlesToRevalidate.clear();
449 
450     Vector<CachedResourceClient*> clientsToMove;
451     HashCountedSet<CachedResourceClient*>::iterator end2 = m_clients.end();
452     for (HashCountedSet<CachedResourceClient*>::iterator it = m_clients.begin(); it != end2; ++it) {
453         CachedResourceClient* client = it->first;
454         unsigned count = it->second;
455         while (count) {
456             clientsToMove.append(client);
457             --count;
458         }
459     }
460     // Equivalent of calling removeClient() for all clients
461     m_clients.clear();
462 
463     unsigned moveCount = clientsToMove.size();
464     for (unsigned n = 0; n < moveCount; ++n)
465         m_resourceToRevalidate->addClientToSet(clientsToMove[n]);
466     for (unsigned n = 0; n < moveCount; ++n) {
467         // Calling didAddClient for a client may end up removing another client. In that case it won't be in the set anymore.
468         if (m_resourceToRevalidate->m_clients.contains(clientsToMove[n]))
469             m_resourceToRevalidate->didAddClient(clientsToMove[n]);
470     }
471 }
472 
updateResponseAfterRevalidation(const ResourceResponse & validatingResponse)473 void CachedResource::updateResponseAfterRevalidation(const ResourceResponse& validatingResponse)
474 {
475     m_responseTimestamp = currentTime();
476 
477     DEFINE_STATIC_LOCAL(const AtomicString, contentHeaderPrefix, ("content-"));
478     // RFC2616 10.3.5
479     // Update cached headers from the 304 response
480     const HTTPHeaderMap& newHeaders = validatingResponse.httpHeaderFields();
481     HTTPHeaderMap::const_iterator end = newHeaders.end();
482     for (HTTPHeaderMap::const_iterator it = newHeaders.begin(); it != end; ++it) {
483         // Don't allow 304 response to update content headers, these can't change but some servers send wrong values.
484         if (it->first.startsWith(contentHeaderPrefix, false))
485             continue;
486         m_response.setHTTPHeaderField(it->first, it->second);
487     }
488 }
489 
registerHandle(CachedResourceHandleBase * h)490 void CachedResource::registerHandle(CachedResourceHandleBase* h)
491 {
492     ++m_handleCount;
493     if (m_resourceToRevalidate)
494         m_handlesToRevalidate.add(h);
495 }
496 
unregisterHandle(CachedResourceHandleBase * h)497 void CachedResource::unregisterHandle(CachedResourceHandleBase* h)
498 {
499     ASSERT(m_handleCount > 0);
500     --m_handleCount;
501 
502     if (m_resourceToRevalidate)
503          m_handlesToRevalidate.remove(h);
504 
505     if (!m_handleCount)
506         deleteIfPossible();
507 }
508 
canUseCacheValidator() const509 bool CachedResource::canUseCacheValidator() const
510 {
511     if (m_loading || errorOccurred())
512         return false;
513 
514     if (m_response.cacheControlContainsNoStore())
515         return false;
516     return m_response.hasCacheValidatorFields();
517 }
518 
mustRevalidateDueToCacheHeaders(CachePolicy cachePolicy) const519 bool CachedResource::mustRevalidateDueToCacheHeaders(CachePolicy cachePolicy) const
520 {
521     ASSERT(cachePolicy == CachePolicyRevalidate || cachePolicy == CachePolicyCache || cachePolicy == CachePolicyVerify);
522 
523     if (cachePolicy == CachePolicyRevalidate)
524         return true;
525 
526     if (m_response.cacheControlContainsNoCache() || m_response.cacheControlContainsNoStore()) {
527         LOG(ResourceLoading, "CachedResource %p mustRevalidate because of m_response.cacheControlContainsNoCache() || m_response.cacheControlContainsNoStore()\n", this);
528         return true;
529     }
530 
531     if (cachePolicy == CachePolicyCache) {
532         if (m_response.cacheControlContainsMustRevalidate() && isExpired()) {
533             LOG(ResourceLoading, "CachedResource %p mustRevalidate because of cachePolicy == CachePolicyCache and m_response.cacheControlContainsMustRevalidate() && isExpired()\n", this);
534             return true;
535         }
536         return false;
537     }
538 
539     // CachePolicyVerify
540     if (isExpired()) {
541         LOG(ResourceLoading, "CachedResource %p mustRevalidate because of isExpired()\n", this);
542         return true;
543     }
544 
545     return false;
546 }
547 
isSafeToMakePurgeable() const548 bool CachedResource::isSafeToMakePurgeable() const
549 {
550     return !hasClients() && !m_proxyResource && !m_resourceToRevalidate;
551 }
552 
makePurgeable(bool purgeable)553 bool CachedResource::makePurgeable(bool purgeable)
554 {
555     if (purgeable) {
556         ASSERT(isSafeToMakePurgeable());
557 
558         if (m_purgeableData) {
559             ASSERT(!m_data);
560             return true;
561         }
562         if (!m_data)
563             return false;
564 
565         // Should not make buffer purgeable if it has refs other than this since we don't want two copies.
566         if (!m_data->hasOneRef())
567             return false;
568 
569         if (m_data->hasPurgeableBuffer()) {
570             m_purgeableData = m_data->releasePurgeableBuffer();
571         } else {
572             m_purgeableData = PurgeableBuffer::create(m_data->data(), m_data->size());
573             if (!m_purgeableData)
574                 return false;
575             m_purgeableData->setPurgePriority(purgePriority());
576         }
577 
578         m_purgeableData->makePurgeable(true);
579         m_data.clear();
580         return true;
581     }
582 
583     if (!m_purgeableData)
584         return true;
585     ASSERT(!m_data);
586     ASSERT(!hasClients());
587 
588     if (!m_purgeableData->makePurgeable(false))
589         return false;
590 
591     m_data = SharedBuffer::adoptPurgeableBuffer(m_purgeableData.release());
592     return true;
593 }
594 
isPurgeable() const595 bool CachedResource::isPurgeable() const
596 {
597     return m_purgeableData && m_purgeableData->isPurgeable();
598 }
599 
wasPurged() const600 bool CachedResource::wasPurged() const
601 {
602     return m_purgeableData && m_purgeableData->wasPurged();
603 }
604 
overheadSize() const605 unsigned CachedResource::overheadSize() const
606 {
607     return sizeof(CachedResource) + m_response.memoryUsage() + 576;
608     /*
609         576 = 192 +                   // average size of m_url
610               384;                    // average size of m_clients hash map
611     */
612 }
613 
setLoadPriority(ResourceLoadPriority loadPriority)614 void CachedResource::setLoadPriority(ResourceLoadPriority loadPriority)
615 {
616     if (loadPriority == ResourceLoadPriorityUnresolved)
617         return;
618     m_loadPriority = loadPriority;
619 }
620 
621 }
622