This source file includes following definitions.
- GetProxyServerScheme
- ResultCallback
- GetProxyForURL
- CancelRequest
- GetLoadState
- CancelSetPacScript
- SetPacScript
#include "net/proxy/proxy_resolver_mac.h"
#include <CoreFoundation/CoreFoundation.h>
#include "base/logging.h"
#include "base/mac/foundation_util.h"
#include "base/mac/scoped_cftyperef.h"
#include "base/strings/string_util.h"
#include "base/strings/sys_string_conversions.h"
#include "net/base/net_errors.h"
#include "net/proxy/proxy_info.h"
#include "net/proxy/proxy_server.h"
#if defined(OS_IOS)
#include <CFNetwork/CFProxySupport.h>
#else
#include <CoreServices/CoreServices.h>
#endif
namespace {
net::ProxyServer::Scheme GetProxyServerScheme(CFStringRef proxy_type) {
if (CFEqual(proxy_type, kCFProxyTypeNone))
return net::ProxyServer::SCHEME_DIRECT;
if (CFEqual(proxy_type, kCFProxyTypeHTTP))
return net::ProxyServer::SCHEME_HTTP;
if (CFEqual(proxy_type, kCFProxyTypeHTTPS)) {
return net::ProxyServer::SCHEME_HTTP;
}
if (CFEqual(proxy_type, kCFProxyTypeSOCKS)) {
return net::ProxyServer::SCHEME_SOCKS5;
}
return net::ProxyServer::SCHEME_INVALID;
}
void ResultCallback(void* client, CFArrayRef proxies, CFErrorRef error) {
DCHECK((proxies != NULL) == (error == NULL));
CFTypeRef* result_ptr = reinterpret_cast<CFTypeRef*>(client);
DCHECK(result_ptr != NULL);
DCHECK(*result_ptr == NULL);
if (error != NULL) {
*result_ptr = CFRetain(error);
} else {
*result_ptr = CFRetain(proxies);
}
CFRunLoopStop(CFRunLoopGetCurrent());
}
}
namespace net {
ProxyResolverMac::ProxyResolverMac()
: ProxyResolver(false ) {
}
ProxyResolverMac::~ProxyResolverMac() {}
int ProxyResolverMac::GetProxyForURL(const GURL& query_url,
ProxyInfo* results,
const CompletionCallback& ,
RequestHandle* ,
const BoundNetLog& net_log) {
base::ScopedCFTypeRef<CFStringRef> query_ref(
base::SysUTF8ToCFStringRef(query_url.spec()));
base::ScopedCFTypeRef<CFURLRef> query_url_ref(
CFURLCreateWithString(kCFAllocatorDefault, query_ref.get(), NULL));
if (!query_url_ref.get())
return ERR_FAILED;
base::ScopedCFTypeRef<CFStringRef> pac_ref(base::SysUTF8ToCFStringRef(
script_data_->type() == ProxyResolverScriptData::TYPE_AUTO_DETECT
? std::string()
: script_data_->url().spec()));
base::ScopedCFTypeRef<CFURLRef> pac_url_ref(
CFURLCreateWithString(kCFAllocatorDefault, pac_ref.get(), NULL));
if (!pac_url_ref.get())
return ERR_FAILED;
CFArrayRef dummy_result = CFNetworkCopyProxiesForURL(query_url_ref.get(),
NULL);
if (dummy_result)
CFRelease(dummy_result);
CFTypeRef result = NULL;
CFStreamClientContext context = { 0, &result, NULL, NULL, NULL };
base::ScopedCFTypeRef<CFRunLoopSourceRef> runloop_source(
CFNetworkExecuteProxyAutoConfigurationURL(
pac_url_ref.get(), query_url_ref.get(), ResultCallback, &context));
if (!runloop_source)
return ERR_FAILED;
const CFStringRef private_runloop_mode =
CFSTR("org.chromium.ProxyResolverMac");
CFRunLoopAddSource(CFRunLoopGetCurrent(), runloop_source.get(),
private_runloop_mode);
CFRunLoopRunInMode(private_runloop_mode, DBL_MAX, false);
CFRunLoopRemoveSource(CFRunLoopGetCurrent(), runloop_source.get(),
private_runloop_mode);
DCHECK(result != NULL);
if (CFGetTypeID(result) == CFErrorGetTypeID()) {
CFRelease(result);
return ERR_FAILED;
}
base::ScopedCFTypeRef<CFArrayRef> proxy_array_ref(
base::mac::CFCastStrict<CFArrayRef>(result));
DCHECK(proxy_array_ref != NULL);
std::string proxy_uri_list;
CFIndex proxy_array_count = CFArrayGetCount(proxy_array_ref.get());
for (CFIndex i = 0; i < proxy_array_count; ++i) {
CFDictionaryRef proxy_dictionary = base::mac::CFCastStrict<CFDictionaryRef>(
CFArrayGetValueAtIndex(proxy_array_ref.get(), i));
DCHECK(proxy_dictionary != NULL);
CFStringRef proxy_type = base::mac::GetValueFromDictionary<CFStringRef>(
proxy_dictionary, kCFProxyTypeKey);
ProxyServer proxy_server = ProxyServer::FromDictionary(
GetProxyServerScheme(proxy_type),
proxy_dictionary,
kCFProxyHostNameKey,
kCFProxyPortNumberKey);
if (!proxy_server.is_valid())
continue;
if (!proxy_uri_list.empty())
proxy_uri_list += ";";
proxy_uri_list += proxy_server.ToURI();
}
if (!proxy_uri_list.empty())
results->UseNamedProxy(proxy_uri_list);
return OK;
}
void ProxyResolverMac::CancelRequest(RequestHandle request) {
NOTREACHED();
}
LoadState ProxyResolverMac::GetLoadState(RequestHandle request) const {
NOTREACHED();
return LOAD_STATE_IDLE;
}
void ProxyResolverMac::CancelSetPacScript() {
NOTREACHED();
}
int ProxyResolverMac::SetPacScript(
const scoped_refptr<ProxyResolverScriptData>& script_data,
const CompletionCallback& ) {
script_data_ = script_data;
return OK;
}
}