| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/proxy/proxy_config_service_mac.h" | 5 #include "net/proxy/proxy_config_service_mac.h" |
| 6 | 6 |
| 7 #include <CoreFoundation/CoreFoundation.h> | 7 #include <CoreFoundation/CoreFoundation.h> |
| 8 #include <SystemConfiguration/SystemConfiguration.h> | 8 #include <SystemConfiguration/SystemConfiguration.h> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/mac/foundation_util.h" | 12 #include "base/mac/foundation_util.h" |
| 13 #include "base/mac/scoped_cftyperef.h" | 13 #include "base/mac/scoped_cftyperef.h" |
| 14 #include "base/message_loop/message_loop.h" | 14 #include "base/message_loop/message_loop.h" |
| 15 #include "base/strings/sys_string_conversions.h" | 15 #include "base/strings/sys_string_conversions.h" |
| 16 #include "net/base/net_errors.h" | 16 #include "net/base/net_errors.h" |
| 17 #include "net/proxy/proxy_config.h" | 17 #include "net/proxy/proxy_config.h" |
| 18 #include "net/proxy/proxy_info.h" | 18 #include "net/proxy/proxy_info.h" |
| 19 #include "net/proxy/proxy_server.h" | 19 #include "net/proxy/proxy_server.h" |
| 20 | 20 |
| 21 namespace net { | 21 namespace net { |
| 22 | 22 |
| 23 namespace { | 23 namespace { |
| 24 | 24 |
| 25 // Utility function to pull out a boolean value from a dictionary and return it, | 25 // Utility function to pull out a boolean value from a dictionary and return it, |
| 26 // returning a default value if the key is not present. | 26 // returning a default value if the key is not present. |
| 27 bool GetBoolFromDictionary(CFDictionaryRef dict, | 27 bool GetBoolFromDictionary(CFDictionaryRef dict, |
| 28 CFStringRef key, | 28 CFStringRef key, |
| 29 bool default_value) { | 29 bool default_value) { |
| 30 CFNumberRef number = base::mac::GetValueFromDictionary<CFNumberRef>(dict, | 30 CFNumberRef number = |
| 31 key); | 31 base::mac::GetValueFromDictionary<CFNumberRef>(dict, key); |
| 32 if (!number) | 32 if (!number) |
| 33 return default_value; | 33 return default_value; |
| 34 | 34 |
| 35 int int_value; | 35 int int_value; |
| 36 if (CFNumberGetValue(number, kCFNumberIntType, &int_value)) | 36 if (CFNumberGetValue(number, kCFNumberIntType, &int_value)) |
| 37 return int_value; | 37 return int_value; |
| 38 else | 38 else |
| 39 return default_value; | 39 return default_value; |
| 40 } | 40 } |
| 41 | 41 |
| 42 void GetCurrentProxyConfig(ProxyConfig* config) { | 42 void GetCurrentProxyConfig(ProxyConfig* config) { |
| 43 base::ScopedCFTypeRef<CFDictionaryRef> config_dict( | 43 base::ScopedCFTypeRef<CFDictionaryRef> config_dict( |
| 44 SCDynamicStoreCopyProxies(NULL)); | 44 SCDynamicStoreCopyProxies(NULL)); |
| 45 DCHECK(config_dict); | 45 DCHECK(config_dict); |
| 46 | 46 |
| 47 // auto-detect | 47 // auto-detect |
| 48 | 48 |
| 49 // There appears to be no UI for this configuration option, and we're not sure | 49 // There appears to be no UI for this configuration option, and we're not sure |
| 50 // if Apple's proxy code even takes it into account. But the constant is in | 50 // if Apple's proxy code even takes it into account. But the constant is in |
| 51 // the header file so we'll use it. | 51 // the header file so we'll use it. |
| 52 config->set_auto_detect( | 52 config->set_auto_detect(GetBoolFromDictionary( |
| 53 GetBoolFromDictionary(config_dict.get(), | 53 config_dict.get(), kSCPropNetProxiesProxyAutoDiscoveryEnable, false)); |
| 54 kSCPropNetProxiesProxyAutoDiscoveryEnable, | |
| 55 false)); | |
| 56 | 54 |
| 57 // PAC file | 55 // PAC file |
| 58 | 56 |
| 59 if (GetBoolFromDictionary(config_dict.get(), | 57 if (GetBoolFromDictionary( |
| 60 kSCPropNetProxiesProxyAutoConfigEnable, | 58 config_dict.get(), kSCPropNetProxiesProxyAutoConfigEnable, false)) { |
| 61 false)) { | |
| 62 CFStringRef pac_url_ref = base::mac::GetValueFromDictionary<CFStringRef>( | 59 CFStringRef pac_url_ref = base::mac::GetValueFromDictionary<CFStringRef>( |
| 63 config_dict.get(), kSCPropNetProxiesProxyAutoConfigURLString); | 60 config_dict.get(), kSCPropNetProxiesProxyAutoConfigURLString); |
| 64 if (pac_url_ref) | 61 if (pac_url_ref) |
| 65 config->set_pac_url(GURL(base::SysCFStringRefToUTF8(pac_url_ref))); | 62 config->set_pac_url(GURL(base::SysCFStringRefToUTF8(pac_url_ref))); |
| 66 } | 63 } |
| 67 | 64 |
| 68 // proxies (for now ftp, http, https, and SOCKS) | 65 // proxies (for now ftp, http, https, and SOCKS) |
| 69 | 66 |
| 70 if (GetBoolFromDictionary(config_dict.get(), | 67 if (GetBoolFromDictionary( |
| 71 kSCPropNetProxiesFTPEnable, | 68 config_dict.get(), kSCPropNetProxiesFTPEnable, false)) { |
| 72 false)) { | |
| 73 ProxyServer proxy_server = | 69 ProxyServer proxy_server = |
| 74 ProxyServer::FromDictionary(ProxyServer::SCHEME_HTTP, | 70 ProxyServer::FromDictionary(ProxyServer::SCHEME_HTTP, |
| 75 config_dict.get(), | 71 config_dict.get(), |
| 76 kSCPropNetProxiesFTPProxy, | 72 kSCPropNetProxiesFTPProxy, |
| 77 kSCPropNetProxiesFTPPort); | 73 kSCPropNetProxiesFTPPort); |
| 78 if (proxy_server.is_valid()) { | 74 if (proxy_server.is_valid()) { |
| 79 config->proxy_rules().type = | 75 config->proxy_rules().type = |
| 80 ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME; | 76 ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME; |
| 81 config->proxy_rules().proxies_for_ftp.SetSingleProxyServer(proxy_server); | 77 config->proxy_rules().proxies_for_ftp.SetSingleProxyServer(proxy_server); |
| 82 } | 78 } |
| 83 } | 79 } |
| 84 if (GetBoolFromDictionary(config_dict.get(), | 80 if (GetBoolFromDictionary( |
| 85 kSCPropNetProxiesHTTPEnable, | 81 config_dict.get(), kSCPropNetProxiesHTTPEnable, false)) { |
| 86 false)) { | |
| 87 ProxyServer proxy_server = | 82 ProxyServer proxy_server = |
| 88 ProxyServer::FromDictionary(ProxyServer::SCHEME_HTTP, | 83 ProxyServer::FromDictionary(ProxyServer::SCHEME_HTTP, |
| 89 config_dict.get(), | 84 config_dict.get(), |
| 90 kSCPropNetProxiesHTTPProxy, | 85 kSCPropNetProxiesHTTPProxy, |
| 91 kSCPropNetProxiesHTTPPort); | 86 kSCPropNetProxiesHTTPPort); |
| 92 if (proxy_server.is_valid()) { | 87 if (proxy_server.is_valid()) { |
| 93 config->proxy_rules().type = | 88 config->proxy_rules().type = |
| 94 ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME; | 89 ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME; |
| 95 config->proxy_rules().proxies_for_http.SetSingleProxyServer(proxy_server); | 90 config->proxy_rules().proxies_for_http.SetSingleProxyServer(proxy_server); |
| 96 } | 91 } |
| 97 } | 92 } |
| 98 if (GetBoolFromDictionary(config_dict.get(), | 93 if (GetBoolFromDictionary( |
| 99 kSCPropNetProxiesHTTPSEnable, | 94 config_dict.get(), kSCPropNetProxiesHTTPSEnable, false)) { |
| 100 false)) { | |
| 101 ProxyServer proxy_server = | 95 ProxyServer proxy_server = |
| 102 ProxyServer::FromDictionary(ProxyServer::SCHEME_HTTP, | 96 ProxyServer::FromDictionary(ProxyServer::SCHEME_HTTP, |
| 103 config_dict.get(), | 97 config_dict.get(), |
| 104 kSCPropNetProxiesHTTPSProxy, | 98 kSCPropNetProxiesHTTPSProxy, |
| 105 kSCPropNetProxiesHTTPSPort); | 99 kSCPropNetProxiesHTTPSPort); |
| 106 if (proxy_server.is_valid()) { | 100 if (proxy_server.is_valid()) { |
| 107 config->proxy_rules().type = | 101 config->proxy_rules().type = |
| 108 ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME; | 102 ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME; |
| 109 config->proxy_rules().proxies_for_https. | 103 config->proxy_rules().proxies_for_https.SetSingleProxyServer( |
| 110 SetSingleProxyServer(proxy_server); | 104 proxy_server); |
| 111 } | 105 } |
| 112 } | 106 } |
| 113 if (GetBoolFromDictionary(config_dict.get(), | 107 if (GetBoolFromDictionary( |
| 114 kSCPropNetProxiesSOCKSEnable, | 108 config_dict.get(), kSCPropNetProxiesSOCKSEnable, false)) { |
| 115 false)) { | |
| 116 ProxyServer proxy_server = | 109 ProxyServer proxy_server = |
| 117 ProxyServer::FromDictionary(ProxyServer::SCHEME_SOCKS5, | 110 ProxyServer::FromDictionary(ProxyServer::SCHEME_SOCKS5, |
| 118 config_dict.get(), | 111 config_dict.get(), |
| 119 kSCPropNetProxiesSOCKSProxy, | 112 kSCPropNetProxiesSOCKSProxy, |
| 120 kSCPropNetProxiesSOCKSPort); | 113 kSCPropNetProxiesSOCKSPort); |
| 121 if (proxy_server.is_valid()) { | 114 if (proxy_server.is_valid()) { |
| 122 config->proxy_rules().type = | 115 config->proxy_rules().type = |
| 123 ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME; | 116 ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME; |
| 124 config->proxy_rules().fallback_proxies.SetSingleProxyServer(proxy_server); | 117 config->proxy_rules().fallback_proxies.SetSingleProxyServer(proxy_server); |
| 125 } | 118 } |
| (...skipping 15 matching lines...) Expand all Loading... |
| 141 | 134 |
| 142 } else { | 135 } else { |
| 143 config->proxy_rules().bypass_rules.AddRuleFromString( | 136 config->proxy_rules().bypass_rules.AddRuleFromString( |
| 144 base::SysCFStringRefToUTF8(bypass_item_ref)); | 137 base::SysCFStringRefToUTF8(bypass_item_ref)); |
| 145 } | 138 } |
| 146 } | 139 } |
| 147 } | 140 } |
| 148 | 141 |
| 149 // proxy bypass boolean | 142 // proxy bypass boolean |
| 150 | 143 |
| 151 if (GetBoolFromDictionary(config_dict.get(), | 144 if (GetBoolFromDictionary( |
| 152 kSCPropNetProxiesExcludeSimpleHostnames, | 145 config_dict.get(), kSCPropNetProxiesExcludeSimpleHostnames, false)) { |
| 153 false)) { | |
| 154 config->proxy_rules().bypass_rules.AddRuleToBypassLocal(); | 146 config->proxy_rules().bypass_rules.AddRuleToBypassLocal(); |
| 155 } | 147 } |
| 156 | 148 |
| 157 // Source | 149 // Source |
| 158 config->set_source(PROXY_CONFIG_SOURCE_SYSTEM); | 150 config->set_source(PROXY_CONFIG_SOURCE_SYSTEM); |
| 159 } | 151 } |
| 160 | 152 |
| 161 } // namespace | 153 } // namespace |
| 162 | 154 |
| 163 // Reference-counted helper for posting a task to | 155 // Reference-counted helper for posting a task to |
| 164 // ProxyConfigServiceMac::OnProxyConfigChanged between the notifier and IO | 156 // ProxyConfigServiceMac::OnProxyConfigChanged between the notifier and IO |
| 165 // thread. This helper object may outlive the ProxyConfigServiceMac. | 157 // thread. This helper object may outlive the ProxyConfigServiceMac. |
| 166 class ProxyConfigServiceMac::Helper | 158 class ProxyConfigServiceMac::Helper |
| 167 : public base::RefCountedThreadSafe<ProxyConfigServiceMac::Helper> { | 159 : public base::RefCountedThreadSafe<ProxyConfigServiceMac::Helper> { |
| 168 public: | 160 public: |
| 169 explicit Helper(ProxyConfigServiceMac* parent) : parent_(parent) { | 161 explicit Helper(ProxyConfigServiceMac* parent) : parent_(parent) { |
| 170 DCHECK(parent); | 162 DCHECK(parent); |
| 171 } | 163 } |
| 172 | 164 |
| 173 // Called when the parent is destroyed. | 165 // Called when the parent is destroyed. |
| 174 void Orphan() { | 166 void Orphan() { parent_ = NULL; } |
| 175 parent_ = NULL; | |
| 176 } | |
| 177 | 167 |
| 178 void OnProxyConfigChanged(const ProxyConfig& new_config) { | 168 void OnProxyConfigChanged(const ProxyConfig& new_config) { |
| 179 if (parent_) | 169 if (parent_) |
| 180 parent_->OnProxyConfigChanged(new_config); | 170 parent_->OnProxyConfigChanged(new_config); |
| 181 } | 171 } |
| 182 | 172 |
| 183 private: | 173 private: |
| 184 friend class base::RefCountedThreadSafe<Helper>; | 174 friend class base::RefCountedThreadSafe<Helper>; |
| 185 ~Helper() {} | 175 ~Helper() {} |
| 186 | 176 |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 219 DCHECK(io_thread_task_runner_->BelongsToCurrentThread()); | 209 DCHECK(io_thread_task_runner_->BelongsToCurrentThread()); |
| 220 observers_.AddObserver(observer); | 210 observers_.AddObserver(observer); |
| 221 } | 211 } |
| 222 | 212 |
| 223 void ProxyConfigServiceMac::RemoveObserver(Observer* observer) { | 213 void ProxyConfigServiceMac::RemoveObserver(Observer* observer) { |
| 224 DCHECK(io_thread_task_runner_->BelongsToCurrentThread()); | 214 DCHECK(io_thread_task_runner_->BelongsToCurrentThread()); |
| 225 observers_.RemoveObserver(observer); | 215 observers_.RemoveObserver(observer); |
| 226 } | 216 } |
| 227 | 217 |
| 228 net::ProxyConfigService::ConfigAvailability | 218 net::ProxyConfigService::ConfigAvailability |
| 229 ProxyConfigServiceMac::GetLatestProxyConfig(ProxyConfig* config) { | 219 ProxyConfigServiceMac::GetLatestProxyConfig(ProxyConfig* config) { |
| 230 DCHECK(io_thread_task_runner_->BelongsToCurrentThread()); | 220 DCHECK(io_thread_task_runner_->BelongsToCurrentThread()); |
| 231 | 221 |
| 232 // Lazy-initialize by fetching the proxy setting from this thread. | 222 // Lazy-initialize by fetching the proxy setting from this thread. |
| 233 if (!has_fetched_config_) { | 223 if (!has_fetched_config_) { |
| 234 GetCurrentProxyConfig(&last_config_fetched_); | 224 GetCurrentProxyConfig(&last_config_fetched_); |
| 235 has_fetched_config_ = true; | 225 has_fetched_config_ = true; |
| 236 } | 226 } |
| 237 | 227 |
| 238 *config = last_config_fetched_; | 228 *config = last_config_fetched_; |
| 239 return has_fetched_config_ ? CONFIG_VALID : CONFIG_PENDING; | 229 return has_fetched_config_ ? CONFIG_VALID : CONFIG_PENDING; |
| 240 } | 230 } |
| 241 | 231 |
| 242 void ProxyConfigServiceMac::SetDynamicStoreNotificationKeys( | 232 void ProxyConfigServiceMac::SetDynamicStoreNotificationKeys( |
| 243 SCDynamicStoreRef store) { | 233 SCDynamicStoreRef store) { |
| 244 // Called on notifier thread. | 234 // Called on notifier thread. |
| 245 | 235 |
| 246 CFStringRef proxies_key = SCDynamicStoreKeyCreateProxies(NULL); | 236 CFStringRef proxies_key = SCDynamicStoreKeyCreateProxies(NULL); |
| 247 CFArrayRef key_array = CFArrayCreate( | 237 CFArrayRef key_array = CFArrayCreate( |
| 248 NULL, (const void **)(&proxies_key), 1, &kCFTypeArrayCallBacks); | 238 NULL, (const void**)(&proxies_key), 1, &kCFTypeArrayCallBacks); |
| 249 | 239 |
| 250 bool ret = SCDynamicStoreSetNotificationKeys(store, key_array, NULL); | 240 bool ret = SCDynamicStoreSetNotificationKeys(store, key_array, NULL); |
| 251 // TODO(willchan): Figure out a proper way to handle this rather than crash. | 241 // TODO(willchan): Figure out a proper way to handle this rather than crash. |
| 252 CHECK(ret); | 242 CHECK(ret); |
| 253 | 243 |
| 254 CFRelease(key_array); | 244 CFRelease(key_array); |
| 255 CFRelease(proxies_key); | 245 CFRelease(proxies_key); |
| 256 } | 246 } |
| 257 | 247 |
| 258 void ProxyConfigServiceMac::OnNetworkConfigChange(CFArrayRef changed_keys) { | 248 void ProxyConfigServiceMac::OnNetworkConfigChange(CFArrayRef changed_keys) { |
| (...skipping 11 matching lines...) Expand all Loading... |
| 270 | 260 |
| 271 void ProxyConfigServiceMac::OnProxyConfigChanged( | 261 void ProxyConfigServiceMac::OnProxyConfigChanged( |
| 272 const ProxyConfig& new_config) { | 262 const ProxyConfig& new_config) { |
| 273 DCHECK(io_thread_task_runner_->BelongsToCurrentThread()); | 263 DCHECK(io_thread_task_runner_->BelongsToCurrentThread()); |
| 274 | 264 |
| 275 // Keep track of the last value we have seen. | 265 // Keep track of the last value we have seen. |
| 276 has_fetched_config_ = true; | 266 has_fetched_config_ = true; |
| 277 last_config_fetched_ = new_config; | 267 last_config_fetched_ = new_config; |
| 278 | 268 |
| 279 // Notify all the observers. | 269 // Notify all the observers. |
| 280 FOR_EACH_OBSERVER(Observer, observers_, | 270 FOR_EACH_OBSERVER( |
| 281 OnProxyConfigChanged(new_config, CONFIG_VALID)); | 271 Observer, observers_, OnProxyConfigChanged(new_config, CONFIG_VALID)); |
| 282 } | 272 } |
| 283 | 273 |
| 284 } // namespace net | 274 } // namespace net |
| OLD | NEW |