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
3 // found in the LICENSE file.
5 #include "chrome/browser/net/proxy_service_factory.h"
9 #include "base/command_line.h"
10 #include "base/metrics/field_trial.h"
11 #include "base/strings/string_number_conversions.h"
12 #include "base/threading/thread.h"
13 #include "chrome/browser/browser_process.h"
14 #include "chrome/browser/io_thread.h"
15 #include "chrome/browser/net/pref_proxy_config_tracker_impl.h"
16 #include "chrome/common/chrome_switches.h"
17 #include "content/public/browser/browser_thread.h"
18 #include "net/log/net_log.h"
19 #include "net/proxy/dhcp_proxy_script_fetcher_factory.h"
20 #include "net/proxy/proxy_config_service.h"
21 #include "net/proxy/proxy_script_fetcher_impl.h"
22 #include "net/proxy/proxy_service.h"
23 #include "net/proxy/proxy_service_v8.h"
24 #include "net/url_request/url_request_context.h"
26 #if defined(OS_CHROMEOS)
27 #include "chrome/browser/chromeos/proxy_config_service_impl.h"
28 #include "chromeos/network/dhcp_proxy_script_fetcher_chromeos.h"
29 #endif // defined(OS_CHROMEOS)
32 #include "net/proxy/proxy_resolver_v8.h"
35 #if !defined(OS_IOS) && !defined(OS_ANDROID)
36 #include "chrome/browser/net/utility_process_mojo_proxy_resolver_factory.h"
37 #include "net/proxy/proxy_service_mojo.h"
40 using content::BrowserThread
;
44 #if !defined(OS_ANDROID)
45 bool EnableOutOfProcessV8Pac(const base::CommandLine
& command_line
) {
46 const std::string group_name
=
47 base::FieldTrialList::FindFullName("OutOfProcessPac");
49 if (command_line
.HasSwitch(switches::kDisableOutOfProcessPac
))
51 if (command_line
.HasSwitch(switches::kV8PacMojoOutOfProcess
))
53 return group_name
== "Enabled";
55 #endif // !defined(OS_ANDROID)
60 net::ProxyConfigService
* ProxyServiceFactory::CreateProxyConfigService(
61 PrefProxyConfigTracker
* tracker
) {
62 // The linux gconf-based proxy settings getter relies on being initialized
63 // from the UI thread.
64 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
66 scoped_ptr
<net::ProxyConfigService
> base_service
;
68 #if !defined(OS_CHROMEOS)
69 // On ChromeOS, base service is NULL; chromeos::ProxyConfigServiceImpl
70 // determines the effective proxy config to take effect in the network layer,
71 // be it from prefs or system (which is network shill on chromeos).
73 // For other platforms, create a baseline service that provides proxy
74 // configuration in case nothing is configured through prefs (Note: prefs
75 // include command line and configuration policy).
77 // TODO(port): the IO and FILE message loops are only used by Linux. Can
78 // that code be moved to chrome/browser instead of being in net, so that it
79 // can use BrowserThread instead of raw MessageLoop pointers? See bug 25354.
80 base_service
.reset(net::ProxyService::CreateSystemProxyConfigService(
81 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO
),
82 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE)));
83 #endif // !defined(OS_CHROMEOS)
85 return tracker
->CreateTrackingProxyConfigService(base_service
.Pass())
90 PrefProxyConfigTracker
*
91 ProxyServiceFactory::CreatePrefProxyConfigTrackerOfProfile(
92 PrefService
* profile_prefs
,
93 PrefService
* local_state_prefs
) {
94 #if defined(OS_CHROMEOS)
95 return new chromeos::ProxyConfigServiceImpl(profile_prefs
, local_state_prefs
);
97 return new PrefProxyConfigTrackerImpl(profile_prefs
);
98 #endif // defined(OS_CHROMEOS)
102 PrefProxyConfigTracker
*
103 ProxyServiceFactory::CreatePrefProxyConfigTrackerOfLocalState(
104 PrefService
* local_state_prefs
) {
105 #if defined(OS_CHROMEOS)
106 return new chromeos::ProxyConfigServiceImpl(NULL
, local_state_prefs
);
108 return new PrefProxyConfigTrackerImpl(local_state_prefs
);
109 #endif // defined(OS_CHROMEOS)
113 net::ProxyService
* ProxyServiceFactory::CreateProxyService(
114 net::NetLog
* net_log
,
115 net::URLRequestContext
* context
,
116 net::NetworkDelegate
* network_delegate
,
117 net::ProxyConfigService
* proxy_config_service
,
118 const base::CommandLine
& command_line
,
119 bool quick_check_enabled
) {
120 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
124 bool use_v8
= !command_line
.HasSwitch(switches::kWinHttpProxyResolver
);
125 // TODO(eroman): Figure out why this doesn't work in single-process mode.
126 // Should be possible now that a private isolate is used.
127 // http://crbug.com/474654
128 if (use_v8
&& command_line
.HasSwitch(switches::kSingleProcess
)) {
129 LOG(ERROR
) << "Cannot use V8 Proxy resolver in single process mode.";
130 use_v8
= false; // Fallback to non-v8 implementation.
132 #endif // defined(OS_IOS)
134 size_t num_pac_threads
= 0u; // Use default number of threads.
136 // Check the command line for an override on the number of proxy resolver
138 if (command_line
.HasSwitch(switches::kNumPacThreads
)) {
139 std::string s
= command_line
.GetSwitchValueASCII(switches::kNumPacThreads
);
141 // Parse the switch (it should be a positive integer formatted as decimal).
143 if (base::StringToInt(s
, &n
) && n
> 0) {
144 num_pac_threads
= static_cast<size_t>(n
);
146 LOG(ERROR
) << "Invalid switch for number of PAC threads: " << s
;
150 net::ProxyService
* proxy_service
= NULL
;
155 net::DhcpProxyScriptFetcher
* dhcp_proxy_script_fetcher
;
156 #if defined(OS_CHROMEOS)
157 dhcp_proxy_script_fetcher
=
158 new chromeos::DhcpProxyScriptFetcherChromeos(context
);
160 net::DhcpProxyScriptFetcherFactory dhcp_factory
;
161 dhcp_proxy_script_fetcher
= dhcp_factory
.Create(context
);
164 #if !defined(OS_ANDROID)
165 // In-process Mojo PAC can only be set on the command line, so its presence
166 // should override other options.
167 if (command_line
.HasSwitch(switches::kV8PacMojoInProcess
)) {
168 proxy_service
= net::CreateProxyServiceUsingMojoInProcess(
169 proxy_config_service
, new net::ProxyScriptFetcherImpl(context
),
170 dhcp_proxy_script_fetcher
, context
->host_resolver(), net_log
,
172 } else if (EnableOutOfProcessV8Pac(command_line
)) {
173 proxy_service
= net::CreateProxyServiceUsingMojoFactory(
174 UtilityProcessMojoProxyResolverFactory::GetInstance(),
175 proxy_config_service
, new net::ProxyScriptFetcherImpl(context
),
176 dhcp_proxy_script_fetcher
, context
->host_resolver(), net_log
,
179 #endif // !defined(OS_ANDROID)
181 if (!proxy_service
) {
182 proxy_service
= net::CreateProxyServiceUsingV8ProxyResolver(
183 proxy_config_service
, new net::ProxyScriptFetcherImpl(context
),
184 dhcp_proxy_script_fetcher
, context
->host_resolver(), net_log
,
187 #endif // defined(OS_IOS)
189 proxy_service
= net::ProxyService::CreateUsingSystemProxyResolver(
190 proxy_config_service
,
195 proxy_service
->set_quick_check_enabled(quick_check_enabled
);
197 return proxy_service
;