[net/dns] Watch configd notifications to detect DnsConfig changes on Mac.
libresolv communicates with configd via the BSD libnotify mechanism. Using the
same interface, DnsConfigService can receive signal at the exact moment new
configuration is available to libresolv, then read it using res_ninit.
BUG=121126
TEST=Manually poke at system configuration while DnsConfigService is running.
Review URL: http://codereview.chromium.org/9969190
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@132698 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/net/dns/notify_watcher_mac.h b/net/dns/notify_watcher_mac.h
new file mode 100644
index 0000000..57562bd
--- /dev/null
+++ b/net/dns/notify_watcher_mac.h
@@ -0,0 +1,48 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef NET_DNS_NOTIFY_WATCHER_MAC_H_
+#define NET_DNS_NOTIFY_WATCHER_MAC_H_
+#pragma once
+
+#include "base/callback.h"
+#include "base/message_loop.h"
+
+namespace net {
+
+// Watches for notifications from Libnotify and delivers them to a Callback.
+// After failure the watch is cancelled and will have to be restarted.
+class NotifyWatcherMac : public MessageLoopForIO::Watcher {
+ public:
+ // Called on received notification with true on success and false on error.
+ typedef base::Callback<void(bool succeeded)> CallbackType;
+
+ NotifyWatcherMac();
+
+ // When deleted, automatically cancels.
+ virtual ~NotifyWatcherMac();
+
+ // Registers for notifications for |key|. Returns true if succeeds. If so,
+ // will deliver asynchronous notifications and errors to |callback|.
+ bool Watch(const char* key, const CallbackType& callback);
+
+ // Cancels the watch.
+ void Cancel();
+
+ private:
+ // MessageLoopForIO::Watcher:
+ virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE;
+ virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE {}
+
+ int notify_fd_;
+ int notify_token_;
+ CallbackType callback_;
+ MessageLoopForIO::FileDescriptorWatcher watcher_;
+
+ DISALLOW_COPY_AND_ASSIGN(NotifyWatcherMac);
+};
+
+} // namespace net
+
+#endif // NET_DNS_NOTIFY_WATCHER_MAC_H_