[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 | |
| 5 | #include "net/proxy/proxy_config_service_ios.h" |
| 6 | |
| 7 | #include <CoreFoundation/CoreFoundation.h> |
| 8 | #include <CFNetwork/CFProxySupport.h> |
| 9 | |
| 10 | #include "base/message_loop.h" |
| 11 | #include "base/mac/foundation_util.h" |
| 12 | #include "base/mac/scoped_cftyperef.h" |
| 13 | #include "base/sys_string_conversions.h" |
| 14 | #include "net/proxy/proxy_config.h" |
| 15 | |
| 16 | namespace net { |
| 17 | |
| 18 | namespace { |
| 19 | |
| 20 | const 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. |
| 24 | bool 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 | |
| 39 | void GetCurrentProxyConfig(ProxyConfig* config) { |
| 40 | base::mac::ScopedCFTypeRef<CFDictionaryRef> config_dict( |
| 41 | CFNetworkCopySystemProxySettings()); |
| 42 | DCHECK(config_dict); |
| 43 | |
| 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) |
| 55 | config->set_pac_url(GURL(base::SysCFStringRefToUTF8(pac_url_ref))); |
| 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()) { |
| 79 | config->proxy_rules().type = |
| 80 | ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME; |
| 81 | config->proxy_rules().proxy_for_http = 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().proxy_for_https = proxy_server; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | // Proxy bypass list is not supported. |
| 89 | // The kCFNetworkProxiesExceptionsList key is not available on iOS. |
| 90 | |
| 91 | // Proxy bypass boolean is not supported. |
| 92 | // The kCFNetworkProxiesExcludeSimpleHostnames key is not available on iOS. |
| 93 | |
| 94 | // Source |
| 95 | config->set_source(PROXY_CONFIG_SOURCE_SYSTEM); |
| 96 | } |
| 97 | |
| 98 | } // namespace |
| 99 | |
| 100 | ProxyConfigServiceIOS::ProxyConfigServiceIOS( |
| 101 | base::SingleThreadTaskRunner* io_thread_task_runner) |
| 102 | : PollingProxyConfigService( |
| 103 | base::TimeDelta::FromSeconds(kPollIntervalSec), |
| 104 | GetCurrentProxyConfig), |
| 105 | io_thread_task_runner_(io_thread_task_runner) { |
| 106 | DCHECK(io_thread_task_runner_); |
| 107 | } |
| 108 | |
| 109 | ProxyConfigServiceIOS::~ProxyConfigServiceIOS() { |
| 110 | DCHECK(io_thread_task_runner_->BelongsToCurrentThread()); |
| 111 | } |
| 112 | |
| 113 | } // namespace net |