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/location.h" |
| 10 #include "base/memory/ref_counted.h" |
10 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
11 #include "base/memory/weak_ptr.h" | 12 #include "base/memory/weak_ptr.h" |
12 #include "base/threading/non_thread_safe.h" | 13 #include "base/threading/thread_checker.h" |
13 #include "net/http/http_byte_range.h" | 14 #include "net/http/http_byte_range.h" |
14 #include "net/url_request/url_request_job.h" | 15 #include "net/url_request/url_request_job.h" |
15 | 16 |
16 namespace android_webview { | 17 namespace android_webview { |
17 class InputStream; | 18 class InputStream; |
18 class InputStreamReader; | 19 class InputStreamReader; |
19 } | 20 } |
20 | 21 |
21 namespace base { | 22 namespace base { |
22 class TaskRunner; | 23 class TaskRunner; |
23 } | 24 } |
24 | 25 |
25 namespace net { | 26 namespace net { |
26 class URLRequest; | 27 class URLRequest; |
27 } | 28 } |
28 | 29 |
29 class InputStreamReaderWrapper; | 30 class InputStreamReaderWrapper; |
30 | 31 |
31 // A request job that reads data from a Java InputStream. | 32 // A request job that reads data from a Java InputStream. |
32 class AndroidStreamReaderURLRequestJob : public net::URLRequestJob { | 33 class AndroidStreamReaderURLRequestJob : public net::URLRequestJob { |
33 public: | 34 public: |
34 /* | 35 /* |
35 * We use a delegate so that we can share code for this job in slightly | 36 * We use a delegate so that we can share code for this job in slightly |
36 * different contexts. | 37 * different contexts. |
37 */ | 38 */ |
38 class Delegate { | 39 class Delegate { |
39 public: | 40 public: |
| 41 // This method is called from a worker thread, not from the IO thread. |
40 virtual scoped_ptr<android_webview::InputStream> OpenInputStream( | 42 virtual scoped_ptr<android_webview::InputStream> OpenInputStream( |
41 JNIEnv* env, | 43 JNIEnv* env, |
42 net::URLRequest* request) = 0; | 44 const GURL& url) = 0; |
| 45 |
| 46 // This method is called on the Job's thread if the result of calling |
| 47 // OpenInputStream was null. |
| 48 // Setting the |restart| parameter to true will cause the request to be |
| 49 // restarted with a new job. |
| 50 virtual void OnInputStreamOpenFailed( |
| 51 net::URLRequest* request, |
| 52 bool* restart) = 0; |
43 | 53 |
44 virtual bool GetMimeType( | 54 virtual bool GetMimeType( |
45 JNIEnv* env, | 55 JNIEnv* env, |
46 net::URLRequest* request, | 56 net::URLRequest* request, |
47 android_webview::InputStream* stream, | 57 android_webview::InputStream* stream, |
48 std::string* mime_type) = 0; | 58 std::string* mime_type) = 0; |
49 | 59 |
50 virtual bool GetCharset( | 60 virtual bool GetCharset( |
51 JNIEnv* env, | 61 JNIEnv* env, |
52 net::URLRequest* request, | 62 net::URLRequest* request, |
(...skipping 25 matching lines...) Expand all Loading... |
78 // Gets the TaskRunner for the worker thread. | 88 // Gets the TaskRunner for the worker thread. |
79 // Overridden in unittests. | 89 // Overridden in unittests. |
80 virtual base::TaskRunner* GetWorkerThreadRunner(); | 90 virtual base::TaskRunner* GetWorkerThreadRunner(); |
81 | 91 |
82 // Creates an InputStreamReader instance. | 92 // Creates an InputStreamReader instance. |
83 // Overridden in unittests to return a mock. | 93 // Overridden in unittests to return a mock. |
84 virtual scoped_ptr<android_webview::InputStreamReader> | 94 virtual scoped_ptr<android_webview::InputStreamReader> |
85 CreateStreamReader(android_webview::InputStream* stream); | 95 CreateStreamReader(android_webview::InputStream* stream); |
86 | 96 |
87 private: | 97 private: |
88 void StartAsync(); | 98 void OnInputStreamOpened( |
89 | 99 scoped_ptr<Delegate> delegate, |
| 100 scoped_ptr<android_webview::InputStream> input_stream); |
90 void OnReaderSeekCompleted(int content_size); | 101 void OnReaderSeekCompleted(int content_size); |
91 void OnReaderReadCompleted(int bytes_read); | 102 void OnReaderReadCompleted(int bytes_read); |
92 | 103 |
93 net::HttpByteRange byte_range_; | 104 net::HttpByteRange byte_range_; |
94 scoped_ptr<Delegate> delegate_; | 105 scoped_ptr<Delegate> delegate_; |
95 scoped_refptr<InputStreamReaderWrapper> input_stream_reader_wrapper_; | 106 scoped_refptr<InputStreamReaderWrapper> input_stream_reader_wrapper_; |
96 base::WeakPtrFactory<AndroidStreamReaderURLRequestJob> weak_factory_; | 107 base::WeakPtrFactory<AndroidStreamReaderURLRequestJob> weak_factory_; |
| 108 base::ThreadChecker thread_checker_; |
97 | 109 |
98 DISALLOW_COPY_AND_ASSIGN(AndroidStreamReaderURLRequestJob); | 110 DISALLOW_COPY_AND_ASSIGN(AndroidStreamReaderURLRequestJob); |
99 }; | 111 }; |
100 | 112 |
101 #endif // ANDROID_WEBVIEW_NATIVE_ANDROID_STREAM_READER_URL_REQUEST_JOB_H_ | 113 #endif // ANDROID_WEBVIEW_NATIVE_ANDROID_STREAM_READER_URL_REQUEST_JOB_H_ |
OLD | NEW |