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