1 // Copyright (c) 2011 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/string_number_conversions.h"
9 #include "chrome/browser/browser_process.h"
10 #include "chrome/browser/net/pref_proxy_config_service.h"
11 #include "chrome/browser/io_thread.h"
12 #include "chrome/common/chrome_switches.h"
13 #include "content/browser/browser_thread.h"
14 #include "net/base/net_log.h"
15 #include "net/proxy/proxy_config_service.h"
16 #include "net/proxy/proxy_script_fetcher_impl.h"
17 #include "net/url_request/url_request_context.h"
18
19 #if defined(OS_CHROMEOS)
20 #include "chrome/browser/chromeos/cros/cros_library.h"
21 #include "chrome/browser/chromeos/cros/libcros_service_library.h"
22 #include "chrome/browser/chromeos/proxy_config_service.h"
23 #endif // defined(OS_CHROMEOS)
24
25 // static
CreateProxyConfigService(PrefProxyConfigTracker * proxy_config_tracker)26 net::ProxyConfigService* ProxyServiceFactory::CreateProxyConfigService(
27 PrefProxyConfigTracker* proxy_config_tracker) {
28 // The linux gconf-based proxy settings getter relies on being initialized
29 // from the UI thread.
30 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
31
32 // Create a baseline service that provides proxy configuration in case nothing
33 // is configured through prefs (Note: prefs include command line and
34 // configuration policy).
35 net::ProxyConfigService* base_service = NULL;
36
37 // TODO(port): the IO and FILE message loops are only used by Linux. Can
38 // that code be moved to chrome/browser instead of being in net, so that it
39 // can use BrowserThread instead of raw MessageLoop pointers? See bug 25354.
40 #if defined(OS_CHROMEOS)
41 base_service = new chromeos::ProxyConfigService(
42 g_browser_process->chromeos_proxy_config_service_impl());
43 #else
44 base_service = net::ProxyService::CreateSystemProxyConfigService(
45 g_browser_process->io_thread()->message_loop(),
46 g_browser_process->file_thread()->message_loop());
47 #endif // defined(OS_CHROMEOS)
48
49 return new PrefProxyConfigService(proxy_config_tracker, base_service);
50 }
51
52 // static
CreateProxyService(net::NetLog * net_log,net::URLRequestContext * context,net::ProxyConfigService * proxy_config_service,const CommandLine & command_line)53 net::ProxyService* ProxyServiceFactory::CreateProxyService(
54 net::NetLog* net_log,
55 net::URLRequestContext* context,
56 net::ProxyConfigService* proxy_config_service,
57 const CommandLine& command_line) {
58 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
59
60 bool use_v8 = !command_line.HasSwitch(switches::kWinHttpProxyResolver);
61 if (use_v8 && command_line.HasSwitch(switches::kSingleProcess)) {
62 // See the note about V8 multithreading in net/proxy/proxy_resolver_v8.h
63 // to understand why we have this limitation.
64 LOG(ERROR) << "Cannot use V8 Proxy resolver in single process mode.";
65 use_v8 = false; // Fallback to non-v8 implementation.
66 }
67
68 size_t num_pac_threads = 0u; // Use default number of threads.
69
70 // Check the command line for an override on the number of proxy resolver
71 // threads to use.
72 if (command_line.HasSwitch(switches::kNumPacThreads)) {
73 std::string s = command_line.GetSwitchValueASCII(switches::kNumPacThreads);
74
75 // Parse the switch (it should be a positive integer formatted as decimal).
76 int n;
77 if (base::StringToInt(s, &n) && n > 0) {
78 num_pac_threads = static_cast<size_t>(n);
79 } else {
80 LOG(ERROR) << "Invalid switch for number of PAC threads: " << s;
81 }
82 }
83
84 net::ProxyService* proxy_service;
85 if (use_v8) {
86 proxy_service = net::ProxyService::CreateUsingV8ProxyResolver(
87 proxy_config_service,
88 num_pac_threads,
89 new net::ProxyScriptFetcherImpl(context),
90 context->host_resolver(),
91 net_log);
92 } else {
93 proxy_service = net::ProxyService::CreateUsingSystemProxyResolver(
94 proxy_config_service,
95 num_pac_threads,
96 net_log);
97 }
98
99 #if defined(OS_CHROMEOS)
100 if (chromeos::CrosLibrary::Get()->EnsureLoaded()) {
101 chromeos::CrosLibrary::Get()->GetLibCrosServiceLibrary()->StartService();
102 }
103 #endif // defined(OS_CHROMEOS)
104
105 return proxy_service;
106 }
107