Fix iOS build for XCode 4.6.
[chromium-blink-merge.git] / chrome / browser / net / proxy_service_factory.cc
blob06046a76fdee91bdc5d7025b8fec40925e8fec96
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"
7 #include "base/command_line.h"
8 #include "base/string_number_conversions.h"
9 #include "base/threading/thread.h"
10 #include "chrome/browser/browser_process.h"
11 #include "chrome/browser/io_thread.h"
12 #include "chrome/browser/net/pref_proxy_config_tracker.h"
13 #include "chrome/common/chrome_switches.h"
14 #include "content/public/browser/browser_thread.h"
15 #include "net/base/net_log.h"
16 #include "net/proxy/dhcp_proxy_script_fetcher_factory.h"
17 #include "net/proxy/proxy_config_service.h"
18 #include "net/proxy/proxy_script_fetcher_impl.h"
19 #include "net/proxy/proxy_service.h"
20 #include "net/proxy/proxy_service_v8.h"
21 #include "net/url_request/url_request_context.h"
23 #if defined(OS_CHROMEOS)
24 #include "chrome/browser/chromeos/proxy_config_service_impl.h"
25 #endif // defined(OS_CHROMEOS)
27 using content::BrowserThread;
29 // static
30 ChromeProxyConfigService* ProxyServiceFactory::CreateProxyConfigService(
31 bool wait_for_first_update) {
32 // The linux gconf-based proxy settings getter relies on being initialized
33 // from the UI thread.
34 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
36 net::ProxyConfigService* base_service = NULL;
38 #if !defined(OS_CHROMEOS)
39 // On ChromeOS, base service is NULL; chromeos::ProxyConfigServiceImpl
40 // determines the effective proxy config to take effect in the network layer,
41 // be it from prefs or system (which is network shill on chromeos).
43 // For other platforms, create a baseline service that provides proxy
44 // configuration in case nothing is configured through prefs (Note: prefs
45 // include command line and configuration policy).
47 // TODO(port): the IO and FILE message loops are only used by Linux. Can
48 // that code be moved to chrome/browser instead of being in net, so that it
49 // can use BrowserThread instead of raw MessageLoop pointers? See bug 25354.
50 base_service = net::ProxyService::CreateSystemProxyConfigService(
51 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO),
52 BrowserThread::UnsafeGetMessageLoopForThread(BrowserThread::FILE));
53 #endif // !defined(OS_CHROMEOS)
55 return new ChromeProxyConfigService(base_service, wait_for_first_update);
58 #if defined(OS_CHROMEOS)
59 // static
60 chromeos::ProxyConfigServiceImpl*
61 ProxyServiceFactory::CreatePrefProxyConfigTracker(
62 PrefService* pref_service) {
63 return new chromeos::ProxyConfigServiceImpl(pref_service);
65 #else
66 // static
67 PrefProxyConfigTrackerImpl* ProxyServiceFactory::CreatePrefProxyConfigTracker(
68 PrefService* pref_service) {
69 return new PrefProxyConfigTrackerImpl(pref_service);
71 #endif // defined(OS_CHROMEOS)
73 // static
74 net::ProxyService* ProxyServiceFactory::CreateProxyService(
75 net::NetLog* net_log,
76 net::URLRequestContext* context,
77 net::ProxyConfigService* proxy_config_service,
78 const CommandLine& command_line) {
79 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
81 #if defined(OS_IOS)
82 bool use_v8 = false;
83 #else
84 bool use_v8 = !command_line.HasSwitch(switches::kWinHttpProxyResolver);
85 if (use_v8 && command_line.HasSwitch(switches::kSingleProcess)) {
86 // See the note about V8 multithreading in net/proxy/proxy_resolver_v8.h
87 // to understand why we have this limitation.
88 LOG(ERROR) << "Cannot use V8 Proxy resolver in single process mode.";
89 use_v8 = false; // Fallback to non-v8 implementation.
91 #endif // defined(OS_IOS)
93 size_t num_pac_threads = 0u; // Use default number of threads.
95 // Check the command line for an override on the number of proxy resolver
96 // threads to use.
97 if (command_line.HasSwitch(switches::kNumPacThreads)) {
98 std::string s = command_line.GetSwitchValueASCII(switches::kNumPacThreads);
100 // Parse the switch (it should be a positive integer formatted as decimal).
101 int n;
102 if (base::StringToInt(s, &n) && n > 0) {
103 num_pac_threads = static_cast<size_t>(n);
104 } else {
105 LOG(ERROR) << "Invalid switch for number of PAC threads: " << s;
109 net::ProxyService* proxy_service = NULL;
110 if (use_v8) {
111 #if defined(OS_IOS)
112 NOTREACHED();
113 #else
114 net::DhcpProxyScriptFetcherFactory dhcp_factory;
115 if (command_line.HasSwitch(switches::kDisableDhcpWpad)) {
116 dhcp_factory.set_enabled(false);
119 proxy_service = net::CreateProxyServiceUsingV8ProxyResolver(
120 proxy_config_service,
121 num_pac_threads,
122 new net::ProxyScriptFetcherImpl(context),
123 dhcp_factory.Create(context),
124 context->host_resolver(),
125 net_log,
126 context->network_delegate());
127 #endif // defined(OS_IOS)
128 } else {
129 proxy_service = net::ProxyService::CreateUsingSystemProxyResolver(
130 proxy_config_service,
131 num_pac_threads,
132 net_log);
135 return proxy_service;