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.
5 #include "net/proxy/proxy_config_service_ios.h"
7 #include <CoreFoundation/CoreFoundation.h>
8 #include <CFNetwork/CFProxySupport.h>
10 #include "base/message_loop.h"
11 #include "base/mac/foundation_util.h"
12 #include "base/mac/scoped_cftyperef.h"
13 #include "base/strings/sys_string_conversions.h"
14 #include "net/proxy/proxy_config.h"
20 const int kPollIntervalSec
= 10;
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.
24 bool GetBoolFromDictionary(CFDictionaryRef dict
,
28 base::mac::GetValueFromDictionary
<CFNumberRef
>(dict
, key
);
33 if (CFNumberGetValue(number
, kCFNumberIntType
, &int_value
))
39 void GetCurrentProxyConfig(ProxyConfig
* config
) {
40 base::mac::ScopedCFTypeRef
<CFDictionaryRef
> config_dict(
41 CFNetworkCopySystemProxySettings());
44 // Auto-detect is not supported.
45 // The kCFNetworkProxiesProxyAutoDiscoveryEnable key is not available on iOS.
49 if (GetBoolFromDictionary(config_dict
.get(),
50 kCFNetworkProxiesProxyAutoConfigEnable
,
52 CFStringRef pac_url_ref
= base::mac::GetValueFromDictionary
<CFStringRef
>(
53 config_dict
.get(), kCFNetworkProxiesProxyAutoConfigURLString
);
55 config
->set_pac_url(GURL(base::SysCFStringRefToUTF8(pac_url_ref
)));
58 // Proxies (for now http).
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
,
73 ProxyServer proxy_server
=
74 ProxyServer::FromDictionary(ProxyServer::SCHEME_HTTP
,
76 kCFNetworkProxiesHTTPProxy
,
77 kCFNetworkProxiesHTTPPort
);
78 if (proxy_server
.is_valid()) {
79 config
->proxy_rules().type
=
80 ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME
;
81 config
->proxy_rules().proxies_for_http
.SetSingleProxyServer(proxy_server
);
82 // Desktop Safari applies the HTTP proxy to http:// URLs only, but
83 // Mobile Safari applies the HTTP proxy to https:// URLs as well.
84 config
->proxy_rules().proxies_for_https
.SetSingleProxyServer(
89 // Proxy bypass list is not supported.
90 // The kCFNetworkProxiesExceptionsList key is not available on iOS.
92 // Proxy bypass boolean is not supported.
93 // The kCFNetworkProxiesExcludeSimpleHostnames key is not available on iOS.
96 config
->set_source(PROXY_CONFIG_SOURCE_SYSTEM
);
101 ProxyConfigServiceIOS::ProxyConfigServiceIOS()
102 : PollingProxyConfigService(base::TimeDelta::FromSeconds(kPollIntervalSec
),
103 GetCurrentProxyConfig
) {
106 ProxyConfigServiceIOS::~ProxyConfigServiceIOS() {