proxy_service_factory.cc revision 7dbb3d5cf0c15f500944d211057644d6a2f37371
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.
4
5#include "chrome/browser/net/proxy_service_factory.h"
6
7#include "base/command_line.h"
8#include "base/strings/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"
22
23#if defined(OS_CHROMEOS)
24#include "chrome/browser/chromeos/proxy_config_service_impl.h"
25#endif  // defined(OS_CHROMEOS)
26
27using content::BrowserThread;
28
29// static
30ChromeProxyConfigService* ProxyServiceFactory::CreateProxyConfigService() {
31  // The linux gconf-based proxy settings getter relies on being initialized
32  // from the UI thread.
33  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
34
35  net::ProxyConfigService* base_service = NULL;
36
37#if !defined(OS_CHROMEOS)
38  // On ChromeOS, base service is NULL; chromeos::ProxyConfigServiceImpl
39  // determines the effective proxy config to take effect in the network layer,
40  // be it from prefs or system (which is network shill on chromeos).
41
42  // For other platforms, create a baseline service that provides proxy
43  // configuration in case nothing is configured through prefs (Note: prefs
44  // include command line and configuration policy).
45
46  // TODO(port): the IO and FILE message loops are only used by Linux.  Can
47  // that code be moved to chrome/browser instead of being in net, so that it
48  // can use BrowserThread instead of raw MessageLoop pointers? See bug 25354.
49  base_service = net::ProxyService::CreateSystemProxyConfigService(
50      BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO).get(),
51      BrowserThread::UnsafeGetMessageLoopForThread(BrowserThread::FILE));
52#endif  // !defined(OS_CHROMEOS)
53
54  return new ChromeProxyConfigService(base_service);
55}
56
57// static
58PrefProxyConfigTracker*
59ProxyServiceFactory::CreatePrefProxyConfigTrackerOfProfile(
60    PrefService* profile_prefs,
61    PrefService* local_state_prefs) {
62#if defined(OS_CHROMEOS)
63  return new chromeos::ProxyConfigServiceImpl(profile_prefs, local_state_prefs);
64#else
65  return new PrefProxyConfigTrackerImpl(profile_prefs);
66#endif  // defined(OS_CHROMEOS)
67}
68
69// static
70PrefProxyConfigTracker*
71ProxyServiceFactory::CreatePrefProxyConfigTrackerOfLocalState(
72    PrefService* local_state_prefs) {
73#if defined(OS_CHROMEOS)
74  return new chromeos::ProxyConfigServiceImpl(NULL, local_state_prefs);
75#else
76  return new PrefProxyConfigTrackerImpl(local_state_prefs);
77#endif  // defined(OS_CHROMEOS)
78}
79
80// static
81net::ProxyService* ProxyServiceFactory::CreateProxyService(
82    net::NetLog* net_log,
83    net::URLRequestContext* context,
84    net::NetworkDelegate* network_delegate,
85    net::ProxyConfigService* proxy_config_service,
86    const CommandLine& command_line) {
87  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
88
89#if defined(OS_IOS)
90  bool use_v8 = false;
91#else
92  bool use_v8 = !command_line.HasSwitch(switches::kWinHttpProxyResolver);
93  if (use_v8 && command_line.HasSwitch(switches::kSingleProcess)) {
94    // See the note about V8 multithreading in net/proxy/proxy_resolver_v8.h
95    // to understand why we have this limitation.
96    LOG(ERROR) << "Cannot use V8 Proxy resolver in single process mode.";
97    use_v8 = false;  // Fallback to non-v8 implementation.
98  }
99#endif  // defined(OS_IOS)
100
101  size_t num_pac_threads = 0u;  // Use default number of threads.
102
103  // Check the command line for an override on the number of proxy resolver
104  // threads to use.
105  if (command_line.HasSwitch(switches::kNumPacThreads)) {
106    std::string s = command_line.GetSwitchValueASCII(switches::kNumPacThreads);
107
108    // Parse the switch (it should be a positive integer formatted as decimal).
109    int n;
110    if (base::StringToInt(s, &n) && n > 0) {
111      num_pac_threads = static_cast<size_t>(n);
112    } else {
113      LOG(ERROR) << "Invalid switch for number of PAC threads: " << s;
114    }
115  }
116
117  net::ProxyService* proxy_service = NULL;
118  if (use_v8) {
119#if defined(OS_IOS)
120    NOTREACHED();
121#else
122    net::DhcpProxyScriptFetcherFactory dhcp_factory;
123    if (command_line.HasSwitch(switches::kDisableDhcpWpad)) {
124      dhcp_factory.set_enabled(false);
125    }
126
127    proxy_service = net::CreateProxyServiceUsingV8ProxyResolver(
128        proxy_config_service,
129        new net::ProxyScriptFetcherImpl(context),
130        dhcp_factory.Create(context),
131        context->host_resolver(),
132        net_log,
133        network_delegate);
134#endif  // defined(OS_IOS)
135  } else {
136    proxy_service = net::ProxyService::CreateUsingSystemProxyResolver(
137        proxy_config_service,
138        num_pac_threads,
139        net_log);
140  }
141
142  return proxy_service;
143}
144