Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 "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/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
| 11 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
| 14 #include "base/message_loop.h" | 14 #include "base/message_loop.h" |
| 15 #include "base/message_loop_proxy.h" | 15 #include "base/message_loop_proxy.h" |
| 16 #include "base/string_util.h" | 16 #include "base/string_util.h" |
| 17 #include "base/thread_task_runner_handle.h" | 17 #include "base/thread_task_runner_handle.h" |
| 18 #include "base/values.h" | 18 #include "base/values.h" |
| 19 #include "googleurl/src/gurl.h" | 19 #include "googleurl/src/gurl.h" |
| 20 #include "net/base/completion_callback.h" | 20 #include "net/base/completion_callback.h" |
| 21 #include "net/base/net_errors.h" | 21 #include "net/base/net_errors.h" |
| 22 #include "net/base/net_log.h" | 22 #include "net/base/net_log.h" |
| 23 #include "net/base/net_util.h" | 23 #include "net/base/net_util.h" |
| 24 #include "net/proxy/dhcp_proxy_script_fetcher.h" | 24 #include "net/proxy/dhcp_proxy_script_fetcher.h" |
| 25 #include "net/proxy/multi_threaded_proxy_resolver.h" | 25 #include "net/proxy/multi_threaded_proxy_resolver.h" |
| 26 #include "net/proxy/network_delegate_error_observer.h" | 26 #include "net/proxy/network_delegate_error_observer.h" |
| 27 #include "net/proxy/proxy_config_service_fixed.h" | 27 #include "net/proxy/proxy_config_service_fixed.h" |
| 28 #include "net/proxy/proxy_resolver.h" | 28 #include "net/proxy/proxy_resolver.h" |
| 29 #include "net/proxy/proxy_resolver_js_bindings.h" | 29 #include "net/proxy/proxy_resolver_js_bindings.h" |
|
eroman
2012/08/28 01:37:42
you can delete this too.
tfarina
2012/08/30 02:45:52
Done.
| |
| 30 #include "net/proxy/proxy_resolver_v8.h" | |
| 31 #include "net/proxy/proxy_script_decider.h" | 30 #include "net/proxy/proxy_script_decider.h" |
| 32 #include "net/proxy/proxy_script_fetcher.h" | 31 #include "net/proxy/proxy_script_fetcher.h" |
| 33 #include "net/proxy/sync_host_resolver_bridge.h" | 32 #include "net/proxy/sync_host_resolver_bridge.h" |
| 34 #include "net/url_request/url_request_context.h" | 33 #include "net/url_request/url_request_context.h" |
| 35 | 34 |
| 36 #if defined(OS_WIN) | 35 #if defined(OS_WIN) |
| 37 #include "net/proxy/proxy_config_service_win.h" | 36 #include "net/proxy/proxy_config_service_win.h" |
| 38 #include "net/proxy/proxy_resolver_winhttp.h" | 37 #include "net/proxy/proxy_resolver_winhttp.h" |
| 39 #elif defined(OS_MACOSX) | 38 #elif defined(OS_MACOSX) |
| 40 #include "net/proxy/proxy_config_service_mac.h" | 39 #include "net/proxy/proxy_config_service_mac.h" |
| 41 #include "net/proxy/proxy_resolver_mac.h" | 40 #include "net/proxy/proxy_resolver_mac.h" |
| 42 #elif defined(OS_LINUX) && !defined(OS_CHROMEOS) | 41 #elif defined(OS_LINUX) && !defined(OS_CHROMEOS) |
| 43 #include "net/proxy/proxy_config_service_linux.h" | 42 #include "net/proxy/proxy_config_service_linux.h" |
| 44 #elif defined(OS_ANDROID) | 43 #elif defined(OS_ANDROID) |
| 45 #include "net/proxy/proxy_config_service_android.h" | 44 #include "net/proxy/proxy_config_service_android.h" |
| 46 #endif | 45 #endif |
| 47 | 46 |
| 48 using base::TimeDelta; | 47 using base::TimeDelta; |
| 49 using base::TimeTicks; | 48 using base::TimeTicks; |
| 50 | 49 |
| 51 namespace net { | 50 namespace net { |
| 52 | 51 |
| 53 namespace { | 52 namespace { |
| 54 | 53 |
| 55 const size_t kMaxNumNetLogEntries = 100; | 54 const size_t kMaxNumNetLogEntries = 100; |
| 56 const size_t kDefaultNumPacThreads = 4; | |
| 57 | 55 |
| 58 // When the IP address changes we don't immediately re-run proxy auto-config. | 56 // When the IP address changes we don't immediately re-run proxy auto-config. |
| 59 // Instead, we wait for |kDelayAfterNetworkChangesMs| before | 57 // Instead, we wait for |kDelayAfterNetworkChangesMs| before |
| 60 // attempting to re-valuate proxy auto-config. | 58 // attempting to re-valuate proxy auto-config. |
| 61 // | 59 // |
| 62 // During this time window, any resolve requests sent to the ProxyService will | 60 // During this time window, any resolve requests sent to the ProxyService will |
| 63 // be queued. Once we have waited the required amount of them, the proxy | 61 // be queued. Once we have waited the required amount of them, the proxy |
| 64 // auto-config step will be run, and the queued requests resumed. | 62 // auto-config step will be run, and the queued requests resumed. |
| 65 // | 63 // |
| 66 // The reason we play this game is that our signal for detecting network | 64 // The reason we play this game is that our signal for detecting network |
| (...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 251 virtual int SetPacScript( | 249 virtual int SetPacScript( |
| 252 const scoped_refptr<ProxyResolverScriptData>& pac_script, | 250 const scoped_refptr<ProxyResolverScriptData>& pac_script, |
| 253 const CompletionCallback& callback) OVERRIDE { | 251 const CompletionCallback& callback) OVERRIDE { |
| 254 return OK; | 252 return OK; |
| 255 } | 253 } |
| 256 | 254 |
| 257 private: | 255 private: |
| 258 const std::string pac_string_; | 256 const std::string pac_string_; |
| 259 }; | 257 }; |
| 260 | 258 |
| 261 // This factory creates V8ProxyResolvers with appropriate javascript bindings. | |
| 262 class ProxyResolverFactoryForV8 : public ProxyResolverFactory { | |
| 263 public: | |
| 264 // |async_host_resolver|, |io_loop| and |net_log| must remain | |
| 265 // valid for the duration of our lifetime. | |
| 266 // |async_host_resolver| will only be operated on |io_loop|. | |
| 267 // TODO(willchan): remove io_loop and replace it with origin_loop. | |
| 268 ProxyResolverFactoryForV8(HostResolver* async_host_resolver, | |
| 269 MessageLoop* io_loop, | |
| 270 base::MessageLoopProxy* origin_loop, | |
| 271 NetLog* net_log, | |
| 272 NetworkDelegate* network_delegate) | |
| 273 : ProxyResolverFactory(true /*expects_pac_bytes*/), | |
| 274 async_host_resolver_(async_host_resolver), | |
| 275 io_loop_(io_loop), | |
| 276 origin_loop_(origin_loop), | |
| 277 net_log_(net_log), | |
| 278 network_delegate_(network_delegate) { | |
| 279 } | |
| 280 | |
| 281 virtual ProxyResolver* CreateProxyResolver() OVERRIDE { | |
| 282 // Create a synchronous host resolver wrapper that operates | |
| 283 // |async_host_resolver_| on |io_loop_|. | |
| 284 SyncHostResolverBridge* sync_host_resolver = | |
| 285 new SyncHostResolverBridge(async_host_resolver_, io_loop_); | |
| 286 | |
| 287 NetworkDelegateErrorObserver* error_observer = | |
| 288 new NetworkDelegateErrorObserver( | |
| 289 network_delegate_, origin_loop_.get()); | |
| 290 | |
| 291 // ProxyResolverJSBindings takes ownership of |error_observer| and | |
| 292 // |sync_host_resolver|. | |
| 293 ProxyResolverJSBindings* js_bindings = | |
| 294 ProxyResolverJSBindings::CreateDefault( | |
| 295 sync_host_resolver, net_log_, error_observer); | |
| 296 | |
| 297 // ProxyResolverV8 takes ownership of |js_bindings|. | |
| 298 return new ProxyResolverV8(js_bindings); | |
| 299 } | |
| 300 | |
| 301 private: | |
| 302 HostResolver* const async_host_resolver_; | |
| 303 MessageLoop* io_loop_; | |
| 304 scoped_refptr<base::MessageLoopProxy> origin_loop_; | |
| 305 NetLog* net_log_; | |
| 306 NetworkDelegate* network_delegate_; | |
| 307 }; | |
| 308 | |
| 309 // Creates ProxyResolvers using a platform-specific implementation. | 259 // Creates ProxyResolvers using a platform-specific implementation. |
| 310 class ProxyResolverFactoryForSystem : public ProxyResolverFactory { | 260 class ProxyResolverFactoryForSystem : public ProxyResolverFactory { |
| 311 public: | 261 public: |
| 312 ProxyResolverFactoryForSystem() | 262 ProxyResolverFactoryForSystem() |
| 313 : ProxyResolverFactory(false /*expects_pac_bytes*/) {} | 263 : ProxyResolverFactory(false /*expects_pac_bytes*/) {} |
| 314 | 264 |
| 315 virtual ProxyResolver* CreateProxyResolver() OVERRIDE { | 265 virtual ProxyResolver* CreateProxyResolver() OVERRIDE { |
| 316 DCHECK(IsSupported()); | 266 DCHECK(IsSupported()); |
| 317 #if defined(OS_WIN) | 267 #if defined(OS_WIN) |
| 318 return new ProxyResolverWinHttp(); | 268 return new ProxyResolverWinHttp(); |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 377 virtual void RemoveObserver(Observer* observer) OVERRIDE {} | 327 virtual void RemoveObserver(Observer* observer) OVERRIDE {} |
| 378 virtual ConfigAvailability GetLatestProxyConfig( | 328 virtual ConfigAvailability GetLatestProxyConfig( |
| 379 ProxyConfig* config) OVERRIDE { | 329 ProxyConfig* config) OVERRIDE { |
| 380 return CONFIG_UNSET; | 330 return CONFIG_UNSET; |
| 381 } | 331 } |
| 382 }; | 332 }; |
| 383 #endif | 333 #endif |
| 384 | 334 |
| 385 } // namespace | 335 } // namespace |
| 386 | 336 |
| 337 const size_t ProxyService::kDefaultNumPacThreads = 4; | |
| 338 | |
| 387 // ProxyService::InitProxyResolver -------------------------------------------- | 339 // ProxyService::InitProxyResolver -------------------------------------------- |
| 388 | 340 |
| 389 // This glues together two asynchronous steps: | 341 // This glues together two asynchronous steps: |
| 390 // (1) ProxyScriptDecider -- try to fetch/validate a sequence of PAC scripts | 342 // (1) ProxyScriptDecider -- try to fetch/validate a sequence of PAC scripts |
| 391 // to figure out what we should configure against. | 343 // to figure out what we should configure against. |
| 392 // (2) Feed the fetched PAC script into the ProxyResolver. | 344 // (2) Feed the fetched PAC script into the ProxyResolver. |
| 393 // | 345 // |
| 394 // InitProxyResolver is a single-use class which encapsulates cancellation as | 346 // InitProxyResolver is a single-use class which encapsulates cancellation as |
| 395 // part of its destructor. Start() or StartSkipDecider() should be called just | 347 // part of its destructor. Start() or StartSkipDecider() should be called just |
| 396 // once. The instance can be destroyed at any time, and the request will be | 348 // once. The instance can be destroyed at any time, and the request will be |
| (...skipping 515 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 912 next_config_id_(1), | 864 next_config_id_(1), |
| 913 current_state_(STATE_NONE) , | 865 current_state_(STATE_NONE) , |
| 914 net_log_(net_log), | 866 net_log_(net_log), |
| 915 stall_proxy_auto_config_delay_(TimeDelta::FromMilliseconds( | 867 stall_proxy_auto_config_delay_(TimeDelta::FromMilliseconds( |
| 916 kDelayAfterNetworkChangesMs)) { | 868 kDelayAfterNetworkChangesMs)) { |
| 917 NetworkChangeNotifier::AddIPAddressObserver(this); | 869 NetworkChangeNotifier::AddIPAddressObserver(this); |
| 918 ResetConfigService(config_service); | 870 ResetConfigService(config_service); |
| 919 } | 871 } |
| 920 | 872 |
| 921 // static | 873 // static |
| 922 ProxyService* ProxyService::CreateUsingV8ProxyResolver( | |
| 923 ProxyConfigService* proxy_config_service, | |
| 924 size_t num_pac_threads, | |
| 925 ProxyScriptFetcher* proxy_script_fetcher, | |
| 926 DhcpProxyScriptFetcher* dhcp_proxy_script_fetcher, | |
| 927 HostResolver* host_resolver, | |
| 928 NetLog* net_log, | |
| 929 NetworkDelegate* network_delegate) { | |
| 930 DCHECK(proxy_config_service); | |
| 931 DCHECK(proxy_script_fetcher); | |
| 932 DCHECK(dhcp_proxy_script_fetcher); | |
| 933 DCHECK(host_resolver); | |
| 934 | |
| 935 if (num_pac_threads == 0) | |
| 936 num_pac_threads = kDefaultNumPacThreads; | |
| 937 | |
| 938 ProxyResolverFactory* sync_resolver_factory = | |
| 939 new ProxyResolverFactoryForV8( | |
| 940 host_resolver, | |
| 941 MessageLoop::current(), | |
| 942 base::MessageLoopProxy::current(), | |
| 943 net_log, | |
| 944 network_delegate); | |
| 945 | |
| 946 ProxyResolver* proxy_resolver = | |
| 947 new MultiThreadedProxyResolver(sync_resolver_factory, num_pac_threads); | |
| 948 | |
| 949 ProxyService* proxy_service = | |
| 950 new ProxyService(proxy_config_service, proxy_resolver, net_log); | |
| 951 | |
| 952 // Configure fetchers to use for PAC script downloads and auto-detect. | |
| 953 proxy_service->SetProxyScriptFetchers(proxy_script_fetcher, | |
| 954 dhcp_proxy_script_fetcher); | |
| 955 | |
| 956 return proxy_service; | |
| 957 } | |
| 958 | |
| 959 // static | |
| 960 ProxyService* ProxyService::CreateUsingSystemProxyResolver( | 874 ProxyService* ProxyService::CreateUsingSystemProxyResolver( |
| 961 ProxyConfigService* proxy_config_service, | 875 ProxyConfigService* proxy_config_service, |
| 962 size_t num_pac_threads, | 876 size_t num_pac_threads, |
| 963 NetLog* net_log) { | 877 NetLog* net_log) { |
| 964 DCHECK(proxy_config_service); | 878 DCHECK(proxy_config_service); |
| 965 | 879 |
| 966 if (!ProxyResolverFactoryForSystem::IsSupported()) { | 880 if (!ProxyResolverFactoryForSystem::IsSupported()) { |
| 967 LOG(WARNING) << "PAC support disabled because there is no " | 881 LOG(WARNING) << "PAC support disabled because there is no " |
| 968 "system implementation"; | 882 "system implementation"; |
| 969 return CreateWithoutProxyResolver(proxy_config_service, net_log); | 883 return CreateWithoutProxyResolver(proxy_config_service, net_log); |
| (...skipping 660 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1630 OnCompletion(result_); | 1544 OnCompletion(result_); |
| 1631 } | 1545 } |
| 1632 } | 1546 } |
| 1633 | 1547 |
| 1634 void SyncProxyServiceHelper::OnCompletion(int rv) { | 1548 void SyncProxyServiceHelper::OnCompletion(int rv) { |
| 1635 result_ = rv; | 1549 result_ = rv; |
| 1636 event_.Signal(); | 1550 event_.Signal(); |
| 1637 } | 1551 } |
| 1638 | 1552 |
| 1639 } // namespace net | 1553 } // namespace net |
| OLD | NEW |