#ifndef Resource_h
#define Resource_h
#include "core/fetch/ResourceLoaderOptions.h"
#include "platform/Timer.h"
#include "platform/network/ResourceError.h"
#include "platform/network/ResourceLoadPriority.h"
#include "platform/network/ResourceRequest.h"
#include "platform/network/ResourceResponse.h"
#include "wtf/HashCountedSet.h"
#include "wtf/HashSet.h"
#include "wtf/OwnPtr.h"
#include "wtf/text/WTFString.h"
#define ENABLE_RESOURCE_IS_DELETED_CHECK
namespace WebCore {
struct FetchInitiatorInfo;
class MemoryCache;
class CachedMetadata;
class ResourceClient;
class ResourcePtrBase;
class ResourceFetcher;
class InspectorResource;
class ResourceLoader;
class SecurityOrigin;
class SharedBuffer;
class Resource {
WTF_MAKE_NONCOPYABLE(Resource); WTF_MAKE_FAST_ALLOCATED;
friend class MemoryCache;
friend class InspectorResource;
public:
enum Type {
MainResource,
Image,
CSSStyleSheet,
Script,
Font,
Raw,
SVGDocument,
XSLStyleSheet,
LinkPrefetch,
LinkSubresource,
TextTrack,
Shader,
ImportResource
};
enum Status {
Unknown,
Pending,
Cached,
LoadError,
DecodeError
};
Resource(const ResourceRequest&, Type);
virtual ~Resource();
enum CacheLiveResourcePriority {
CacheLiveResourcePriorityLow = 0,
CacheLiveResourcePriorityHigh
};
virtual void load(ResourceFetcher*, const ResourceLoaderOptions&);
virtual void setEncoding(const String&) { }
virtual String encoding() const { return String(); }
virtual void appendData(const char*, int);
virtual void error(Resource::Status);
void setNeedsSynchronousCacheHit(bool needsSynchronousCacheHit) { m_needsSynchronousCacheHit = needsSynchronousCacheHit; }
void setResourceError(const ResourceError& error) { m_error = error; }
const ResourceError& resourceError() const { return m_error; }
void setIdentifier(unsigned long identifier) { m_identifier = identifier; }
unsigned long identifier() const { return m_identifier; }
virtual bool shouldIgnoreHTTPStatusCodeErrors() const { return false; }
ResourceRequest& resourceRequest() { return m_resourceRequest; }
const ResourceRequest& lastResourceRequest();
const KURL& url() const { return m_resourceRequest.url();}
Type type() const { return static_cast<Type>(m_type); }
const ResourceLoaderOptions& options() const { return m_options; }
void setOptions(const ResourceLoaderOptions& options) { m_options = options; }
void didChangePriority(ResourceLoadPriority, int intraPriorityValue);
void addClient(ResourceClient*);
void removeClient(ResourceClient*);
bool hasClients() const { return !m_clients.isEmpty() || !m_clientsAwaitingCallback.isEmpty(); }
bool deleteIfPossible();
enum PreloadResult {
PreloadNotReferenced,
PreloadReferenced,
PreloadReferencedWhileLoading,
PreloadReferencedWhileComplete
};
PreloadResult preloadResult() const { return static_cast<PreloadResult>(m_preloadResult); }
virtual void didAddClient(ResourceClient*);
virtual void didRemoveClient(ResourceClient*) { }
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; }
size_t size() const { return encodedSize() + decodedSize() + overheadSize(); }
size_t encodedSize() const { return m_encodedSize; }
size_t decodedSize() const { return m_decodedSize; }
size_t overheadSize() const;
bool isLoaded() const { return !m_loading; }
bool isLoading() const { return m_loading; }
void setLoading(bool b) { m_loading = b; }
virtual bool stillNeedsLoad() const { return false; }
ResourceLoader* loader() const { return m_loader.get(); }
virtual bool isImage() const { return false; }
bool ignoreForRequestCount() const
{
return type() == MainResource
|| type() == LinkPrefetch
|| type() == LinkSubresource
|| type() == Raw;
}
void finish(double finishTime = 0.0);
bool passesAccessControlCheck(SecurityOrigin*);
bool passesAccessControlCheck(SecurityOrigin*, String& errorDescription);
void setCacheLiveResourcePriority(CacheLiveResourcePriority);
unsigned cacheLiveResourcePriority() const { return m_cacheLiveResourcePriority; }
void clearLoader();
SharedBuffer* resourceBuffer() const { return m_data.get(); }
void setResourceBuffer(PassRefPtr<SharedBuffer>);
virtual void willSendRequest(ResourceRequest&, const ResourceResponse&);
virtual void updateRequest(const ResourceRequest&) { }
virtual void responseReceived(const ResourceResponse&);
void setResponse(const ResourceResponse& response) { m_response = response; }
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_loader && !m_preloadCount && !m_handleCount && !m_protectorCount && !m_resourceToRevalidate && !m_proxyResource; }
bool hasOneHandle() const;
AtomicString accept() const { return m_accept; }
void setAccept(const AtomicString& accept) { m_accept = accept; }
bool wasCanceled() const { return m_error.isCancellation(); }
bool errorOccurred() const { return m_status == LoadError || m_status == DecodeError; }
bool loadFailedOrCanceled() { return !m_error.isNull(); }
DataBufferingPolicy dataBufferingPolicy() const { return m_options.dataBufferingPolicy; }
void setDataBufferingPolicy(DataBufferingPolicy);
bool isPreloaded() const { return m_preloadCount; }
void increasePreloadCount() { ++m_preloadCount; }
void decreasePreloadCount() { ASSERT(m_preloadCount); --m_preloadCount; }
void registerHandle(ResourcePtrBase* h);
void unregisterHandle(ResourcePtrBase* h);
bool canReuseRedirectChain() const;
bool mustRevalidateDueToCacheHeaders() const;
bool canUseCacheValidator() const;
bool isCacheValidator() const { return m_resourceToRevalidate; }
Resource* resourceToRevalidate() const { return m_resourceToRevalidate; }
void setResourceToRevalidate(Resource*);
bool isPurgeable() const;
bool wasPurged() const;
bool lock();
virtual void didSendData(unsigned long long , unsigned long long ) { }
virtual void didDownloadData(int) { }
double loadFinishTime() const { return m_loadFinishTime; }
virtual bool canReuse(const ResourceRequest&) const { return true; }
void prune();
static const char* resourceTypeToString(Type, const FetchInitiatorInfo&);
#ifdef ENABLE_RESOURCE_IS_DELETED_CHECK
void assertAlive() const { RELEASE_ASSERT(!m_deleted); }
#else
void assertAlive() const { }
#endif
protected:
virtual void checkNotify();
virtual void finishOnePart();
class InternalResourcePtr {
public:
explicit InternalResourcePtr(Resource* resource)
: m_resource(resource)
{
m_resource->incrementProtectorCount();
}
~InternalResourcePtr()
{
m_resource->decrementProtectorCount();
m_resource->deleteIfPossible();
}
private:
Resource* m_resource;
};
void incrementProtectorCount() { m_protectorCount++; }
void decrementProtectorCount() { m_protectorCount--; }
void setEncodedSize(size_t);
void setDecodedSize(size_t);
void didAccessDecodedData(double timeStamp);
virtual void switchClientsToRevalidatedResource();
void clearResourceToRevalidate();
void updateResponseAfterRevalidation(const ResourceResponse& validatingResponse);
void finishPendingClients();
HashCountedSet<ResourceClient*> m_clients;
HashCountedSet<ResourceClient*> m_clientsAwaitingCallback;
class ResourceCallback {
public:
static ResourceCallback* callbackHandler();
void schedule(Resource*);
void cancel(Resource*);
bool isScheduled(Resource*) const;
private:
ResourceCallback();
void timerFired(Timer<ResourceCallback>*);
Timer<ResourceCallback> m_callbackTimer;
HashSet<Resource*> m_resourcesWithPendingClients;
};
bool hasClient(ResourceClient* client) { return m_clients.contains(client) || m_clientsAwaitingCallback.contains(client); }
struct RedirectPair {
public:
explicit RedirectPair(const ResourceRequest& request, const ResourceResponse& redirectResponse)
: m_request(request)
, m_redirectResponse(redirectResponse)
{
}
const ResourceRequest m_request;
const ResourceResponse m_redirectResponse;
};
const Vector<RedirectPair>& redirectChain() const { return m_redirectChain; }
virtual bool isSafeToUnlock() const { return false; }
virtual void destroyDecodedDataIfPossible() { }
ResourceRequest m_resourceRequest;
AtomicString m_accept;
RefPtr<ResourceLoader> m_loader;
ResourceLoaderOptions m_options;
ResourceResponse m_response;
double m_responseTimestamp;
RefPtr<SharedBuffer> m_data;
Timer<Resource> m_cancelTimer;
private:
bool addClientToSet(ResourceClient*);
void cancelTimerFired(Timer<Resource>*);
void revalidationSucceeded(const ResourceResponse&);
void revalidationFailed();
bool unlock();
void failBeforeStarting();
String m_fragmentIdentifierForRequest;
RefPtr<CachedMetadata> m_cachedMetadata;
ResourceError m_error;
double m_lastDecodedAccessTime;
double m_loadFinishTime;
unsigned long m_identifier;
size_t m_encodedSize;
size_t m_decodedSize;
unsigned m_handleCount;
unsigned m_preloadCount;
unsigned m_protectorCount;
unsigned m_preloadResult : 2;
unsigned m_cacheLiveResourcePriority : 2;
unsigned m_requestedFromNetworkingLayer : 1;
unsigned m_loading : 1;
unsigned m_switchingClientsToRevalidatedResource : 1;
unsigned m_type : 4;
unsigned m_status : 3;
unsigned m_wasPurged : 1;
unsigned m_needsSynchronousCacheHit : 1;
#ifdef ENABLE_RESOURCE_IS_DELETED_CHECK
bool m_deleted;
#endif
Resource* m_resourceToRevalidate;
Resource* m_proxyResource;
HashSet<ResourcePtrBase*> m_handlesToRevalidate;
Vector<RedirectPair> m_redirectChain;
};
#if !LOG_DISABLED
const char* ResourceTypeName(Resource::Type);
#endif
#define DEFINE_RESOURCE_TYPE_CASTS(typeName) \
DEFINE_TYPE_CASTS(typeName##Resource, Resource, resource, resource->type() == Resource::typeName, resource.type() == Resource::typeName); \
inline typeName##Resource* to##typeName##Resource(const ResourcePtr<Resource>& ptr) { return to##typeName##Resource(ptr.get()); }
}
#endif