#include "config.h"
#include "DNS.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 {
const int namesToResolveImmediately = 4;
const double coalesceDelayInSeconds = 1.0;
const int maxSimultaneousRequests = 8;
const int maxRequestsToQueue = 64;
const double retryResolvingInSeconds = 0.1;
static bool proxyIsEnabledInSystemPreferences()
{
#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;
}
class DNSResolveQueue : public TimerBase {
public:
static DNSResolveQueue& shared();
void add(const String&);
void decrementRequestCount();
private:
DNSResolveQueue();
void resolve(const String&);
virtual void fired();
HashSet<String> m_names;
int m_requestsInFlight;
};
DNSResolveQueue::DNSResolveQueue()
: m_requestsInFlight(0)
{
}
DNSResolveQueue& DNSResolveQueue::shared()
{
DEFINE_STATIC_LOCAL(DNSResolveQueue, names, ());
return names;
}
void DNSResolveQueue::add(const String& name)
{
if (!m_names.size()) {
if (proxyIsEnabledInSystemPreferences())
return;
if (atomicIncrement(&m_requestsInFlight) <= namesToResolveImmediately) {
resolve(name);
return;
}
atomicDecrement(&m_requestsInFlight);
}
if (m_names.size() < maxRequestsToQueue) {
m_names.add(name);
if (!isActive())
startOneShot(coalesceDelayInSeconds);
}
}
void DNSResolveQueue::decrementRequestCount()
{
atomicDecrement(&m_requestsInFlight);
}
void DNSResolveQueue::fired()
{
if (proxyIsEnabledInSystemPreferences()) {
m_names.clear();
return;
}
int requestsAllowed = maxSimultaneousRequests - m_requestsInFlight;
for (; !m_names.isEmpty() && requestsAllowed > 0; --requestsAllowed) {
atomicIncrement(&m_requestsInFlight);
HashSet<String>::iterator currentName = m_names.begin();
resolve(*currentName);
m_names.remove(currentName);
}
if (!m_names.isEmpty())
startOneShot(retryResolvingInSeconds);
}
static void clientCallback(CFHostRef theHost, CFHostInfoType, const CFStreamError*, void*)
{
DNSResolveQueue::shared().decrementRequestCount(); CFRelease(theHost);
}
void DNSResolveQueue::resolve(const String& hostname)
{
ASSERT(isMainThread());
RetainPtr<CFStringRef> hostnameCF(AdoptCF, hostname.createCFString());
RetainPtr<CFHostRef> host(AdoptCF, CFHostCreateWithName(0, hostnameCF.get()));
if (!host) {
atomicDecrement(&m_requestsInFlight);
return;
}
CFHostClientContext context = { 0, 0, 0, 0, 0 };
Boolean result = CFHostSetClient(host.get(), clientCallback, &context);
ASSERT_UNUSED(result, result);
#if !PLATFORM(WIN)
CFHostScheduleWithRunLoop(host.get(), CFRunLoopGetMain(), kCFRunLoopCommonModes);
#else
CFHostScheduleWithRunLoop(host.get(), loaderRunLoop(), kCFRunLoopDefaultMode);
#endif
CFHostStartInfoResolution(host.get(), kCFHostAddresses, 0);
host.releaseRef(); }
void prefetchDNS(const String& hostname)
{
ASSERT(isMainThread());
if (hostname.isEmpty())
return;
DNSResolveQueue::shared().add(hostname);
}
}