#ifndef ANDROID_WEBVIEW_NATIVE_ANDROID_STREAM_READER_URL_REQUEST_JOB_H_
#define ANDROID_WEBVIEW_NATIVE_ANDROID_STREAM_READER_URL_REQUEST_JOB_H_
#include <string>
#include "base/android/scoped_java_ref.h"
#include "base/location.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/threading/thread_checker.h"
#include "net/http/http_byte_range.h"
#include "net/url_request/url_request_job.h"
namespace android_webview {
class InputStream;
class InputStreamReader;
}
namespace base {
class TaskRunner;
}
namespace net {
class HttpResponseInfo;
class URLRequest;
}
class InputStreamReaderWrapper;
class AndroidStreamReaderURLRequestJob : public net::URLRequestJob {
public:
class Delegate {
public:
virtual scoped_ptr<android_webview::InputStream> OpenInputStream(
JNIEnv* env,
const GURL& url) = 0;
virtual void OnInputStreamOpenFailed(
net::URLRequest* request,
bool* restart) = 0;
virtual bool GetMimeType(
JNIEnv* env,
net::URLRequest* request,
android_webview::InputStream* stream,
std::string* mime_type) = 0;
virtual bool GetCharset(
JNIEnv* env,
net::URLRequest* request,
android_webview::InputStream* stream,
std::string* charset) = 0;
virtual ~Delegate() {}
};
AndroidStreamReaderURLRequestJob(
net::URLRequest* request,
net::NetworkDelegate* network_delegate,
scoped_ptr<Delegate> delegate);
virtual void Start() OVERRIDE;
virtual void Kill() OVERRIDE;
virtual bool ReadRawData(net::IOBuffer* buf,
int buf_size,
int* bytes_read) OVERRIDE;
virtual void SetExtraRequestHeaders(
const net::HttpRequestHeaders& headers) OVERRIDE;
virtual bool GetMimeType(std::string* mime_type) const OVERRIDE;
virtual bool GetCharset(std::string* charset) OVERRIDE;
virtual int GetResponseCode() const OVERRIDE;
virtual void GetResponseInfo(net::HttpResponseInfo* info) OVERRIDE;
protected:
virtual ~AndroidStreamReaderURLRequestJob();
virtual base::TaskRunner* GetWorkerThreadRunner();
virtual scoped_ptr<android_webview::InputStreamReader>
CreateStreamReader(android_webview::InputStream* stream);
private:
void HeadersComplete(int status_code, const std::string& status_text);
void OnInputStreamOpened(
scoped_ptr<Delegate> delegate,
scoped_ptr<android_webview::InputStream> input_stream);
void OnReaderSeekCompleted(int content_size);
void OnReaderReadCompleted(int bytes_read);
net::HttpByteRange byte_range_;
scoped_ptr<net::HttpResponseInfo> response_info_;
scoped_ptr<Delegate> delegate_;
scoped_refptr<InputStreamReaderWrapper> input_stream_reader_wrapper_;
base::WeakPtrFactory<AndroidStreamReaderURLRequestJob> weak_factory_;
base::ThreadChecker thread_checker_;
DISALLOW_COPY_AND_ASSIGN(AndroidStreamReaderURLRequestJob);
};
#endif