#include "config.h"
#include "DNS.h"
#include "StringHash.h"
#include "Timer.h"
#include <wtf/HashSet.h>
#include <wtf/RetainPtr.h>
#include <wtf/StdLibExtras.h>
#if PLATFORM(WIN)
#include "LoaderRunLoopCF.h"
#endif
#ifdef BUILDING_ON_TIGER
extern "C" CFRunLoopRef CFRunLoopGetMain();
#endif
namespace WebCore {
const int namesToResolveImmediately = 4;
const double coalesceDelay = 1.0;
const int maxRequestsToSend = 64;
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 (atomicIncrement(&m_requestsInFlight) <= namesToResolveImmediately) {
resolve(name);
return;
}
atomicDecrement(&m_requestsInFlight);
}
m_names.add(name);
if (!isActive())
startOneShot(coalesceDelay);
}
void DNSResolveQueue::decrementRequestCount()
{
atomicDecrement(&m_requestsInFlight);
}
void DNSResolveQueue::fired()
{
int requestsAllowed = maxRequestsToSend - m_requestsInFlight;
for (HashSet<String>::iterator iter = m_names.begin(); iter != m_names.end() && requestsAllowed > 0; ++iter, --requestsAllowed) {
atomicIncrement(&m_requestsInFlight);
resolve(*iter);
}
m_names.clear();
}
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);
}
}