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 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
44 | 44 |
45 // A mixin that forwards logging to (Bound)NetLog and ProxyResolverErrorObserver | 45 // A mixin that forwards logging to (Bound)NetLog and ProxyResolverErrorObserver |
46 // and DNS requests to a MojoHostResolverImpl, which is implemented in terms of | 46 // and DNS requests to a MojoHostResolverImpl, which is implemented in terms of |
47 // a HostResolver. | 47 // a HostResolver. |
48 template <typename ClientInterface> | 48 template <typename ClientInterface> |
49 class ClientMixin : public ClientInterface { | 49 class ClientMixin : public ClientInterface { |
50 public: | 50 public: |
51 ClientMixin(HostResolver* host_resolver, | 51 ClientMixin(HostResolver* host_resolver, |
52 ProxyResolverErrorObserver* error_observer, | 52 ProxyResolverErrorObserver* error_observer, |
53 NetLog* net_log, | 53 NetLog* net_log, |
54 const BoundNetLog& bound_net_log) | 54 const NetLogWithSource& net_log_with_source) |
55 : host_resolver_(host_resolver, bound_net_log), | 55 : host_resolver_(host_resolver, net_log_with_source), |
56 error_observer_(error_observer), | 56 error_observer_(error_observer), |
57 net_log_(net_log), | 57 net_log_(net_log), |
58 bound_net_log_(bound_net_log) {} | 58 net_log_with_source_(net_log_with_source) {} |
59 | 59 |
60 // Overridden from ClientInterface: | 60 // Overridden from ClientInterface: |
61 void Alert(const mojo::String& message) override { | 61 void Alert(const mojo::String& message) override { |
62 base::string16 message_str = message.To<base::string16>(); | 62 base::string16 message_str = message.To<base::string16>(); |
63 auto callback = NetLog::StringCallback("message", &message_str); | 63 auto callback = NetLog::StringCallback("message", &message_str); |
64 bound_net_log_.AddEvent(NetLogEventType::PAC_JAVASCRIPT_ALERT, callback); | 64 net_log_with_source_.AddEvent(NetLogEventType::PAC_JAVASCRIPT_ALERT, |
| 65 callback); |
65 if (net_log_) | 66 if (net_log_) |
66 net_log_->AddGlobalEntry(NetLogEventType::PAC_JAVASCRIPT_ALERT, callback); | 67 net_log_->AddGlobalEntry(NetLogEventType::PAC_JAVASCRIPT_ALERT, callback); |
67 } | 68 } |
68 | 69 |
69 void OnError(int32_t line_number, const mojo::String& message) override { | 70 void OnError(int32_t line_number, const mojo::String& message) override { |
70 base::string16 message_str = message.To<base::string16>(); | 71 base::string16 message_str = message.To<base::string16>(); |
71 auto callback = base::Bind(&NetLogErrorCallback, line_number, &message_str); | 72 auto callback = base::Bind(&NetLogErrorCallback, line_number, &message_str); |
72 bound_net_log_.AddEvent(NetLogEventType::PAC_JAVASCRIPT_ERROR, callback); | 73 net_log_with_source_.AddEvent(NetLogEventType::PAC_JAVASCRIPT_ERROR, |
| 74 callback); |
73 if (net_log_) | 75 if (net_log_) |
74 net_log_->AddGlobalEntry(NetLogEventType::PAC_JAVASCRIPT_ERROR, callback); | 76 net_log_->AddGlobalEntry(NetLogEventType::PAC_JAVASCRIPT_ERROR, callback); |
75 if (error_observer_) | 77 if (error_observer_) |
76 error_observer_->OnPACScriptError(line_number, message_str); | 78 error_observer_->OnPACScriptError(line_number, message_str); |
77 } | 79 } |
78 | 80 |
79 void ResolveDns(interfaces::HostResolverRequestInfoPtr request_info, | 81 void ResolveDns(interfaces::HostResolverRequestInfoPtr request_info, |
80 interfaces::HostResolverRequestClientPtr client) override { | 82 interfaces::HostResolverRequestClientPtr client) override { |
81 host_resolver_.Resolve(std::move(request_info), std::move(client)); | 83 host_resolver_.Resolve(std::move(request_info), std::move(client)); |
82 } | 84 } |
83 | 85 |
84 protected: | 86 protected: |
85 bool dns_request_in_progress() { | 87 bool dns_request_in_progress() { |
86 return host_resolver_.request_in_progress(); | 88 return host_resolver_.request_in_progress(); |
87 } | 89 } |
88 | 90 |
89 private: | 91 private: |
90 MojoHostResolverImpl host_resolver_; | 92 MojoHostResolverImpl host_resolver_; |
91 ProxyResolverErrorObserver* const error_observer_; | 93 ProxyResolverErrorObserver* const error_observer_; |
92 NetLog* const net_log_; | 94 NetLog* const net_log_; |
93 const BoundNetLog bound_net_log_; | 95 const NetLogWithSource net_log_with_source_; |
94 }; | 96 }; |
95 | 97 |
96 // Implementation of ProxyResolver that connects to a Mojo service to evaluate | 98 // Implementation of ProxyResolver that connects to a Mojo service to evaluate |
97 // PAC scripts. This implementation only knows about Mojo services, and | 99 // PAC scripts. This implementation only knows about Mojo services, and |
98 // therefore that service may live in or out of process. | 100 // therefore that service may live in or out of process. |
99 // | 101 // |
100 // This implementation reports disconnections from the Mojo service (i.e. if the | 102 // This implementation reports disconnections from the Mojo service (i.e. if the |
101 // service is out-of-process and that process crashes) using the error code | 103 // service is out-of-process and that process crashes) using the error code |
102 // ERR_PAC_SCRIPT_TERMINATED. | 104 // ERR_PAC_SCRIPT_TERMINATED. |
103 class ProxyResolverMojo : public ProxyResolver { | 105 class ProxyResolverMojo : public ProxyResolver { |
104 public: | 106 public: |
105 // Constructs a ProxyResolverMojo that connects to a mojo proxy resolver | 107 // Constructs a ProxyResolverMojo that connects to a mojo proxy resolver |
106 // implementation using |resolver_ptr|. The implementation uses | 108 // implementation using |resolver_ptr|. The implementation uses |
107 // |host_resolver| as the DNS resolver, using |host_resolver_binding| to | 109 // |host_resolver| as the DNS resolver, using |host_resolver_binding| to |
108 // communicate with it. When deleted, the closure contained within | 110 // communicate with it. When deleted, the closure contained within |
109 // |on_delete_callback_runner| will be run. | 111 // |on_delete_callback_runner| will be run. |
110 ProxyResolverMojo( | 112 ProxyResolverMojo( |
111 interfaces::ProxyResolverPtr resolver_ptr, | 113 interfaces::ProxyResolverPtr resolver_ptr, |
112 HostResolver* host_resolver, | 114 HostResolver* host_resolver, |
113 std::unique_ptr<base::ScopedClosureRunner> on_delete_callback_runner, | 115 std::unique_ptr<base::ScopedClosureRunner> on_delete_callback_runner, |
114 std::unique_ptr<ProxyResolverErrorObserver> error_observer, | 116 std::unique_ptr<ProxyResolverErrorObserver> error_observer, |
115 NetLog* net_log); | 117 NetLog* net_log); |
116 ~ProxyResolverMojo() override; | 118 ~ProxyResolverMojo() override; |
117 | 119 |
118 // ProxyResolver implementation: | 120 // ProxyResolver implementation: |
119 int GetProxyForURL(const GURL& url, | 121 int GetProxyForURL(const GURL& url, |
120 ProxyInfo* results, | 122 ProxyInfo* results, |
121 const net::CompletionCallback& callback, | 123 const net::CompletionCallback& callback, |
122 RequestHandle* request, | 124 RequestHandle* request, |
123 const BoundNetLog& net_log) override; | 125 const NetLogWithSource& net_log) override; |
124 void CancelRequest(RequestHandle request) override; | 126 void CancelRequest(RequestHandle request) override; |
125 LoadState GetLoadState(RequestHandle request) const override; | 127 LoadState GetLoadState(RequestHandle request) const override; |
126 | 128 |
127 private: | 129 private: |
128 class Job; | 130 class Job; |
129 | 131 |
130 // Mojo error handler. | 132 // Mojo error handler. |
131 void OnConnectionError(); | 133 void OnConnectionError(); |
132 | 134 |
133 void RemoveJob(Job* job); | 135 void RemoveJob(Job* job); |
(...skipping 16 matching lines...) Expand all Loading... |
150 DISALLOW_COPY_AND_ASSIGN(ProxyResolverMojo); | 152 DISALLOW_COPY_AND_ASSIGN(ProxyResolverMojo); |
151 }; | 153 }; |
152 | 154 |
153 class ProxyResolverMojo::Job | 155 class ProxyResolverMojo::Job |
154 : public ClientMixin<interfaces::ProxyResolverRequestClient> { | 156 : public ClientMixin<interfaces::ProxyResolverRequestClient> { |
155 public: | 157 public: |
156 Job(ProxyResolverMojo* resolver, | 158 Job(ProxyResolverMojo* resolver, |
157 const GURL& url, | 159 const GURL& url, |
158 ProxyInfo* results, | 160 ProxyInfo* results, |
159 const CompletionCallback& callback, | 161 const CompletionCallback& callback, |
160 const BoundNetLog& net_log); | 162 const NetLogWithSource& net_log); |
161 ~Job() override; | 163 ~Job() override; |
162 | 164 |
163 // Cancels the job and prevents the callback from being run. | 165 // Cancels the job and prevents the callback from being run. |
164 void Cancel(); | 166 void Cancel(); |
165 | 167 |
166 // Returns the LoadState of this job. | 168 // Returns the LoadState of this job. |
167 LoadState GetLoadState(); | 169 LoadState GetLoadState(); |
168 | 170 |
169 private: | 171 private: |
170 // Mojo error handler. | 172 // Mojo error handler. |
(...skipping 10 matching lines...) Expand all Loading... |
181 CompletionCallback callback_; | 183 CompletionCallback callback_; |
182 | 184 |
183 base::ThreadChecker thread_checker_; | 185 base::ThreadChecker thread_checker_; |
184 mojo::Binding<interfaces::ProxyResolverRequestClient> binding_; | 186 mojo::Binding<interfaces::ProxyResolverRequestClient> binding_; |
185 }; | 187 }; |
186 | 188 |
187 ProxyResolverMojo::Job::Job(ProxyResolverMojo* resolver, | 189 ProxyResolverMojo::Job::Job(ProxyResolverMojo* resolver, |
188 const GURL& url, | 190 const GURL& url, |
189 ProxyInfo* results, | 191 ProxyInfo* results, |
190 const CompletionCallback& callback, | 192 const CompletionCallback& callback, |
191 const BoundNetLog& net_log) | 193 const NetLogWithSource& net_log) |
192 : ClientMixin<interfaces::ProxyResolverRequestClient>( | 194 : ClientMixin<interfaces::ProxyResolverRequestClient>( |
193 resolver->host_resolver_, | 195 resolver->host_resolver_, |
194 resolver->error_observer_.get(), | 196 resolver->error_observer_.get(), |
195 resolver->net_log_, | 197 resolver->net_log_, |
196 net_log), | 198 net_log), |
197 resolver_(resolver), | 199 resolver_(resolver), |
198 url_(url), | 200 url_(url), |
199 results_(results), | 201 results_(results), |
200 callback_(callback), | 202 callback_(callback), |
201 binding_(this) { | 203 binding_(this) { |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
278 DCHECK(thread_checker_.CalledOnValidThread()); | 280 DCHECK(thread_checker_.CalledOnValidThread()); |
279 size_t num_erased = pending_jobs_.erase(job); | 281 size_t num_erased = pending_jobs_.erase(job); |
280 DCHECK(num_erased); | 282 DCHECK(num_erased); |
281 delete job; | 283 delete job; |
282 } | 284 } |
283 | 285 |
284 int ProxyResolverMojo::GetProxyForURL(const GURL& url, | 286 int ProxyResolverMojo::GetProxyForURL(const GURL& url, |
285 ProxyInfo* results, | 287 ProxyInfo* results, |
286 const CompletionCallback& callback, | 288 const CompletionCallback& callback, |
287 RequestHandle* request, | 289 RequestHandle* request, |
288 const BoundNetLog& net_log) { | 290 const NetLogWithSource& net_log) { |
289 DCHECK(thread_checker_.CalledOnValidThread()); | 291 DCHECK(thread_checker_.CalledOnValidThread()); |
290 | 292 |
291 if (!mojo_proxy_resolver_ptr_) | 293 if (!mojo_proxy_resolver_ptr_) |
292 return ERR_PAC_SCRIPT_TERMINATED; | 294 return ERR_PAC_SCRIPT_TERMINATED; |
293 | 295 |
294 Job* job = new Job(this, url, results, callback, net_log); | 296 Job* job = new Job(this, url, results, callback, net_log); |
295 bool inserted = pending_jobs_.insert(job).second; | 297 bool inserted = pending_jobs_.insert(job).second; |
296 DCHECK(inserted); | 298 DCHECK(inserted); |
297 *request = job; | 299 *request = job; |
298 | 300 |
(...skipping 13 matching lines...) Expand all Loading... |
312 CHECK_EQ(1u, pending_jobs_.count(job)); | 314 CHECK_EQ(1u, pending_jobs_.count(job)); |
313 return job->GetLoadState(); | 315 return job->GetLoadState(); |
314 } | 316 } |
315 | 317 |
316 } // namespace | 318 } // namespace |
317 | 319 |
318 // A Job to create a ProxyResolver instance. | 320 // A Job to create a ProxyResolver instance. |
319 // | 321 // |
320 // Note: a Job instance is not tied to a particular resolve request, and hence | 322 // Note: a Job instance is not tied to a particular resolve request, and hence |
321 // there is no per-request logging to be done (any netlog events are only sent | 323 // there is no per-request logging to be done (any netlog events are only sent |
322 // globally) so this always uses an empty BoundNetLog. | 324 // globally) so this always uses an empty NetLogWithSource. |
323 class ProxyResolverFactoryMojo::Job | 325 class ProxyResolverFactoryMojo::Job |
324 : public ClientMixin<interfaces::ProxyResolverFactoryRequestClient>, | 326 : public ClientMixin<interfaces::ProxyResolverFactoryRequestClient>, |
325 public ProxyResolverFactory::Request { | 327 public ProxyResolverFactory::Request { |
326 public: | 328 public: |
327 Job(ProxyResolverFactoryMojo* factory, | 329 Job(ProxyResolverFactoryMojo* factory, |
328 const scoped_refptr<ProxyResolverScriptData>& pac_script, | 330 const scoped_refptr<ProxyResolverScriptData>& pac_script, |
329 std::unique_ptr<ProxyResolver>* resolver, | 331 std::unique_ptr<ProxyResolver>* resolver, |
330 const CompletionCallback& callback, | 332 const CompletionCallback& callback, |
331 std::unique_ptr<ProxyResolverErrorObserver> error_observer) | 333 std::unique_ptr<ProxyResolverErrorObserver> error_observer) |
332 : ClientMixin<interfaces::ProxyResolverFactoryRequestClient>( | 334 : ClientMixin<interfaces::ProxyResolverFactoryRequestClient>( |
333 factory->host_resolver_, | 335 factory->host_resolver_, |
334 error_observer.get(), | 336 error_observer.get(), |
335 factory->net_log_, | 337 factory->net_log_, |
336 BoundNetLog()), | 338 NetLogWithSource()), |
337 factory_(factory), | 339 factory_(factory), |
338 resolver_(resolver), | 340 resolver_(resolver), |
339 callback_(callback), | 341 callback_(callback), |
340 binding_(this), | 342 binding_(this), |
341 error_observer_(std::move(error_observer)) { | 343 error_observer_(std::move(error_observer)) { |
342 on_delete_callback_runner_ = factory_->mojo_proxy_factory_->CreateResolver( | 344 on_delete_callback_runner_ = factory_->mojo_proxy_factory_->CreateResolver( |
343 mojo::String::From(pac_script->utf16()), mojo::GetProxy(&resolver_ptr_), | 345 mojo::String::From(pac_script->utf16()), mojo::GetProxy(&resolver_ptr_), |
344 binding_.CreateInterfacePtrAndBind()); | 346 binding_.CreateInterfacePtrAndBind()); |
345 resolver_ptr_.set_connection_error_handler( | 347 resolver_ptr_.set_connection_error_handler( |
346 base::Bind(&ProxyResolverFactoryMojo::Job::OnConnectionError, | 348 base::Bind(&ProxyResolverFactoryMojo::Job::OnConnectionError, |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
401 return ERR_PAC_SCRIPT_FAILED; | 403 return ERR_PAC_SCRIPT_FAILED; |
402 } | 404 } |
403 request->reset(new Job(this, pac_script, resolver, callback, | 405 request->reset(new Job(this, pac_script, resolver, callback, |
404 error_observer_factory_.is_null() | 406 error_observer_factory_.is_null() |
405 ? nullptr | 407 ? nullptr |
406 : error_observer_factory_.Run())); | 408 : error_observer_factory_.Run())); |
407 return ERR_IO_PENDING; | 409 return ERR_IO_PENDING; |
408 } | 410 } |
409 | 411 |
410 } // namespace net | 412 } // namespace net |
OLD | NEW |