Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(665)

Side by Side Diff: android_webview/browser/net/android_stream_reader_url_request_job.cc

Issue 1350553005: [Android WebView] Call shouldInterceptRequest on a background thread (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Final version? Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
80 ~InputStreamReaderWrapper() {} 80 ~InputStreamReaderWrapper() {}
81 81
82 scoped_ptr<InputStream> input_stream_; 82 scoped_ptr<InputStream> input_stream_;
83 scoped_ptr<InputStreamReader> input_stream_reader_; 83 scoped_ptr<InputStreamReader> input_stream_reader_;
84 84
85 DISALLOW_COPY_AND_ASSIGN(InputStreamReaderWrapper); 85 DISALLOW_COPY_AND_ASSIGN(InputStreamReaderWrapper);
86 }; 86 };
87 87
88 AndroidStreamReaderURLRequestJob::AndroidStreamReaderURLRequestJob( 88 AndroidStreamReaderURLRequestJob::AndroidStreamReaderURLRequestJob(
89 net::URLRequest* request, 89 net::URLRequest* request,
90 net::NetworkDelegate* network_delegate)
91 : URLRequestJob(request, network_delegate),
92 weak_factory_(this) {
93 }
94
95 AndroidStreamReaderURLRequestJob::AndroidStreamReaderURLRequestJob(
96 net::URLRequest* request,
90 net::NetworkDelegate* network_delegate, 97 net::NetworkDelegate* network_delegate,
91 scoped_ptr<Delegate> delegate) 98 scoped_ptr<Delegate> delegate)
92 : URLRequestJob(request, network_delegate), 99 : URLRequestJob(request, network_delegate),
93 delegate_(delegate.Pass()), 100 delegate_(delegate.Pass()),
94 weak_factory_(this) { 101 weak_factory_(this) {
95 DCHECK(delegate_); 102 DCHECK(delegate_);
96 } 103 }
97 104
98 AndroidStreamReaderURLRequestJob::~AndroidStreamReaderURLRequestJob() { 105 AndroidStreamReaderURLRequestJob::~AndroidStreamReaderURLRequestJob() {
99 } 106 }
100 107
108 void AndroidStreamReaderURLRequestJob::DeliverResponseFromDelegate(
109 scoped_ptr<Delegate> delegate) {
110 DCHECK(!delegate_);
111 delegate_ = delegate.Pass();
112 DCHECK(delegate_);
113 MaybeStart();
boliu 2015/09/28 23:26:28 Does this need to check that Start has indeed been
mnaganov (inactive) 2015/09/29 00:14:06 Good point! Also we don't want to start if we have
114 }
115
116 void AndroidStreamReaderURLRequestJob::DeliverResponseFromNetwork() {
117 DCHECK(!delegate_);
118 NotifyRestartRequired();
119 }
120
101 namespace { 121 namespace {
102 122
103 typedef base::Callback< 123 typedef base::Callback<
104 void(scoped_ptr<AndroidStreamReaderURLRequestJob::Delegate>, 124 void(scoped_ptr<AndroidStreamReaderURLRequestJob::Delegate>,
105 scoped_ptr<InputStream>)> OnInputStreamOpenedCallback; 125 scoped_ptr<InputStream>)> OnInputStreamOpenedCallback;
106 126
107 // static 127 // static
108 void OpenInputStreamOnWorkerThread( 128 void OpenInputStreamOnWorkerThread(
109 scoped_refptr<base::SingleThreadTaskRunner> job_thread_task_runner, 129 scoped_refptr<base::SingleThreadTaskRunner> job_thread_task_runner,
110 scoped_ptr<AndroidStreamReaderURLRequestJob::Delegate> delegate, 130 scoped_ptr<AndroidStreamReaderURLRequestJob::Delegate> delegate,
111 const GURL& url, 131 const GURL& url,
112 OnInputStreamOpenedCallback callback) { 132 OnInputStreamOpenedCallback callback) {
113 JNIEnv* env = AttachCurrentThread(); 133 JNIEnv* env = AttachCurrentThread();
114 DCHECK(env); 134 DCHECK(env);
115 135
116 scoped_ptr<InputStream> input_stream = delegate->OpenInputStream(env, url); 136 scoped_ptr<InputStream> input_stream = delegate->OpenInputStream(env, url);
117 job_thread_task_runner->PostTask( 137 job_thread_task_runner->PostTask(
118 FROM_HERE, base::Bind(callback, base::Passed(delegate.Pass()), 138 FROM_HERE, base::Bind(callback, base::Passed(delegate.Pass()),
119 base::Passed(input_stream.Pass()))); 139 base::Passed(input_stream.Pass())));
120 } 140 }
121 141
122 } // namespace 142 } // namespace
123 143
124 void AndroidStreamReaderURLRequestJob::Start() { 144 void AndroidStreamReaderURLRequestJob::MaybeStart() {
125 DCHECK(thread_checker_.CalledOnValidThread()); 145 DCHECK(thread_checker_.CalledOnValidThread());
146 if (!delegate_) return;
147
126 // Start reading asynchronously so that all error reporting and data 148 // Start reading asynchronously so that all error reporting and data
127 // callbacks happen as they would for network requests. 149 // callbacks happen as they would for network requests.
128 SetStatus(net::URLRequestStatus(net::URLRequestStatus::IO_PENDING, 150 SetStatus(net::URLRequestStatus(net::URLRequestStatus::IO_PENDING,
129 net::ERR_IO_PENDING)); 151 net::ERR_IO_PENDING));
130 152
131 // This could be done in the InputStreamReader but would force more 153 // This could be done in the InputStreamReader but would force more
132 // complex synchronization in the delegate. 154 // complex synchronization in the delegate.
133 GetWorkerThreadRunner()->PostTask( 155 GetWorkerThreadRunner()->PostTask(
134 FROM_HERE, 156 FROM_HERE,
135 base::Bind( 157 base::Bind(
136 &OpenInputStreamOnWorkerThread, 158 &OpenInputStreamOnWorkerThread,
137 base::MessageLoop::current()->task_runner(), 159 base::MessageLoop::current()->task_runner(),
138 // This is intentional - the job could be deleted while the callback 160 // This is intentional - the job could be deleted while the callback
139 // is executing on the background thread. 161 // is executing on the background thread.
140 // The delegate will be "returned" to the job once the InputStream 162 // The delegate will be "returned" to the job once the InputStream
141 // open attempt is completed. 163 // open attempt is completed.
142 base::Passed(&delegate_), request()->url(), 164 base::Passed(&delegate_), request()->url(),
143 base::Bind(&AndroidStreamReaderURLRequestJob::OnInputStreamOpened, 165 base::Bind(&AndroidStreamReaderURLRequestJob::OnInputStreamOpened,
144 weak_factory_.GetWeakPtr()))); 166 weak_factory_.GetWeakPtr())));
145 } 167 }
146 168
169 void AndroidStreamReaderURLRequestJob::Start() {
170 MaybeStart();
171 }
172
147 void AndroidStreamReaderURLRequestJob::Kill() { 173 void AndroidStreamReaderURLRequestJob::Kill() {
148 DCHECK(thread_checker_.CalledOnValidThread()); 174 DCHECK(thread_checker_.CalledOnValidThread());
149 weak_factory_.InvalidateWeakPtrs(); 175 weak_factory_.InvalidateWeakPtrs();
150 URLRequestJob::Kill(); 176 URLRequestJob::Kill();
151 } 177 }
152 178
153 scoped_ptr<InputStreamReader> 179 scoped_ptr<InputStreamReader>
154 AndroidStreamReaderURLRequestJob::CreateStreamReader(InputStream* stream) { 180 AndroidStreamReaderURLRequestJob::CreateStreamReader(InputStream* stream) {
155 return make_scoped_ptr(new InputStreamReader(stream)); 181 return make_scoped_ptr(new InputStreamReader(stream));
156 } 182 }
(...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after
360 // because we need to do multipart encoding here. 386 // because we need to do multipart encoding here.
361 NotifyDone(net::URLRequestStatus( 387 NotifyDone(net::URLRequestStatus(
362 net::URLRequestStatus::FAILED, 388 net::URLRequestStatus::FAILED,
363 net::ERR_REQUEST_RANGE_NOT_SATISFIABLE)); 389 net::ERR_REQUEST_RANGE_NOT_SATISFIABLE));
364 } 390 }
365 } 391 }
366 } 392 }
367 } 393 }
368 394
369 } // namespace android_webview 395 } // namespace android_webview
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698