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/common/chrome_switches.h"
16 #include "components/proxy_config/pref_proxy_config_tracker_impl.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(
99 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO
));
100 #endif // defined(OS_CHROMEOS)
104 PrefProxyConfigTracker
*
105 ProxyServiceFactory::CreatePrefProxyConfigTrackerOfLocalState(
106 PrefService
* local_state_prefs
) {
107 #if defined(OS_CHROMEOS)
108 return new chromeos::ProxyConfigServiceImpl(NULL
, local_state_prefs
);
110 return new PrefProxyConfigTrackerImpl(
112 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO
));
113 #endif // defined(OS_CHROMEOS)
117 net::ProxyService
* ProxyServiceFactory::CreateProxyService(
118 net::NetLog
* net_log
,
119 net::URLRequestContext
* context
,
120 net::NetworkDelegate
* network_delegate
,
121 net::ProxyConfigService
* proxy_config_service
,
122 const base::CommandLine
& command_line
,
123 bool quick_check_enabled
) {
124 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
128 bool use_v8
= !command_line
.HasSwitch(switches::kWinHttpProxyResolver
);
129 // TODO(eroman): Figure out why this doesn't work in single-process mode.
130 // Should be possible now that a private isolate is used.
131 // http://crbug.com/474654
132 if (use_v8
&& command_line
.HasSwitch(switches::kSingleProcess
)) {
133 LOG(ERROR
) << "Cannot use V8 Proxy resolver in single process mode.";
134 use_v8
= false; // Fallback to non-v8 implementation.
136 #endif // defined(OS_IOS)
138 size_t num_pac_threads
= 0u; // Use default number of threads.
140 // Check the command line for an override on the number of proxy resolver
142 if (command_line
.HasSwitch(switches::kNumPacThreads
)) {
143 std::string s
= command_line
.GetSwitchValueASCII(switches::kNumPacThreads
);
145 // Parse the switch (it should be a positive integer formatted as decimal).
147 if (base::StringToInt(s
, &n
) && n
> 0) {
148 num_pac_threads
= static_cast<size_t>(n
);
150 LOG(ERROR
) << "Invalid switch for number of PAC threads: " << s
;
154 net::ProxyService
* proxy_service
= NULL
;
159 scoped_ptr
<net::DhcpProxyScriptFetcher
> dhcp_proxy_script_fetcher
;
160 #if defined(OS_CHROMEOS)
161 dhcp_proxy_script_fetcher
.reset(
162 new chromeos::DhcpProxyScriptFetcherChromeos(context
));
164 net::DhcpProxyScriptFetcherFactory dhcp_factory
;
165 dhcp_proxy_script_fetcher
= dhcp_factory
.Create(context
);
168 #if !defined(OS_ANDROID)
169 // In-process Mojo PAC can only be set on the command line, so its presence
170 // should override other options.
171 if (command_line
.HasSwitch(switches::kV8PacMojoInProcess
)) {
172 proxy_service
= net::CreateProxyServiceUsingMojoInProcess(
173 proxy_config_service
, new net::ProxyScriptFetcherImpl(context
),
174 dhcp_proxy_script_fetcher
.Pass(), context
->host_resolver(), net_log
,
176 } else if (EnableOutOfProcessV8Pac(command_line
)) {
177 proxy_service
= net::CreateProxyServiceUsingMojoFactory(
178 UtilityProcessMojoProxyResolverFactory::GetInstance(),
179 proxy_config_service
, new net::ProxyScriptFetcherImpl(context
),
180 dhcp_proxy_script_fetcher
.Pass(), context
->host_resolver(), net_log
,
183 #endif // !defined(OS_ANDROID)
185 if (!proxy_service
) {
186 proxy_service
= net::CreateProxyServiceUsingV8ProxyResolver(
187 proxy_config_service
, new net::ProxyScriptFetcherImpl(context
),
188 dhcp_proxy_script_fetcher
.Pass(), context
->host_resolver(), net_log
,
191 #endif // defined(OS_IOS)
193 proxy_service
= net::ProxyService::CreateUsingSystemProxyResolver(
194 proxy_config_service
,
199 proxy_service
->set_quick_check_enabled(quick_check_enabled
);
201 return proxy_service
;