proxy_config_service.cc 5.2 KB

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