• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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.
6 
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.
11 
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.
16 
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.
21 */
22 
23 #ifndef CachedResource_h
24 #define CachedResource_h
25 
26 #include "CachePolicy.h"
27 #include "FrameLoaderTypes.h"
28 #include "PlatformString.h"
29 #include "ResourceResponse.h"
30 #include "SharedBuffer.h"
31 #include <wtf/HashCountedSet.h>
32 #include <wtf/HashSet.h>
33 #include <wtf/OwnPtr.h>
34 #include <wtf/Vector.h>
35 #include <time.h>
36 
37 namespace WebCore {
38 
39 class Cache;
40 class CachedResourceClient;
41 class CachedResourceHandleBase;
42 class DocLoader;
43 class InspectorResource;
44 class Request;
45 class PurgeableBuffer;
46 
47 // A resource that is held in the cache. Classes who want to use this object should derive
48 // from CachedResourceClient, to get the function calls in case the requested data has arrived.
49 // This class also does the actual communication with the loader to obtain the resource from the network.
50 class CachedResource : public Noncopyable {
51     friend class Cache;
52     friend class InspectorResource;
53 
54 public:
55     enum Type {
56         ImageResource,
57         CSSStyleSheet,
58         Script,
59         FontResource
60 #if ENABLE(XSLT)
61         , XSLStyleSheet
62 #endif
63 #if ENABLE(XBL)
64         , XBL
65 #endif
66     };
67 
68     enum Status {
69         NotCached,    // this URL is not cached
70         Unknown,      // let cache decide what to do with it
71         New,          // inserting new item
72         Pending,      // only partially loaded
73         Cached        // regular case
74     };
75 
76     CachedResource(const String& url, Type);
77     virtual ~CachedResource();
78 
load(DocLoader * docLoader)79     virtual void load(DocLoader* docLoader)  { load(docLoader, false, DoSecurityCheck, true); }
80     void load(DocLoader*, bool incremental, SecurityCheckPolicy, bool sendResourceLoadCallbacks);
81 
setEncoding(const String &)82     virtual void setEncoding(const String&) { }
encoding()83     virtual String encoding() const { return String(); }
84     virtual void data(PassRefPtr<SharedBuffer> data, bool allDataReceived) = 0;
85     virtual void error() = 0;
httpStatusCodeError()86     virtual void httpStatusCodeError() { error(); } // Images keep loading in spite of HTTP errors (for legacy compat with <img>, etc.).
87 
url()88     const String &url() const { return m_url; }
type()89     Type type() const { return m_type; }
90 
91     void addClient(CachedResourceClient*);
92     void removeClient(CachedResourceClient*);
hasClients()93     bool hasClients() const { return !m_clients.isEmpty(); }
94     void deleteIfPossible();
95 
96     enum PreloadResult {
97         PreloadNotReferenced,
98         PreloadReferenced,
99         PreloadReferencedWhileLoading,
100         PreloadReferencedWhileComplete
101     };
preloadResult()102     PreloadResult preloadResult() const { return m_preloadResult; }
setRequestedFromNetworkingLayer()103     void setRequestedFromNetworkingLayer() { m_requestedFromNetworkingLayer = true; }
104 
105     virtual void didAddClient(CachedResourceClient*) = 0;
allClientsRemoved()106     virtual void allClientsRemoved() { }
107 
count()108     unsigned count() const { return m_clients.size(); }
109 
status()110     Status status() const { return m_status; }
111 
size()112     unsigned size() const { return encodedSize() + decodedSize() + overheadSize(); }
encodedSize()113     unsigned encodedSize() const { return m_encodedSize; }
decodedSize()114     unsigned decodedSize() const { return m_decodedSize; }
115     unsigned overheadSize() const;
116 
isLoaded()117     bool isLoaded() const { return !m_loading; }
setLoading(bool b)118     void setLoading(bool b) { m_loading = b; }
119 
isImage()120     virtual bool isImage() const { return false; }
121 
accessCount()122     unsigned accessCount() const { return m_accessCount; }
increaseAccessCount()123     void increaseAccessCount() { m_accessCount++; }
124 
125     // Computes the status of an object after loading.
126     // Updates the expire date on the cache entry file
127     void finish();
128 
129     // Called by the cache if the object has been removed from the cache
130     // while still being referenced. This means the object should delete itself
131     // if the number of clients observing it ever drops to 0.
132     // The resource can be brought back to cache after successful revalidation.
setInCache(bool inCache)133     void setInCache(bool inCache) { m_inCache = inCache; }
inCache()134     bool inCache() const { return m_inCache; }
135 
setInLiveDecodedResourcesList(bool b)136     void setInLiveDecodedResourcesList(bool b) { m_inLiveDecodedResourcesList = b; }
inLiveDecodedResourcesList()137     bool inLiveDecodedResourcesList() { return m_inLiveDecodedResourcesList; }
138 
139     void setRequest(Request*);
140 
data()141     SharedBuffer* data() const { ASSERT(!m_purgeableData); return m_data.get(); }
142 
143     void setResponse(const ResourceResponse&);
response()144     const ResourceResponse& response() const { return m_response; }
145 
canDelete()146     bool canDelete() const { return !hasClients() && !m_request && !m_preloadCount && !m_handleCount && !m_resourceToRevalidate && !m_proxyResource; }
147 
148     bool isExpired() const;
149 
schedule()150     virtual bool schedule() const { return false; }
151 
152     // List of acceptable MIME types separated by ",".
153     // A MIME type may contain a wildcard, e.g. "text/*".
accept()154     String accept() const { return m_accept; }
setAccept(const String & accept)155     void setAccept(const String& accept) { m_accept = accept; }
156 
errorOccurred()157     bool errorOccurred() const { return m_errorOccurred; }
sendResourceLoadCallbacks()158     bool sendResourceLoadCallbacks() const { return m_sendResourceLoadCallbacks; }
159 
destroyDecodedData()160     virtual void destroyDecodedData() { }
161 
setDocLoader(DocLoader * docLoader)162     void setDocLoader(DocLoader* docLoader) { m_docLoader = docLoader; }
163 
isPreloaded()164     bool isPreloaded() const { return m_preloadCount; }
increasePreloadCount()165     void increasePreloadCount() { ++m_preloadCount; }
decreasePreloadCount()166     void decreasePreloadCount() { ASSERT(m_preloadCount); --m_preloadCount; }
167 
registerHandle(CachedResourceHandleBase * h)168     void registerHandle(CachedResourceHandleBase* h) { ++m_handleCount; if (m_resourceToRevalidate) m_handlesToRevalidate.add(h); }
unregisterHandle(CachedResourceHandleBase * h)169     void unregisterHandle(CachedResourceHandleBase* h) { ASSERT(m_handleCount > 0); --m_handleCount; if (m_resourceToRevalidate) m_handlesToRevalidate.remove(h); if (!m_handleCount) deleteIfPossible(); }
170 
171     bool canUseCacheValidator() const;
172     bool mustRevalidate(CachePolicy) const;
isCacheValidator()173     bool isCacheValidator() const { return m_resourceToRevalidate; }
resourceToRevalidate()174     CachedResource* resourceToRevalidate() const { return m_resourceToRevalidate; }
175 
176     bool isPurgeable() const;
177     bool wasPurged() const;
178 
179     // This is used by the archive machinery to get at a purged resource without
180     // triggering a load. We should make it protected again if we can find a
181     // better way to handle the archive case.
182     bool makePurgeable(bool purgeable);
183 
184 protected:
185     void setEncodedSize(unsigned);
186     void setDecodedSize(unsigned);
187     void didAccessDecodedData(double timeStamp);
188 
189     bool isSafeToMakePurgeable() const;
190 
191     HashCountedSet<CachedResourceClient*> m_clients;
192 
193     String m_url;
194     String m_accept;
195     Request* m_request;
196 
197     ResourceResponse m_response;
198     double m_responseTimestamp;
199 
200     RefPtr<SharedBuffer> m_data;
201     OwnPtr<PurgeableBuffer> m_purgeableData;
202 
203     Type m_type;
204     Status m_status;
205 
206     bool m_errorOccurred;
207 
208 private:
209     void addClientToSet(CachedResourceClient*);
210 
211     // These are called by the friendly Cache only
212     void setResourceToRevalidate(CachedResource*);
213     void switchClientsToRevalidatedResource();
214     void clearResourceToRevalidate();
215     void updateResponseAfterRevalidation(const ResourceResponse& validatingResponse);
216 
217     double currentAge() const;
218     double freshnessLifetime() const;
219 
220     unsigned m_encodedSize;
221     unsigned m_decodedSize;
222     unsigned m_accessCount;
223     unsigned m_inLiveDecodedResourcesList;
224     double m_lastDecodedAccessTime; // Used as a "thrash guard" in the cache
225 
226     bool m_sendResourceLoadCallbacks;
227 
228     unsigned m_preloadCount;
229     PreloadResult m_preloadResult;
230     bool m_requestedFromNetworkingLayer;
231 
232 protected:
233     bool m_inCache;
234     bool m_loading;
235 #ifndef NDEBUG
236     bool m_deleted;
237     unsigned m_lruIndex;
238 #endif
239 
240 private:
241     CachedResource* m_nextInAllResourcesList;
242     CachedResource* m_prevInAllResourcesList;
243 
244     CachedResource* m_nextInLiveResourcesList;
245     CachedResource* m_prevInLiveResourcesList;
246 
247     DocLoader* m_docLoader; // only non-0 for resources that are not in the cache
248 
249     unsigned m_handleCount;
250     // 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
251     // using HTTP If-Modified-Since/If-None-Match headers. If the response is 304 all clients of this resource are moved
252     // to to be clients of m_resourceToRevalidate and the resource is deleted. If not, the field is zeroed and this
253     // resources becomes normal resource load.
254     CachedResource* m_resourceToRevalidate;
255 
256     // If this field is non-null, the resource has a proxy for checking whether it is still up to date (see m_resourceToRevalidate).
257     CachedResource* m_proxyResource;
258 
259     // These handles will need to be updated to point to the m_resourceToRevalidate in case we get 304 response.
260     HashSet<CachedResourceHandleBase*> m_handlesToRevalidate;
261 };
262 
263 }
264 
265 #endif
266