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 |
15 namespace net { | 21 namespace net { |
16 class URLRequest; | 22 class URLRequest; |
17 } | 23 } |
18 | 24 |
19 // A request job that reads data from a Java InputStream. | 25 // A request job that reads data from a Java InputStream. |
20 class AndroidStreamReaderURLRequestJob : public net::URLRequestJob { | 26 class AndroidStreamReaderURLRequestJob : public net::URLRequestJob { |
21 public: | 27 public: |
22 /* | 28 /* |
23 * We use a delegate so that we can share code for this job in slightly | 29 * We use a delegate so that we can share code for this job in slightly |
24 * different contexts. | 30 * different contexts. |
25 */ | 31 */ |
26 class Delegate { | 32 class Delegate { |
27 public: | 33 public: |
28 virtual base::android::ScopedJavaLocalRef<jobject> OpenInputStream( | 34 virtual scoped_ptr<android_webview::InputStream> OpenInputStream( |
29 JNIEnv* env, | 35 JNIEnv* env, |
30 net::URLRequest* request) = 0; | 36 net::URLRequest* request) = 0; |
31 | 37 |
32 virtual bool GetMimeType( | 38 virtual bool GetMimeType( |
33 JNIEnv* env, | 39 JNIEnv* env, |
34 net::URLRequest* request, | 40 net::URLRequest* request, |
35 jobject stream, | 41 const android_webview::InputStream& stream, |
36 std::string* mime_type) = 0; | 42 std::string* mime_type) = 0; |
37 | 43 |
38 virtual bool GetCharset( | 44 virtual bool GetCharset( |
39 JNIEnv* env, | 45 JNIEnv* env, |
40 net::URLRequest* request, | 46 net::URLRequest* request, |
41 jobject stream, | 47 const android_webview::InputStream& stream, |
42 std::string* charset) = 0; | 48 std::string* charset) = 0; |
43 | 49 |
44 virtual ~Delegate() {} | 50 virtual ~Delegate() {} |
45 }; | 51 }; |
46 | 52 |
47 AndroidStreamReaderURLRequestJob( | 53 AndroidStreamReaderURLRequestJob( |
48 net::URLRequest* request, | 54 net::URLRequest* request, |
49 net::NetworkDelegate* network_delegate, | 55 net::NetworkDelegate* network_delegate, |
50 scoped_ptr<Delegate> delegate); | 56 scoped_ptr<Delegate> delegate); |
51 | 57 |
52 // URLRequestJob: | 58 // URLRequestJob: |
53 virtual void Start() OVERRIDE; | 59 virtual void Start() OVERRIDE; |
| 60 virtual void Kill() OVERRIDE; |
54 virtual bool ReadRawData(net::IOBuffer* buf, | 61 virtual bool ReadRawData(net::IOBuffer* buf, |
55 int buf_size, | 62 int buf_size, |
56 int* bytes_read) OVERRIDE; | 63 int* bytes_read) OVERRIDE; |
57 virtual void SetExtraRequestHeaders( | 64 virtual void SetExtraRequestHeaders( |
58 const net::HttpRequestHeaders& headers) OVERRIDE; | 65 const net::HttpRequestHeaders& headers) OVERRIDE; |
59 virtual bool GetMimeType(std::string* mime_type) const OVERRIDE; | 66 virtual bool GetMimeType(std::string* mime_type) const OVERRIDE; |
60 virtual bool GetCharset(std::string* charset) OVERRIDE; | 67 virtual bool GetCharset(std::string* charset) OVERRIDE; |
61 | 68 |
62 protected: | 69 protected: |
63 virtual ~AndroidStreamReaderURLRequestJob(); | 70 virtual ~AndroidStreamReaderURLRequestJob(); |
64 | 71 |
| 72 // Posts the |task| to the background worker thread. |
| 73 // Useful to override in tests. |
| 74 virtual void PostToWorkerThread(const tracked_objects::Location& from_here, |
| 75 const base::Closure& task); |
| 76 |
| 77 // Creates an InputStreamReader instance. |
| 78 virtual android_webview::InputStreamReader* |
| 79 CreateStreamReader(android_webview::InputStream* stream); |
| 80 |
65 private: | 81 private: |
66 // Verify the requested range against the stream size. | 82 void StartAsync(); |
67 bool VerifyRequestedRange(JNIEnv* env); | |
68 | 83 |
69 // Skip to the first byte of the requested read range. | 84 void OnReaderSeekCompleted(int content_size); |
70 bool SkipToRequestedRange(JNIEnv* env); | 85 void OnReaderReadCompleted(int bytes_read); |
71 | |
72 void StartAsync(); | |
73 | 86 |
74 net::HttpByteRange byte_range_; | 87 net::HttpByteRange byte_range_; |
75 scoped_ptr<Delegate> delegate_; | 88 scoped_ptr<Delegate> delegate_; |
76 base::android::ScopedJavaGlobalRef<jobject> stream_; | 89 android_webview::InputStreamReader* input_stream_reader_; |
77 base::android::ScopedJavaGlobalRef<jbyteArray> buffer_; | 90 scoped_ptr<android_webview::InputStream> stream_; |
78 base::WeakPtrFactory<AndroidStreamReaderURLRequestJob> weak_factory_; | 91 base::WeakPtrFactory<AndroidStreamReaderURLRequestJob> weak_factory_; |
79 | 92 |
80 DISALLOW_COPY_AND_ASSIGN(AndroidStreamReaderURLRequestJob); | 93 DISALLOW_COPY_AND_ASSIGN(AndroidStreamReaderURLRequestJob); |
81 }; | 94 }; |
82 | 95 |
83 bool RegisterAndroidStreamReaderUrlRequestJob(JNIEnv* env); | |
84 | |
85 #endif // ANDROID_WEBVIEW_NATIVE_ANDROID_STREAM_READER_URL_REQUEST_JOB_H_ | 96 #endif // ANDROID_WEBVIEW_NATIVE_ANDROID_STREAM_READER_URL_REQUEST_JOB_H_ |
OLD | NEW |