| 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 #include "android_webview/browser/net/android_stream_reader_url_request_job.h" | 5 #include "android_webview/browser/net/android_stream_reader_url_request_job.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "android_webview/browser/input_stream.h" | 9 #include "android_webview/browser/input_stream.h" |
| 10 #include "android_webview/browser/net/aw_stream_reader_job_delegate_impl.h" |
| 10 #include "android_webview/browser/net/input_stream_reader.h" | 11 #include "android_webview/browser/net/input_stream_reader.h" |
| 11 #include "base/android/jni_android.h" | 12 #include "base/android/jni_android.h" |
| 12 #include "base/android/jni_string.h" | 13 #include "base/android/jni_string.h" |
| 13 #include "base/bind.h" | 14 #include "base/bind.h" |
| 14 #include "base/bind_helpers.h" | 15 #include "base/bind_helpers.h" |
| 15 #include "base/lazy_instance.h" | 16 #include "base/lazy_instance.h" |
| 16 #include "base/single_thread_task_runner.h" | 17 #include "base/single_thread_task_runner.h" |
| 17 #include "base/strings/string_number_conversions.h" | 18 #include "base/strings/string_number_conversions.h" |
| 18 #include "base/task_runner.h" | 19 #include "base/task_runner.h" |
| 19 #include "base/threading/sequenced_worker_pool.h" | 20 #include "base/threading/sequenced_worker_pool.h" |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 80 ~InputStreamReaderWrapper() {} | 81 ~InputStreamReaderWrapper() {} |
| 81 | 82 |
| 82 scoped_ptr<InputStream> input_stream_; | 83 scoped_ptr<InputStream> input_stream_; |
| 83 scoped_ptr<InputStreamReader> input_stream_reader_; | 84 scoped_ptr<InputStreamReader> input_stream_reader_; |
| 84 | 85 |
| 85 DISALLOW_COPY_AND_ASSIGN(InputStreamReaderWrapper); | 86 DISALLOW_COPY_AND_ASSIGN(InputStreamReaderWrapper); |
| 86 }; | 87 }; |
| 87 | 88 |
| 88 AndroidStreamReaderURLRequestJob::AndroidStreamReaderURLRequestJob( | 89 AndroidStreamReaderURLRequestJob::AndroidStreamReaderURLRequestJob( |
| 89 net::URLRequest* request, | 90 net::URLRequest* request, |
| 91 net::NetworkDelegate* network_delegate) |
| 92 : URLRequestJob(request, network_delegate), |
| 93 weak_factory_(this) { |
| 94 } |
| 95 |
| 96 AndroidStreamReaderURLRequestJob::AndroidStreamReaderURLRequestJob( |
| 97 net::URLRequest* request, |
| 90 net::NetworkDelegate* network_delegate, | 98 net::NetworkDelegate* network_delegate, |
| 91 scoped_ptr<Delegate> delegate) | 99 scoped_ptr<Delegate> delegate) |
| 92 : URLRequestJob(request, network_delegate), | 100 : URLRequestJob(request, network_delegate), |
| 93 delegate_(delegate.Pass()), | 101 delegate_(delegate.Pass()), |
| 94 weak_factory_(this) { | 102 weak_factory_(this) { |
| 95 DCHECK(delegate_); | 103 DCHECK(delegate_); |
| 96 } | 104 } |
| 97 | 105 |
| 98 AndroidStreamReaderURLRequestJob::~AndroidStreamReaderURLRequestJob() { | 106 AndroidStreamReaderURLRequestJob::~AndroidStreamReaderURLRequestJob() { |
| 99 } | 107 } |
| 100 | 108 |
| 109 base::Callback<void(scoped_ptr<AwWebResourceResponse>)> |
| 110 AndroidStreamReaderURLRequestJob::GetWebResourceResponseCallback() { |
| 111 return base::Bind( |
| 112 &AndroidStreamReaderURLRequestJob::WebResourceResponseObtained, |
| 113 weak_factory_.GetWeakPtr()); |
| 114 } |
| 115 |
| 116 void AndroidStreamReaderURLRequestJob::WebResourceResponseObtained( |
| 117 scoped_ptr<AwWebResourceResponse> response) { |
| 118 DCHECK(!delegate_); |
| 119 if (response) { |
| 120 delegate_.reset(new AwStreamReaderJobDelegateImpl(response.Pass())); |
| 121 MaybeStart(); |
| 122 } else { |
| 123 NotifyRestartRequired(); |
| 124 } |
| 125 } |
| 126 |
| 101 namespace { | 127 namespace { |
| 102 | 128 |
| 103 typedef base::Callback< | 129 typedef base::Callback< |
| 104 void(scoped_ptr<AndroidStreamReaderURLRequestJob::Delegate>, | 130 void(scoped_ptr<AndroidStreamReaderURLRequestJob::Delegate>, |
| 105 scoped_ptr<InputStream>)> OnInputStreamOpenedCallback; | 131 scoped_ptr<InputStream>)> OnInputStreamOpenedCallback; |
| 106 | 132 |
| 107 // static | 133 // static |
| 108 void OpenInputStreamOnWorkerThread( | 134 void OpenInputStreamOnWorkerThread( |
| 109 scoped_refptr<base::SingleThreadTaskRunner> job_thread_task_runner, | 135 scoped_refptr<base::SingleThreadTaskRunner> job_thread_task_runner, |
| 110 scoped_ptr<AndroidStreamReaderURLRequestJob::Delegate> delegate, | 136 scoped_ptr<AndroidStreamReaderURLRequestJob::Delegate> delegate, |
| 111 const GURL& url, | 137 const GURL& url, |
| 112 OnInputStreamOpenedCallback callback) { | 138 OnInputStreamOpenedCallback callback) { |
| 113 JNIEnv* env = AttachCurrentThread(); | 139 JNIEnv* env = AttachCurrentThread(); |
| 114 DCHECK(env); | 140 DCHECK(env); |
| 115 | 141 |
| 116 scoped_ptr<InputStream> input_stream = delegate->OpenInputStream(env, url); | 142 scoped_ptr<InputStream> input_stream = delegate->OpenInputStream(env, url); |
| 117 job_thread_task_runner->PostTask( | 143 job_thread_task_runner->PostTask( |
| 118 FROM_HERE, base::Bind(callback, base::Passed(delegate.Pass()), | 144 FROM_HERE, base::Bind(callback, base::Passed(delegate.Pass()), |
| 119 base::Passed(input_stream.Pass()))); | 145 base::Passed(input_stream.Pass()))); |
| 120 } | 146 } |
| 121 | 147 |
| 122 } // namespace | 148 } // namespace |
| 123 | 149 |
| 124 void AndroidStreamReaderURLRequestJob::Start() { | 150 void AndroidStreamReaderURLRequestJob::MaybeStart() { |
| 125 DCHECK(thread_checker_.CalledOnValidThread()); | 151 DCHECK(thread_checker_.CalledOnValidThread()); |
| 152 DCHECK(!has_been_killed()); |
| 153 if (!has_been_started() || !delegate_) |
| 154 return; |
| 155 |
| 126 // Start reading asynchronously so that all error reporting and data | 156 // Start reading asynchronously so that all error reporting and data |
| 127 // callbacks happen as they would for network requests. | 157 // callbacks happen as they would for network requests. |
| 128 SetStatus(net::URLRequestStatus(net::URLRequestStatus::IO_PENDING, | 158 SetStatus(net::URLRequestStatus(net::URLRequestStatus::IO_PENDING, |
| 129 net::ERR_IO_PENDING)); | 159 net::ERR_IO_PENDING)); |
| 130 | 160 |
| 131 // This could be done in the InputStreamReader but would force more | 161 // This could be done in the InputStreamReader but would force more |
| 132 // complex synchronization in the delegate. | 162 // complex synchronization in the delegate. |
| 133 GetWorkerThreadRunner()->PostTask( | 163 GetWorkerThreadRunner()->PostTask( |
| 134 FROM_HERE, | 164 FROM_HERE, |
| 135 base::Bind( | 165 base::Bind( |
| 136 &OpenInputStreamOnWorkerThread, | 166 &OpenInputStreamOnWorkerThread, |
| 137 base::MessageLoop::current()->task_runner(), | 167 base::MessageLoop::current()->task_runner(), |
| 138 // This is intentional - the job could be deleted while the callback | 168 // This is intentional - the job could be deleted while the callback |
| 139 // is executing on the background thread. | 169 // is executing on the background thread. |
| 140 // The delegate will be "returned" to the job once the InputStream | 170 // The delegate will be "returned" to the job once the InputStream |
| 141 // open attempt is completed. | 171 // open attempt is completed. |
| 142 base::Passed(&delegate_), request()->url(), | 172 base::Passed(&delegate_), request()->url(), |
| 143 base::Bind(&AndroidStreamReaderURLRequestJob::OnInputStreamOpened, | 173 base::Bind(&AndroidStreamReaderURLRequestJob::OnInputStreamOpened, |
| 144 weak_factory_.GetWeakPtr()))); | 174 weak_factory_.GetWeakPtr()))); |
| 145 } | 175 } |
| 146 | 176 |
| 177 void AndroidStreamReaderURLRequestJob::Start() { |
| 178 DCHECK(!has_been_started()); |
| 179 has_been_started_ = true; |
| 180 MaybeStart(); |
| 181 } |
| 182 |
| 147 void AndroidStreamReaderURLRequestJob::Kill() { | 183 void AndroidStreamReaderURLRequestJob::Kill() { |
| 148 DCHECK(thread_checker_.CalledOnValidThread()); | 184 DCHECK(thread_checker_.CalledOnValidThread()); |
| 185 has_been_killed_ = true; |
| 149 weak_factory_.InvalidateWeakPtrs(); | 186 weak_factory_.InvalidateWeakPtrs(); |
| 150 URLRequestJob::Kill(); | 187 URLRequestJob::Kill(); |
| 151 } | 188 } |
| 152 | 189 |
| 153 scoped_ptr<InputStreamReader> | 190 scoped_ptr<InputStreamReader> |
| 154 AndroidStreamReaderURLRequestJob::CreateStreamReader(InputStream* stream) { | 191 AndroidStreamReaderURLRequestJob::CreateStreamReader(InputStream* stream) { |
| 155 return make_scoped_ptr(new InputStreamReader(stream)); | 192 return make_scoped_ptr(new InputStreamReader(stream)); |
| 156 } | 193 } |
| 157 | 194 |
| 158 void AndroidStreamReaderURLRequestJob::OnInputStreamOpened( | 195 void AndroidStreamReaderURLRequestJob::OnInputStreamOpened( |
| (...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 360 // because we need to do multipart encoding here. | 397 // because we need to do multipart encoding here. |
| 361 NotifyDone(net::URLRequestStatus( | 398 NotifyDone(net::URLRequestStatus( |
| 362 net::URLRequestStatus::FAILED, | 399 net::URLRequestStatus::FAILED, |
| 363 net::ERR_REQUEST_RANGE_NOT_SATISFIABLE)); | 400 net::ERR_REQUEST_RANGE_NOT_SATISFIABLE)); |
| 364 } | 401 } |
| 365 } | 402 } |
| 366 } | 403 } |
| 367 } | 404 } |
| 368 | 405 |
| 369 } // namespace android_webview | 406 } // namespace android_webview |
| OLD | NEW |