Chromium Code Reviews| 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/input_stream_reader.h" | 10 #include "android_webview/browser/net/input_stream_reader.h" |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 88 AndroidStreamReaderURLRequestJob::AndroidStreamReaderURLRequestJob( | 88 AndroidStreamReaderURLRequestJob::AndroidStreamReaderURLRequestJob( |
| 89 net::URLRequest* request, | 89 net::URLRequest* request, |
| 90 net::NetworkDelegate* network_delegate, | 90 net::NetworkDelegate* network_delegate, |
| 91 scoped_ptr<Delegate> delegate) | 91 scoped_ptr<Delegate> delegate) |
| 92 : URLRequestJob(request, network_delegate), | 92 : URLRequestJob(request, network_delegate), |
| 93 delegate_(delegate.Pass()), | 93 delegate_(delegate.Pass()), |
| 94 weak_factory_(this) { | 94 weak_factory_(this) { |
| 95 DCHECK(delegate_); | 95 DCHECK(delegate_); |
| 96 } | 96 } |
| 97 | 97 |
| 98 AndroidStreamReaderURLRequestJob::AndroidStreamReaderURLRequestJob( | |
| 99 net::URLRequest* request, | |
| 100 net::NetworkDelegate* network_delegate, | |
| 101 scoped_ptr<DelegateObtainer> delegate_obtainer, | |
| 102 bool) | |
| 103 : URLRequestJob(request, network_delegate), | |
| 104 delegate_obtainer_(delegate_obtainer.Pass()), | |
| 105 weak_factory_(this) { | |
| 106 DCHECK(delegate_obtainer_); | |
| 107 } | |
| 108 | |
| 98 AndroidStreamReaderURLRequestJob::~AndroidStreamReaderURLRequestJob() { | 109 AndroidStreamReaderURLRequestJob::~AndroidStreamReaderURLRequestJob() { |
| 99 } | 110 } |
| 100 | 111 |
| 101 namespace { | 112 namespace { |
| 102 | 113 |
| 103 typedef base::Callback< | 114 typedef base::Callback< |
| 104 void(scoped_ptr<AndroidStreamReaderURLRequestJob::Delegate>, | 115 void(scoped_ptr<AndroidStreamReaderURLRequestJob::Delegate>, |
| 105 scoped_ptr<InputStream>)> OnInputStreamOpenedCallback; | 116 scoped_ptr<InputStream>)> OnInputStreamOpenedCallback; |
| 106 | 117 |
| 107 // static | 118 // static |
| 108 void OpenInputStreamOnWorkerThread( | 119 void OpenInputStreamOnWorkerThread( |
| 109 scoped_refptr<base::SingleThreadTaskRunner> job_thread_task_runner, | 120 scoped_refptr<base::SingleThreadTaskRunner> job_thread_task_runner, |
| 110 scoped_ptr<AndroidStreamReaderURLRequestJob::Delegate> delegate, | 121 scoped_ptr<AndroidStreamReaderURLRequestJob::Delegate> delegate, |
| 111 const GURL& url, | 122 const GURL& url, |
| 112 OnInputStreamOpenedCallback callback) { | 123 OnInputStreamOpenedCallback callback) { |
| 113 JNIEnv* env = AttachCurrentThread(); | 124 JNIEnv* env = AttachCurrentThread(); |
| 114 DCHECK(env); | 125 DCHECK(env); |
| 115 | 126 |
| 116 scoped_ptr<InputStream> input_stream = delegate->OpenInputStream(env, url); | 127 scoped_ptr<InputStream> input_stream = delegate->OpenInputStream(env, url); |
| 117 job_thread_task_runner->PostTask( | 128 job_thread_task_runner->PostTask( |
| 118 FROM_HERE, base::Bind(callback, base::Passed(delegate.Pass()), | 129 FROM_HERE, base::Bind(callback, base::Passed(delegate.Pass()), |
| 119 base::Passed(input_stream.Pass()))); | 130 base::Passed(input_stream.Pass()))); |
| 120 } | 131 } |
| 121 | 132 |
| 122 } // namespace | 133 } // namespace |
| 123 | 134 |
| 124 void AndroidStreamReaderURLRequestJob::Start() { | 135 void AndroidStreamReaderURLRequestJob::Start() { |
| 125 DCHECK(thread_checker_.CalledOnValidThread()); | 136 DCHECK(thread_checker_.CalledOnValidThread()); |
| 126 // Start reading asynchronously so that all error reporting and data | 137 if (!delegate_) { |
| 127 // callbacks happen as they would for network requests. | 138 DCHECK(delegate_obtainer_); |
| 128 SetStatus(net::URLRequestStatus(net::URLRequestStatus::IO_PENDING, | 139 delegate_obtainer_->ObtainDelegate( |
| 129 net::ERR_IO_PENDING)); | 140 request(), |
| 130 | 141 base::Bind(&AndroidStreamReaderURLRequestJob::DelegateObtained, |
| 131 // This could be done in the InputStreamReader but would force more | 142 weak_factory_.GetWeakPtr())); |
| 132 // complex synchronization in the delegate. | 143 } else { |
| 133 GetWorkerThreadRunner()->PostTask( | 144 DoStart(); |
| 134 FROM_HERE, | 145 } |
| 135 base::Bind( | |
| 136 &OpenInputStreamOnWorkerThread, | |
| 137 base::MessageLoop::current()->task_runner(), | |
| 138 // This is intentional - the job could be deleted while the callback | |
| 139 // is executing on the background thread. | |
| 140 // The delegate will be "returned" to the job once the InputStream | |
| 141 // open attempt is completed. | |
| 142 base::Passed(&delegate_), request()->url(), | |
| 143 base::Bind(&AndroidStreamReaderURLRequestJob::OnInputStreamOpened, | |
| 144 weak_factory_.GetWeakPtr()))); | |
| 145 } | 146 } |
| 146 | 147 |
| 147 void AndroidStreamReaderURLRequestJob::Kill() { | 148 void AndroidStreamReaderURLRequestJob::Kill() { |
| 148 DCHECK(thread_checker_.CalledOnValidThread()); | 149 DCHECK(thread_checker_.CalledOnValidThread()); |
| 149 weak_factory_.InvalidateWeakPtrs(); | 150 weak_factory_.InvalidateWeakPtrs(); |
| 150 URLRequestJob::Kill(); | 151 URLRequestJob::Kill(); |
| 151 } | 152 } |
| 152 | 153 |
| 153 scoped_ptr<InputStreamReader> | 154 scoped_ptr<InputStreamReader> |
| 154 AndroidStreamReaderURLRequestJob::CreateStreamReader(InputStream* stream) { | 155 AndroidStreamReaderURLRequestJob::CreateStreamReader(InputStream* stream) { |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 283 return false; | 284 return false; |
| 284 | 285 |
| 285 // Since it's possible for this call to alter the InputStream a | 286 // Since it's possible for this call to alter the InputStream a |
| 286 // Seek or ReadRawData operation running in the background is not permitted. | 287 // Seek or ReadRawData operation running in the background is not permitted. |
| 287 DCHECK(!request_->status().is_io_pending()); | 288 DCHECK(!request_->status().is_io_pending()); |
| 288 | 289 |
| 289 return delegate_->GetCharset( | 290 return delegate_->GetCharset( |
| 290 env, request(), input_stream_reader_wrapper_->input_stream(), charset); | 291 env, request(), input_stream_reader_wrapper_->input_stream(), charset); |
| 291 } | 292 } |
| 292 | 293 |
| 294 void AndroidStreamReaderURLRequestJob::DelegateObtained( | |
| 295 scoped_ptr<Delegate> delegate) { | |
| 296 DCHECK(!delegate_); | |
| 297 delegate_obtainer_.reset(nullptr); | |
|
boliu
2015/10/01 19:13:56
nit: just reset()
mnaganov (inactive)
2015/10/01 19:51:03
Done.
| |
| 298 if (delegate) { | |
| 299 delegate_.swap(delegate); | |
|
boliu
2015/10/01 19:13:56
DCHECK(!delegate_) before swap? I think that's cor
mnaganov (inactive)
2015/10/01 19:51:03
Look three lines above :)
| |
| 300 DoStart(); | |
| 301 } else { | |
| 302 NotifyRestartRequired(); | |
| 303 } | |
| 304 } | |
| 305 | |
| 306 void AndroidStreamReaderURLRequestJob::DoStart() { | |
| 307 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 308 // Start reading asynchronously so that all error reporting and data | |
| 309 // callbacks happen as they would for network requests. | |
| 310 SetStatus(net::URLRequestStatus(net::URLRequestStatus::IO_PENDING, | |
| 311 net::ERR_IO_PENDING)); | |
| 312 | |
| 313 // This could be done in the InputStreamReader but would force more | |
| 314 // complex synchronization in the delegate. | |
| 315 GetWorkerThreadRunner()->PostTask( | |
| 316 FROM_HERE, | |
| 317 base::Bind( | |
| 318 &OpenInputStreamOnWorkerThread, | |
| 319 base::MessageLoop::current()->task_runner(), | |
| 320 // This is intentional - the job could be deleted while the callback | |
| 321 // is executing on the background thread. | |
| 322 // The delegate will be "returned" to the job once the InputStream | |
| 323 // open attempt is completed. | |
| 324 base::Passed(&delegate_), request()->url(), | |
| 325 base::Bind(&AndroidStreamReaderURLRequestJob::OnInputStreamOpened, | |
| 326 weak_factory_.GetWeakPtr()))); | |
| 327 } | |
| 328 | |
| 293 void AndroidStreamReaderURLRequestJob::HeadersComplete( | 329 void AndroidStreamReaderURLRequestJob::HeadersComplete( |
| 294 int status_code, | 330 int status_code, |
| 295 const std::string& status_text) { | 331 const std::string& status_text) { |
| 296 std::string status("HTTP/1.1 "); | 332 std::string status("HTTP/1.1 "); |
| 297 status.append(base::IntToString(status_code)); | 333 status.append(base::IntToString(status_code)); |
| 298 status.append(" "); | 334 status.append(" "); |
| 299 status.append(status_text); | 335 status.append(status_text); |
| 300 // HttpResponseHeaders expects its input string to be terminated by two NULs. | 336 // HttpResponseHeaders expects its input string to be terminated by two NULs. |
| 301 status.append("\0\0", 2); | 337 status.append("\0\0", 2); |
| 302 net::HttpResponseHeaders* headers = new net::HttpResponseHeaders(status); | 338 net::HttpResponseHeaders* headers = new net::HttpResponseHeaders(status); |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 360 // because we need to do multipart encoding here. | 396 // because we need to do multipart encoding here. |
| 361 NotifyDone(net::URLRequestStatus( | 397 NotifyDone(net::URLRequestStatus( |
| 362 net::URLRequestStatus::FAILED, | 398 net::URLRequestStatus::FAILED, |
| 363 net::ERR_REQUEST_RANGE_NOT_SATISFIABLE)); | 399 net::ERR_REQUEST_RANGE_NOT_SATISFIABLE)); |
| 364 } | 400 } |
| 365 } | 401 } |
| 366 } | 402 } |
| 367 } | 403 } |
| 368 | 404 |
| 369 } // namespace android_webview | 405 } // namespace android_webview |
| OLD | NEW |