#ifndef CachedResource_h
#define CachedResource_h
#include "CachePolicy.h"
#include "FrameLoaderTypes.h"
#include "PlatformString.h"
#include "PurgePriority.h"
#include "ResourceLoadPriority.h"
#include "ResourceResponse.h"
#include <wtf/HashCountedSet.h>
#include <wtf/HashSet.h>
#include <wtf/OwnPtr.h>
#include <wtf/Vector.h>
#include <time.h>
namespace WebCore {
class MemoryCache;
class CachedMetadata;
class CachedResourceClient;
class CachedResourceHandleBase;
class CachedResourceLoader;
class CachedResourceRequest;
class Frame;
class InspectorResource;
class PurgeableBuffer;
class CachedResource {
WTF_MAKE_NONCOPYABLE(CachedResource); WTF_MAKE_FAST_ALLOCATED;
friend class MemoryCache;
friend class InspectorResource;
public:
enum Type {
ImageResource,
CSSStyleSheet,
Script,
FontResource
#if ENABLE(XSLT)
, XSLStyleSheet
#endif
#if ENABLE(LINK_PREFETCH)
, LinkResource
#endif
};
enum Status {
Unknown, Pending, Cached, Canceled,
LoadError,
DecodeError
};
CachedResource(const String& url, Type);
virtual ~CachedResource();
virtual void load(CachedResourceLoader* cachedResourceLoader) { load(cachedResourceLoader, false, DoSecurityCheck, true); }
void load(CachedResourceLoader*, bool incremental, SecurityCheckPolicy, bool sendResourceLoadCallbacks);
virtual void setEncoding(const String&) { }
virtual String encoding() const { return String(); }
virtual void data(PassRefPtr<SharedBuffer> data, bool allDataReceived);
virtual void error(CachedResource::Status);
virtual bool shouldIgnoreHTTPStatusCodeErrors() const { return false; }
const String &url() const { return m_url; }
Type type() const { return static_cast<Type>(m_type); }
ResourceLoadPriority loadPriority() const { return m_loadPriority; }
void setLoadPriority(ResourceLoadPriority);
void addClient(CachedResourceClient*);
void removeClient(CachedResourceClient*);
bool hasClients() const { return !m_clients.isEmpty(); }
void deleteIfPossible();
enum PreloadResult {
PreloadNotReferenced,
PreloadReferenced,
PreloadReferencedWhileLoading,
PreloadReferencedWhileComplete
};
PreloadResult preloadResult() const { return static_cast<PreloadResult>(m_preloadResult); }
void setRequestedFromNetworkingLayer() { m_requestedFromNetworkingLayer = true; }
virtual void didAddClient(CachedResourceClient*);
virtual void allClientsRemoved() { }
unsigned count() const { return m_clients.size(); }
Status status() const { return static_cast<Status>(m_status); }
void setStatus(Status status) { m_status = status; }
unsigned size() const { return encodedSize() + decodedSize() + overheadSize(); }
unsigned encodedSize() const { return m_encodedSize; }
unsigned decodedSize() const { return m_decodedSize; }
unsigned overheadSize() const;
bool isLoaded() const { return !m_loading; }
bool isLoading() const { return m_loading; }
void setLoading(bool b) { m_loading = b; }
virtual bool isImage() const { return false; }
bool isLinkResource() const
{
#if ENABLE(LINK_PREFETCH)
return type() == LinkResource;
#else
return false;
#endif
}
unsigned accessCount() const { return m_accessCount; }
void increaseAccessCount() { m_accessCount++; }
void finish();
void setInCache(bool inCache) { m_inCache = inCache; }
bool inCache() const { return m_inCache; }
void setInLiveDecodedResourcesList(bool b) { m_inLiveDecodedResourcesList = b; }
bool inLiveDecodedResourcesList() { return m_inLiveDecodedResourcesList; }
void setRequest(CachedResourceRequest*);
SharedBuffer* data() const { ASSERT(!m_purgeableData); return m_data.get(); }
void setResponse(const ResourceResponse&);
const ResourceResponse& response() const { return m_response; }
void setSerializedCachedMetadata(const char*, size_t);
void setCachedMetadata(unsigned dataTypeID, const char*, size_t);
CachedMetadata* cachedMetadata(unsigned dataTypeID) const;
bool canDelete() const { return !hasClients() && !m_request && !m_preloadCount && !m_handleCount && !m_resourceToRevalidate && !m_proxyResource; }
bool isExpired() const;
String accept() const { return m_accept; }
void setAccept(const String& accept) { m_accept = accept; }
bool wasCanceled() const { return m_status == Canceled; }
bool errorOccurred() const { return (m_status == LoadError || m_status == DecodeError); }
bool sendResourceLoadCallbacks() const { return m_sendResourceLoadCallbacks; }
virtual void destroyDecodedData() { }
void setOwningCachedResourceLoader(CachedResourceLoader* cachedResourceLoader) { m_owningCachedResourceLoader = cachedResourceLoader; }
bool isPreloaded() const { return m_preloadCount; }
void increasePreloadCount() { ++m_preloadCount; }
void decreasePreloadCount() { ASSERT(m_preloadCount); --m_preloadCount; }
void registerHandle(CachedResourceHandleBase* h);
void unregisterHandle(CachedResourceHandleBase* h);
bool canUseCacheValidator() const;
bool mustRevalidateDueToCacheHeaders(CachePolicy) const;
bool isCacheValidator() const { return m_resourceToRevalidate; }
CachedResource* resourceToRevalidate() const { return m_resourceToRevalidate; }
bool isPurgeable() const;
bool wasPurged() const;
bool makePurgeable(bool purgeable);
void setResourceToRevalidate(CachedResource*);
void switchClientsToRevalidatedResource();
void clearResourceToRevalidate();
void updateResponseAfterRevalidation(const ResourceResponse& validatingResponse);
protected:
void checkNotify();
void setEncodedSize(unsigned);
void setDecodedSize(unsigned);
void didAccessDecodedData(double timeStamp);
bool isSafeToMakePurgeable() const;
HashCountedSet<CachedResourceClient*> m_clients;
String m_url;
String m_accept;
CachedResourceRequest* m_request;
ResourceLoadPriority m_loadPriority;
ResourceResponse m_response;
double m_responseTimestamp;
RefPtr<SharedBuffer> m_data;
OwnPtr<PurgeableBuffer> m_purgeableData;
private:
void addClientToSet(CachedResourceClient*);
virtual PurgePriority purgePriority() const { return PurgeDefault; }
double currentAge() const;
double freshnessLifetime() const;
RefPtr<CachedMetadata> m_cachedMetadata;
double m_lastDecodedAccessTime;
unsigned m_encodedSize;
unsigned m_decodedSize;
unsigned m_accessCount;
unsigned m_handleCount;
unsigned m_preloadCount;
unsigned m_preloadResult : 2;
bool m_inLiveDecodedResourcesList : 1;
bool m_requestedFromNetworkingLayer : 1;
bool m_sendResourceLoadCallbacks : 1;
bool m_inCache : 1;
bool m_loading : 1;
unsigned m_type : 3; unsigned m_status : 3;
#ifndef NDEBUG
bool m_deleted;
unsigned m_lruIndex;
#endif
CachedResource* m_nextInAllResourcesList;
CachedResource* m_prevInAllResourcesList;
CachedResource* m_nextInLiveResourcesList;
CachedResource* m_prevInLiveResourcesList;
CachedResourceLoader* m_owningCachedResourceLoader;
CachedResource* m_resourceToRevalidate;
CachedResource* m_proxyResource;
HashSet<CachedResourceHandleBase*> m_handlesToRevalidate;
};
}
#endif