OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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_service.h" | 5 #include "net/proxy/proxy_service.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/message_loop.h" | 11 #include "base/message_loop.h" |
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
193 GURL url_; | 193 GURL url_; |
194 ProxyResolver::RequestHandle resolve_job_; | 194 ProxyResolver::RequestHandle resolve_job_; |
195 ProxyConfig::ID config_id_; // The config id when the resolve was started. | 195 ProxyConfig::ID config_id_; // The config id when the resolve was started. |
196 BoundNetLog net_log_; | 196 BoundNetLog net_log_; |
197 }; | 197 }; |
198 | 198 |
199 // ProxyService --------------------------------------------------------------- | 199 // ProxyService --------------------------------------------------------------- |
200 | 200 |
201 ProxyService::ProxyService(ProxyConfigService* config_service, | 201 ProxyService::ProxyService(ProxyConfigService* config_service, |
202 ProxyResolver* resolver, | 202 ProxyResolver* resolver, |
203 NetworkChangeNotifier* network_change_notifier, | |
204 NetLog* net_log) | 203 NetLog* net_log) |
205 : config_service_(config_service), | 204 : config_service_(config_service), |
206 resolver_(resolver), | 205 resolver_(resolver), |
207 next_config_id_(1), | 206 next_config_id_(1), |
208 should_use_proxy_resolver_(false), | 207 should_use_proxy_resolver_(false), |
209 ALLOW_THIS_IN_INITIALIZER_LIST(init_proxy_resolver_callback_( | 208 ALLOW_THIS_IN_INITIALIZER_LIST(init_proxy_resolver_callback_( |
210 this, &ProxyService::OnInitProxyResolverComplete)), | 209 this, &ProxyService::OnInitProxyResolverComplete)), |
211 net_log_(net_log), | 210 net_log_(net_log) { |
212 network_change_notifier_(network_change_notifier) { | 211 NetworkChangeNotifier::AddObserver(this); |
213 // Register to receive network change notifications. | |
214 if (network_change_notifier_) | |
215 network_change_notifier_->AddObserver(this); | |
216 } | 212 } |
217 | 213 |
218 // static | 214 // static |
219 ProxyService* ProxyService::Create( | 215 ProxyService* ProxyService::Create( |
220 ProxyConfigService* proxy_config_service, | 216 ProxyConfigService* proxy_config_service, |
221 bool use_v8_resolver, | 217 bool use_v8_resolver, |
222 URLRequestContext* url_request_context, | 218 URLRequestContext* url_request_context, |
223 NetworkChangeNotifier* network_change_notifier, | |
224 NetLog* net_log, | 219 NetLog* net_log, |
225 MessageLoop* io_loop) { | 220 MessageLoop* io_loop) { |
226 ProxyResolver* proxy_resolver = NULL; | 221 ProxyResolver* proxy_resolver = NULL; |
227 | 222 |
228 if (use_v8_resolver) { | 223 if (use_v8_resolver) { |
229 // Use the IO thread's host resolver (but since it is not threadsafe, | 224 // Use the IO thread's host resolver (but since it is not threadsafe, |
230 // bridge requests from the PAC thread over to the IO thread). | 225 // bridge requests from the PAC thread over to the IO thread). |
231 SyncHostResolverBridge* sync_host_resolver = | 226 SyncHostResolverBridge* sync_host_resolver = |
232 new SyncHostResolverBridge(url_request_context->host_resolver(), | 227 new SyncHostResolverBridge(url_request_context->host_resolver(), |
233 io_loop); | 228 io_loop); |
234 | 229 |
235 // Send javascript errors and alerts to LOG(INFO). | 230 // Send javascript errors and alerts to LOG(INFO). |
236 ProxyResolverJSBindings* js_bindings = | 231 ProxyResolverJSBindings* js_bindings = |
237 ProxyResolverJSBindings::CreateDefault(sync_host_resolver); | 232 ProxyResolverJSBindings::CreateDefault(sync_host_resolver); |
238 | 233 |
239 // Wrap the (synchronous) ProxyResolver implementation in a single-threaded | 234 // Wrap the (synchronous) ProxyResolver implementation in a single-threaded |
240 // asynchronous resolver. This version of SingleThreadedProxyResolver | 235 // asynchronous resolver. This version of SingleThreadedProxyResolver |
241 // additionally aborts any synchronous host resolves to avoid deadlock | 236 // additionally aborts any synchronous host resolves to avoid deadlock |
242 // during shutdown. | 237 // during shutdown. |
243 proxy_resolver = | 238 proxy_resolver = |
244 new SingleThreadedProxyResolverUsingBridgedHostResolver( | 239 new SingleThreadedProxyResolverUsingBridgedHostResolver( |
245 new ProxyResolverV8(js_bindings), | 240 new ProxyResolverV8(js_bindings), |
246 sync_host_resolver); | 241 sync_host_resolver); |
247 } else { | 242 } else { |
248 proxy_resolver = | 243 proxy_resolver = |
249 new SingleThreadedProxyResolver(CreateNonV8ProxyResolver()); | 244 new SingleThreadedProxyResolver(CreateNonV8ProxyResolver()); |
250 } | 245 } |
251 | 246 |
252 ProxyService* proxy_service = new ProxyService( | 247 ProxyService* proxy_service = |
253 proxy_config_service, proxy_resolver, network_change_notifier, | 248 new ProxyService(proxy_config_service, proxy_resolver, net_log); |
254 net_log); | |
255 | 249 |
256 if (proxy_resolver->expects_pac_bytes()) { | 250 if (proxy_resolver->expects_pac_bytes()) { |
257 // Configure PAC script downloads to be issued using |url_request_context|. | 251 // Configure PAC script downloads to be issued using |url_request_context|. |
258 DCHECK(url_request_context); | 252 DCHECK(url_request_context); |
259 proxy_service->SetProxyScriptFetcher( | 253 proxy_service->SetProxyScriptFetcher( |
260 ProxyScriptFetcher::Create(url_request_context)); | 254 ProxyScriptFetcher::Create(url_request_context)); |
261 } | 255 } |
262 | 256 |
263 return proxy_service; | 257 return proxy_service; |
264 } | 258 } |
265 | 259 |
266 // static | 260 // static |
267 ProxyService* ProxyService::CreateFixed(const ProxyConfig& pc) { | 261 ProxyService* ProxyService::CreateFixed(const ProxyConfig& pc) { |
268 return Create(new ProxyConfigServiceFixed(pc), false, NULL, NULL, | 262 return Create(new ProxyConfigServiceFixed(pc), false, NULL, NULL, NULL); |
269 NULL, NULL); | |
270 } | 263 } |
271 | 264 |
272 // static | 265 // static |
273 ProxyService* ProxyService::CreateNull() { | 266 ProxyService* ProxyService::CreateNull() { |
274 // Use a configuration fetcher and proxy resolver which always fail. | 267 // Use a configuration fetcher and proxy resolver which always fail. |
275 return new ProxyService(new ProxyConfigServiceNull, | 268 return new ProxyService(new ProxyConfigServiceNull, new ProxyResolverNull, |
276 new ProxyResolverNull, | |
277 NULL, | |
278 NULL); | 269 NULL); |
279 } | 270 } |
280 | 271 |
281 int ProxyService::ResolveProxy(const GURL& raw_url, | 272 int ProxyService::ResolveProxy(const GURL& raw_url, |
282 ProxyInfo* result, | 273 ProxyInfo* result, |
283 CompletionCallback* callback, | 274 CompletionCallback* callback, |
284 PacRequest** pac_request, | 275 PacRequest** pac_request, |
285 const BoundNetLog& net_log) { | 276 const BoundNetLog& net_log) { |
286 DCHECK(callback); | 277 DCHECK(callback); |
287 | 278 |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
334 // May need to go through ProxyResolver for this. | 325 // May need to go through ProxyResolver for this. |
335 return ERR_IO_PENDING; | 326 return ERR_IO_PENDING; |
336 } | 327 } |
337 | 328 |
338 // Use the manual proxy settings. | 329 // Use the manual proxy settings. |
339 config_.proxy_rules().Apply(url, result); | 330 config_.proxy_rules().Apply(url, result); |
340 return OK; | 331 return OK; |
341 } | 332 } |
342 | 333 |
343 ProxyService::~ProxyService() { | 334 ProxyService::~ProxyService() { |
344 // Unregister to receive network change notifications. | 335 NetworkChangeNotifier::RemoveObserver(this); |
345 if (network_change_notifier_) | |
346 network_change_notifier_->RemoveObserver(this); | |
347 | 336 |
348 // Cancel any inprogress requests. | 337 // Cancel any inprogress requests. |
349 for (PendingRequests::iterator it = pending_requests_.begin(); | 338 for (PendingRequests::iterator it = pending_requests_.begin(); |
350 it != pending_requests_.end(); | 339 it != pending_requests_.end(); |
351 ++it) { | 340 ++it) { |
352 (*it)->Cancel(); | 341 (*it)->Cancel(); |
353 } | 342 } |
354 | 343 |
355 // Make sure that InitProxyResolver gets destroyed BEFORE the | 344 // Make sure that InitProxyResolver gets destroyed BEFORE the |
356 // CapturingNetLog it is using is deleted. | 345 // CapturingNetLog it is using is deleted. |
(...skipping 311 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
668 const TimeDelta kProxyConfigMaxAge = TimeDelta::FromSeconds(5); | 657 const TimeDelta kProxyConfigMaxAge = TimeDelta::FromSeconds(5); |
669 | 658 |
670 // Periodically check for a new config. | 659 // Periodically check for a new config. |
671 if (!config_has_been_initialized() || | 660 if (!config_has_been_initialized() || |
672 (TimeTicks::Now() - config_last_update_time_) > kProxyConfigMaxAge) | 661 (TimeTicks::Now() - config_last_update_time_) > kProxyConfigMaxAge) |
673 UpdateConfig(net_log); | 662 UpdateConfig(net_log); |
674 } | 663 } |
675 | 664 |
676 | 665 |
677 void ProxyService::OnIPAddressChanged() { | 666 void ProxyService::OnIPAddressChanged() { |
678 DCHECK(network_change_notifier_); | |
679 | |
680 // Mark the current configuration as being un-initialized. | 667 // Mark the current configuration as being un-initialized. |
681 // | 668 // |
682 // This will force us to re-fetch the configuration (and re-run all of | 669 // This will force us to re-fetch the configuration (and re-run all of |
683 // the initialization steps) on the next ResolveProxy() request, as part | 670 // the initialization steps) on the next ResolveProxy() request, as part |
684 // of UpdateConfigIfOld(). | 671 // of UpdateConfigIfOld(). |
685 config_.set_id(ProxyConfig::INVALID_ID); | 672 config_.set_id(ProxyConfig::INVALID_ID); |
686 } | 673 } |
687 | 674 |
688 SyncProxyServiceHelper::SyncProxyServiceHelper(MessageLoop* io_message_loop, | 675 SyncProxyServiceHelper::SyncProxyServiceHelper(MessageLoop* io_message_loop, |
689 ProxyService* proxy_service) | 676 ProxyService* proxy_service) |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
743 OnCompletion(result_); | 730 OnCompletion(result_); |
744 } | 731 } |
745 } | 732 } |
746 | 733 |
747 void SyncProxyServiceHelper::OnCompletion(int rv) { | 734 void SyncProxyServiceHelper::OnCompletion(int rv) { |
748 result_ = rv; | 735 result_ = rv; |
749 event_.Signal(); | 736 event_.Signal(); |
750 } | 737 } |
751 | 738 |
752 } // namespace net | 739 } // namespace net |
OLD | NEW |