| 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" | |
| 30 #include "net/proxy/proxy_resolver_v8.h" | |
| 31 #include "net/proxy/proxy_script_decider.h" | 29 #include "net/proxy/proxy_script_decider.h" |
| 32 #include "net/proxy/proxy_script_fetcher.h" | 30 #include "net/proxy/proxy_script_fetcher.h" |
| 33 #include "net/proxy/sync_host_resolver_bridge.h" | 31 #include "net/proxy/sync_host_resolver_bridge.h" |
| 34 #include "net/url_request/url_request_context.h" | 32 #include "net/url_request/url_request_context.h" |
| 35 | 33 |
| 36 #if defined(OS_WIN) | 34 #if defined(OS_WIN) |
| 37 #include "net/proxy/proxy_config_service_win.h" | 35 #include "net/proxy/proxy_config_service_win.h" |
| 38 #include "net/proxy/proxy_resolver_winhttp.h" | 36 #include "net/proxy/proxy_resolver_winhttp.h" |
| 39 #elif defined(OS_MACOSX) | 37 #elif defined(OS_MACOSX) |
| 40 #include "net/proxy/proxy_config_service_mac.h" | 38 #include "net/proxy/proxy_config_service_mac.h" |
| 41 #include "net/proxy/proxy_resolver_mac.h" | 39 #include "net/proxy/proxy_resolver_mac.h" |
| 42 #elif defined(OS_LINUX) && !defined(OS_CHROMEOS) | 40 #elif defined(OS_LINUX) && !defined(OS_CHROMEOS) |
| 43 #include "net/proxy/proxy_config_service_linux.h" | 41 #include "net/proxy/proxy_config_service_linux.h" |
| 44 #elif defined(OS_ANDROID) | 42 #elif defined(OS_ANDROID) |
| 45 #include "net/proxy/proxy_config_service_android.h" | 43 #include "net/proxy/proxy_config_service_android.h" |
| 46 #endif | 44 #endif |
| 47 | 45 |
| 48 using base::TimeDelta; | 46 using base::TimeDelta; |
| 49 using base::TimeTicks; | 47 using base::TimeTicks; |
| 50 | 48 |
| 51 namespace net { | 49 namespace net { |
| 52 | 50 |
| 53 namespace { | 51 namespace { |
| 54 | 52 |
| 55 const size_t kMaxNumNetLogEntries = 100; | 53 const size_t kMaxNumNetLogEntries = 100; |
| 56 const size_t kDefaultNumPacThreads = 4; | |
| 57 | 54 |
| 58 // When the IP address changes we don't immediately re-run proxy auto-config. | 55 // When the IP address changes we don't immediately re-run proxy auto-config. |
| 59 // Instead, we wait for |kDelayAfterNetworkChangesMs| before | 56 // Instead, we wait for |kDelayAfterNetworkChangesMs| before |
| 60 // attempting to re-valuate proxy auto-config. | 57 // attempting to re-valuate proxy auto-config. |
| 61 // | 58 // |
| 62 // During this time window, any resolve requests sent to the ProxyService will | 59 // 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 | 60 // 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. | 61 // auto-config step will be run, and the queued requests resumed. |
| 65 // | 62 // |
| 66 // The reason we play this game is that our signal for detecting network | 63 // 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( | 248 virtual int SetPacScript( |
| 252 const scoped_refptr<ProxyResolverScriptData>& pac_script, | 249 const scoped_refptr<ProxyResolverScriptData>& pac_script, |
| 253 const CompletionCallback& callback) OVERRIDE { | 250 const CompletionCallback& callback) OVERRIDE { |
| 254 return OK; | 251 return OK; |
| 255 } | 252 } |
| 256 | 253 |
| 257 private: | 254 private: |
| 258 const std::string pac_string_; | 255 const std::string pac_string_; |
| 259 }; | 256 }; |
| 260 | 257 |
| 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. | 258 // Creates ProxyResolvers using a platform-specific implementation. |
| 310 class ProxyResolverFactoryForSystem : public ProxyResolverFactory { | 259 class ProxyResolverFactoryForSystem : public ProxyResolverFactory { |
| 311 public: | 260 public: |
| 312 ProxyResolverFactoryForSystem() | 261 ProxyResolverFactoryForSystem() |
| 313 : ProxyResolverFactory(false /*expects_pac_bytes*/) {} | 262 : ProxyResolverFactory(false /*expects_pac_bytes*/) {} |
| 314 | 263 |
| 315 virtual ProxyResolver* CreateProxyResolver() OVERRIDE { | 264 virtual ProxyResolver* CreateProxyResolver() OVERRIDE { |
| 316 DCHECK(IsSupported()); | 265 DCHECK(IsSupported()); |
| 317 #if defined(OS_WIN) | 266 #if defined(OS_WIN) |
| 318 return new ProxyResolverWinHttp(); | 267 return new ProxyResolverWinHttp(); |
| (...skipping 593 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 912 next_config_id_(1), | 861 next_config_id_(1), |
| 913 current_state_(STATE_NONE) , | 862 current_state_(STATE_NONE) , |
| 914 net_log_(net_log), | 863 net_log_(net_log), |
| 915 stall_proxy_auto_config_delay_(TimeDelta::FromMilliseconds( | 864 stall_proxy_auto_config_delay_(TimeDelta::FromMilliseconds( |
| 916 kDelayAfterNetworkChangesMs)) { | 865 kDelayAfterNetworkChangesMs)) { |
| 917 NetworkChangeNotifier::AddIPAddressObserver(this); | 866 NetworkChangeNotifier::AddIPAddressObserver(this); |
| 918 ResetConfigService(config_service); | 867 ResetConfigService(config_service); |
| 919 } | 868 } |
| 920 | 869 |
| 921 // static | 870 // 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( | 871 ProxyService* ProxyService::CreateUsingSystemProxyResolver( |
| 961 ProxyConfigService* proxy_config_service, | 872 ProxyConfigService* proxy_config_service, |
| 962 size_t num_pac_threads, | 873 size_t num_pac_threads, |
| 963 NetLog* net_log) { | 874 NetLog* net_log) { |
| 964 DCHECK(proxy_config_service); | 875 DCHECK(proxy_config_service); |
| 965 | 876 |
| 966 if (!ProxyResolverFactoryForSystem::IsSupported()) { | 877 if (!ProxyResolverFactoryForSystem::IsSupported()) { |
| 967 LOG(WARNING) << "PAC support disabled because there is no " | 878 LOG(WARNING) << "PAC support disabled because there is no " |
| 968 "system implementation"; | 879 "system implementation"; |
| 969 return CreateWithoutProxyResolver(proxy_config_service, net_log); | 880 return CreateWithoutProxyResolver(proxy_config_service, net_log); |
| (...skipping 660 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1630 OnCompletion(result_); | 1541 OnCompletion(result_); |
| 1631 } | 1542 } |
| 1632 } | 1543 } |
| 1633 | 1544 |
| 1634 void SyncProxyServiceHelper::OnCompletion(int rv) { | 1545 void SyncProxyServiceHelper::OnCompletion(int rv) { |
| 1635 result_ = rv; | 1546 result_ = rv; |
| 1636 event_.Signal(); | 1547 event_.Signal(); |
| 1637 } | 1548 } |
| 1638 | 1549 |
| 1639 } // namespace net | 1550 } // namespace net |
| OLD | NEW |