#ifndef CachedResource_h
#define CachedResource_h
#include "CachePolicy.h"
#include "CacheValidation.h"
#include "FrameLoaderTypes.h"
#include "ResourceError.h"
#include "ResourceLoadPriority.h"
#include "ResourceLoaderOptions.h"
#include "ResourceRequest.h"
#include "ResourceResponse.h"
#include "SessionID.h"
#include "Timer.h"
#include <time.h>
#include <wtf/HashCountedSet.h>
#include <wtf/HashSet.h>
#include <wtf/TypeCasts.h>
#include <wtf/Vector.h>
#include <wtf/text/WTFString.h>
namespace WebCore {
class MemoryCache;
class CachedResourceClient;
class CachedResourceHandleBase;
class CachedResourceLoader;
class InspectorResource;
class SecurityOrigin;
class SharedBuffer;
class SubresourceLoader;
class TextResourceDecoder;
class CachedResource {
WTF_MAKE_NONCOPYABLE(CachedResource); WTF_MAKE_FAST_ALLOCATED;
friend class MemoryCache;
friend class InspectorResource;
public:
enum Type {
MainResource,
ImageResource,
CSSStyleSheet,
Script,
FontResource,
#if ENABLE(SVG_FONTS)
SVGFontResource,
#endif
RawResource,
SVGDocumentResource
#if ENABLE(XSLT)
, XSLStyleSheet
#endif
#if ENABLE(LINK_PREFETCH)
, LinkPrefetch
, LinkSubresource
#endif
#if ENABLE(VIDEO_TRACK)
, TextTrackResource
#endif
};
enum Status {
Unknown, Pending, Cached, LoadError,
DecodeError
};
CachedResource(const ResourceRequest&, Type, SessionID);
virtual ~CachedResource();
virtual void load(CachedResourceLoader&, const ResourceLoaderOptions&);
virtual void setEncoding(const String&) { }
virtual String encoding() const { return String(); }
virtual const TextResourceDecoder* textResourceDecoder() const { return nullptr; }
virtual void addDataBuffer(SharedBuffer&);
virtual void addData(const char* data, unsigned length);
virtual void finishLoading(SharedBuffer*);
virtual void error(CachedResource::Status);
void setResourceError(const ResourceError& error) { m_error = error; }
const ResourceError& resourceError() const { return m_error; }
virtual bool shouldIgnoreHTTPStatusCodeErrors() const { return false; }
ResourceRequest& resourceRequest() { return m_resourceRequest; }
const URL& url() const { return m_resourceRequest.url();}
#if ENABLE(CACHE_PARTITIONING)
const String& cachePartition() const { return m_resourceRequest.cachePartition(); }
#endif
SessionID sessionID() const { return m_sessionID; }
Type type() const { return static_cast<Type>(m_type); }
ResourceLoadPriority loadPriority() const { return m_loadPriority; }
void setLoadPriority(const Optional<ResourceLoadPriority>&);
WEBCORE_EXPORT void addClient(CachedResourceClient*);
WEBCORE_EXPORT void removeClient(CachedResourceClient*);
bool hasClients() const { return !m_clients.isEmpty() || !m_clientsAwaitingCallback.isEmpty(); }
bool hasClient(CachedResourceClient* client) { return m_clients.contains(client) || m_clientsAwaitingCallback.contains(client); }
bool deleteIfPossible();
enum PreloadResult {
PreloadNotReferenced,
PreloadReferenced,
PreloadReferencedWhileLoading,
PreloadReferencedWhileComplete
};
PreloadResult preloadResult() const { return static_cast<PreloadResult>(m_preloadResult); }
virtual void didAddClient(CachedResourceClient*);
virtual void didRemoveClient(CachedResourceClient*) { }
virtual void allClientsRemoved() { }
void destroyDecodedDataIfNeeded();
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;
virtual bool decodedDataIsPurgeable() const { return false; }
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; }
SubresourceLoader* loader() { return m_loader.get(); }
bool areAllClientsXMLHttpRequests() const;
bool isImage() const { return type() == ImageResource; }
bool isMainOrRawResource() const { return type() == MainResource || type() == RawResource; }
bool ignoreForRequestCount() const
{
return type() == MainResource
#if ENABLE(LINK_PREFETCH)
|| type() == LinkPrefetch
|| type() == LinkSubresource
#endif
|| type() == RawResource;
}
unsigned accessCount() const { return m_accessCount; }
void increaseAccessCount() { m_accessCount++; }
void finish();
bool passesAccessControlCheck(SecurityOrigin&);
bool passesSameOriginPolicyCheck(SecurityOrigin&);
void setInCache(bool inCache) { m_inCache = inCache; }
bool inCache() const { return m_inCache; }
void clearLoader();
SharedBuffer* resourceBuffer() const { return m_data.get(); }
virtual void redirectReceived(ResourceRequest&, const ResourceResponse&);
virtual void responseReceived(const ResourceResponse&);
void setResponse(const ResourceResponse& response) { m_response = response; }
const ResourceResponse& response() const { return m_response; }
const ResourceResponse& responseForSameOriginPolicyChecks() const;
bool canDelete() const { return !hasClients() && !m_loader && !m_preloadCount && !m_handleCount && !m_resourceToRevalidate && !m_proxyResource; }
bool hasOneHandle() const { return m_handleCount == 1; }
bool isExpired() const;
String accept() const { return m_accept; }
void setAccept(const String& accept) { m_accept = accept; }
void cancelLoad();
bool wasCanceled() const { return m_error.isCancellation(); }
bool errorOccurred() const { return m_status == LoadError || m_status == DecodeError; }
bool loadFailedOrCanceled() const { return !m_error.isNull(); }
bool shouldSendResourceLoadCallbacks() const { return m_options.sendLoadCallbacks() == SendCallbacks; }
DataBufferingPolicy dataBufferingPolicy() const { return m_options.dataBufferingPolicy(); }
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);
WEBCORE_EXPORT void unregisterHandle(CachedResourceHandleBase* h);
bool canUseCacheValidator() const;
enum class RevalidationDecision { No, YesDueToCachePolicy, YesDueToNoStore, YesDueToNoCache, YesDueToExpired };
virtual RevalidationDecision makeRevalidationDecision(CachePolicy) const;
bool redirectChainAllowsReuse(ReuseExpiredRedirectionOrNot) const;
bool isCacheValidator() const { return m_resourceToRevalidate; }
CachedResource* resourceToRevalidate() const { return m_resourceToRevalidate; }
void setResourceToRevalidate(CachedResource*);
virtual void switchClientsToRevalidatedResource();
void clearResourceToRevalidate();
void updateResponseAfterRevalidation(const ResourceResponse& validatingResponse);
virtual void didSendData(unsigned long long , unsigned long long ) { }
void setLoadFinishTime(double finishTime) { m_loadFinishTime = finishTime; }
double loadFinishTime() const { return m_loadFinishTime; }
virtual bool canReuse(const ResourceRequest&) const { return true; }
#if USE(FOUNDATION) || USE(SOUP)
WEBCORE_EXPORT void tryReplaceEncodedData(SharedBuffer&);
#endif
#if USE(SOUP)
virtual char* getOrCreateReadBuffer(size_t , size_t& ) { return nullptr; }
#endif
unsigned long identifierForLoadWithoutResourceLoader() const { return m_identifierForLoadWithoutResourceLoader; }
protected:
void setEncodedSize(unsigned);
void setDecodedSize(unsigned);
void didAccessDecodedData(double timeStamp);
HashCountedSet<CachedResourceClient*> m_clients;
ResourceRequest m_resourceRequest;
RefPtr<SubresourceLoader> m_loader;
ResourceLoaderOptions m_options;
ResourceResponse m_response;
ResourceResponse m_redirectResponseForSameOriginPolicyChecks;
RefPtr<SharedBuffer> m_data;
DeferrableOneShotTimer m_decodedDataDeletionTimer;
private:
class Callback;
bool addClientToSet(CachedResourceClient*);
void decodedDataDeletionTimerFired();
virtual void checkNotify();
virtual bool mayTryReplaceEncodedData() const { return false; }
std::chrono::microseconds freshnessLifetime(const ResourceResponse&) const;
void addAdditionalRequestHeaders(CachedResourceLoader&);
void failBeforeStarting();
HashMap<CachedResourceClient*, std::unique_ptr<Callback>> m_clientsAwaitingCallback;
SessionID m_sessionID;
String m_accept;
ResourceLoadPriority m_loadPriority;
std::chrono::system_clock::time_point m_responseTimestamp;
String m_fragmentIdentifierForRequest;
ResourceError m_error;
double m_lastDecodedAccessTime; double m_loadFinishTime;
unsigned m_encodedSize;
unsigned m_decodedSize;
unsigned m_accessCount;
unsigned m_handleCount;
unsigned m_preloadCount;
unsigned m_preloadResult : 2;
bool m_requestedFromNetworkingLayer : 1;
bool m_inCache : 1;
bool m_loading : 1;
bool m_switchingClientsToRevalidatedResource : 1;
unsigned m_type : 4; unsigned m_status : 3;
#ifndef NDEBUG
bool m_deleted;
unsigned m_lruIndex;
#endif
CachedResourceLoader* m_owningCachedResourceLoader;
CachedResource* m_resourceToRevalidate;
CachedResource* m_proxyResource;
HashSet<CachedResourceHandleBase*> m_handlesToRevalidate;
RedirectChainCacheStatus m_redirectChainCacheStatus;
unsigned long m_identifierForLoadWithoutResourceLoader { 0 };
};
class CachedResource::Callback {
public:
Callback(CachedResource&, CachedResourceClient&);
void cancel();
private:
void timerFired();
CachedResource& m_resource;
CachedResourceClient& m_client;
Timer m_timer;
};
}
#define SPECIALIZE_TYPE_TRAITS_CACHED_RESOURCE(ToClassName, CachedResourceType) \
SPECIALIZE_TYPE_TRAITS_BEGIN(WebCore::ToClassName) \
static bool isType(const WebCore::CachedResource& resource) { return resource.type() == WebCore::CachedResourceType; } \
SPECIALIZE_TYPE_TRAITS_END()
#endif // CachedResource_h