#include "config.h"
#include "DNS.h"
#include "DNSResolveQueue.h"
#include "URL.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(IOS)
#include <CFNetwork/CFNetwork.h>
#endif
#if PLATFORM(WIN)
#include "LoaderRunLoopCF.h"
#include <CFNetwork/CFNetwork.h>
#endif
namespace WebCore {
bool DNSResolveQueue::platformProxyIsEnabledInSystemPreferences()
{
RetainPtr<CFDictionaryRef> proxySettings = adoptCF(CFNetworkCopySystemProxySettings());
if (!proxySettings)
return false;
RetainPtr<CFURLRef> httpCFURL = URL(ParsedURLString, "http://example.com/").createCFURL();
RetainPtr<CFURLRef> httpsCFURL = URL(ParsedURLString, "https://example.com/").createCFURL();
RetainPtr<CFArrayRef> httpProxyArray = adoptCF(CFNetworkCopyProxiesForURL(httpCFURL.get(), proxySettings.get()));
RetainPtr<CFArrayRef> httpsProxyArray = adoptCF(CFNetworkCopyProxiesForURL(httpsCFURL.get(), 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<CFHostRef> host = adoptCF(CFHostCreateWithName(0, hostname.createCFString().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);
}
}