#include "config.h"
#include "DNS.h"
#include "DNSResolveQueue.h"
#include "KURL.h"
#include "Timer.h"
#include <wtf/HashSet.h>
#include <wtf/MainThread.h>
#include <wtf/RetainPtr.h>
#include <wtf/StdLibExtras.h>
#include <wtf/text/StringHash.h>
#if PLATFORM(WIN)
#include "LoaderRunLoopCF.h"
#include <CFNetwork/CFNetwork.h>
#endif
#if defined(BUILDING_ON_LEOPARD)
#include <SystemConfiguration/SystemConfiguration.h>
#endif
namespace WebCore {
bool DNSResolveQueue::platformProxyIsEnabledInSystemPreferences()
{
#if !defined(BUILDING_ON_LEOPARD)
RetainPtr<CFDictionaryRef> proxySettings(AdoptCF, CFNetworkCopySystemProxySettings());
#else
RetainPtr<CFDictionaryRef> proxySettings(AdoptCF, SCDynamicStoreCopyProxies(0));
#endif
if (!proxySettings)
return false;
static CFURLRef httpCFURL = KURL(ParsedURLString, "http://example.com/").createCFURL();
static CFURLRef httpsCFURL = KURL(ParsedURLString, "https://example.com/").createCFURL();
RetainPtr<CFArrayRef> httpProxyArray(AdoptCF, CFNetworkCopyProxiesForURL(httpCFURL, proxySettings.get()));
RetainPtr<CFArrayRef> httpsProxyArray(AdoptCF, CFNetworkCopyProxiesForURL(httpsCFURL, proxySettings.get()));
CFIndex httpProxyCount = CFArrayGetCount(httpProxyArray.get());
CFIndex httpsProxyCount = CFArrayGetCount(httpsProxyArray.get());
if (httpProxyCount == 1 && CFEqual(CFDictionaryGetValue(static_cast<CFDictionaryRef>(CFArrayGetValueAtIndex(httpProxyArray.get(), 0)), kCFProxyTypeKey), kCFProxyTypeNone))
httpProxyCount = 0;
if (httpsProxyCount == 1 && CFEqual(CFDictionaryGetValue(static_cast<CFDictionaryRef>(CFArrayGetValueAtIndex(httpsProxyArray.get(), 0)), kCFProxyTypeKey), kCFProxyTypeNone))
httpsProxyCount = 0;
return httpProxyCount || httpsProxyCount;
}
static void clientCallback(CFHostRef theHost, CFHostInfoType, const CFStreamError*, void*)
{
DNSResolveQueue::shared().decrementRequestCount(); CFRelease(theHost);
}
void DNSResolveQueue::platformResolve(const String& hostname)
{
ASSERT(isMainThread());
RetainPtr<CFStringRef> hostnameCF(AdoptCF, hostname.createCFString());
RetainPtr<CFHostRef> host(AdoptCF, CFHostCreateWithName(0, hostnameCF.get()));
if (!host) {
decrementRequestCount();
return;
}
CFHostClientContext context = { 0, 0, 0, 0, 0 };
CFHostRef leakedHost = host.leakRef(); Boolean result = CFHostSetClient(leakedHost, clientCallback, &context);
ASSERT_UNUSED(result, result);
#if !PLATFORM(WIN)
CFHostScheduleWithRunLoop(leakedHost, CFRunLoopGetMain(), kCFRunLoopCommonModes);
#else
CFHostScheduleWithRunLoop(leakedHost, loaderRunLoop(), kCFRunLoopDefaultMode);
#endif
CFHostStartInfoResolution(leakedHost, kCFHostAddresses, 0);
}
void prefetchDNS(const String& hostname)
{
ASSERT(isMainThread());
if (hostname.isEmpty())
return;
DNSResolveQueue::shared().add(hostname);
}
}