| 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_BROWSER_NET_ANDROID_STREAM_READER_URL_REQUEST_JOB_H_ |
| 6 #define ANDROID_WEBVIEW_NATIVE_ANDROID_STREAM_READER_URL_REQUEST_JOB_H_ | 6 #define ANDROID_WEBVIEW_BROWSER_NET_ANDROID_STREAM_READER_URL_REQUEST_JOB_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/android/scoped_java_ref.h" | 10 #include "base/android/scoped_java_ref.h" |
| 11 #include "base/callback.h" |
| 11 #include "base/location.h" | 12 #include "base/location.h" |
| 13 #include "base/macros.h" |
| 12 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
| 13 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/memory/weak_ptr.h" | 16 #include "base/memory/weak_ptr.h" |
| 15 #include "base/threading/thread_checker.h" | 17 #include "base/threading/thread_checker.h" |
| 16 #include "net/http/http_byte_range.h" | 18 #include "net/http/http_byte_range.h" |
| 17 #include "net/url_request/url_request_job.h" | 19 #include "net/url_request/url_request_job.h" |
| 18 | 20 |
| 19 namespace base { | 21 namespace base { |
| 20 class TaskRunner; | 22 class TaskRunner; |
| 21 } | 23 } |
| (...skipping 12 matching lines...) Expand all Loading... |
| 34 | 36 |
| 35 // A request job that reads data from a Java InputStream. | 37 // A request job that reads data from a Java InputStream. |
| 36 class AndroidStreamReaderURLRequestJob : public net::URLRequestJob { | 38 class AndroidStreamReaderURLRequestJob : public net::URLRequestJob { |
| 37 public: | 39 public: |
| 38 /* | 40 /* |
| 39 * We use a delegate so that we can share code for this job in slightly | 41 * We use a delegate so that we can share code for this job in slightly |
| 40 * different contexts. | 42 * different contexts. |
| 41 */ | 43 */ |
| 42 class Delegate { | 44 class Delegate { |
| 43 public: | 45 public: |
| 46 virtual ~Delegate() {} |
| 47 |
| 44 // This method is called from a worker thread, not from the IO thread. | 48 // This method is called from a worker thread, not from the IO thread. |
| 45 virtual scoped_ptr<android_webview::InputStream> OpenInputStream( | 49 virtual scoped_ptr<android_webview::InputStream> OpenInputStream( |
| 46 JNIEnv* env, | 50 JNIEnv* env, |
| 47 const GURL& url) = 0; | 51 const GURL& url) = 0; |
| 48 | 52 |
| 49 // This method is called on the Job's thread if the result of calling | 53 // This method is called on the Job's thread if the result of calling |
| 50 // OpenInputStream was null. | 54 // OpenInputStream was null. |
| 51 // Setting the |restart| parameter to true will cause the request to be | 55 // Setting the |restart| parameter to true will cause the request to be |
| 52 // restarted with a new job. | 56 // restarted with a new job. |
| 53 virtual void OnInputStreamOpenFailed( | 57 virtual void OnInputStreamOpenFailed( |
| 54 net::URLRequest* request, | 58 net::URLRequest* request, |
| 55 bool* restart) = 0; | 59 bool* restart) = 0; |
| 56 | 60 |
| 57 virtual bool GetMimeType( | 61 virtual bool GetMimeType( |
| 58 JNIEnv* env, | 62 JNIEnv* env, |
| 59 net::URLRequest* request, | 63 net::URLRequest* request, |
| 60 android_webview::InputStream* stream, | 64 android_webview::InputStream* stream, |
| 61 std::string* mime_type) = 0; | 65 std::string* mime_type) = 0; |
| 62 | 66 |
| 63 virtual bool GetCharset( | 67 virtual bool GetCharset( |
| 64 JNIEnv* env, | 68 JNIEnv* env, |
| 65 net::URLRequest* request, | 69 net::URLRequest* request, |
| 66 android_webview::InputStream* stream, | 70 android_webview::InputStream* stream, |
| 67 std::string* charset) = 0; | 71 std::string* charset) = 0; |
| 68 | 72 |
| 69 virtual void AppendResponseHeaders(JNIEnv* env, | 73 virtual void AppendResponseHeaders(JNIEnv* env, |
| 70 net::HttpResponseHeaders* headers) = 0; | 74 net::HttpResponseHeaders* headers) = 0; |
| 75 }; |
| 71 | 76 |
| 72 virtual ~Delegate() {} | 77 class DelegateObtainer { |
| 78 public: |
| 79 virtual ~DelegateObtainer() {} |
| 80 |
| 81 typedef base::Callback<void(scoped_ptr<Delegate>)> Callback; |
| 82 virtual void ObtainDelegate(net::URLRequest* request, |
| 83 const Callback& callback) = 0; |
| 73 }; | 84 }; |
| 74 | 85 |
| 75 AndroidStreamReaderURLRequestJob( | 86 AndroidStreamReaderURLRequestJob( |
| 76 net::URLRequest* request, | 87 net::URLRequest* request, |
| 77 net::NetworkDelegate* network_delegate, | 88 net::NetworkDelegate* network_delegate, |
| 78 scoped_ptr<Delegate> delegate); | 89 scoped_ptr<Delegate> delegate); |
| 90 AndroidStreamReaderURLRequestJob( |
| 91 net::URLRequest* request, |
| 92 net::NetworkDelegate* network_delegate, |
| 93 scoped_ptr<DelegateObtainer> delegate_obtainer, |
| 94 bool); // resolve ambiguity |
| 79 | 95 |
| 80 // URLRequestJob: | 96 // URLRequestJob: |
| 81 void Start() override; | 97 void Start() override; |
| 82 void Kill() override; | 98 void Kill() override; |
| 83 bool ReadRawData(net::IOBuffer* buf, int buf_size, int* bytes_read) override; | 99 bool ReadRawData(net::IOBuffer* buf, int buf_size, int* bytes_read) override; |
| 84 void SetExtraRequestHeaders(const net::HttpRequestHeaders& headers) override; | 100 void SetExtraRequestHeaders(const net::HttpRequestHeaders& headers) override; |
| 85 bool GetMimeType(std::string* mime_type) const override; | 101 bool GetMimeType(std::string* mime_type) const override; |
| 86 bool GetCharset(std::string* charset) override; | 102 bool GetCharset(std::string* charset) override; |
| 87 int GetResponseCode() const override; | 103 int GetResponseCode() const override; |
| 88 void GetResponseInfo(net::HttpResponseInfo* info) override; | 104 void GetResponseInfo(net::HttpResponseInfo* info) override; |
| 89 | 105 |
| 90 protected: | 106 protected: |
| 91 ~AndroidStreamReaderURLRequestJob() override; | 107 ~AndroidStreamReaderURLRequestJob() override; |
| 92 | 108 |
| 93 // Gets the TaskRunner for the worker thread. | 109 // Gets the TaskRunner for the worker thread. |
| 94 // Overridden in unittests. | 110 // Overridden in unittests. |
| 95 virtual base::TaskRunner* GetWorkerThreadRunner(); | 111 virtual base::TaskRunner* GetWorkerThreadRunner(); |
| 96 | 112 |
| 97 // Creates an InputStreamReader instance. | 113 // Creates an InputStreamReader instance. |
| 98 // Overridden in unittests to return a mock. | 114 // Overridden in unittests to return a mock. |
| 99 virtual scoped_ptr<android_webview::InputStreamReader> | 115 virtual scoped_ptr<android_webview::InputStreamReader> |
| 100 CreateStreamReader(android_webview::InputStream* stream); | 116 CreateStreamReader(android_webview::InputStream* stream); |
| 101 | 117 |
| 102 private: | 118 private: |
| 119 // Used as a callback when obtaining the delegate asynchronously, |
| 120 // see DelegateObtainer. |
| 121 void DelegateObtained(scoped_ptr<Delegate> delegate); |
| 122 // Actual URLRequestJob::Start implementation. |
| 123 void DoStart(); |
| 124 |
| 103 void HeadersComplete(int status_code, const std::string& status_text); | 125 void HeadersComplete(int status_code, const std::string& status_text); |
| 104 | 126 |
| 105 void OnInputStreamOpened( | 127 void OnInputStreamOpened( |
| 106 scoped_ptr<Delegate> delegate, | 128 scoped_ptr<Delegate> delegate, |
| 107 scoped_ptr<android_webview::InputStream> input_stream); | 129 scoped_ptr<android_webview::InputStream> input_stream); |
| 108 void OnReaderSeekCompleted(int content_size); | 130 void OnReaderSeekCompleted(int content_size); |
| 109 void OnReaderReadCompleted(int bytes_read); | 131 void OnReaderReadCompleted(int bytes_read); |
| 110 | 132 |
| 111 net::HttpByteRange byte_range_; | 133 net::HttpByteRange byte_range_; |
| 112 scoped_ptr<net::HttpResponseInfo> response_info_; | 134 scoped_ptr<net::HttpResponseInfo> response_info_; |
| 113 scoped_ptr<Delegate> delegate_; | 135 scoped_ptr<Delegate> delegate_; |
| 136 scoped_ptr<DelegateObtainer> delegate_obtainer_; |
| 114 scoped_refptr<InputStreamReaderWrapper> input_stream_reader_wrapper_; | 137 scoped_refptr<InputStreamReaderWrapper> input_stream_reader_wrapper_; |
| 115 base::ThreadChecker thread_checker_; | 138 base::ThreadChecker thread_checker_; |
| 116 | 139 |
| 117 base::WeakPtrFactory<AndroidStreamReaderURLRequestJob> weak_factory_; | 140 base::WeakPtrFactory<AndroidStreamReaderURLRequestJob> weak_factory_; |
| 118 | 141 |
| 119 DISALLOW_COPY_AND_ASSIGN(AndroidStreamReaderURLRequestJob); | 142 DISALLOW_COPY_AND_ASSIGN(AndroidStreamReaderURLRequestJob); |
| 120 }; | 143 }; |
| 121 | 144 |
| 122 } // namespace android_webview | 145 } // namespace android_webview |
| 123 | 146 |
| 124 #endif // ANDROID_WEBVIEW_NATIVE_ANDROID_STREAM_READER_URL_REQUEST_JOB_H_ | 147 #endif // ANDROID_WEBVIEW_BROWSER_NET_ANDROID_STREAM_READER_URL_REQUEST_JOB_H_ |
| OLD | NEW |