blob: 14c1f905520dce27fd96cc1b69fda3d48d829b86 [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]18b577412013-07-18 04:19:1512#include "base/message_loop/message_loop.h"
[email protected]40d11e02013-03-28 17:43:1413#include "base/strings/sys_string_conversions.h"
Ramin Halavatica8d5252018-03-12 05:33:4914#include "net/proxy_resolution/proxy_config_with_annotation.h"
[email protected]7444123f2012-09-04 15:51:2215
16namespace net {
17
18namespace {
19
20const int kPollIntervalSec = 10;
21
22// Utility function to pull out a boolean value from a dictionary and return it,
23// returning a default value if the key is not present.
24bool GetBoolFromDictionary(CFDictionaryRef dict,
25 CFStringRef key,
26 bool default_value) {
27 CFNumberRef number =
28 base::mac::GetValueFromDictionary<CFNumberRef>(dict, key);
29 if (!number)
30 return default_value;
31
32 int int_value;
33 if (CFNumberGetValue(number, kCFNumberIntType, &int_value))
34 return int_value;
35 else
36 return default_value;
37}
38
Ramin Halavatica8d5252018-03-12 05:33:4939void GetCurrentProxyConfig(const NetworkTrafficAnnotationTag traffic_annotation,
40 ProxyConfigWithAnnotation* config) {
[email protected]3df79f42013-06-24 18:49:0541 base::ScopedCFTypeRef<CFDictionaryRef> config_dict(
[email protected]7444123f2012-09-04 15:51:2242 CFNetworkCopySystemProxySettings());
43 DCHECK(config_dict);
Ramin Halavatica8d5252018-03-12 05:33:4944 ProxyConfig proxy_config;
[email protected]7444123f2012-09-04 15:51:2245 // Auto-detect is not supported.
46 // The kCFNetworkProxiesProxyAutoDiscoveryEnable key is not available on iOS.
47
48 // PAC file
49
50 if (GetBoolFromDictionary(config_dict.get(),
51 kCFNetworkProxiesProxyAutoConfigEnable,
52 false)) {
53 CFStringRef pac_url_ref = base::mac::GetValueFromDictionary<CFStringRef>(
54 config_dict.get(), kCFNetworkProxiesProxyAutoConfigURLString);
55 if (pac_url_ref)
Ramin Halavatica8d5252018-03-12 05:33:4956 proxy_config.set_pac_url(GURL(base::SysCFStringRefToUTF8(pac_url_ref)));
[email protected]7444123f2012-09-04 15:51:2257 }
58
59 // Proxies (for now http).
60
61 // The following keys are not available on iOS:
62 // kCFNetworkProxiesFTPEnable
63 // kCFNetworkProxiesFTPProxy
64 // kCFNetworkProxiesFTPPort
65 // kCFNetworkProxiesHTTPSEnable
66 // kCFNetworkProxiesHTTPSProxy
67 // kCFNetworkProxiesHTTPSPort
68 // kCFNetworkProxiesSOCKSEnable
69 // kCFNetworkProxiesSOCKSProxy
70 // kCFNetworkProxiesSOCKSPort
71 if (GetBoolFromDictionary(config_dict.get(),
72 kCFNetworkProxiesHTTPEnable,
73 false)) {
74 ProxyServer proxy_server =
75 ProxyServer::FromDictionary(ProxyServer::SCHEME_HTTP,
76 config_dict.get(),
77 kCFNetworkProxiesHTTPProxy,
78 kCFNetworkProxiesHTTPPort);
79 if (proxy_server.is_valid()) {
Ramin Halavatica8d5252018-03-12 05:33:4980 proxy_config.proxy_rules().type =
Lily Houghtone6b617e2018-01-19 20:13:0781 ProxyConfig::ProxyRules::Type::PROXY_LIST_PER_SCHEME;
Ramin Halavatica8d5252018-03-12 05:33:4982 proxy_config.proxy_rules().proxies_for_http.SetSingleProxyServer(
83 proxy_server);
[email protected]7444123f2012-09-04 15:51:2284 // Desktop Safari applies the HTTP proxy to http:// URLs only, but
85 // Mobile Safari applies the HTTP proxy to https:// URLs as well.
Ramin Halavatica8d5252018-03-12 05:33:4986 proxy_config.proxy_rules().proxies_for_https.SetSingleProxyServer(
[email protected]2189e092013-03-16 18:02:0287 proxy_server);
[email protected]7444123f2012-09-04 15:51:2288 }
89 }
90
91 // Proxy bypass list is not supported.
92 // The kCFNetworkProxiesExceptionsList key is not available on iOS.
93
94 // Proxy bypass boolean is not supported.
95 // The kCFNetworkProxiesExcludeSimpleHostnames key is not available on iOS.
96
97 // Source
Ramin Halavatica8d5252018-03-12 05:33:4998 *config = ProxyConfigWithAnnotation(proxy_config, traffic_annotation);
[email protected]7444123f2012-09-04 15:51:2299}
100
101} // namespace
102
Ramin Halavatica8d5252018-03-12 05:33:49103ProxyConfigServiceIOS::ProxyConfigServiceIOS(
104 const NetworkTrafficAnnotationTag& traffic_annotation)
[email protected]d6d73562012-09-06 11:05:03105 : PollingProxyConfigService(base::TimeDelta::FromSeconds(kPollIntervalSec),
Ramin Halavatica8d5252018-03-12 05:33:49106 GetCurrentProxyConfig,
107 traffic_annotation) {}
[email protected]7444123f2012-09-04 15:51:22108
109ProxyConfigServiceIOS::~ProxyConfigServiceIOS() {
[email protected]7444123f2012-09-04 15:51:22110}
111
112} // namespace net