This source file includes following definitions.
- DynamicStoreCallback
- weak_factory_
- Init
- CleanUp
- InitNotifications
#include "net/base/network_config_watcher_mac.h"
#include <algorithm>
#include "base/bind.h"
#include "base/compiler_specific.h"
#include "base/memory/weak_ptr.h"
#include "base/message_loop/message_loop.h"
#include "base/threading/thread.h"
#include "base/threading/thread_restrictions.h"
namespace net {
namespace {
#if !defined(OS_IOS)
void DynamicStoreCallback(SCDynamicStoreRef ,
CFArrayRef changed_keys,
void* config_delegate) {
NetworkConfigWatcherMac::Delegate* net_config_delegate =
static_cast<NetworkConfigWatcherMac::Delegate*>(config_delegate);
net_config_delegate->OnNetworkConfigChange(changed_keys);
}
#endif
class NetworkConfigWatcherMacThread : public base::Thread {
public:
NetworkConfigWatcherMacThread(NetworkConfigWatcherMac::Delegate* delegate);
virtual ~NetworkConfigWatcherMacThread();
protected:
virtual void Init() OVERRIDE;
virtual void CleanUp() OVERRIDE;
private:
void InitNotifications();
base::ScopedCFTypeRef<CFRunLoopSourceRef> run_loop_source_;
NetworkConfigWatcherMac::Delegate* const delegate_;
base::WeakPtrFactory<NetworkConfigWatcherMacThread> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(NetworkConfigWatcherMacThread);
};
NetworkConfigWatcherMacThread::NetworkConfigWatcherMacThread(
NetworkConfigWatcherMac::Delegate* delegate)
: base::Thread("NetworkConfigWatcher"),
delegate_(delegate),
weak_factory_(this) {}
NetworkConfigWatcherMacThread::~NetworkConfigWatcherMacThread() {
base::ThreadRestrictions::ScopedAllowIO allow_io;
Stop();
}
void NetworkConfigWatcherMacThread::Init() {
base::ThreadRestrictions::SetIOAllowed(false);
delegate_->Init();
const base::TimeDelta kInitializationDelay = base::TimeDelta::FromSeconds(1);
message_loop()->PostDelayedTask(
FROM_HERE,
base::Bind(&NetworkConfigWatcherMacThread::InitNotifications,
weak_factory_.GetWeakPtr()),
kInitializationDelay);
}
void NetworkConfigWatcherMacThread::CleanUp() {
if (!run_loop_source_.get())
return;
CFRunLoopRemoveSource(CFRunLoopGetCurrent(), run_loop_source_.get(),
kCFRunLoopCommonModes);
run_loop_source_.reset();
}
void NetworkConfigWatcherMacThread::InitNotifications() {
#if !defined(OS_IOS)
SCDynamicStoreContext context = {
0,
delegate_,
NULL,
NULL,
NULL,
};
base::ScopedCFTypeRef<SCDynamicStoreRef> store(SCDynamicStoreCreate(
NULL, CFSTR("org.chromium"), DynamicStoreCallback, &context));
run_loop_source_.reset(SCDynamicStoreCreateRunLoopSource(
NULL, store.get(), 0));
CFRunLoopAddSource(CFRunLoopGetCurrent(), run_loop_source_.get(),
kCFRunLoopCommonModes);
#endif
delegate_->StartReachabilityNotifications();
#if !defined(OS_IOS)
delegate_->SetDynamicStoreNotificationKeys(store.get());
#endif
}
}
NetworkConfigWatcherMac::NetworkConfigWatcherMac(Delegate* delegate)
: notifier_thread_(new NetworkConfigWatcherMacThread(delegate)) {
base::Thread::Options thread_options(base::MessageLoop::TYPE_UI, 0);
notifier_thread_->StartWithOptions(thread_options);
}
NetworkConfigWatcherMac::~NetworkConfigWatcherMac() {}
}