proxy_config_service_ios.cc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. #include "net/proxy_resolution/proxy_config_service_ios.h"
  5. #include <CoreFoundation/CoreFoundation.h>
  6. #include <CFNetwork/CFProxySupport.h>
  7. #include "base/mac/foundation_util.h"
  8. #include "base/mac/scoped_cftyperef.h"
  9. #include "base/strings/sys_string_conversions.h"
  10. #include "net/base/proxy_server.h"
  11. #include "net/base/proxy_string_util.h"
  12. #include "net/proxy_resolution/proxy_config_with_annotation.h"
  13. namespace net {
  14. namespace {
  15. const int kPollIntervalSec = 10;
  16. // Utility function to pull out a boolean value from a dictionary and return it,
  17. // returning a default value if the key is not present.
  18. bool GetBoolFromDictionary(CFDictionaryRef dict,
  19. CFStringRef key,
  20. bool default_value) {
  21. CFNumberRef number =
  22. base::mac::GetValueFromDictionary<CFNumberRef>(dict, key);
  23. if (!number)
  24. return default_value;
  25. int int_value;
  26. if (CFNumberGetValue(number, kCFNumberIntType, &int_value))
  27. return int_value;
  28. else
  29. return default_value;
  30. }
  31. void GetCurrentProxyConfig(const NetworkTrafficAnnotationTag traffic_annotation,
  32. ProxyConfigWithAnnotation* config) {
  33. base::ScopedCFTypeRef<CFDictionaryRef> config_dict(
  34. CFNetworkCopySystemProxySettings());
  35. DCHECK(config_dict);
  36. ProxyConfig proxy_config;
  37. // Auto-detect is not supported.
  38. // The kCFNetworkProxiesProxyAutoDiscoveryEnable key is not available on iOS.
  39. // PAC file
  40. if (GetBoolFromDictionary(config_dict.get(),
  41. kCFNetworkProxiesProxyAutoConfigEnable,
  42. false)) {
  43. CFStringRef pac_url_ref = base::mac::GetValueFromDictionary<CFStringRef>(
  44. config_dict.get(), kCFNetworkProxiesProxyAutoConfigURLString);
  45. if (pac_url_ref)
  46. proxy_config.set_pac_url(GURL(base::SysCFStringRefToUTF8(pac_url_ref)));
  47. }
  48. // Proxies (for now http).
  49. // The following keys are not available on iOS:
  50. // kCFNetworkProxiesFTPEnable
  51. // kCFNetworkProxiesFTPProxy
  52. // kCFNetworkProxiesFTPPort
  53. // kCFNetworkProxiesHTTPSEnable
  54. // kCFNetworkProxiesHTTPSProxy
  55. // kCFNetworkProxiesHTTPSPort
  56. // kCFNetworkProxiesSOCKSEnable
  57. // kCFNetworkProxiesSOCKSProxy
  58. // kCFNetworkProxiesSOCKSPort
  59. if (GetBoolFromDictionary(config_dict.get(),
  60. kCFNetworkProxiesHTTPEnable,
  61. false)) {
  62. ProxyServer proxy_server = ProxyDictionaryToProxyServer(
  63. ProxyServer::SCHEME_HTTP, config_dict.get(), kCFNetworkProxiesHTTPProxy,
  64. kCFNetworkProxiesHTTPPort);
  65. if (proxy_server.is_valid()) {
  66. proxy_config.proxy_rules().type =
  67. ProxyConfig::ProxyRules::Type::PROXY_LIST_PER_SCHEME;
  68. proxy_config.proxy_rules().proxies_for_http.SetSingleProxyServer(
  69. proxy_server);
  70. // Desktop Safari applies the HTTP proxy to http:// URLs only, but
  71. // Mobile Safari applies the HTTP proxy to https:// URLs as well.
  72. proxy_config.proxy_rules().proxies_for_https.SetSingleProxyServer(
  73. proxy_server);
  74. }
  75. }
  76. // Proxy bypass list is not supported.
  77. // The kCFNetworkProxiesExceptionsList key is not available on iOS.
  78. // Proxy bypass boolean is not supported.
  79. // The kCFNetworkProxiesExcludeSimpleHostnames key is not available on iOS.
  80. // Source
  81. proxy_config.set_from_system(true);
  82. *config = ProxyConfigWithAnnotation(proxy_config, traffic_annotation);
  83. }
  84. } // namespace
  85. ProxyConfigServiceIOS::ProxyConfigServiceIOS(
  86. const NetworkTrafficAnnotationTag& traffic_annotation)
  87. : PollingProxyConfigService(base::Seconds(kPollIntervalSec),
  88. GetCurrentProxyConfig,
  89. traffic_annotation) {}
  90. ProxyConfigServiceIOS::~ProxyConfigServiceIOS() = default;
  91. } // namespace net