proxy_service_factory.cc revision ca12bfac764ba476d6cd062bf1dde12cc64c3f40
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_impl.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 30net::ProxyConfigService* ProxyServiceFactory::CreateProxyConfigService( 31 PrefProxyConfigTracker* tracker) { 32 // The linux gconf-based proxy settings getter relies on being initialized 33 // from the UI thread. 34 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 35 36 scoped_ptr<net::ProxyConfigService> base_service; 37 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). 42 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). 46 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.reset(net::ProxyService::CreateSystemProxyConfigService( 51 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO).get(), 52 BrowserThread::UnsafeGetMessageLoopForThread(BrowserThread::FILE))); 53#endif // !defined(OS_CHROMEOS) 54 55 return tracker->CreateTrackingProxyConfigService(base_service.Pass()) 56 .release(); 57} 58 59// static 60PrefProxyConfigTracker* 61ProxyServiceFactory::CreatePrefProxyConfigTrackerOfProfile( 62 PrefService* profile_prefs, 63 PrefService* local_state_prefs) { 64#if defined(OS_CHROMEOS) 65 return new chromeos::ProxyConfigServiceImpl(profile_prefs, local_state_prefs); 66#else 67 return new PrefProxyConfigTrackerImpl(profile_prefs); 68#endif // defined(OS_CHROMEOS) 69} 70 71// static 72PrefProxyConfigTracker* 73ProxyServiceFactory::CreatePrefProxyConfigTrackerOfLocalState( 74 PrefService* local_state_prefs) { 75#if defined(OS_CHROMEOS) 76 return new chromeos::ProxyConfigServiceImpl(NULL, local_state_prefs); 77#else 78 return new PrefProxyConfigTrackerImpl(local_state_prefs); 79#endif // defined(OS_CHROMEOS) 80} 81 82// static 83net::ProxyService* ProxyServiceFactory::CreateProxyService( 84 net::NetLog* net_log, 85 net::URLRequestContext* context, 86 net::NetworkDelegate* network_delegate, 87 net::ProxyConfigService* proxy_config_service, 88 const CommandLine& command_line) { 89 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 90 91#if defined(OS_IOS) 92 bool use_v8 = false; 93#else 94 bool use_v8 = !command_line.HasSwitch(switches::kWinHttpProxyResolver); 95 if (use_v8 && command_line.HasSwitch(switches::kSingleProcess)) { 96 // See the note about V8 multithreading in net/proxy/proxy_resolver_v8.h 97 // to understand why we have this limitation. 98 LOG(ERROR) << "Cannot use V8 Proxy resolver in single process mode."; 99 use_v8 = false; // Fallback to non-v8 implementation. 100 } 101#endif // defined(OS_IOS) 102 103 size_t num_pac_threads = 0u; // Use default number of threads. 104 105 // Check the command line for an override on the number of proxy resolver 106 // threads to use. 107 if (command_line.HasSwitch(switches::kNumPacThreads)) { 108 std::string s = command_line.GetSwitchValueASCII(switches::kNumPacThreads); 109 110 // Parse the switch (it should be a positive integer formatted as decimal). 111 int n; 112 if (base::StringToInt(s, &n) && n > 0) { 113 num_pac_threads = static_cast<size_t>(n); 114 } else { 115 LOG(ERROR) << "Invalid switch for number of PAC threads: " << s; 116 } 117 } 118 119 net::ProxyService* proxy_service = NULL; 120 if (use_v8) { 121#if defined(OS_IOS) 122 NOTREACHED(); 123#else 124 net::DhcpProxyScriptFetcherFactory dhcp_factory; 125 if (command_line.HasSwitch(switches::kDisableDhcpWpad)) { 126 dhcp_factory.set_enabled(false); 127 } 128 129 proxy_service = net::CreateProxyServiceUsingV8ProxyResolver( 130 proxy_config_service, 131 new net::ProxyScriptFetcherImpl(context), 132 dhcp_factory.Create(context), 133 context->host_resolver(), 134 net_log, 135 network_delegate); 136#endif // defined(OS_IOS) 137 } else { 138 proxy_service = net::ProxyService::CreateUsingSystemProxyResolver( 139 proxy_config_service, 140 num_pac_threads, 141 net_log); 142 } 143 144 return proxy_service; 145} 146