| 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 has_been_started_(false), |
| 94 weak_factory_(this) { |
| 95 } |
| 96 |
| 97 AndroidStreamReaderURLRequestJob::AndroidStreamReaderURLRequestJob( |
| 98 net::URLRequest* request, |
| 90 net::NetworkDelegate* network_delegate, | 99 net::NetworkDelegate* network_delegate, |
| 91 scoped_ptr<Delegate> delegate) | 100 scoped_ptr<Delegate> delegate) |
| 92 : URLRequestJob(request, network_delegate), | 101 : URLRequestJob(request, network_delegate), |
| 93 delegate_(delegate.Pass()), | 102 delegate_(delegate.Pass()), |
| 103 has_been_started_(false), |
| 94 weak_factory_(this) { | 104 weak_factory_(this) { |
| 95 DCHECK(delegate_); | 105 DCHECK(delegate_); |
| 96 } | 106 } |
| 97 | 107 |
| 98 AndroidStreamReaderURLRequestJob::~AndroidStreamReaderURLRequestJob() { | 108 AndroidStreamReaderURLRequestJob::~AndroidStreamReaderURLRequestJob() { |
| 99 } | 109 } |
| 100 | 110 |
| 111 base::Callback<void(scoped_ptr<AwWebResourceResponse>)> |
| 112 AndroidStreamReaderURLRequestJob::GetWebResourceResponseCallback() { |
| 113 return base::Bind( |
| 114 &AndroidStreamReaderURLRequestJob::WebResourceResponseObtained, |
| 115 weak_factory_.GetWeakPtr()); |
| 116 } |
| 117 |
| 118 void AndroidStreamReaderURLRequestJob::WebResourceResponseObtained( |
| 119 scoped_ptr<AwWebResourceResponse> response) { |
| 120 DCHECK(!delegate_); |
| 121 if (response) { |
| 122 delegate_.reset(new AwStreamReaderJobDelegateImpl(response.Pass())); |
| 123 MaybeStart(); |
| 124 } else { |
| 125 NotifyRestartRequired(); |
| 126 } |
| 127 } |
| 128 |
| 101 namespace { | 129 namespace { |
| 102 | 130 |
| 103 typedef base::Callback< | 131 typedef base::Callback< |
| 104 void(scoped_ptr<AndroidStreamReaderURLRequestJob::Delegate>, | 132 void(scoped_ptr<AndroidStreamReaderURLRequestJob::Delegate>, |
| 105 scoped_ptr<InputStream>)> OnInputStreamOpenedCallback; | 133 scoped_ptr<InputStream>)> OnInputStreamOpenedCallback; |
| 106 | 134 |
| 107 // static | 135 // static |
| 108 void OpenInputStreamOnWorkerThread( | 136 void OpenInputStreamOnWorkerThread( |
| 109 scoped_refptr<base::SingleThreadTaskRunner> job_thread_task_runner, | 137 scoped_refptr<base::SingleThreadTaskRunner> job_thread_task_runner, |
| 110 scoped_ptr<AndroidStreamReaderURLRequestJob::Delegate> delegate, | 138 scoped_ptr<AndroidStreamReaderURLRequestJob::Delegate> delegate, |
| 111 const GURL& url, | 139 const GURL& url, |
| 112 OnInputStreamOpenedCallback callback) { | 140 OnInputStreamOpenedCallback callback) { |
| 113 JNIEnv* env = AttachCurrentThread(); | 141 JNIEnv* env = AttachCurrentThread(); |
| 114 DCHECK(env); | 142 DCHECK(env); |
| 115 | 143 |
| 116 scoped_ptr<InputStream> input_stream = delegate->OpenInputStream(env, url); | 144 scoped_ptr<InputStream> input_stream = delegate->OpenInputStream(env, url); |
| 117 job_thread_task_runner->PostTask( | 145 job_thread_task_runner->PostTask( |
| 118 FROM_HERE, base::Bind(callback, base::Passed(delegate.Pass()), | 146 FROM_HERE, base::Bind(callback, base::Passed(delegate.Pass()), |
| 119 base::Passed(input_stream.Pass()))); | 147 base::Passed(input_stream.Pass()))); |
| 120 } | 148 } |
| 121 | 149 |
| 122 } // namespace | 150 } // namespace |
| 123 | 151 |
| 124 void AndroidStreamReaderURLRequestJob::Start() { | 152 void AndroidStreamReaderURLRequestJob::MaybeStart() { |
| 125 DCHECK(thread_checker_.CalledOnValidThread()); | 153 DCHECK(thread_checker_.CalledOnValidThread()); |
| 154 if (!has_been_started() || !delegate_) |
| 155 return; |
| 156 |
| 126 // Start reading asynchronously so that all error reporting and data | 157 // Start reading asynchronously so that all error reporting and data |
| 127 // callbacks happen as they would for network requests. | 158 // callbacks happen as they would for network requests. |
| 128 SetStatus(net::URLRequestStatus(net::URLRequestStatus::IO_PENDING, | 159 SetStatus(net::URLRequestStatus(net::URLRequestStatus::IO_PENDING, |
| 129 net::ERR_IO_PENDING)); | 160 net::ERR_IO_PENDING)); |
| 130 | 161 |
| 131 // This could be done in the InputStreamReader but would force more | 162 // This could be done in the InputStreamReader but would force more |
| 132 // complex synchronization in the delegate. | 163 // complex synchronization in the delegate. |
| 133 GetWorkerThreadRunner()->PostTask( | 164 GetWorkerThreadRunner()->PostTask( |
| 134 FROM_HERE, | 165 FROM_HERE, |
| 135 base::Bind( | 166 base::Bind( |
| 136 &OpenInputStreamOnWorkerThread, | 167 &OpenInputStreamOnWorkerThread, |
| 137 base::MessageLoop::current()->task_runner(), | 168 base::MessageLoop::current()->task_runner(), |
| 138 // This is intentional - the job could be deleted while the callback | 169 // This is intentional - the job could be deleted while the callback |
| 139 // is executing on the background thread. | 170 // is executing on the background thread. |
| 140 // The delegate will be "returned" to the job once the InputStream | 171 // The delegate will be "returned" to the job once the InputStream |
| 141 // open attempt is completed. | 172 // open attempt is completed. |
| 142 base::Passed(&delegate_), request()->url(), | 173 base::Passed(&delegate_), request()->url(), |
| 143 base::Bind(&AndroidStreamReaderURLRequestJob::OnInputStreamOpened, | 174 base::Bind(&AndroidStreamReaderURLRequestJob::OnInputStreamOpened, |
| 144 weak_factory_.GetWeakPtr()))); | 175 weak_factory_.GetWeakPtr()))); |
| 145 } | 176 } |
| 146 | 177 |
| 178 void AndroidStreamReaderURLRequestJob::Start() { |
| 179 DCHECK(!has_been_started()); |
| 180 has_been_started_ = true; |
| 181 MaybeStart(); |
| 182 } |
| 183 |
| 147 void AndroidStreamReaderURLRequestJob::Kill() { | 184 void AndroidStreamReaderURLRequestJob::Kill() { |
| 148 DCHECK(thread_checker_.CalledOnValidThread()); | 185 DCHECK(thread_checker_.CalledOnValidThread()); |
| 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 } |
| (...skipping 203 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 |