Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / android_webview / browser / net / android_stream_reader_url_request_job.h
blobb7d6a621c8144b6c249a9ffe3b21a011ebe83fa2
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef ANDROID_WEBVIEW_NATIVE_ANDROID_STREAM_READER_URL_REQUEST_JOB_H_
6 #define ANDROID_WEBVIEW_NATIVE_ANDROID_STREAM_READER_URL_REQUEST_JOB_H_
8 #include <string>
10 #include "base/android/scoped_java_ref.h"
11 #include "base/location.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/threading/thread_checker.h"
16 #include "net/http/http_byte_range.h"
17 #include "net/url_request/url_request_job.h"
19 namespace android_webview {
20 class InputStream;
21 class InputStreamReader;
24 namespace base {
25 class TaskRunner;
28 namespace net {
29 class HttpResponseHeaders;
30 class HttpResponseInfo;
31 class URLRequest;
34 class InputStreamReaderWrapper;
36 // A request job that reads data from a Java InputStream.
37 class AndroidStreamReaderURLRequestJob : public net::URLRequestJob {
38 public:
40 * We use a delegate so that we can share code for this job in slightly
41 * different contexts.
43 class Delegate {
44 public:
45 // This method is called from a worker thread, not from the IO thread.
46 virtual scoped_ptr<android_webview::InputStream> OpenInputStream(
47 JNIEnv* env,
48 const GURL& url) = 0;
50 // This method is called on the Job's thread if the result of calling
51 // OpenInputStream was null.
52 // Setting the |restart| parameter to true will cause the request to be
53 // restarted with a new job.
54 virtual void OnInputStreamOpenFailed(
55 net::URLRequest* request,
56 bool* restart) = 0;
58 virtual bool GetMimeType(
59 JNIEnv* env,
60 net::URLRequest* request,
61 android_webview::InputStream* stream,
62 std::string* mime_type) = 0;
64 virtual bool GetCharset(
65 JNIEnv* env,
66 net::URLRequest* request,
67 android_webview::InputStream* stream,
68 std::string* charset) = 0;
70 virtual void AppendResponseHeaders(JNIEnv* env,
71 net::HttpResponseHeaders* headers) = 0;
73 virtual ~Delegate() {}
76 AndroidStreamReaderURLRequestJob(
77 net::URLRequest* request,
78 net::NetworkDelegate* network_delegate,
79 scoped_ptr<Delegate> delegate);
81 // URLRequestJob:
82 virtual void Start() OVERRIDE;
83 virtual void Kill() OVERRIDE;
84 virtual bool ReadRawData(net::IOBuffer* buf,
85 int buf_size,
86 int* bytes_read) OVERRIDE;
87 virtual void SetExtraRequestHeaders(
88 const net::HttpRequestHeaders& headers) OVERRIDE;
89 virtual bool GetMimeType(std::string* mime_type) const OVERRIDE;
90 virtual bool GetCharset(std::string* charset) OVERRIDE;
91 virtual int GetResponseCode() const OVERRIDE;
92 virtual void GetResponseInfo(net::HttpResponseInfo* info) OVERRIDE;
94 protected:
95 virtual ~AndroidStreamReaderURLRequestJob();
97 // Gets the TaskRunner for the worker thread.
98 // Overridden in unittests.
99 virtual base::TaskRunner* GetWorkerThreadRunner();
101 // Creates an InputStreamReader instance.
102 // Overridden in unittests to return a mock.
103 virtual scoped_ptr<android_webview::InputStreamReader>
104 CreateStreamReader(android_webview::InputStream* stream);
106 private:
107 void HeadersComplete(int status_code, const std::string& status_text);
109 void OnInputStreamOpened(
110 scoped_ptr<Delegate> delegate,
111 scoped_ptr<android_webview::InputStream> input_stream);
112 void OnReaderSeekCompleted(int content_size);
113 void OnReaderReadCompleted(int bytes_read);
115 net::HttpByteRange byte_range_;
116 scoped_ptr<net::HttpResponseInfo> response_info_;
117 scoped_ptr<Delegate> delegate_;
118 scoped_refptr<InputStreamReaderWrapper> input_stream_reader_wrapper_;
119 base::WeakPtrFactory<AndroidStreamReaderURLRequestJob> weak_factory_;
120 base::ThreadChecker thread_checker_;
122 DISALLOW_COPY_AND_ASSIGN(AndroidStreamReaderURLRequestJob);
125 #endif // ANDROID_WEBVIEW_NATIVE_ANDROID_STREAM_READER_URL_REQUEST_JOB_H_