OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "net/proxy/proxy_resolver_factory_mojo.h" | 5 #include "net/proxy/proxy_resolver_factory_mojo.h" |
6 | 6 |
7 #include <set> | 7 #include <set> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
117 HostResolver* host_resolver, | 117 HostResolver* host_resolver, |
118 std::unique_ptr<base::ScopedClosureRunner> on_delete_callback_runner, | 118 std::unique_ptr<base::ScopedClosureRunner> on_delete_callback_runner, |
119 std::unique_ptr<ProxyResolverErrorObserver> error_observer, | 119 std::unique_ptr<ProxyResolverErrorObserver> error_observer, |
120 NetLog* net_log); | 120 NetLog* net_log); |
121 ~ProxyResolverMojo() override; | 121 ~ProxyResolverMojo() override; |
122 | 122 |
123 // ProxyResolver implementation: | 123 // ProxyResolver implementation: |
124 int GetProxyForURL(const GURL& url, | 124 int GetProxyForURL(const GURL& url, |
125 ProxyInfo* results, | 125 ProxyInfo* results, |
126 const net::CompletionCallback& callback, | 126 const net::CompletionCallback& callback, |
127 RequestHandle* request, | 127 std::unique_ptr<Request>* request, |
128 const NetLogWithSource& net_log) override; | 128 const NetLogWithSource& net_log) override; |
129 void CancelRequest(RequestHandle request) override; | |
130 LoadState GetLoadState(RequestHandle request) const override; | |
131 | 129 |
132 private: | 130 private: |
133 class Job; | 131 class Job; |
| 132 class RequestImpl; |
| 133 |
| 134 base::ThreadChecker thread_checker_; |
134 | 135 |
135 // Mojo error handler. | 136 // Mojo error handler. |
136 void OnConnectionError(); | 137 void OnConnectionError(); |
137 | 138 |
138 void RemoveJob(Job* job); | |
139 | |
140 // Connection to the Mojo proxy resolver. | 139 // Connection to the Mojo proxy resolver. |
141 interfaces::ProxyResolverPtr mojo_proxy_resolver_ptr_; | 140 interfaces::ProxyResolverPtr mojo_proxy_resolver_ptr_; |
142 | 141 |
143 HostResolver* host_resolver_; | 142 HostResolver* host_resolver_; |
144 | 143 |
145 std::unique_ptr<ProxyResolverErrorObserver> error_observer_; | 144 std::unique_ptr<ProxyResolverErrorObserver> error_observer_; |
146 | 145 |
147 NetLog* net_log_; | 146 NetLog* net_log_; |
148 | 147 |
149 std::set<Job*> pending_jobs_; | |
150 | |
151 base::ThreadChecker thread_checker_; | |
152 | |
153 std::unique_ptr<base::ScopedClosureRunner> on_delete_callback_runner_; | 148 std::unique_ptr<base::ScopedClosureRunner> on_delete_callback_runner_; |
154 | 149 |
155 DISALLOW_COPY_AND_ASSIGN(ProxyResolverMojo); | 150 DISALLOW_COPY_AND_ASSIGN(ProxyResolverMojo); |
156 }; | 151 }; |
157 | 152 |
| 153 class ProxyResolverMojo::RequestImpl : public ProxyResolver::Request { |
| 154 public: |
| 155 explicit RequestImpl(std::unique_ptr<Job> job); |
| 156 |
| 157 ~RequestImpl() override {} |
| 158 |
| 159 LoadState GetLoadState() override; |
| 160 |
| 161 private: |
| 162 std::unique_ptr<Job> job_; |
| 163 |
| 164 DISALLOW_COPY_AND_ASSIGN(RequestImpl); |
| 165 }; |
| 166 |
158 class ProxyResolverMojo::Job | 167 class ProxyResolverMojo::Job |
159 : public ClientMixin<interfaces::ProxyResolverRequestClient> { | 168 : public ClientMixin<interfaces::ProxyResolverRequestClient> { |
160 public: | 169 public: |
161 Job(ProxyResolverMojo* resolver, | 170 Job(ProxyResolverMojo* resolver, |
162 const GURL& url, | 171 const GURL& url, |
163 ProxyInfo* results, | 172 ProxyInfo* results, |
164 const CompletionCallback& callback, | 173 const CompletionCallback& callback, |
165 const NetLogWithSource& net_log); | 174 const NetLogWithSource& net_log); |
166 ~Job() override; | 175 ~Job() override; |
167 | 176 |
168 // Cancels the job and prevents the callback from being run. | |
169 void Cancel(); | |
170 | |
171 // Returns the LoadState of this job. | 177 // Returns the LoadState of this job. |
172 LoadState GetLoadState(); | 178 LoadState GetLoadState(); |
173 | 179 |
174 private: | 180 private: |
175 // Mojo error handler. | 181 // Mojo error handler. |
176 void OnConnectionError(); | 182 void OnConnectionError(); |
177 | 183 |
178 // Overridden from interfaces::ProxyResolverRequestClient: | 184 // Overridden from interfaces::ProxyResolverRequestClient: |
179 void ReportResult(int32_t error, const net::ProxyInfo& proxy_info) override; | 185 void ReportResult(int32_t error, const net::ProxyInfo& proxy_info) override; |
180 | 186 |
181 ProxyResolverMojo* resolver_; | 187 // Completes a request with a result code. |
| 188 void CompleteRequest(int result); |
| 189 |
182 const GURL url_; | 190 const GURL url_; |
183 ProxyInfo* results_; | 191 ProxyInfo* results_; |
184 CompletionCallback callback_; | 192 CompletionCallback callback_; |
185 | 193 |
186 base::ThreadChecker thread_checker_; | 194 base::ThreadChecker thread_checker_; |
187 mojo::Binding<interfaces::ProxyResolverRequestClient> binding_; | 195 mojo::Binding<interfaces::ProxyResolverRequestClient> binding_; |
| 196 |
| 197 DISALLOW_COPY_AND_ASSIGN(Job); |
188 }; | 198 }; |
189 | 199 |
| 200 ProxyResolverMojo::RequestImpl::RequestImpl(std::unique_ptr<Job> job) |
| 201 : job_(std::move(job)) {} |
| 202 |
| 203 LoadState ProxyResolverMojo::RequestImpl::GetLoadState() { |
| 204 return job_->GetLoadState(); |
| 205 } |
| 206 |
190 ProxyResolverMojo::Job::Job(ProxyResolverMojo* resolver, | 207 ProxyResolverMojo::Job::Job(ProxyResolverMojo* resolver, |
191 const GURL& url, | 208 const GURL& url, |
192 ProxyInfo* results, | 209 ProxyInfo* results, |
193 const CompletionCallback& callback, | 210 const CompletionCallback& callback, |
194 const NetLogWithSource& net_log) | 211 const NetLogWithSource& net_log) |
195 : ClientMixin<interfaces::ProxyResolverRequestClient>( | 212 : ClientMixin<interfaces::ProxyResolverRequestClient>( |
196 resolver->host_resolver_, | 213 resolver->host_resolver_, |
197 resolver->error_observer_.get(), | 214 resolver->error_observer_.get(), |
198 resolver->net_log_, | 215 resolver->net_log_, |
199 net_log), | 216 net_log), |
200 resolver_(resolver), | |
201 url_(url), | 217 url_(url), |
202 results_(results), | 218 results_(results), |
203 callback_(callback), | 219 callback_(callback), |
204 binding_(this) { | 220 binding_(this) { |
205 resolver_->mojo_proxy_resolver_ptr_->GetProxyForUrl( | 221 resolver->mojo_proxy_resolver_ptr_->GetProxyForUrl( |
206 url_, binding_.CreateInterfacePtrAndBind()); | 222 url_, binding_.CreateInterfacePtrAndBind()); |
207 binding_.set_connection_error_handler(base::Bind( | 223 binding_.set_connection_error_handler(base::Bind( |
208 &ProxyResolverMojo::Job::OnConnectionError, base::Unretained(this))); | 224 &ProxyResolverMojo::Job::OnConnectionError, base::Unretained(this))); |
209 } | 225 } |
210 | 226 |
211 ProxyResolverMojo::Job::~Job() { | 227 ProxyResolverMojo::Job::~Job() {} |
212 DCHECK(thread_checker_.CalledOnValidThread()); | |
213 if (!callback_.is_null()) | |
214 callback_.Run(ERR_PAC_SCRIPT_TERMINATED); | |
215 } | |
216 | |
217 void ProxyResolverMojo::Job::Cancel() { | |
218 DCHECK(thread_checker_.CalledOnValidThread()); | |
219 DCHECK(!callback_.is_null()); | |
220 callback_.Reset(); | |
221 } | |
222 | 228 |
223 LoadState ProxyResolverMojo::Job::GetLoadState() { | 229 LoadState ProxyResolverMojo::Job::GetLoadState() { |
224 return dns_request_in_progress() ? LOAD_STATE_RESOLVING_HOST_IN_PROXY_SCRIPT | 230 return dns_request_in_progress() ? LOAD_STATE_RESOLVING_HOST_IN_PROXY_SCRIPT |
225 : LOAD_STATE_RESOLVING_PROXY_FOR_URL; | 231 : LOAD_STATE_RESOLVING_PROXY_FOR_URL; |
226 } | 232 } |
227 | 233 |
228 void ProxyResolverMojo::Job::OnConnectionError() { | 234 void ProxyResolverMojo::Job::OnConnectionError() { |
229 DCHECK(thread_checker_.CalledOnValidThread()); | 235 DCHECK(thread_checker_.CalledOnValidThread()); |
230 DVLOG(1) << "ProxyResolverMojo::Job::OnConnectionError"; | 236 DVLOG(1) << "ProxyResolverMojo::Job::OnConnectionError"; |
231 resolver_->RemoveJob(this); | 237 CompleteRequest(ERR_PAC_SCRIPT_TERMINATED); |
| 238 } |
| 239 |
| 240 void ProxyResolverMojo::Job::CompleteRequest(int result) { |
| 241 DCHECK(thread_checker_.CalledOnValidThread()); |
| 242 CompletionCallback callback = base::ResetAndReturn(&callback_); |
| 243 binding_.Close(); |
| 244 callback.Run(result); |
232 } | 245 } |
233 | 246 |
234 void ProxyResolverMojo::Job::ReportResult(int32_t error, | 247 void ProxyResolverMojo::Job::ReportResult(int32_t error, |
235 const ProxyInfo& proxy_info) { | 248 const ProxyInfo& proxy_info) { |
236 DCHECK(thread_checker_.CalledOnValidThread()); | 249 DCHECK(thread_checker_.CalledOnValidThread()); |
237 DVLOG(1) << "ProxyResolverMojo::Job::ReportResult: " << error; | 250 DVLOG(1) << "ProxyResolverMojo::Job::ReportResult: " << error; |
238 | 251 |
239 if (error == OK) { | 252 if (error == OK) { |
240 *results_ = proxy_info; | 253 *results_ = proxy_info; |
241 DVLOG(1) << "Servers: " << results_->ToPacString(); | 254 DVLOG(1) << "Servers: " << results_->ToPacString(); |
242 } | 255 } |
243 | 256 |
244 CompletionCallback callback = callback_; | 257 CompleteRequest(error); |
245 callback_.Reset(); | |
246 resolver_->RemoveJob(this); | |
247 callback.Run(error); | |
248 } | 258 } |
249 | 259 |
250 ProxyResolverMojo::ProxyResolverMojo( | 260 ProxyResolverMojo::ProxyResolverMojo( |
251 interfaces::ProxyResolverPtr resolver_ptr, | 261 interfaces::ProxyResolverPtr resolver_ptr, |
252 HostResolver* host_resolver, | 262 HostResolver* host_resolver, |
253 std::unique_ptr<base::ScopedClosureRunner> on_delete_callback_runner, | 263 std::unique_ptr<base::ScopedClosureRunner> on_delete_callback_runner, |
254 std::unique_ptr<ProxyResolverErrorObserver> error_observer, | 264 std::unique_ptr<ProxyResolverErrorObserver> error_observer, |
255 NetLog* net_log) | 265 NetLog* net_log) |
256 : mojo_proxy_resolver_ptr_(std::move(resolver_ptr)), | 266 : mojo_proxy_resolver_ptr_(std::move(resolver_ptr)), |
257 host_resolver_(host_resolver), | 267 host_resolver_(host_resolver), |
258 error_observer_(std::move(error_observer)), | 268 error_observer_(std::move(error_observer)), |
259 net_log_(net_log), | 269 net_log_(net_log), |
260 on_delete_callback_runner_(std::move(on_delete_callback_runner)) { | 270 on_delete_callback_runner_(std::move(on_delete_callback_runner)) { |
261 mojo_proxy_resolver_ptr_.set_connection_error_handler(base::Bind( | 271 mojo_proxy_resolver_ptr_.set_connection_error_handler(base::Bind( |
262 &ProxyResolverMojo::OnConnectionError, base::Unretained(this))); | 272 &ProxyResolverMojo::OnConnectionError, base::Unretained(this))); |
263 } | 273 } |
264 | 274 |
265 ProxyResolverMojo::~ProxyResolverMojo() { | 275 ProxyResolverMojo::~ProxyResolverMojo() { |
266 DCHECK(thread_checker_.CalledOnValidThread()); | 276 DCHECK(thread_checker_.CalledOnValidThread()); |
267 // All pending requests should have been cancelled. | |
268 DCHECK(pending_jobs_.empty()); | |
269 } | 277 } |
270 | 278 |
271 void ProxyResolverMojo::OnConnectionError() { | 279 void ProxyResolverMojo::OnConnectionError() { |
272 DCHECK(thread_checker_.CalledOnValidThread()); | 280 DCHECK(thread_checker_.CalledOnValidThread()); |
273 DVLOG(1) << "ProxyResolverMojo::OnConnectionError"; | 281 DVLOG(1) << "ProxyResolverMojo::OnConnectionError"; |
274 | 282 |
275 // Disconnect from the Mojo proxy resolver service. | 283 // Disconnect from the Mojo proxy resolver service. |
276 mojo_proxy_resolver_ptr_.reset(); | 284 mojo_proxy_resolver_ptr_.reset(); |
277 } | 285 } |
278 | 286 |
279 void ProxyResolverMojo::RemoveJob(Job* job) { | |
280 DCHECK(thread_checker_.CalledOnValidThread()); | |
281 size_t num_erased = pending_jobs_.erase(job); | |
282 DCHECK(num_erased); | |
283 delete job; | |
284 } | |
285 | |
286 int ProxyResolverMojo::GetProxyForURL(const GURL& url, | 287 int ProxyResolverMojo::GetProxyForURL(const GURL& url, |
287 ProxyInfo* results, | 288 ProxyInfo* results, |
288 const CompletionCallback& callback, | 289 const CompletionCallback& callback, |
289 RequestHandle* request, | 290 std::unique_ptr<Request>* request, |
290 const NetLogWithSource& net_log) { | 291 const NetLogWithSource& net_log) { |
291 DCHECK(thread_checker_.CalledOnValidThread()); | 292 DCHECK(thread_checker_.CalledOnValidThread()); |
292 | 293 |
293 if (!mojo_proxy_resolver_ptr_) | 294 if (!mojo_proxy_resolver_ptr_) |
294 return ERR_PAC_SCRIPT_TERMINATED; | 295 return ERR_PAC_SCRIPT_TERMINATED; |
295 | 296 |
296 Job* job = new Job(this, url, results, callback, net_log); | 297 std::unique_ptr<Job> job(new Job(this, url, results, callback, net_log)); |
297 bool inserted = pending_jobs_.insert(job).second; | 298 request->reset(new RequestImpl(std::move(job))); |
298 DCHECK(inserted); | |
299 *request = job; | |
300 | 299 |
301 return ERR_IO_PENDING; | 300 return ERR_IO_PENDING; |
302 } | 301 } |
303 | 302 |
304 void ProxyResolverMojo::CancelRequest(RequestHandle request) { | |
305 DCHECK(thread_checker_.CalledOnValidThread()); | |
306 Job* job = static_cast<Job*>(request); | |
307 DCHECK(job); | |
308 job->Cancel(); | |
309 RemoveJob(job); | |
310 } | |
311 | |
312 LoadState ProxyResolverMojo::GetLoadState(RequestHandle request) const { | |
313 Job* job = static_cast<Job*>(request); | |
314 CHECK_EQ(1u, pending_jobs_.count(job)); | |
315 return job->GetLoadState(); | |
316 } | |
317 | |
318 } // namespace | 303 } // namespace |
319 | 304 |
320 // A Job to create a ProxyResolver instance. | 305 // A Job to create a ProxyResolver instance. |
321 // | 306 // |
322 // Note: a Job instance is not tied to a particular resolve request, and hence | 307 // Note: a Job instance is not tied to a particular resolve request, and hence |
323 // there is no per-request logging to be done (any netlog events are only sent | 308 // there is no per-request logging to be done (any netlog events are only sent |
324 // globally) so this always uses an empty NetLogWithSource. | 309 // globally) so this always uses an empty NetLogWithSource. |
325 class ProxyResolverFactoryMojo::Job | 310 class ProxyResolverFactoryMojo::Job |
326 : public ClientMixin<interfaces::ProxyResolverFactoryRequestClient>, | 311 : public ClientMixin<interfaces::ProxyResolverFactoryRequestClient>, |
327 public ProxyResolverFactory::Request { | 312 public ProxyResolverFactory::Request { |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
403 return ERR_PAC_SCRIPT_FAILED; | 388 return ERR_PAC_SCRIPT_FAILED; |
404 } | 389 } |
405 request->reset(new Job(this, pac_script, resolver, callback, | 390 request->reset(new Job(this, pac_script, resolver, callback, |
406 error_observer_factory_.is_null() | 391 error_observer_factory_.is_null() |
407 ? nullptr | 392 ? nullptr |
408 : error_observer_factory_.Run())); | 393 : error_observer_factory_.Run())); |
409 return ERR_IO_PENDING; | 394 return ERR_IO_PENDING; |
410 } | 395 } |
411 | 396 |
412 } // namespace net | 397 } // namespace net |
OLD | NEW |