[email protected] | 7444123f | 2012-09-04 15:51:22 | [diff] [blame] | 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 | |
Lily Houghton | 582d462 | 2018-01-22 22:43:40 | [diff] [blame] | 5 | #include "net/proxy_resolution/proxy_config_service_ios.h" |
[email protected] | 7444123f | 2012-09-04 15:51:22 | [diff] [blame] | 6 | |
| 7 | #include <CoreFoundation/CoreFoundation.h> |
| 8 | #include <CFNetwork/CFProxySupport.h> |
| 9 | |
[email protected] | 7444123f | 2012-09-04 15:51:22 | [diff] [blame] | 10 | #include "base/mac/foundation_util.h" |
| 11 | #include "base/mac/scoped_cftyperef.h" |
[email protected] | 40d11e0 | 2013-03-28 17:43:14 | [diff] [blame] | 12 | #include "base/strings/sys_string_conversions.h" |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 13 | #include "net/proxy_resolution/proxy_config_with_annotation.h" |
[email protected] | 7444123f | 2012-09-04 15:51:22 | [diff] [blame] | 14 | |
| 15 | namespace net { |
| 16 | |
| 17 | namespace { |
| 18 | |
| 19 | const int kPollIntervalSec = 10; |
| 20 | |
| 21 | // Utility function to pull out a boolean value from a dictionary and return it, |
| 22 | // returning a default value if the key is not present. |
| 23 | bool GetBoolFromDictionary(CFDictionaryRef dict, |
| 24 | CFStringRef key, |
| 25 | bool default_value) { |
| 26 | CFNumberRef number = |
| 27 | base::mac::GetValueFromDictionary<CFNumberRef>(dict, key); |
| 28 | if (!number) |
| 29 | return default_value; |
| 30 | |
| 31 | int int_value; |
| 32 | if (CFNumberGetValue(number, kCFNumberIntType, &int_value)) |
| 33 | return int_value; |
| 34 | else |
| 35 | return default_value; |
| 36 | } |
| 37 | |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 38 | void GetCurrentProxyConfig(const NetworkTrafficAnnotationTag traffic_annotation, |
| 39 | ProxyConfigWithAnnotation* config) { |
[email protected] | 3df79f4 | 2013-06-24 18:49:05 | [diff] [blame] | 40 | base::ScopedCFTypeRef<CFDictionaryRef> config_dict( |
[email protected] | 7444123f | 2012-09-04 15:51:22 | [diff] [blame] | 41 | CFNetworkCopySystemProxySettings()); |
| 42 | DCHECK(config_dict); |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 43 | ProxyConfig proxy_config; |
[email protected] | 7444123f | 2012-09-04 15:51:22 | [diff] [blame] | 44 | // Auto-detect is not supported. |
| 45 | // The kCFNetworkProxiesProxyAutoDiscoveryEnable key is not available on iOS. |
| 46 | |
| 47 | // PAC file |
| 48 | |
| 49 | if (GetBoolFromDictionary(config_dict.get(), |
| 50 | kCFNetworkProxiesProxyAutoConfigEnable, |
| 51 | false)) { |
| 52 | CFStringRef pac_url_ref = base::mac::GetValueFromDictionary<CFStringRef>( |
| 53 | config_dict.get(), kCFNetworkProxiesProxyAutoConfigURLString); |
| 54 | if (pac_url_ref) |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 55 | proxy_config.set_pac_url(GURL(base::SysCFStringRefToUTF8(pac_url_ref))); |
[email protected] | 7444123f | 2012-09-04 15:51:22 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | // Proxies (for now http). |
| 59 | |
| 60 | // The following keys are not available on iOS: |
| 61 | // kCFNetworkProxiesFTPEnable |
| 62 | // kCFNetworkProxiesFTPProxy |
| 63 | // kCFNetworkProxiesFTPPort |
| 64 | // kCFNetworkProxiesHTTPSEnable |
| 65 | // kCFNetworkProxiesHTTPSProxy |
| 66 | // kCFNetworkProxiesHTTPSPort |
| 67 | // kCFNetworkProxiesSOCKSEnable |
| 68 | // kCFNetworkProxiesSOCKSProxy |
| 69 | // kCFNetworkProxiesSOCKSPort |
| 70 | if (GetBoolFromDictionary(config_dict.get(), |
| 71 | kCFNetworkProxiesHTTPEnable, |
| 72 | false)) { |
| 73 | ProxyServer proxy_server = |
| 74 | ProxyServer::FromDictionary(ProxyServer::SCHEME_HTTP, |
| 75 | config_dict.get(), |
| 76 | kCFNetworkProxiesHTTPProxy, |
| 77 | kCFNetworkProxiesHTTPPort); |
| 78 | if (proxy_server.is_valid()) { |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 79 | proxy_config.proxy_rules().type = |
Lily Houghton | e6b617e | 2018-01-19 20:13:07 | [diff] [blame] | 80 | ProxyConfig::ProxyRules::Type::PROXY_LIST_PER_SCHEME; |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 81 | proxy_config.proxy_rules().proxies_for_http.SetSingleProxyServer( |
| 82 | proxy_server); |
[email protected] | 7444123f | 2012-09-04 15:51:22 | [diff] [blame] | 83 | // Desktop Safari applies the HTTP proxy to http:// URLs only, but |
| 84 | // Mobile Safari applies the HTTP proxy to https:// URLs as well. |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 85 | proxy_config.proxy_rules().proxies_for_https.SetSingleProxyServer( |
[email protected] | 2189e09 | 2013-03-16 18:02:02 | [diff] [blame] | 86 | proxy_server); |
[email protected] | 7444123f | 2012-09-04 15:51:22 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | |
| 90 | // Proxy bypass list is not supported. |
| 91 | // The kCFNetworkProxiesExceptionsList key is not available on iOS. |
| 92 | |
| 93 | // Proxy bypass boolean is not supported. |
| 94 | // The kCFNetworkProxiesExcludeSimpleHostnames key is not available on iOS. |
| 95 | |
| 96 | // Source |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 97 | *config = ProxyConfigWithAnnotation(proxy_config, traffic_annotation); |
[email protected] | 7444123f | 2012-09-04 15:51:22 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | } // namespace |
| 101 | |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 102 | ProxyConfigServiceIOS::ProxyConfigServiceIOS( |
| 103 | const NetworkTrafficAnnotationTag& traffic_annotation) |
[email protected] | d6d7356 | 2012-09-06 11:05:03 | [diff] [blame] | 104 | : PollingProxyConfigService(base::TimeDelta::FromSeconds(kPollIntervalSec), |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 105 | GetCurrentProxyConfig, |
| 106 | traffic_annotation) {} |
[email protected] | 7444123f | 2012-09-04 15:51:22 | [diff] [blame] | 107 | |
| 108 | ProxyConfigServiceIOS::~ProxyConfigServiceIOS() { |
[email protected] | 7444123f | 2012-09-04 15:51:22 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | } // namespace net |