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/Vector.h>
38 class CachedResourceClient;
39 class CachedResourceHandleBase;
43 // A resource that is held in the cache. Classes who want to use this object should derive
44 // from CachedResourceClient, to get the function calls in case the requested data has arrived.
45 // This class also does the actual communication with the loader to obtain the resource from the network.
46 class CachedResource {
64 NotCached, // this URL is not cached
65 Unknown, // let cache decide what to do with it
66 New, // inserting new item
67 Pending, // only partially loaded
68 Cached // regular case
71 CachedResource(const String& url, Type);
72 virtual ~CachedResource();
74 virtual void load(DocLoader* docLoader) { load(docLoader, false, false, true); }
75 void load(DocLoader*, bool incremental, bool skipCanLoadCheck, bool sendResourceLoadCallbacks);
77 virtual void setEncoding(const String&) { }
78 virtual String encoding() const { return String(); }
79 virtual void data(PassRefPtr<SharedBuffer> data, bool allDataReceived) = 0;
80 virtual void error() = 0;
82 const String &url() const { return m_url; }
83 Type type() const { return m_type; }
85 virtual void addClient(CachedResourceClient*);
86 void removeClient(CachedResourceClient*);
87 bool hasClients() const { return !m_clients.isEmpty(); }
88 void deleteIfPossible();
93 PreloadReferencedWhileLoading,
94 PreloadReferencedWhileComplete
96 PreloadResult preloadResult() const { return m_preloadResult; }
97 void setRequestedFromNetworkingLayer() { m_requestedFromNetworkingLayer = true; }
99 virtual void allClientsRemoved() { };
101 unsigned count() const { return m_clients.size(); }
103 Status status() const { return m_status; }
105 unsigned size() const { return encodedSize() + decodedSize(); }
106 unsigned encodedSize() const { return m_encodedSize; }
107 unsigned decodedSize() const { return m_decodedSize; }
109 bool isLoaded() const { return !m_loading; }
110 void setLoading(bool b) { m_loading = b; }
112 virtual bool isImage() const { return false; }
114 unsigned accessCount() const { return m_accessCount; }
115 void increaseAccessCount() { m_accessCount++; }
117 // Computes the status of an object after loading.
118 // Updates the expire date on the cache entry file
121 // Called by the cache if the object has been removed from the cache
122 // while still being referenced. This means the object should delete itself
123 // if the number of clients observing it ever drops to 0.
124 void setInCache(bool b) { m_inCache = b; }
125 bool inCache() const { return m_inCache; }
127 void setInLiveDecodedResourcesList(bool b) { m_inLiveDecodedResourcesList = b; }
128 bool inLiveDecodedResourcesList() { return m_inLiveDecodedResourcesList; }
130 void setRequest(Request*);
132 SharedBuffer* data() const { return m_data.get(); }
134 void setResponse(const ResourceResponse&);
135 const ResourceResponse& response() const { return m_response; }
137 bool canDelete() const { return !hasClients() && !m_request && !m_preloadCount && !m_handleCount && !m_resourceToRevalidate && !m_isBeingRevalidated; }
139 bool isExpired() const;
141 virtual bool schedule() const { return false; }
143 // List of acceptable MIME types seperated by ",".
144 // A MIME type may contain a wildcard, e.g. "text/*".
145 String accept() const { return m_accept; }
146 void setAccept(const String& accept) { m_accept = accept; }
148 bool errorOccurred() const { return m_errorOccurred; }
149 bool sendResourceLoadCallbacks() const { return m_sendResourceLoadCallbacks; }
151 virtual void destroyDecodedData() {};
153 void setDocLoader(DocLoader* docLoader) { m_docLoader = docLoader; }
155 bool isPreloaded() const { return m_preloadCount; }
156 void increasePreloadCount() { ++m_preloadCount; }
157 void decreasePreloadCount() { ASSERT(m_preloadCount); --m_preloadCount; }
159 void registerHandle(CachedResourceHandleBase* h) { ++m_handleCount; if (m_resourceToRevalidate) m_handlesToRevalidate.add(h); }
160 void unregisterHandle(CachedResourceHandleBase* h) { --m_handleCount; if (m_resourceToRevalidate) m_handlesToRevalidate.remove(h); if (!m_handleCount) deleteIfPossible(); }
162 bool canUseCacheValidator() const;
163 bool mustRevalidate(CachePolicy) const;
164 bool isCacheValidator() const { return m_resourceToRevalidate; }
165 CachedResource* resourceToRevalidate() const { return m_resourceToRevalidate; }
168 void setEncodedSize(unsigned);
169 void setDecodedSize(unsigned);
170 void didAccessDecodedData(double timeStamp);
172 HashCountedSet<CachedResourceClient*> m_clients;
178 ResourceResponse m_response;
179 RefPtr<SharedBuffer> m_data;
184 bool m_errorOccurred;
187 // These are called by the friendly Cache only
188 void setResourceToRevalidate(CachedResource*);
189 void switchClientsToRevalidatedResource();
190 void clearResourceToRevalidate();
191 void setExpirationDate(time_t expirationDate) { m_expirationDate = expirationDate; }
193 unsigned m_encodedSize;
194 unsigned m_decodedSize;
195 unsigned m_accessCount;
196 unsigned m_inLiveDecodedResourcesList;
197 double m_lastDecodedAccessTime; // Used as a "thrash guard" in the cache
199 bool m_sendResourceLoadCallbacks;
201 unsigned m_preloadCount;
202 PreloadResult m_preloadResult;
203 bool m_requestedFromNetworkingLayer;
208 bool m_expireDateChanged;
215 CachedResource* m_nextInAllResourcesList;
216 CachedResource* m_prevInAllResourcesList;
218 CachedResource* m_nextInLiveResourcesList;
219 CachedResource* m_prevInLiveResourcesList;
221 DocLoader* m_docLoader; // only non-0 for resources that are not in the cache
223 unsigned m_handleCount;
224 // 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
225 // using HTTP If-Modified-Since/If-None-Match headers. If the response is 304 all clients of this resource are moved
226 // to to be clients of m_resourceToRevalidate and the resource is deleted. If not, the field is zeroed and this
227 // resources becomes normal resource load.
228 CachedResource* m_resourceToRevalidate;
229 bool m_isBeingRevalidated;
230 // These handles will need to be updated to point to the m_resourceToRevalidate in case we get 304 response.
231 HashSet<CachedResourceHandleBase*> m_handlesToRevalidate;
233 time_t m_expirationDate;