This source file includes following definitions.
- OnDNSChanged
- MaybeReload
- SlotReturnFunction
- EnsureDnsReloaderInit
- DnsReloaderMaybeReload
#include "net/base/dns_reloader.h"
#if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_OPENBSD) && \
!defined(OS_ANDROID)
#include <resolv.h>
#include "base/basictypes.h"
#include "base/lazy_instance.h"
#include "base/logging.h"
#include "base/message_loop/message_loop.h"
#include "base/synchronization/lock.h"
#include "base/threading/thread_local_storage.h"
#include "net/base/network_change_notifier.h"
namespace {
class DnsReloader : public net::NetworkChangeNotifier::DNSObserver {
public:
struct ReloadState {
int resolver_generation;
};
virtual void OnDNSChanged() OVERRIDE {
DCHECK(base::MessageLoopForIO::IsCurrent());
base::AutoLock l(lock_);
resolver_generation_++;
}
void MaybeReload() {
ReloadState* reload_state = static_cast<ReloadState*>(tls_index_.Get());
base::AutoLock l(lock_);
if (!reload_state) {
reload_state = new ReloadState();
reload_state->resolver_generation = resolver_generation_;
res_ninit(&_res);
tls_index_.Set(reload_state);
} else if (reload_state->resolver_generation != resolver_generation_) {
reload_state->resolver_generation = resolver_generation_;
res_nclose(&_res);
res_ninit(&_res);
}
}
static void SlotReturnFunction(void* data) {
ReloadState* reload_state = static_cast<ReloadState*>(data);
if (reload_state)
res_nclose(&_res);
delete reload_state;
}
private:
DnsReloader() : resolver_generation_(0) {
tls_index_.Initialize(SlotReturnFunction);
net::NetworkChangeNotifier::AddDNSObserver(this);
}
virtual ~DnsReloader() {
NOTREACHED();
}
base::Lock lock_;
int resolver_generation_;
friend struct base::DefaultLazyInstanceTraits<DnsReloader>;
static base::ThreadLocalStorage::StaticSlot tls_index_;
DISALLOW_COPY_AND_ASSIGN(DnsReloader);
};
base::ThreadLocalStorage::StaticSlot DnsReloader::tls_index_ = TLS_INITIALIZER;
base::LazyInstance<DnsReloader>::Leaky
g_dns_reloader = LAZY_INSTANCE_INITIALIZER;
}
namespace net {
void EnsureDnsReloaderInit() {
DnsReloader* t ALLOW_UNUSED = g_dns_reloader.Pointer();
}
void DnsReloaderMaybeReload() {
DnsReloader* dns_reloader = g_dns_reloader.Pointer();
dns_reloader->MaybeReload();
}
}
#endif