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