blob: f7c48fb1c1222c6defe93db25ef43cc700cf2b55 [file] [log] [blame]
[email protected]7444123f2012-09-04 15:51:221// 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 Houghton582d4622018-01-22 22:43:405#include "net/proxy_resolution/proxy_config_service_ios.h"
[email protected]7444123f2012-09-04 15:51:226
7#include <CoreFoundation/CoreFoundation.h>
8#include <CFNetwork/CFProxySupport.h>
9
[email protected]7444123f2012-09-04 15:51:2210#include "base/mac/foundation_util.h"
11#include "base/mac/scoped_cftyperef.h"
[email protected]40d11e02013-03-28 17:43:1412#include "base/strings/sys_string_conversions.h"
Ramin Halavatica8d5252018-03-12 05:33:4913#include "net/proxy_resolution/proxy_config_with_annotation.h"
[email protected]7444123f2012-09-04 15:51:2214
15namespace net {
16
17namespace {
18
19const 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.
23bool 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 Halavatica8d5252018-03-12 05:33:4938void GetCurrentProxyConfig(const NetworkTrafficAnnotationTag traffic_annotation,
39 ProxyConfigWithAnnotation* config) {
[email protected]3df79f42013-06-24 18:49:0540 base::ScopedCFTypeRef<CFDictionaryRef> config_dict(
[email protected]7444123f2012-09-04 15:51:2241 CFNetworkCopySystemProxySettings());
42 DCHECK(config_dict);
Ramin Halavatica8d5252018-03-12 05:33:4943 ProxyConfig proxy_config;
[email protected]7444123f2012-09-04 15:51:2244 // 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 Halavatica8d5252018-03-12 05:33:4955 proxy_config.set_pac_url(GURL(base::SysCFStringRefToUTF8(pac_url_ref)));
[email protected]7444123f2012-09-04 15:51:2256 }
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 Halavatica8d5252018-03-12 05:33:4979 proxy_config.proxy_rules().type =
Lily Houghtone6b617e2018-01-19 20:13:0780 ProxyConfig::ProxyRules::Type::PROXY_LIST_PER_SCHEME;
Ramin Halavatica8d5252018-03-12 05:33:4981 proxy_config.proxy_rules().proxies_for_http.SetSingleProxyServer(
82 proxy_server);
[email protected]7444123f2012-09-04 15:51:2283 // Desktop Safari applies the HTTP proxy to http:// URLs only, but
84 // Mobile Safari applies the HTTP proxy to https:// URLs as well.
Ramin Halavatica8d5252018-03-12 05:33:4985 proxy_config.proxy_rules().proxies_for_https.SetSingleProxyServer(
[email protected]2189e092013-03-16 18:02:0286 proxy_server);
[email protected]7444123f2012-09-04 15:51:2287 }
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 Halavatica8d5252018-03-12 05:33:4997 *config = ProxyConfigWithAnnotation(proxy_config, traffic_annotation);
[email protected]7444123f2012-09-04 15:51:2298}
99
100} // namespace
101
Ramin Halavatica8d5252018-03-12 05:33:49102ProxyConfigServiceIOS::ProxyConfigServiceIOS(
103 const NetworkTrafficAnnotationTag& traffic_annotation)
[email protected]d6d73562012-09-06 11:05:03104 : PollingProxyConfigService(base::TimeDelta::FromSeconds(kPollIntervalSec),
Ramin Halavatica8d5252018-03-12 05:33:49105 GetCurrentProxyConfig,
106 traffic_annotation) {}
[email protected]7444123f2012-09-04 15:51:22107
108ProxyConfigServiceIOS::~ProxyConfigServiceIOS() {
[email protected]7444123f2012-09-04 15:51:22109}
110
111} // namespace net