2     Copyright (C) 1998 Lars Knoll (knoll@mpi-hd.mpg.de) 
   3     Copyright (C) 2001 Dirk Mueller <mueller@kde.org> 
   4     Copyright (C) 2006 Samuel Weinig (sam.weinig@gmail.com) 
   5     Copyright (C) 2004, 2005, 2006, 2007 Apple Inc. All rights reserved. 
   7     This library is free software; you can redistribute it and/or 
   8     modify it under the terms of the GNU Library General Public 
   9     License as published by the Free Software Foundation; either 
  10     version 2 of the License, or (at your option) any later version. 
  12     This library is distributed in the hope that it will be useful, 
  13     but WITHOUT ANY WARRANTY; without even the implied warranty of 
  14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
  15     Library General Public License for more details. 
  17     You should have received a copy of the GNU Library General Public License 
  18     along with this library; see the file COPYING.LIB.  If not, write to 
  19     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 
  20     Boston, MA 02110-1301, USA. 
  23 #ifndef CachedResource_h 
  24 #define CachedResource_h 
  26 #include "CachePolicy.h" 
  27 #include "PlatformString.h" 
  28 #include "ResourceResponse.h" 
  29 #include "SharedBuffer.h" 
  30 #include <wtf/HashCountedSet.h> 
  31 #include <wtf/HashSet.h> 
  32 #include <wtf/OwnPtr.h> 
  33 #include <wtf/Vector.h> 
  39 class CachedResourceClient
; 
  40 class CachedResourceHandleBase
; 
  42 class InspectorResource
; 
  44 class PurgeableBuffer
; 
  46 // A resource that is held in the cache. Classes who want to use this object should derive 
  47 // from CachedResourceClient, to get the function calls in case the requested data has arrived. 
  48 // This class also does the actual communication with the loader to obtain the resource from the network. 
  49 class CachedResource 
{ 
  51     friend class InspectorResource
; 
  68         NotCached
,    // this URL is not cached 
  69         Unknown
,      // let cache decide what to do with it 
  70         New
,          // inserting new item 
  71         Pending
,      // only partially loaded 
  72         Cached        
// regular case 
  75     CachedResource(const String
& url
, Type
); 
  76     virtual ~CachedResource(); 
  78     virtual void load(DocLoader
* docLoader
)  { load(docLoader
, false, false, true); } 
  79     void load(DocLoader
*, bool incremental
, bool skipCanLoadCheck
, bool sendResourceLoadCallbacks
); 
  81     virtual void setEncoding(const String
&) { } 
  82     virtual String 
encoding() const { return String(); } 
  83     virtual void data(PassRefPtr
<SharedBuffer
> data
, bool allDataReceived
) = 0; 
  84     virtual void error() = 0; 
  86     const String 
&url() const { return m_url
; } 
  87     Type 
type() const { return m_type
; } 
  89     virtual void addClient(CachedResourceClient
*); 
  90     void removeClient(CachedResourceClient
*); 
  91     bool hasClients() const { return !m_clients
.isEmpty(); } 
  92     void deleteIfPossible(); 
  97         PreloadReferencedWhileLoading
, 
  98         PreloadReferencedWhileComplete
 
 100     PreloadResult 
preloadResult() const { return m_preloadResult
; } 
 101     void setRequestedFromNetworkingLayer() { m_requestedFromNetworkingLayer 
= true; } 
 103     virtual void allClientsRemoved() { } 
 105     unsigned count() const { return m_clients
.size(); } 
 107     Status 
status() const { return m_status
; } 
 109     unsigned size() const { return encodedSize() + decodedSize() + overheadSize(); } 
 110     unsigned encodedSize() const { return m_encodedSize
; } 
 111     unsigned decodedSize() const { return m_decodedSize
; } 
 112     unsigned overheadSize() const; 
 114     bool isLoaded() const { return !m_loading
; } 
 115     void setLoading(bool b
) { m_loading 
= b
; } 
 117     virtual bool isImage() const { return false; } 
 119     unsigned accessCount() const { return m_accessCount
; } 
 120     void increaseAccessCount() { m_accessCount
++; } 
 122     // Computes the status of an object after loading.   
 123     // Updates the expire date on the cache entry file 
 126     // Called by the cache if the object has been removed from the cache 
 127     // while still being referenced. This means the object should delete itself 
 128     // if the number of clients observing it ever drops to 0. 
 129     void setInCache(bool b
) { m_inCache 
= b
; } 
 130     bool inCache() const { return m_inCache
; } 
 132     void setInLiveDecodedResourcesList(bool b
) { m_inLiveDecodedResourcesList 
= b
; } 
 133     bool inLiveDecodedResourcesList() { return m_inLiveDecodedResourcesList
; } 
 135     void setRequest(Request
*); 
 137     SharedBuffer
* data() const { ASSERT(!m_purgeableData
); return m_data
.get(); } 
 139     void setResponse(const ResourceResponse
&); 
 140     const ResourceResponse
& response() const { return m_response
; } 
 142     bool canDelete() const { return !hasClients() && !m_request 
&& !m_preloadCount 
&& !m_handleCount 
&& !m_resourceToRevalidate 
&& !m_isBeingRevalidated
; } 
 144     bool isExpired() const; 
 146     virtual bool schedule() const { return false; } 
 148     // List of acceptable MIME types seperated by ",". 
 149     // A MIME type may contain a wildcard, e.g. "text/*". 
 150     String 
accept() const { return m_accept
; } 
 151     void setAccept(const String
& accept
) { m_accept 
= accept
; } 
 153     bool errorOccurred() const { return m_errorOccurred
; } 
 154     bool sendResourceLoadCallbacks() const { return m_sendResourceLoadCallbacks
; } 
 156     virtual void destroyDecodedData() { } 
 158     void setDocLoader(DocLoader
* docLoader
) { m_docLoader 
= docLoader
; } 
 160     bool isPreloaded() const { return m_preloadCount
; } 
 161     void increasePreloadCount() { ++m_preloadCount
; } 
 162     void decreasePreloadCount() { ASSERT(m_preloadCount
); --m_preloadCount
; } 
 164     void registerHandle(CachedResourceHandleBase
* h
) { ++m_handleCount
; if (m_resourceToRevalidate
) m_handlesToRevalidate
.add(h
); } 
 165     void unregisterHandle(CachedResourceHandleBase
* h
) { --m_handleCount
; if (m_resourceToRevalidate
) m_handlesToRevalidate
.remove(h
); if (!m_handleCount
) deleteIfPossible(); } 
 167     bool canUseCacheValidator() const; 
 168     bool mustRevalidate(CachePolicy
) const; 
 169     bool isCacheValidator() const { return m_resourceToRevalidate
; } 
 170     CachedResource
* resourceToRevalidate() const { return m_resourceToRevalidate
; } 
 172     bool isPurgeable() const; 
 173     bool wasPurged() const; 
 176     void setEncodedSize(unsigned); 
 177     void setDecodedSize(unsigned); 
 178     void didAccessDecodedData(double timeStamp
); 
 180     bool makePurgeable(bool purgeable
); 
 181     bool isSafeToMakePurgeable() const; 
 183     DocLoader
* docLoader() const { return m_docLoader
; } 
 185     HashCountedSet
<CachedResourceClient
*> m_clients
; 
 191     ResourceResponse m_response
; 
 192     RefPtr
<SharedBuffer
> m_data
; 
 193     OwnPtr
<PurgeableBuffer
> m_purgeableData
; 
 198     bool m_errorOccurred
; 
 201     // These are called by the friendly Cache only 
 202     void setResourceToRevalidate(CachedResource
*); 
 203     void switchClientsToRevalidatedResource(); 
 204     void clearResourceToRevalidate(); 
 205     void setExpirationDate(time_t expirationDate
) { m_expirationDate 
= expirationDate
; } 
 207     unsigned m_encodedSize
; 
 208     unsigned m_decodedSize
; 
 209     unsigned m_accessCount
; 
 210     unsigned m_inLiveDecodedResourcesList
; 
 211     double m_lastDecodedAccessTime
; // Used as a "thrash guard" in the cache 
 213     bool m_sendResourceLoadCallbacks
; 
 215     unsigned m_preloadCount
; 
 216     PreloadResult m_preloadResult
; 
 217     bool m_requestedFromNetworkingLayer
; 
 222     bool m_expireDateChanged
; 
 229     CachedResource
* m_nextInAllResourcesList
; 
 230     CachedResource
* m_prevInAllResourcesList
; 
 232     CachedResource
* m_nextInLiveResourcesList
; 
 233     CachedResource
* m_prevInLiveResourcesList
; 
 235     DocLoader
* m_docLoader
; // only non-0 for resources that are not in the cache 
 237     unsigned m_handleCount
; 
 238     // If this field is non-null we are using the resource as a proxy for checking whether an existing resource is still up to date 
 239     // using HTTP If-Modified-Since/If-None-Match headers. If the response is 304 all clients of this resource are moved 
 240     // to to be clients of m_resourceToRevalidate and the resource is deleted. If not, the field is zeroed and this 
 241     // resources becomes normal resource load. 
 242     CachedResource
* m_resourceToRevalidate
; 
 243     bool m_isBeingRevalidated
; 
 244     // These handles will need to be updated to point to the m_resourceToRevalidate in case we get 304 response. 
 245     HashSet
<CachedResourceHandleBase
*> m_handlesToRevalidate
; 
 247     time_t m_expirationDate
;