15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/net/proxy_service_factory.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/command_line.h"
82a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/strings/string_number_conversions.h"
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/threading/thread.h"
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/browser_process.h"
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/io_thread.h"
12ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch#include "chrome/browser/net/pref_proxy_config_tracker_impl.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/common/chrome_switches.h"
145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/common/pref_names.h"
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/browser_thread.h"
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/base/net_log.h"
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/proxy/dhcp_proxy_script_fetcher_factory.h"
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/proxy/proxy_config_service.h"
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/proxy/proxy_script_fetcher_impl.h"
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/proxy/proxy_service.h"
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/proxy/proxy_service_v8.h"
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/url_request/url_request_context.h"
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_CHROMEOS)
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/chromeos/proxy_config_service_impl.h"
263551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)#include "chromeos/network/dhcp_proxy_script_fetcher_chromeos.h"
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // defined(OS_CHROMEOS)
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
29c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch#if !defined(OS_IOS)
30c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch#include "net/proxy/proxy_resolver_v8.h"
31c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch#endif
32c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using content::BrowserThread;
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
36ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdochnet::ProxyConfigService* ProxyServiceFactory::CreateProxyConfigService(
37ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    PrefProxyConfigTracker* tracker) {
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The linux gconf-based proxy settings getter relies on being initialized
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // from the UI thread.
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
42ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  scoped_ptr<net::ProxyConfigService> base_service;
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if !defined(OS_CHROMEOS)
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // On ChromeOS, base service is NULL; chromeos::ProxyConfigServiceImpl
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // determines the effective proxy config to take effect in the network layer,
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // be it from prefs or system (which is network shill on chromeos).
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // For other platforms, create a baseline service that provides proxy
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // configuration in case nothing is configured through prefs (Note: prefs
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // include command line and configuration policy).
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // TODO(port): the IO and FILE message loops are only used by Linux.  Can
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // that code be moved to chrome/browser instead of being in net, so that it
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // can use BrowserThread instead of raw MessageLoop pointers? See bug 25354.
56ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  base_service.reset(net::ProxyService::CreateSystemProxyConfigService(
571320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO),
581320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE)));
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // !defined(OS_CHROMEOS)
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
61ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  return tracker->CreateTrackingProxyConfigService(base_service.Pass())
62ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch      .release();
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
667dbb3d5cf0c15f500944d211057644d6a2f37371Ben MurdochPrefProxyConfigTracker*
677dbb3d5cf0c15f500944d211057644d6a2f37371Ben MurdochProxyServiceFactory::CreatePrefProxyConfigTrackerOfProfile(
687dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    PrefService* profile_prefs,
697dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    PrefService* local_state_prefs) {
707dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#if defined(OS_CHROMEOS)
717dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  return new chromeos::ProxyConfigServiceImpl(profile_prefs, local_state_prefs);
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#else
737dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  return new PrefProxyConfigTrackerImpl(profile_prefs);
747dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#endif  // defined(OS_CHROMEOS)
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
767dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
777dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch// static
787dbb3d5cf0c15f500944d211057644d6a2f37371Ben MurdochPrefProxyConfigTracker*
797dbb3d5cf0c15f500944d211057644d6a2f37371Ben MurdochProxyServiceFactory::CreatePrefProxyConfigTrackerOfLocalState(
807dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    PrefService* local_state_prefs) {
817dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#if defined(OS_CHROMEOS)
827dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  return new chromeos::ProxyConfigServiceImpl(NULL, local_state_prefs);
837dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#else
847dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  return new PrefProxyConfigTrackerImpl(local_state_prefs);
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // defined(OS_CHROMEOS)
867dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch}
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)net::ProxyService* ProxyServiceFactory::CreateProxyService(
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    net::NetLog* net_log,
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    net::URLRequestContext* context,
922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    net::NetworkDelegate* network_delegate,
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    net::ProxyConfigService* proxy_config_service,
945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const CommandLine& command_line,
955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    bool quick_check_enabled) {
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_IOS)
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool use_v8 = false;
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#else
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool use_v8 = !command_line.HasSwitch(switches::kWinHttpProxyResolver);
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (use_v8 && command_line.HasSwitch(switches::kSingleProcess)) {
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // See the note about V8 multithreading in net/proxy/proxy_resolver_v8.h
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // to understand why we have this limitation.
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    LOG(ERROR) << "Cannot use V8 Proxy resolver in single process mode.";
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    use_v8 = false;  // Fallback to non-v8 implementation.
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // defined(OS_IOS)
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  size_t num_pac_threads = 0u;  // Use default number of threads.
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Check the command line for an override on the number of proxy resolver
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // threads to use.
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (command_line.HasSwitch(switches::kNumPacThreads)) {
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    std::string s = command_line.GetSwitchValueASCII(switches::kNumPacThreads);
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Parse the switch (it should be a positive integer formatted as decimal).
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    int n;
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (base::StringToInt(s, &n) && n > 0) {
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      num_pac_threads = static_cast<size_t>(n);
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    } else {
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      LOG(ERROR) << "Invalid switch for number of PAC threads: " << s;
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  net::ProxyService* proxy_service = NULL;
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (use_v8) {
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_IOS)
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    NOTREACHED();
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#else
131c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    net::ProxyResolverV8::EnsureIsolateCreated();
132c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
1333551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    net::DhcpProxyScriptFetcher* dhcp_proxy_script_fetcher;
1343551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)#if defined(OS_CHROMEOS)
1353551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    dhcp_proxy_script_fetcher =
1363551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)        new chromeos::DhcpProxyScriptFetcherChromeos(context);
1373551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)#else
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    net::DhcpProxyScriptFetcherFactory dhcp_factory;
1393551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    dhcp_proxy_script_fetcher = dhcp_factory.Create(context);
1403551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)#endif
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    proxy_service = net::CreateProxyServiceUsingV8ProxyResolver(
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        proxy_config_service,
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        new net::ProxyScriptFetcherImpl(context),
1453551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)        dhcp_proxy_script_fetcher,
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        context->host_resolver(),
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        net_log,
1482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        network_delegate);
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // defined(OS_IOS)
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  } else {
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    proxy_service = net::ProxyService::CreateUsingSystemProxyResolver(
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        proxy_config_service,
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        num_pac_threads,
1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        net_log);
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  proxy_service->set_quick_check_enabled(quick_check_enabled);
1585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return proxy_service;
1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
161