#ifndef ResourceLoader_h
#define ResourceLoader_h
#include "core/fetch/ResourceLoaderOptions.h"
#include "platform/network/ResourceRequest.h"
#include "public/platform/WebURLLoader.h"
#include "public/platform/WebURLLoaderClient.h"
#include "wtf/Forward.h"
#include "wtf/RefCounted.h"
namespace blink {
class WebThreadedDataReceiver;
}
namespace WebCore {
class Resource;
class KURL;
class ResourceError;
class ResourceResponse;
class ResourceLoaderHost;
class ResourceLoader FINAL : public RefCounted<ResourceLoader>, protected blink::WebURLLoaderClient {
public:
static PassRefPtr<ResourceLoader> create(ResourceLoaderHost*, Resource*, const ResourceRequest&, const ResourceLoaderOptions&);
virtual ~ResourceLoader();
void start();
void changeToSynchronous();
void cancel();
void cancel(const ResourceError&);
void cancelIfNotFinishing();
Resource* cachedResource() { return m_resource; }
const ResourceRequest& originalRequest() const { return m_originalRequest; }
void setDefersLoading(bool);
bool defersLoading() const { return m_defersLoading; }
void attachThreadedDataReceiver(PassOwnPtr<blink::WebThreadedDataReceiver>);
void releaseResources();
void didChangePriority(ResourceLoadPriority, int intraPriorityValue);
virtual void willSendRequest(blink::WebURLLoader*, blink::WebURLRequest&, const blink::WebURLResponse& redirectResponse) OVERRIDE;
virtual void didSendData(blink::WebURLLoader*, unsigned long long bytesSent, unsigned long long totalBytesToBeSent) OVERRIDE;
virtual void didReceiveResponse(blink::WebURLLoader*, const blink::WebURLResponse&) OVERRIDE;
virtual void didReceiveData(blink::WebURLLoader*, const char*, int, int encodedDataLength) OVERRIDE;
virtual void didReceiveCachedMetadata(blink::WebURLLoader*, const char* data, int length) OVERRIDE;
virtual void didFinishLoading(blink::WebURLLoader*, double finishTime, int64 encodedDataLength) OVERRIDE;
virtual void didFail(blink::WebURLLoader*, const blink::WebURLError&) OVERRIDE;
virtual void didDownloadData(blink::WebURLLoader*, int, int) OVERRIDE;
const KURL& url() const { return m_request.url(); }
bool shouldSniffContent() const { return m_options.sniffContent == SniffContent; }
bool isLoadedBy(ResourceLoaderHost*) const;
bool reachedTerminalState() const { return m_state == Terminated; }
const ResourceRequest& request() const { return m_request; }
private:
ResourceLoader(ResourceLoaderHost*, Resource*, const ResourceLoaderOptions&);
void init(const ResourceRequest&);
void requestSynchronously();
void didFinishLoadingOnePart(double finishTime, int64_t encodedDataLength);
bool responseNeedsAccessControlCheck() const;
ResourceRequest& applyOptions(ResourceRequest&) const;
OwnPtr<blink::WebURLLoader> m_loader;
RefPtr<ResourceLoaderHost> m_host;
ResourceRequest m_request;
ResourceRequest m_originalRequest;
bool m_notifiedLoadComplete;
bool m_defersLoading;
ResourceRequest m_deferredRequest;
ResourceLoaderOptions m_options;
enum ResourceLoaderState {
Initialized,
Finishing,
Terminated
};
enum ConnectionState {
ConnectionStateNew,
ConnectionStateStarted,
ConnectionStateReceivedResponse,
ConnectionStateReceivingData,
ConnectionStateFinishedLoading,
ConnectionStateCanceled,
ConnectionStateFailed,
};
class RequestCountTracker {
public:
RequestCountTracker(ResourceLoaderHost*, Resource*);
~RequestCountTracker();
private:
ResourceLoaderHost* m_host;
Resource* m_resource;
};
Resource* m_resource;
ResourceLoaderState m_state;
ConnectionState m_connectionState;
OwnPtr<RequestCountTracker> m_requestCountTracker;
};
}
#endif