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