Don't show supervised user as "already on this device" while they're being imported.
[chromium-blink-merge.git] / net / proxy / proxy_resolver_mac.cc
blobdf438d198283bac837007dce93a669d12b7db087
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/strings/string_util.h"
13 #include "base/strings/sys_string_conversions.h"
14 #include "net/base/net_errors.h"
15 #include "net/proxy/proxy_info.h"
16 #include "net/proxy/proxy_resolver.h"
17 #include "net/proxy/proxy_server.h"
19 #if defined(OS_IOS)
20 #include <CFNetwork/CFProxySupport.h>
21 #else
22 #include <CoreServices/CoreServices.h>
23 #endif
25 namespace net {
27 namespace {
29 // Utility function to map a CFProxyType to a ProxyServer::Scheme.
30 // If the type is unknown, returns ProxyServer::SCHEME_INVALID.
31 ProxyServer::Scheme GetProxyServerScheme(CFStringRef proxy_type) {
32 if (CFEqual(proxy_type, kCFProxyTypeNone))
33 return ProxyServer::SCHEME_DIRECT;
34 if (CFEqual(proxy_type, kCFProxyTypeHTTP))
35 return ProxyServer::SCHEME_HTTP;
36 if (CFEqual(proxy_type, kCFProxyTypeHTTPS)) {
37 // The "HTTPS" on the Mac side here means "proxy applies to https://" URLs;
38 // the proxy itself is still expected to be an HTTP proxy.
39 return ProxyServer::SCHEME_HTTP;
41 if (CFEqual(proxy_type, kCFProxyTypeSOCKS)) {
42 // We can't tell whether this was v4 or v5. We will assume it is
43 // v5 since that is the only version OS X supports.
44 return ProxyServer::SCHEME_SOCKS5;
46 return ProxyServer::SCHEME_INVALID;
49 // Callback for CFNetworkExecuteProxyAutoConfigurationURL. |client| is a pointer
50 // to a CFTypeRef. This stashes either |error| or |proxies| in that location.
51 void ResultCallback(void* client, CFArrayRef proxies, CFErrorRef error) {
52 DCHECK((proxies != NULL) == (error == NULL));
54 CFTypeRef* result_ptr = reinterpret_cast<CFTypeRef*>(client);
55 DCHECK(result_ptr != NULL);
56 DCHECK(*result_ptr == NULL);
58 if (error != NULL) {
59 *result_ptr = CFRetain(error);
60 } else {
61 *result_ptr = CFRetain(proxies);
63 CFRunLoopStop(CFRunLoopGetCurrent());
66 class ProxyResolverMac : public ProxyResolver {
67 public:
68 explicit ProxyResolverMac(
69 const scoped_refptr<ProxyResolverScriptData>& script_data);
70 ~ProxyResolverMac() override;
72 // ProxyResolver methods:
73 int GetProxyForURL(const GURL& url,
74 ProxyInfo* results,
75 const CompletionCallback& callback,
76 RequestHandle* request,
77 const BoundNetLog& net_log) override;
79 void CancelRequest(RequestHandle request) override;
81 LoadState GetLoadState(RequestHandle request) const override;
83 private:
84 const scoped_refptr<ProxyResolverScriptData> script_data_;
87 ProxyResolverMac::ProxyResolverMac(
88 const scoped_refptr<ProxyResolverScriptData>& script_data)
89 : script_data_(script_data) {
92 ProxyResolverMac::~ProxyResolverMac() {}
94 // Gets the proxy information for a query URL from a PAC. Implementation
95 // inspired by http://developer.apple.com/samplecode/CFProxySupportTool/
96 int ProxyResolverMac::GetProxyForURL(const GURL& query_url,
97 ProxyInfo* results,
98 const CompletionCallback& /*callback*/,
99 RequestHandle* /*request*/,
100 const BoundNetLog& net_log) {
101 base::ScopedCFTypeRef<CFStringRef> query_ref(
102 base::SysUTF8ToCFStringRef(query_url.spec()));
103 base::ScopedCFTypeRef<CFURLRef> query_url_ref(
104 CFURLCreateWithString(kCFAllocatorDefault, query_ref.get(), NULL));
105 if (!query_url_ref.get())
106 return ERR_FAILED;
107 base::ScopedCFTypeRef<CFStringRef> pac_ref(base::SysUTF8ToCFStringRef(
108 script_data_->type() == ProxyResolverScriptData::TYPE_AUTO_DETECT
109 ? std::string()
110 : script_data_->url().spec()));
111 base::ScopedCFTypeRef<CFURLRef> pac_url_ref(
112 CFURLCreateWithString(kCFAllocatorDefault, pac_ref.get(), NULL));
113 if (!pac_url_ref.get())
114 return ERR_FAILED;
116 // Work around <rdar://problem/5530166>. This dummy call to
117 // CFNetworkCopyProxiesForURL initializes some state within CFNetwork that is
118 // required by CFNetworkExecuteProxyAutoConfigurationURL.
120 CFArrayRef dummy_result = CFNetworkCopyProxiesForURL(query_url_ref.get(),
121 NULL);
122 if (dummy_result)
123 CFRelease(dummy_result);
125 // We cheat here. We need to act as if we were synchronous, so we pump the
126 // runloop ourselves. Our caller moved us to a new thread anyway, so this is
127 // OK to do. (BTW, CFNetworkExecuteProxyAutoConfigurationURL returns a
128 // runloop source we need to release despite its name.)
130 CFTypeRef result = NULL;
131 CFStreamClientContext context = { 0, &result, NULL, NULL, NULL };
132 base::ScopedCFTypeRef<CFRunLoopSourceRef> runloop_source(
133 CFNetworkExecuteProxyAutoConfigurationURL(
134 pac_url_ref.get(), query_url_ref.get(), ResultCallback, &context));
135 if (!runloop_source)
136 return ERR_FAILED;
138 const CFStringRef private_runloop_mode =
139 CFSTR("org.chromium.ProxyResolverMac");
141 CFRunLoopAddSource(CFRunLoopGetCurrent(), runloop_source.get(),
142 private_runloop_mode);
143 CFRunLoopRunInMode(private_runloop_mode, DBL_MAX, false);
144 CFRunLoopSourceInvalidate(runloop_source.get());
145 DCHECK(result != NULL);
147 if (CFGetTypeID(result) == CFErrorGetTypeID()) {
148 // TODO(avi): do something better than this
149 CFRelease(result);
150 return ERR_FAILED;
152 base::ScopedCFTypeRef<CFArrayRef> proxy_array_ref(
153 base::mac::CFCastStrict<CFArrayRef>(result));
154 DCHECK(proxy_array_ref != NULL);
156 // This string will be an ordered list of <proxy-uri> entries, separated by
157 // semi-colons. It is the format that ProxyInfo::UseNamedProxy() expects.
158 // proxy-uri = [<proxy-scheme>"://"]<proxy-host>":"<proxy-port>
159 // (This also includes entries for direct connection, as "direct://").
160 std::string proxy_uri_list;
162 CFIndex proxy_array_count = CFArrayGetCount(proxy_array_ref.get());
163 for (CFIndex i = 0; i < proxy_array_count; ++i) {
164 CFDictionaryRef proxy_dictionary = base::mac::CFCastStrict<CFDictionaryRef>(
165 CFArrayGetValueAtIndex(proxy_array_ref.get(), i));
166 DCHECK(proxy_dictionary != NULL);
168 // The dictionary may have the following keys:
169 // - kCFProxyTypeKey : The type of the proxy
170 // - kCFProxyHostNameKey
171 // - kCFProxyPortNumberKey : The meat we're after.
172 // - kCFProxyUsernameKey
173 // - kCFProxyPasswordKey : Despite the existence of these keys in the
174 // documentation, they're never populated. Even if a
175 // username/password were to be set in the network
176 // proxy system preferences, we'd need to fetch it
177 // from the Keychain ourselves. CFProxy is such a
178 // tease.
179 // - kCFProxyAutoConfigurationURLKey : If the PAC file specifies another
180 // PAC file, I'm going home.
182 CFStringRef proxy_type = base::mac::GetValueFromDictionary<CFStringRef>(
183 proxy_dictionary, kCFProxyTypeKey);
184 ProxyServer proxy_server = ProxyServer::FromDictionary(
185 GetProxyServerScheme(proxy_type),
186 proxy_dictionary,
187 kCFProxyHostNameKey,
188 kCFProxyPortNumberKey);
189 if (!proxy_server.is_valid())
190 continue;
192 if (!proxy_uri_list.empty())
193 proxy_uri_list += ";";
194 proxy_uri_list += proxy_server.ToURI();
197 if (!proxy_uri_list.empty())
198 results->UseNamedProxy(proxy_uri_list);
199 // Else do nothing (results is already guaranteed to be in the default state).
201 return OK;
204 void ProxyResolverMac::CancelRequest(RequestHandle request) {
205 NOTREACHED();
208 LoadState ProxyResolverMac::GetLoadState(RequestHandle request) const {
209 NOTREACHED();
210 return LOAD_STATE_IDLE;
213 } // namespace
215 ProxyResolverFactoryMac::ProxyResolverFactoryMac()
216 : ProxyResolverFactory(false /*expects_pac_bytes*/) {
219 int ProxyResolverFactoryMac::CreateProxyResolver(
220 const scoped_refptr<ProxyResolverScriptData>& pac_script,
221 scoped_ptr<ProxyResolver>* resolver,
222 const CompletionCallback& callback,
223 scoped_ptr<Request>* request) {
224 resolver->reset(new ProxyResolverMac(pac_script));
225 return OK;
228 } // namespace net