1 // Copyright 2022 The Chromium Authors 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 "net/proxy_resolution/proxy_config_service.h" 6 7 #include <memory> 8 9 #include "base/logging.h" 10 #include "base/memory/scoped_refptr.h" 11 #include "base/task/sequenced_task_runner.h" 12 #include "base/task/single_thread_task_runner.h" 13 #include "build/build_config.h" 14 #include "net/proxy_resolution/proxy_config_with_annotation.h" 15 16 #if BUILDFLAG(IS_WIN) 17 #include "net/proxy_resolution/win/proxy_config_service_win.h" 18 #elif BUILDFLAG(IS_IOS) 19 #include "net/proxy_resolution/proxy_config_service_ios.h" 20 #elif BUILDFLAG(IS_MAC) 21 #include "net/proxy_resolution/proxy_config_service_mac.h" 22 #elif BUILDFLAG(IS_LINUX) 23 #include "net/proxy_resolution/proxy_config_service_linux.h" 24 #elif BUILDFLAG(IS_ANDROID) 25 #include "net/proxy_resolution/proxy_config_service_android.h" 26 #endif 27 28 #if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_APPLE) || BUILDFLAG(IS_LINUX) 29 #include "net/traffic_annotation/network_traffic_annotation.h" 30 #endif 31 32 namespace net { 33 34 namespace { 35 #if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_APPLE) || BUILDFLAG(IS_LINUX) 36 constexpr net::NetworkTrafficAnnotationTag kSystemProxyConfigTrafficAnnotation = 37 net::DefineNetworkTrafficAnnotation("proxy_config_system", R"( 38 semantics { 39 sender: "Proxy Config" 40 description: 41 "Establishing a connection through a proxy server using system proxy " 42 "settings." 43 trigger: 44 "Whenever a network request is made when the system proxy settings " 45 "are used, and they indicate to use a proxy server." 46 data: 47 "Proxy configuration." 48 destination: OTHER 49 destination_other: 50 "The proxy server specified in the configuration." 51 } 52 policy { 53 cookies_allowed: NO 54 setting: 55 "User cannot override system proxy settings, but can change them " 56 "through 'Advanced/System/Open proxy settings'." 57 policy_exception_justification: 58 "Using 'ProxySettings' policy can set Chrome to use specific " 59 "proxy settings and avoid system proxy." 60 })"); 61 #endif 62 63 #if BUILDFLAG(IS_CHROMEOS_ASH) 64 class UnsetProxyConfigService : public ProxyConfigService { 65 public: 66 UnsetProxyConfigService() = default; 67 ~UnsetProxyConfigService() override = default; 68 AddObserver(Observer * observer)69 void AddObserver(Observer* observer) override {} RemoveObserver(Observer * observer)70 void RemoveObserver(Observer* observer) override {} GetLatestProxyConfig(ProxyConfigWithAnnotation * config)71 ConfigAvailability GetLatestProxyConfig( 72 ProxyConfigWithAnnotation* config) override { 73 return CONFIG_UNSET; 74 } 75 }; 76 #endif 77 78 // Config getter that always returns direct settings. 79 class ProxyConfigServiceDirect : public ProxyConfigService { 80 public: 81 // ProxyConfigService implementation: AddObserver(Observer * observer)82 void AddObserver(Observer* observer) override {} RemoveObserver(Observer * observer)83 void RemoveObserver(Observer* observer) override {} GetLatestProxyConfig(ProxyConfigWithAnnotation * config)84 ConfigAvailability GetLatestProxyConfig( 85 ProxyConfigWithAnnotation* config) override { 86 *config = ProxyConfigWithAnnotation::CreateDirect(); 87 return CONFIG_VALID; 88 } 89 }; 90 91 } // namespace 92 93 // static 94 std::unique_ptr<ProxyConfigService> CreateSystemProxyConfigService(scoped_refptr<base::SequencedTaskRunner> main_task_runner)95ProxyConfigService::CreateSystemProxyConfigService( 96 scoped_refptr<base::SequencedTaskRunner> main_task_runner) { 97 #if BUILDFLAG(IS_WIN) 98 return std::make_unique<ProxyConfigServiceWin>( 99 kSystemProxyConfigTrafficAnnotation); 100 #elif BUILDFLAG(IS_IOS) 101 return std::make_unique<ProxyConfigServiceIOS>( 102 kSystemProxyConfigTrafficAnnotation); 103 #elif BUILDFLAG(IS_MAC) 104 return std::make_unique<ProxyConfigServiceMac>( 105 std::move(main_task_runner), kSystemProxyConfigTrafficAnnotation); 106 #elif BUILDFLAG(IS_CHROMEOS_ASH) 107 LOG(ERROR) << "ProxyConfigService for ChromeOS should be created in " 108 << "profile_io_data.cc::CreateProxyConfigService and this should " 109 << "be used only for examples."; 110 return std::make_unique<UnsetProxyConfigService>(); 111 #elif BUILDFLAG(IS_LINUX) 112 std::unique_ptr<ProxyConfigServiceLinux> linux_config_service( 113 std::make_unique<ProxyConfigServiceLinux>()); 114 115 // Assume we got called on the thread that runs the default glib 116 // main loop, so the current thread is where we should be running 117 // gsettings calls from. 118 scoped_refptr<base::SingleThreadTaskRunner> glib_thread_task_runner = 119 base::SingleThreadTaskRunner::GetCurrentDefault(); 120 121 // Synchronously fetch the current proxy config (since we are running on 122 // glib_default_loop). Additionally register for notifications (delivered in 123 // either |glib_default_loop| or an internal sequenced task runner) to 124 // keep us updated when the proxy config changes. 125 linux_config_service->SetupAndFetchInitialConfig( 126 glib_thread_task_runner, std::move(main_task_runner), 127 kSystemProxyConfigTrafficAnnotation); 128 129 return std::move(linux_config_service); 130 #elif BUILDFLAG(IS_ANDROID) 131 return std::make_unique<ProxyConfigServiceAndroid>( 132 std::move(main_task_runner), 133 base::SingleThreadTaskRunner::GetCurrentDefault()); 134 #elif BUILDFLAG(IS_FUCHSIA) 135 // TODO(crbug.com/889195): Implement a system proxy service for Fuchsia. 136 return std::make_unique<ProxyConfigServiceDirect>(); 137 #else 138 LOG(WARNING) << "Failed to choose a system proxy settings fetcher " 139 "for this platform."; 140 return std::make_unique<ProxyConfigServiceDirect>(); 141 #endif 142 } 143 UsesPolling()144bool ProxyConfigService::UsesPolling() { 145 return false; 146 } 147 148 } // namespace net 149