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