OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef ANDROID_WEBVIEW_NATIVE_ANDROID_STREAM_READER_URL_REQUEST_JOB_H_ | 5 #ifndef ANDROID_WEBVIEW_NATIVE_ANDROID_STREAM_READER_URL_REQUEST_JOB_H_ |
6 #define ANDROID_WEBVIEW_NATIVE_ANDROID_STREAM_READER_URL_REQUEST_JOB_H_ | 6 #define ANDROID_WEBVIEW_NATIVE_ANDROID_STREAM_READER_URL_REQUEST_JOB_H_ |
7 | 7 |
8 #include "base/android/scoped_java_ref.h" | 8 #include "base/android/scoped_java_ref.h" |
| 9 #include "base/location.h" |
9 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
10 #include "base/memory/weak_ptr.h" | 11 #include "base/memory/weak_ptr.h" |
11 #include "base/threading/non_thread_safe.h" | 12 #include "base/threading/non_thread_safe.h" |
12 #include "net/http/http_byte_range.h" | 13 #include "net/http/http_byte_range.h" |
13 #include "net/url_request/url_request_job.h" | 14 #include "net/url_request/url_request_job.h" |
14 | 15 |
| 16 namespace android_webview { |
| 17 class InputStream; |
| 18 class InputStreamReader; |
| 19 } |
| 20 |
| 21 namespace base { |
| 22 class TaskRunner; |
| 23 } |
| 24 |
15 namespace net { | 25 namespace net { |
16 class URLRequest; | 26 class URLRequest; |
17 } | 27 } |
18 | 28 |
19 // A request job that reads data from a Java InputStream. | 29 // A request job that reads data from a Java InputStream. |
20 class AndroidStreamReaderURLRequestJob : public net::URLRequestJob { | 30 class AndroidStreamReaderURLRequestJob : public net::URLRequestJob { |
21 public: | 31 public: |
22 /* | 32 /* |
23 * We use a delegate so that we can share code for this job in slightly | 33 * We use a delegate so that we can share code for this job in slightly |
24 * different contexts. | 34 * different contexts. |
25 */ | 35 */ |
26 class Delegate { | 36 class Delegate { |
27 public: | 37 public: |
28 virtual base::android::ScopedJavaLocalRef<jobject> OpenInputStream( | 38 virtual scoped_ptr<android_webview::InputStream> OpenInputStream( |
29 JNIEnv* env, | 39 JNIEnv* env, |
30 net::URLRequest* request) = 0; | 40 net::URLRequest* request) = 0; |
31 | 41 |
32 virtual bool GetMimeType( | 42 virtual bool GetMimeType( |
33 JNIEnv* env, | 43 JNIEnv* env, |
34 net::URLRequest* request, | 44 net::URLRequest* request, |
35 jobject stream, | 45 const android_webview::InputStream& stream, |
36 std::string* mime_type) = 0; | 46 std::string* mime_type) = 0; |
37 | 47 |
38 virtual bool GetCharset( | 48 virtual bool GetCharset( |
39 JNIEnv* env, | 49 JNIEnv* env, |
40 net::URLRequest* request, | 50 net::URLRequest* request, |
41 jobject stream, | 51 const android_webview::InputStream& stream, |
42 std::string* charset) = 0; | 52 std::string* charset) = 0; |
43 | 53 |
44 virtual ~Delegate() {} | 54 virtual ~Delegate() {} |
45 }; | 55 }; |
46 | 56 |
47 AndroidStreamReaderURLRequestJob( | 57 AndroidStreamReaderURLRequestJob( |
48 net::URLRequest* request, | 58 net::URLRequest* request, |
49 net::NetworkDelegate* network_delegate, | 59 net::NetworkDelegate* network_delegate, |
50 scoped_ptr<Delegate> delegate); | 60 scoped_ptr<Delegate> delegate); |
51 | 61 |
52 // URLRequestJob: | 62 // URLRequestJob: |
53 virtual void Start() OVERRIDE; | 63 virtual void Start() OVERRIDE; |
| 64 virtual void Kill() OVERRIDE; |
54 virtual bool ReadRawData(net::IOBuffer* buf, | 65 virtual bool ReadRawData(net::IOBuffer* buf, |
55 int buf_size, | 66 int buf_size, |
56 int* bytes_read) OVERRIDE; | 67 int* bytes_read) OVERRIDE; |
57 virtual void SetExtraRequestHeaders( | 68 virtual void SetExtraRequestHeaders( |
58 const net::HttpRequestHeaders& headers) OVERRIDE; | 69 const net::HttpRequestHeaders& headers) OVERRIDE; |
59 virtual bool GetMimeType(std::string* mime_type) const OVERRIDE; | 70 virtual bool GetMimeType(std::string* mime_type) const OVERRIDE; |
60 virtual bool GetCharset(std::string* charset) OVERRIDE; | 71 virtual bool GetCharset(std::string* charset) OVERRIDE; |
61 | 72 |
62 protected: | 73 protected: |
63 virtual ~AndroidStreamReaderURLRequestJob(); | 74 virtual ~AndroidStreamReaderURLRequestJob(); |
64 | 75 |
| 76 // Gets the TaskRunner for the worker thread. |
| 77 // Overridden in unittests. |
| 78 virtual base::TaskRunner* GetWorkerThreadRunner(); |
| 79 |
| 80 // Creates an InputStreamReader instance. |
| 81 // Overridden in unittests to return a mock. |
| 82 virtual scoped_refptr<android_webview::InputStreamReader> |
| 83 CreateStreamReader(android_webview::InputStream* stream); |
| 84 |
65 private: | 85 private: |
66 // Verify the requested range against the stream size. | 86 void StartAsync(); |
67 bool VerifyRequestedRange(JNIEnv* env); | |
68 | 87 |
69 // Skip to the first byte of the requested read range. | 88 void OnReaderSeekCompleted(int content_size); |
70 bool SkipToRequestedRange(JNIEnv* env); | 89 void OnReaderReadCompleted(int bytes_read); |
71 | |
72 void StartAsync(); | |
73 | 90 |
74 net::HttpByteRange byte_range_; | 91 net::HttpByteRange byte_range_; |
75 scoped_ptr<Delegate> delegate_; | 92 scoped_ptr<Delegate> delegate_; |
76 base::android::ScopedJavaGlobalRef<jobject> stream_; | 93 scoped_refptr<android_webview::InputStreamReader> input_stream_reader_; |
77 base::android::ScopedJavaGlobalRef<jbyteArray> buffer_; | 94 scoped_ptr<android_webview::InputStream> stream_; |
78 base::WeakPtrFactory<AndroidStreamReaderURLRequestJob> weak_factory_; | 95 base::WeakPtrFactory<AndroidStreamReaderURLRequestJob> weak_factory_; |
79 | 96 |
80 DISALLOW_COPY_AND_ASSIGN(AndroidStreamReaderURLRequestJob); | 97 DISALLOW_COPY_AND_ASSIGN(AndroidStreamReaderURLRequestJob); |
81 }; | 98 }; |
82 | 99 |
83 bool RegisterAndroidStreamReaderUrlRequestJob(JNIEnv* env); | |
84 | |
85 #endif // ANDROID_WEBVIEW_NATIVE_ANDROID_STREAM_READER_URL_REQUEST_JOB_H_ | 100 #endif // ANDROID_WEBVIEW_NATIVE_ANDROID_STREAM_READER_URL_REQUEST_JOB_H_ |
OLD | NEW |