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/strings/string_number_conversions.h"
11 #include "base/threading/thread.h"
12 #include "chrome/browser/browser_process.h"
13 #include "chrome/browser/io_thread.h"
14 #include "chrome/browser/net/pref_proxy_config_tracker_impl.h"
15 #include "chrome/common/chrome_switches.h"
16 #include "content/public/browser/browser_thread.h"
17 #include "net/log/net_log.h"
18 #include "net/proxy/dhcp_proxy_script_fetcher_factory.h"
19 #include "net/proxy/proxy_config_service.h"
20 #include "net/proxy/proxy_script_fetcher_impl.h"
21 #include "net/proxy/proxy_service.h"
22 #include "net/proxy/proxy_service_v8.h"
23 #include "net/url_request/url_request_context.h"
25 #if defined(OS_CHROMEOS)
26 #include "chrome/browser/chromeos/proxy_config_service_impl.h"
27 #include "chromeos/network/dhcp_proxy_script_fetcher_chromeos.h"
28 #endif // defined(OS_CHROMEOS)
31 #include "net/proxy/proxy_resolver_v8.h"
34 #if !defined(OS_IOS) && !defined(OS_ANDROID)
35 #include "chrome/browser/net/utility_process_mojo_proxy_resolver_factory.h"
36 #include "net/proxy/proxy_service_mojo.h"
39 using content::BrowserThread
;
42 net::ProxyConfigService
* ProxyServiceFactory::CreateProxyConfigService(
43 PrefProxyConfigTracker
* tracker
) {
44 // The linux gconf-based proxy settings getter relies on being initialized
45 // from the UI thread.
46 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
48 scoped_ptr
<net::ProxyConfigService
> base_service
;
50 #if !defined(OS_CHROMEOS)
51 // On ChromeOS, base service is NULL; chromeos::ProxyConfigServiceImpl
52 // determines the effective proxy config to take effect in the network layer,
53 // be it from prefs or system (which is network shill on chromeos).
55 // For other platforms, create a baseline service that provides proxy
56 // configuration in case nothing is configured through prefs (Note: prefs
57 // include command line and configuration policy).
59 // TODO(port): the IO and FILE message loops are only used by Linux. Can
60 // that code be moved to chrome/browser instead of being in net, so that it
61 // can use BrowserThread instead of raw MessageLoop pointers? See bug 25354.
62 base_service
.reset(net::ProxyService::CreateSystemProxyConfigService(
63 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO
),
64 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE)));
65 #endif // !defined(OS_CHROMEOS)
67 return tracker
->CreateTrackingProxyConfigService(base_service
.Pass())
72 PrefProxyConfigTracker
*
73 ProxyServiceFactory::CreatePrefProxyConfigTrackerOfProfile(
74 PrefService
* profile_prefs
,
75 PrefService
* local_state_prefs
) {
76 #if defined(OS_CHROMEOS)
77 return new chromeos::ProxyConfigServiceImpl(profile_prefs
, local_state_prefs
);
79 return new PrefProxyConfigTrackerImpl(profile_prefs
);
80 #endif // defined(OS_CHROMEOS)
84 PrefProxyConfigTracker
*
85 ProxyServiceFactory::CreatePrefProxyConfigTrackerOfLocalState(
86 PrefService
* local_state_prefs
) {
87 #if defined(OS_CHROMEOS)
88 return new chromeos::ProxyConfigServiceImpl(NULL
, local_state_prefs
);
90 return new PrefProxyConfigTrackerImpl(local_state_prefs
);
91 #endif // defined(OS_CHROMEOS)
95 net::ProxyService
* ProxyServiceFactory::CreateProxyService(
97 net::URLRequestContext
* context
,
98 net::NetworkDelegate
* network_delegate
,
99 net::ProxyConfigService
* proxy_config_service
,
100 const base::CommandLine
& command_line
,
101 bool quick_check_enabled
) {
102 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
106 bool use_v8
= !command_line
.HasSwitch(switches::kWinHttpProxyResolver
);
107 // TODO(eroman): Figure out why this doesn't work in single-process mode.
108 // Should be possible now that a private isolate is used.
109 // http://crbug.com/474654
110 if (use_v8
&& command_line
.HasSwitch(switches::kSingleProcess
)) {
111 LOG(ERROR
) << "Cannot use V8 Proxy resolver in single process mode.";
112 use_v8
= false; // Fallback to non-v8 implementation.
114 #endif // defined(OS_IOS)
116 size_t num_pac_threads
= 0u; // Use default number of threads.
118 // Check the command line for an override on the number of proxy resolver
120 if (command_line
.HasSwitch(switches::kNumPacThreads
)) {
121 std::string s
= command_line
.GetSwitchValueASCII(switches::kNumPacThreads
);
123 // Parse the switch (it should be a positive integer formatted as decimal).
125 if (base::StringToInt(s
, &n
) && n
> 0) {
126 num_pac_threads
= static_cast<size_t>(n
);
128 LOG(ERROR
) << "Invalid switch for number of PAC threads: " << s
;
132 net::ProxyService
* proxy_service
= NULL
;
137 net::DhcpProxyScriptFetcher
* dhcp_proxy_script_fetcher
;
138 #if defined(OS_CHROMEOS)
139 dhcp_proxy_script_fetcher
=
140 new chromeos::DhcpProxyScriptFetcherChromeos(context
);
142 net::DhcpProxyScriptFetcherFactory dhcp_factory
;
143 dhcp_proxy_script_fetcher
= dhcp_factory
.Create(context
);
146 #if !defined(OS_ANDROID)
147 if (command_line
.HasSwitch(switches::kV8PacMojoOutOfProcess
)) {
148 proxy_service
= net::CreateProxyServiceUsingMojoFactory(
149 UtilityProcessMojoProxyResolverFactory::GetInstance(),
150 proxy_config_service
, new net::ProxyScriptFetcherImpl(context
),
151 dhcp_proxy_script_fetcher
, context
->host_resolver(), net_log
,
153 } else if (command_line
.HasSwitch(switches::kV8PacMojoInProcess
)) {
154 proxy_service
= net::CreateProxyServiceUsingMojoInProcess(
155 proxy_config_service
, new net::ProxyScriptFetcherImpl(context
),
156 dhcp_proxy_script_fetcher
, context
->host_resolver(), net_log
,
159 #endif // !defined(OS_ANDROID)
161 if (!proxy_service
) {
162 proxy_service
= net::CreateProxyServiceUsingV8ProxyResolver(
163 proxy_config_service
, new net::ProxyScriptFetcherImpl(context
),
164 dhcp_proxy_script_fetcher
, context
->host_resolver(), net_log
,
167 #endif // defined(OS_IOS)
169 proxy_service
= net::ProxyService::CreateUsingSystemProxyResolver(
170 proxy_config_service
,
175 proxy_service
->set_quick_check_enabled(quick_check_enabled
);
177 return proxy_service
;