PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/net/proxy_resolution/proxy_resolver_mac.cc

https://github.com/chromium/chromium
C++ | 373 lines | 238 code | 53 blank | 82 comment | 29 complexity | a116a8107d4a4806915d50953c354632 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, Apache-2.0, BSD-3-Clause
  1. // Copyright (c) 2011 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. #include "net/proxy_resolution/proxy_resolver_mac.h"
  5. #include <CoreFoundation/CoreFoundation.h>
  6. #include <memory>
  7. #include "base/check.h"
  8. #include "base/lazy_instance.h"
  9. #include "base/mac/foundation_util.h"
  10. #include "base/mac/scoped_cftyperef.h"
  11. #include "base/strings/string_util.h"
  12. #include "base/strings/sys_string_conversions.h"
  13. #include "base/synchronization/lock.h"
  14. #include "base/threading/thread_checker.h"
  15. #include "build/build_config.h"
  16. #include "net/base/net_errors.h"
  17. #include "net/base/proxy_server.h"
  18. #include "net/base/proxy_string_util.h"
  19. #include "net/proxy_resolution/proxy_info.h"
  20. #include "net/proxy_resolution/proxy_list.h"
  21. #include "net/proxy_resolution/proxy_resolver.h"
  22. #include "url/gurl.h"
  23. #if BUILDFLAG(IS_IOS)
  24. #include <CFNetwork/CFProxySupport.h>
  25. #else
  26. #include <CoreServices/CoreServices.h>
  27. #endif
  28. namespace net {
  29. class NetworkIsolationKey;
  30. namespace {
  31. // A lock shared by all ProxyResolverMac instances. It is used to synchronize
  32. // the events of multiple CFNetworkExecuteProxyAutoConfigurationURL run loop
  33. // sources. These events are:
  34. // 1. Adding the source to the run loop.
  35. // 2. Handling the source result.
  36. // 3. Removing the source from the run loop.
  37. static base::LazyInstance<base::Lock>::Leaky g_cfnetwork_pac_runloop_lock =
  38. LAZY_INSTANCE_INITIALIZER;
  39. // Forward declaration of the callback function used by the
  40. // SynchronizedRunLoopObserver class.
  41. void RunLoopObserverCallBackFunc(CFRunLoopObserverRef observer,
  42. CFRunLoopActivity activity,
  43. void* info);
  44. // Utility function to map a CFProxyType to a ProxyServer::Scheme.
  45. // If the type is unknown, returns ProxyServer::SCHEME_INVALID.
  46. ProxyServer::Scheme GetProxyServerScheme(CFStringRef proxy_type) {
  47. if (CFEqual(proxy_type, kCFProxyTypeNone))
  48. return ProxyServer::SCHEME_DIRECT;
  49. if (CFEqual(proxy_type, kCFProxyTypeHTTP))
  50. return ProxyServer::SCHEME_HTTP;
  51. if (CFEqual(proxy_type, kCFProxyTypeHTTPS)) {
  52. // The "HTTPS" on the Mac side here means "proxy applies to https://" URLs;
  53. // the proxy itself is still expected to be an HTTP proxy.
  54. return ProxyServer::SCHEME_HTTP;
  55. }
  56. if (CFEqual(proxy_type, kCFProxyTypeSOCKS)) {
  57. // We can't tell whether this was v4 or v5. We will assume it is
  58. // v5 since that is the only version OS X supports.
  59. return ProxyServer::SCHEME_SOCKS5;
  60. }
  61. return ProxyServer::SCHEME_INVALID;
  62. }
  63. // Callback for CFNetworkExecuteProxyAutoConfigurationURL. |client| is a pointer
  64. // to a CFTypeRef. This stashes either |error| or |proxies| in that location.
  65. void ResultCallback(void* client, CFArrayRef proxies, CFErrorRef error) {
  66. DCHECK((proxies != NULL) == (error == NULL));
  67. CFTypeRef* result_ptr = reinterpret_cast<CFTypeRef*>(client);
  68. DCHECK(result_ptr != NULL);
  69. DCHECK(*result_ptr == NULL);
  70. if (error != NULL) {
  71. *result_ptr = CFRetain(error);
  72. } else {
  73. *result_ptr = CFRetain(proxies);
  74. }
  75. CFRunLoopStop(CFRunLoopGetCurrent());
  76. }
  77. #pragma mark - SynchronizedRunLoopObserver
  78. // A run loop observer that guarantees that no two run loop sources protected
  79. // by the same lock will be fired concurrently in different threads.
  80. // The observer does not prevent the parallel execution of the sources but only
  81. // synchronizes the run loop events associated with the sources. In the context
  82. // of proxy resolver, the observer is used to synchronize the execution of the
  83. // callbacks function that handles the result of
  84. // CFNetworkExecuteProxyAutoConfigurationURL execution.
  85. class SynchronizedRunLoopObserver final {
  86. public:
  87. // Creates the instance of an observer that will synchronize the sources
  88. // using a given |lock|.
  89. SynchronizedRunLoopObserver(base::Lock& lock);
  90. SynchronizedRunLoopObserver(const SynchronizedRunLoopObserver&) = delete;
  91. SynchronizedRunLoopObserver& operator=(const SynchronizedRunLoopObserver&) =
  92. delete;
  93. // Destructor.
  94. ~SynchronizedRunLoopObserver();
  95. // Adds the observer to the current run loop for a given run loop mode.
  96. // This method should always be paired with |RemoveFromCurrentRunLoop|.
  97. void AddToCurrentRunLoop(const CFStringRef mode);
  98. // Removes the observer from the current run loop for a given run loop mode.
  99. // This method should always be paired with |AddToCurrentRunLoop|.
  100. void RemoveFromCurrentRunLoop(const CFStringRef mode);
  101. // Callback function that is called when an observable run loop event occurs.
  102. void RunLoopObserverCallBack(CFRunLoopObserverRef observer,
  103. CFRunLoopActivity activity);
  104. private:
  105. // Lock to use to synchronize the run loop sources.
  106. base::Lock& lock_;
  107. // Indicates whether the current observer holds the lock. It is used to
  108. // avoid double locking and releasing.
  109. bool lock_acquired_ = false;
  110. // The underlying CFRunLoopObserverRef structure wrapped by this instance.
  111. base::ScopedCFTypeRef<CFRunLoopObserverRef> observer_;
  112. // Validates that all methods of this class are executed on the same thread.
  113. base::ThreadChecker thread_checker_;
  114. };
  115. SynchronizedRunLoopObserver::SynchronizedRunLoopObserver(base::Lock& lock)
  116. : lock_(lock) {
  117. CFRunLoopObserverContext observer_context = {0, this, NULL, NULL, NULL};
  118. observer_.reset(CFRunLoopObserverCreate(
  119. kCFAllocatorDefault,
  120. kCFRunLoopBeforeSources | kCFRunLoopBeforeWaiting | kCFRunLoopExit, true,
  121. 0, RunLoopObserverCallBackFunc, &observer_context));
  122. }
  123. SynchronizedRunLoopObserver::~SynchronizedRunLoopObserver() {
  124. DCHECK(thread_checker_.CalledOnValidThread());
  125. DCHECK(!lock_acquired_);
  126. }
  127. void SynchronizedRunLoopObserver::AddToCurrentRunLoop(const CFStringRef mode) {
  128. DCHECK(thread_checker_.CalledOnValidThread());
  129. CFRunLoopAddObserver(CFRunLoopGetCurrent(), observer_.get(), mode);
  130. }
  131. void SynchronizedRunLoopObserver::RemoveFromCurrentRunLoop(
  132. const CFStringRef mode) {
  133. DCHECK(thread_checker_.CalledOnValidThread());
  134. CFRunLoopRemoveObserver(CFRunLoopGetCurrent(), observer_.get(), mode);
  135. }
  136. void SynchronizedRunLoopObserver::RunLoopObserverCallBack(
  137. CFRunLoopObserverRef observer,
  138. CFRunLoopActivity activity) NO_THREAD_SAFETY_ANALYSIS {
  139. DCHECK(thread_checker_.CalledOnValidThread());
  140. // Acquire the lock when a source has been signaled and going to be fired.
  141. // In the context of the proxy resolver that happens when the proxy for a
  142. // given URL has been resolved and the callback function that handles the
  143. // result is going to be fired.
  144. // Release the lock when all source events have been handled.
  145. //
  146. // NO_THREAD_SAFETY_ANALYSIS: Runtime dependent locking.
  147. switch (activity) {
  148. case kCFRunLoopBeforeSources:
  149. if (!lock_acquired_) {
  150. lock_.Acquire();
  151. lock_acquired_ = true;
  152. }
  153. break;
  154. case kCFRunLoopBeforeWaiting:
  155. case kCFRunLoopExit:
  156. if (lock_acquired_) {
  157. lock_acquired_ = false;
  158. lock_.Release();
  159. }
  160. break;
  161. }
  162. }
  163. void RunLoopObserverCallBackFunc(CFRunLoopObserverRef observer,
  164. CFRunLoopActivity activity,
  165. void* info) {
  166. // Forward the call to the instance of SynchronizedRunLoopObserver
  167. // that is associated with the current CF run loop observer.
  168. SynchronizedRunLoopObserver* observerInstance =
  169. (SynchronizedRunLoopObserver*)info;
  170. observerInstance->RunLoopObserverCallBack(observer, activity);
  171. }
  172. #pragma mark - ProxyResolverMac
  173. class ProxyResolverMac : public ProxyResolver {
  174. public:
  175. explicit ProxyResolverMac(const scoped_refptr<PacFileData>& script_data);
  176. ~ProxyResolverMac() override;
  177. // ProxyResolver methods:
  178. int GetProxyForURL(const GURL& url,
  179. const NetworkIsolationKey& network_isolation_key,
  180. ProxyInfo* results,
  181. CompletionOnceCallback callback,
  182. std::unique_ptr<Request>* request,
  183. const NetLogWithSource& net_log) override;
  184. private:
  185. const scoped_refptr<PacFileData> script_data_;
  186. };
  187. ProxyResolverMac::ProxyResolverMac(
  188. const scoped_refptr<PacFileData>& script_data)
  189. : script_data_(script_data) {}
  190. ProxyResolverMac::~ProxyResolverMac() {}
  191. // Gets the proxy information for a query URL from a PAC. Implementation
  192. // inspired by http://developer.apple.com/samplecode/CFProxySupportTool/
  193. int ProxyResolverMac::GetProxyForURL(
  194. const GURL& query_url,
  195. const NetworkIsolationKey& network_isolation_key,
  196. ProxyInfo* results,
  197. CompletionOnceCallback /*callback*/,
  198. std::unique_ptr<Request>* /*request*/,
  199. const NetLogWithSource& net_log) {
  200. // OS X's system resolver does not support WebSocket URLs in proxy.pac, as of
  201. // version 10.13.5. See https://crbug.com/862121.
  202. GURL mutable_query_url = query_url;
  203. if (query_url.SchemeIsWSOrWSS()) {
  204. GURL::Replacements replacements;
  205. replacements.SetSchemeStr(query_url.SchemeIsCryptographic() ? "https"
  206. : "http");
  207. mutable_query_url = query_url.ReplaceComponents(replacements);
  208. }
  209. base::ScopedCFTypeRef<CFStringRef> query_ref(
  210. base::SysUTF8ToCFStringRef(mutable_query_url.spec()));
  211. base::ScopedCFTypeRef<CFURLRef> query_url_ref(
  212. CFURLCreateWithString(kCFAllocatorDefault, query_ref.get(), NULL));
  213. if (!query_url_ref.get())
  214. return ERR_FAILED;
  215. base::ScopedCFTypeRef<CFStringRef> pac_ref(base::SysUTF8ToCFStringRef(
  216. script_data_->type() == PacFileData::TYPE_AUTO_DETECT
  217. ? std::string()
  218. : script_data_->url().spec()));
  219. base::ScopedCFTypeRef<CFURLRef> pac_url_ref(
  220. CFURLCreateWithString(kCFAllocatorDefault, pac_ref.get(), NULL));
  221. if (!pac_url_ref.get())
  222. return ERR_FAILED;
  223. // Work around <rdar://problem/5530166>. This dummy call to
  224. // CFNetworkCopyProxiesForURL initializes some state within CFNetwork that is
  225. // required by CFNetworkExecuteProxyAutoConfigurationURL.
  226. base::ScopedCFTypeRef<CFDictionaryRef> empty_dictionary(
  227. CFDictionaryCreate(NULL, NULL, NULL, 0, NULL, NULL));
  228. CFArrayRef dummy_result =
  229. CFNetworkCopyProxiesForURL(query_url_ref.get(), empty_dictionary);
  230. if (dummy_result)
  231. CFRelease(dummy_result);
  232. // We cheat here. We need to act as if we were synchronous, so we pump the
  233. // runloop ourselves. Our caller moved us to a new thread anyway, so this is
  234. // OK to do. (BTW, CFNetworkExecuteProxyAutoConfigurationURL returns a
  235. // runloop source we need to release despite its name.)
  236. CFTypeRef result = NULL;
  237. CFStreamClientContext context = { 0, &result, NULL, NULL, NULL };
  238. base::ScopedCFTypeRef<CFRunLoopSourceRef> runloop_source(
  239. CFNetworkExecuteProxyAutoConfigurationURL(
  240. pac_url_ref.get(), query_url_ref.get(), ResultCallback, &context));
  241. if (!runloop_source)
  242. return ERR_FAILED;
  243. const CFStringRef private_runloop_mode =
  244. CFSTR("org.chromium.ProxyResolverMac");
  245. // Add the run loop observer to synchronize events of
  246. // CFNetworkExecuteProxyAutoConfigurationURL sources. See the definition of
  247. // |g_cfnetwork_pac_runloop_lock|.
  248. SynchronizedRunLoopObserver observer(g_cfnetwork_pac_runloop_lock.Get());
  249. observer.AddToCurrentRunLoop(private_runloop_mode);
  250. // Make sure that no CFNetworkExecuteProxyAutoConfigurationURL sources
  251. // are added to the run loop concurrently.
  252. {
  253. base::AutoLock lock(g_cfnetwork_pac_runloop_lock.Get());
  254. CFRunLoopAddSource(CFRunLoopGetCurrent(), runloop_source.get(),
  255. private_runloop_mode);
  256. }
  257. CFRunLoopRunInMode(private_runloop_mode, DBL_MAX, false);
  258. // Make sure that no CFNetworkExecuteProxyAutoConfigurationURL sources
  259. // are removed from the run loop concurrently.
  260. {
  261. base::AutoLock lock(g_cfnetwork_pac_runloop_lock.Get());
  262. CFRunLoopRemoveSource(CFRunLoopGetCurrent(), runloop_source.get(),
  263. private_runloop_mode);
  264. }
  265. observer.RemoveFromCurrentRunLoop(private_runloop_mode);
  266. DCHECK(result != NULL);
  267. if (CFGetTypeID(result) == CFErrorGetTypeID()) {
  268. // TODO(avi): do something better than this
  269. CFRelease(result);
  270. return ERR_FAILED;
  271. }
  272. base::ScopedCFTypeRef<CFArrayRef> proxy_array_ref(
  273. base::mac::CFCastStrict<CFArrayRef>(result));
  274. DCHECK(proxy_array_ref != NULL);
  275. ProxyList proxy_list;
  276. CFIndex proxy_array_count = CFArrayGetCount(proxy_array_ref.get());
  277. for (CFIndex i = 0; i < proxy_array_count; ++i) {
  278. CFDictionaryRef proxy_dictionary = base::mac::CFCastStrict<CFDictionaryRef>(
  279. CFArrayGetValueAtIndex(proxy_array_ref.get(), i));
  280. DCHECK(proxy_dictionary != NULL);
  281. // The dictionary may have the following keys:
  282. // - kCFProxyTypeKey : The type of the proxy
  283. // - kCFProxyHostNameKey
  284. // - kCFProxyPortNumberKey : The meat we're after.
  285. // - kCFProxyUsernameKey
  286. // - kCFProxyPasswordKey : Despite the existence of these keys in the
  287. // documentation, they're never populated. Even if a
  288. // username/password were to be set in the network
  289. // proxy system preferences, we'd need to fetch it
  290. // from the Keychain ourselves. CFProxy is such a
  291. // tease.
  292. // - kCFProxyAutoConfigurationURLKey : If the PAC file specifies another
  293. // PAC file, I'm going home.
  294. CFStringRef proxy_type = base::mac::GetValueFromDictionary<CFStringRef>(
  295. proxy_dictionary, kCFProxyTypeKey);
  296. ProxyServer proxy_server = ProxyDictionaryToProxyServer(
  297. GetProxyServerScheme(proxy_type), proxy_dictionary, kCFProxyHostNameKey,
  298. kCFProxyPortNumberKey);
  299. if (!proxy_server.is_valid())
  300. continue;
  301. proxy_list.AddProxyServer(proxy_server);
  302. }
  303. if (!proxy_list.IsEmpty())
  304. results->UseProxyList(proxy_list);
  305. // Else do nothing (results is already guaranteed to be in the default state).
  306. return OK;
  307. }
  308. } // namespace
  309. ProxyResolverFactoryMac::ProxyResolverFactoryMac()
  310. : ProxyResolverFactory(false /*expects_pac_bytes*/) {
  311. }
  312. int ProxyResolverFactoryMac::CreateProxyResolver(
  313. const scoped_refptr<PacFileData>& pac_script,
  314. std::unique_ptr<ProxyResolver>* resolver,
  315. CompletionOnceCallback callback,
  316. std::unique_ptr<Request>* request) {
  317. *resolver = std::make_unique<ProxyResolverMac>(pac_script);
  318. return OK;
  319. }
  320. } // namespace net