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.
5 #include "net/proxy/proxy_resolver_mac.h"
7 #include <CoreFoundation/CoreFoundation.h>
9 #include "base/logging.h"
10 #include "base/mac/foundation_util.h"
11 #include "base/mac/scoped_cftyperef.h"
12 #include "base/string_util.h"
13 #include "base/sys_string_conversions.h"
14 #include "net/base/net_errors.h"
15 #include "net/proxy/proxy_info.h"
16 #include "net/proxy/proxy_server.h"
19 #include <CFNetwork/CFProxySupport.h>
21 #include <CoreServices/CoreServices.h>
26 // Utility function to map a CFProxyType to a ProxyServer::Scheme.
27 // If the type is unknown, returns ProxyServer::SCHEME_INVALID.
28 net::ProxyServer::Scheme
GetProxyServerScheme(CFStringRef proxy_type
) {
29 if (CFEqual(proxy_type
, kCFProxyTypeNone
))
30 return net::ProxyServer::SCHEME_DIRECT
;
31 if (CFEqual(proxy_type
, kCFProxyTypeHTTP
))
32 return net::ProxyServer::SCHEME_HTTP
;
33 if (CFEqual(proxy_type
, kCFProxyTypeHTTPS
)) {
34 // The "HTTPS" on the Mac side here means "proxy applies to https://" URLs;
35 // the proxy itself is still expected to be an HTTP proxy.
36 return net::ProxyServer::SCHEME_HTTP
;
38 if (CFEqual(proxy_type
, kCFProxyTypeSOCKS
)) {
39 // We can't tell whether this was v4 or v5. We will assume it is
40 // v5 since that is the only version OS X supports.
41 return net::ProxyServer::SCHEME_SOCKS5
;
43 return net::ProxyServer::SCHEME_INVALID
;
46 // Callback for CFNetworkExecuteProxyAutoConfigurationURL. |client| is a pointer
47 // to a CFTypeRef. This stashes either |error| or |proxies| in that location.
48 void ResultCallback(void* client
, CFArrayRef proxies
, CFErrorRef error
) {
49 DCHECK((proxies
!= NULL
) == (error
== NULL
));
51 CFTypeRef
* result_ptr
= reinterpret_cast<CFTypeRef
*>(client
);
52 DCHECK(result_ptr
!= NULL
);
53 DCHECK(*result_ptr
== NULL
);
56 *result_ptr
= CFRetain(error
);
58 *result_ptr
= CFRetain(proxies
);
60 CFRunLoopStop(CFRunLoopGetCurrent());
67 ProxyResolverMac::ProxyResolverMac()
68 : ProxyResolver(false /*expects_pac_bytes*/) {
71 ProxyResolverMac::~ProxyResolverMac() {}
73 // Gets the proxy information for a query URL from a PAC. Implementation
74 // inspired by http://developer.apple.com/samplecode/CFProxySupportTool/
75 int ProxyResolverMac::GetProxyForURL(const GURL
& query_url
,
77 const CompletionCallback
& /*callback*/,
78 RequestHandle
* /*request*/,
79 const BoundNetLog
& net_log
) {
80 base::mac::ScopedCFTypeRef
<CFStringRef
> query_ref(
81 base::SysUTF8ToCFStringRef(query_url
.spec()));
82 base::mac::ScopedCFTypeRef
<CFURLRef
> query_url_ref(
83 CFURLCreateWithString(kCFAllocatorDefault
,
86 if (!query_url_ref
.get())
88 base::mac::ScopedCFTypeRef
<CFStringRef
> pac_ref(
89 base::SysUTF8ToCFStringRef(
90 script_data_
->type() == ProxyResolverScriptData::TYPE_AUTO_DETECT
?
91 std::string() : script_data_
->url().spec()));
92 base::mac::ScopedCFTypeRef
<CFURLRef
> pac_url_ref(
93 CFURLCreateWithString(kCFAllocatorDefault
,
96 if (!pac_url_ref
.get())
99 // Work around <rdar://problem/5530166>. This dummy call to
100 // CFNetworkCopyProxiesForURL initializes some state within CFNetwork that is
101 // required by CFNetworkExecuteProxyAutoConfigurationURL.
103 CFArrayRef dummy_result
= CFNetworkCopyProxiesForURL(query_url_ref
.get(),
106 CFRelease(dummy_result
);
108 // We cheat here. We need to act as if we were synchronous, so we pump the
109 // runloop ourselves. Our caller moved us to a new thread anyway, so this is
110 // OK to do. (BTW, CFNetworkExecuteProxyAutoConfigurationURL returns a
111 // runloop source we need to release despite its name.)
113 CFTypeRef result
= NULL
;
114 CFStreamClientContext context
= { 0, &result
, NULL
, NULL
, NULL
};
115 base::mac::ScopedCFTypeRef
<CFRunLoopSourceRef
> runloop_source(
116 CFNetworkExecuteProxyAutoConfigurationURL(pac_url_ref
.get(),
123 const CFStringRef private_runloop_mode
=
124 CFSTR("org.chromium.ProxyResolverMac");
126 CFRunLoopAddSource(CFRunLoopGetCurrent(), runloop_source
.get(),
127 private_runloop_mode
);
128 CFRunLoopRunInMode(private_runloop_mode
, DBL_MAX
, false);
129 CFRunLoopRemoveSource(CFRunLoopGetCurrent(), runloop_source
.get(),
130 private_runloop_mode
);
131 DCHECK(result
!= NULL
);
133 if (CFGetTypeID(result
) == CFErrorGetTypeID()) {
134 // TODO(avi): do something better than this
138 base::mac::ScopedCFTypeRef
<CFArrayRef
> proxy_array_ref(
139 base::mac::CFCastStrict
<CFArrayRef
>(result
));
140 DCHECK(proxy_array_ref
!= NULL
);
142 // This string will be an ordered list of <proxy-uri> entries, separated by
143 // semi-colons. It is the format that ProxyInfo::UseNamedProxy() expects.
144 // proxy-uri = [<proxy-scheme>"://"]<proxy-host>":"<proxy-port>
145 // (This also includes entries for direct connection, as "direct://").
146 std::string proxy_uri_list
;
148 CFIndex proxy_array_count
= CFArrayGetCount(proxy_array_ref
.get());
149 for (CFIndex i
= 0; i
< proxy_array_count
; ++i
) {
150 CFDictionaryRef proxy_dictionary
= base::mac::CFCastStrict
<CFDictionaryRef
>(
151 CFArrayGetValueAtIndex(proxy_array_ref
.get(), i
));
152 DCHECK(proxy_dictionary
!= NULL
);
154 // The dictionary may have the following keys:
155 // - kCFProxyTypeKey : The type of the proxy
156 // - kCFProxyHostNameKey
157 // - kCFProxyPortNumberKey : The meat we're after.
158 // - kCFProxyUsernameKey
159 // - kCFProxyPasswordKey : Despite the existence of these keys in the
160 // documentation, they're never populated. Even if a
161 // username/password were to be set in the network
162 // proxy system preferences, we'd need to fetch it
163 // from the Keychain ourselves. CFProxy is such a
165 // - kCFProxyAutoConfigurationURLKey : If the PAC file specifies another
166 // PAC file, I'm going home.
168 CFStringRef proxy_type
= base::mac::GetValueFromDictionary
<CFStringRef
>(
169 proxy_dictionary
, kCFProxyTypeKey
);
170 ProxyServer proxy_server
= ProxyServer::FromDictionary(
171 GetProxyServerScheme(proxy_type
),
174 kCFProxyPortNumberKey
);
175 if (!proxy_server
.is_valid())
178 if (!proxy_uri_list
.empty())
179 proxy_uri_list
+= ";";
180 proxy_uri_list
+= proxy_server
.ToURI();
183 if (!proxy_uri_list
.empty())
184 results
->UseNamedProxy(proxy_uri_list
);
185 // Else do nothing (results is already guaranteed to be in the default state).
190 void ProxyResolverMac::CancelRequest(RequestHandle request
) {
194 LoadState
ProxyResolverMac::GetLoadState(RequestHandle request
) const {
196 return LOAD_STATE_IDLE
;
199 LoadState
ProxyResolverMac::GetLoadStateThreadSafe(
200 RequestHandle request
) const {
201 return LOAD_STATE_IDLE
;
204 void ProxyResolverMac::CancelSetPacScript() {
208 int ProxyResolverMac::SetPacScript(
209 const scoped_refptr
<ProxyResolverScriptData
>& script_data
,
210 const CompletionCallback
& /*callback*/) {
211 script_data_
= script_data
;