• 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 "PurgePriority.h"
30 #include "ResourceLoadPriority.h"
31 #include "ResourceResponse.h"
32 #include <wtf/HashCountedSet.h>
33 #include <wtf/HashSet.h>
34 #include <wtf/OwnPtr.h>
35 #include <wtf/Vector.h>
36 #include <time.h>
37 
38 namespace WebCore {
39 
40 class MemoryCache;
41 class CachedMetadata;
42 class CachedResourceClient;
43 class CachedResourceHandleBase;
44 class CachedResourceLoader;
45 class CachedResourceRequest;
46 class Frame;
47 class InspectorResource;
48 class PurgeableBuffer;
49 
50 // A resource that is held in the cache. Classes who want to use this object should derive
51 // from CachedResourceClient, to get the function calls in case the requested data has arrived.
52 // This class also does the actual communication with the loader to obtain the resource from the network.
53 class CachedResource {
54     WTF_MAKE_NONCOPYABLE(CachedResource); WTF_MAKE_FAST_ALLOCATED;
55     friend class MemoryCache;
56     friend class InspectorResource;
57 
58 public:
59     enum Type {
60         ImageResource,
61         CSSStyleSheet,
62         Script,
63         FontResource
64 #if ENABLE(XSLT)
65         , XSLStyleSheet
66 #endif
67 #if ENABLE(LINK_PREFETCH)
68         , LinkResource
69 #endif
70     };
71 
72     enum Status {
73         Unknown,      // let cache decide what to do with it
74         Pending,      // only partially loaded
75         Cached,       // regular case
76         LoadError,
77         DecodeError
78     };
79 
80     CachedResource(const String& url, Type);
81     virtual ~CachedResource();
82 
load(CachedResourceLoader * cachedResourceLoader)83     virtual void load(CachedResourceLoader* cachedResourceLoader)  { load(cachedResourceLoader, false, DoSecurityCheck, true); }
84     void load(CachedResourceLoader*, bool incremental, SecurityCheckPolicy, bool sendResourceLoadCallbacks);
85 
setEncoding(const String &)86     virtual void setEncoding(const String&) { }
encoding()87     virtual String encoding() const { return String(); }
88     virtual void data(PassRefPtr<SharedBuffer> data, bool allDataReceived);
89     virtual void error(CachedResource::Status);
90 
shouldIgnoreHTTPStatusCodeErrors()91     virtual bool shouldIgnoreHTTPStatusCodeErrors() const { return false; }
92 
url()93     const String &url() const { return m_url; }
type()94     Type type() const { return static_cast<Type>(m_type); }
95 
loadPriority()96     ResourceLoadPriority loadPriority() const { return m_loadPriority; }
97     void setLoadPriority(ResourceLoadPriority);
98 
99     void addClient(CachedResourceClient*);
100     void removeClient(CachedResourceClient*);
hasClients()101     bool hasClients() const { return !m_clients.isEmpty(); }
102     void deleteIfPossible();
103 
104     enum PreloadResult {
105         PreloadNotReferenced,
106         PreloadReferenced,
107         PreloadReferencedWhileLoading,
108         PreloadReferencedWhileComplete
109     };
preloadResult()110     PreloadResult preloadResult() const { return static_cast<PreloadResult>(m_preloadResult); }
setRequestedFromNetworkingLayer()111     void setRequestedFromNetworkingLayer() { m_requestedFromNetworkingLayer = true; }
112 
113     virtual void didAddClient(CachedResourceClient*);
allClientsRemoved()114     virtual void allClientsRemoved() { }
115 
count()116     unsigned count() const { return m_clients.size(); }
117 
status()118     Status status() const { return static_cast<Status>(m_status); }
setStatus(Status status)119     void setStatus(Status status) { m_status = status; }
120 
size()121     unsigned size() const { return encodedSize() + decodedSize() + overheadSize(); }
encodedSize()122     unsigned encodedSize() const { return m_encodedSize; }
decodedSize()123     unsigned decodedSize() const { return m_decodedSize; }
124     unsigned overheadSize() const;
125 
isLoaded()126     bool isLoaded() const { return !m_loading; } // FIXME. Method name is inaccurate. Loading might not have started yet.
127 
isLoading()128     bool isLoading() const { return m_loading; }
setLoading(bool b)129     void setLoading(bool b) { m_loading = b; }
130 
isImage()131     virtual bool isImage() const { return false; }
isLinkResource()132     bool isLinkResource() const
133     {
134 #if ENABLE(LINK_PREFETCH)
135         return type() == LinkResource;
136 #else
137         return false;
138 #endif
139     }
140 
accessCount()141     unsigned accessCount() const { return m_accessCount; }
increaseAccessCount()142     void increaseAccessCount() { m_accessCount++; }
143 
144     // Computes the status of an object after loading.
145     // Updates the expire date on the cache entry file
146     void finish();
147 
148     // Called by the cache if the object has been removed from the cache
149     // while still being referenced. This means the object should delete itself
150     // if the number of clients observing it ever drops to 0.
151     // The resource can be brought back to cache after successful revalidation.
setInCache(bool inCache)152     void setInCache(bool inCache) { m_inCache = inCache; }
inCache()153     bool inCache() const { return m_inCache; }
154 
setInLiveDecodedResourcesList(bool b)155     void setInLiveDecodedResourcesList(bool b) { m_inLiveDecodedResourcesList = b; }
inLiveDecodedResourcesList()156     bool inLiveDecodedResourcesList() { return m_inLiveDecodedResourcesList; }
157 
158     void setRequest(CachedResourceRequest*);
159 
data()160     SharedBuffer* data() const { ASSERT(!m_purgeableData); return m_data.get(); }
161 
162     void setResponse(const ResourceResponse&);
response()163     const ResourceResponse& response() const { return m_response; }
164 
165     // Sets the serialized metadata retrieved from the platform's cache.
166     void setSerializedCachedMetadata(const char*, size_t);
167 
168     // Caches the given metadata in association with this resource and suggests
169     // that the platform persist it. The dataTypeID is a pseudo-randomly chosen
170     // identifier that is used to distinguish data generated by the caller.
171     void setCachedMetadata(unsigned dataTypeID, const char*, size_t);
172 
173     // Returns cached metadata of the given type associated with this resource.
174     CachedMetadata* cachedMetadata(unsigned dataTypeID) const;
175 
canDelete()176     bool canDelete() const { return !hasClients() && !m_request && !m_preloadCount && !m_handleCount && !m_resourceToRevalidate && !m_proxyResource; }
177 
178     bool isExpired() const;
179 
180     // List of acceptable MIME types separated by ",".
181     // A MIME type may contain a wildcard, e.g. "text/*".
accept()182     String accept() const { return m_accept; }
setAccept(const String & accept)183     void setAccept(const String& accept) { m_accept = accept; }
184 
errorOccurred()185     bool errorOccurred() const { return (status() == LoadError || status() == DecodeError); }
186 
sendResourceLoadCallbacks()187     bool sendResourceLoadCallbacks() const { return m_sendResourceLoadCallbacks; }
188 
destroyDecodedData()189     virtual void destroyDecodedData() { }
190 
setOwningCachedResourceLoader(CachedResourceLoader * cachedResourceLoader)191     void setOwningCachedResourceLoader(CachedResourceLoader* cachedResourceLoader) { m_owningCachedResourceLoader = cachedResourceLoader; }
192 
isPreloaded()193     bool isPreloaded() const { return m_preloadCount; }
increasePreloadCount()194     void increasePreloadCount() { ++m_preloadCount; }
decreasePreloadCount()195     void decreasePreloadCount() { ASSERT(m_preloadCount); --m_preloadCount; }
196 
197     void registerHandle(CachedResourceHandleBase* h);
198     void unregisterHandle(CachedResourceHandleBase* h);
199 
200     bool canUseCacheValidator() const;
201     bool mustRevalidateDueToCacheHeaders(CachePolicy) const;
isCacheValidator()202     bool isCacheValidator() const { return m_resourceToRevalidate; }
resourceToRevalidate()203     CachedResource* resourceToRevalidate() const { return m_resourceToRevalidate; }
204 
205     bool isPurgeable() const;
206     bool wasPurged() const;
207 
208     // This is used by the archive machinery to get at a purged resource without
209     // triggering a load. We should make it protected again if we can find a
210     // better way to handle the archive case.
211     bool makePurgeable(bool purgeable);
212 
213     // HTTP revalidation support methods for CachedResourceLoader.
214     void setResourceToRevalidate(CachedResource*);
215     void switchClientsToRevalidatedResource();
216     void clearResourceToRevalidate();
217     void updateResponseAfterRevalidation(const ResourceResponse& validatingResponse);
218 
219 protected:
220     void checkNotify();
221 
222     void setEncodedSize(unsigned);
223     void setDecodedSize(unsigned);
224     void didAccessDecodedData(double timeStamp);
225 
226     bool isSafeToMakePurgeable() const;
227 
228     HashCountedSet<CachedResourceClient*> m_clients;
229 
230     String m_url;
231     String m_accept;
232     CachedResourceRequest* m_request;
233     ResourceLoadPriority m_loadPriority;
234 
235     ResourceResponse m_response;
236     double m_responseTimestamp;
237 
238     RefPtr<SharedBuffer> m_data;
239     OwnPtr<PurgeableBuffer> m_purgeableData;
240 
241 private:
242     void addClientToSet(CachedResourceClient*);
243 
purgePriority()244     virtual PurgePriority purgePriority() const { return PurgeDefault; }
245 
246     double currentAge() const;
247     double freshnessLifetime() const;
248 
249     RefPtr<CachedMetadata> m_cachedMetadata;
250 
251     double m_lastDecodedAccessTime; // Used as a "thrash guard" in the cache
252 
253     unsigned m_encodedSize;
254     unsigned m_decodedSize;
255     unsigned m_accessCount;
256     unsigned m_handleCount;
257     unsigned m_preloadCount;
258 
259     unsigned m_preloadResult : 2; // PreloadResult
260 
261     bool m_inLiveDecodedResourcesList : 1;
262     bool m_requestedFromNetworkingLayer : 1;
263     bool m_sendResourceLoadCallbacks : 1;
264 
265     bool m_inCache : 1;
266     bool m_loading : 1;
267 
268     unsigned m_type : 3; // Type
269     unsigned m_status : 3; // Status
270 
271 #ifndef NDEBUG
272     bool m_deleted;
273     unsigned m_lruIndex;
274 #endif
275 
276     CachedResource* m_nextInAllResourcesList;
277     CachedResource* m_prevInAllResourcesList;
278 
279     CachedResource* m_nextInLiveResourcesList;
280     CachedResource* m_prevInLiveResourcesList;
281 
282     CachedResourceLoader* m_owningCachedResourceLoader; // only non-0 for resources that are not in the cache
283 
284     // 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
285     // using HTTP If-Modified-Since/If-None-Match headers. If the response is 304 all clients of this resource are moved
286     // to to be clients of m_resourceToRevalidate and the resource is deleted. If not, the field is zeroed and this
287     // resources becomes normal resource load.
288     CachedResource* m_resourceToRevalidate;
289 
290     // If this field is non-null, the resource has a proxy for checking whether it is still up to date (see m_resourceToRevalidate).
291     CachedResource* m_proxyResource;
292 
293     // These handles will need to be updated to point to the m_resourceToRevalidate in case we get 304 response.
294     HashSet<CachedResourceHandleBase*> m_handlesToRevalidate;
295 };
296 
297 }
298 
299 #endif
300