Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(921)

Unified Diff: net/dns/notify_watcher_mac.cc

Issue 9969190: [net/dns] Watch configd notifications to detect DnsConfig changes on Mac. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Fixed missing ':'. Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « net/dns/notify_watcher_mac.h ('k') | net/net.gyp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: net/dns/notify_watcher_mac.cc
diff --git a/net/dns/notify_watcher_mac.cc b/net/dns/notify_watcher_mac.cc
new file mode 100644
index 0000000000000000000000000000000000000000..07abbe4fa5d8ece8fdfcedd7bb7cf34618583bbd
--- /dev/null
+++ b/net/dns/notify_watcher_mac.cc
@@ -0,0 +1,64 @@
+// 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.
+
+#include "net/dns/notify_watcher_mac.h"
+
+#include <notify.h>
+
+#include "base/eintr_wrapper.h"
+#include "base/logging.h"
+
+namespace net {
+
+NotifyWatcherMac::NotifyWatcherMac() : notify_fd_(-1), notify_token_(-1) {}
+
+NotifyWatcherMac::~NotifyWatcherMac() {
+ Cancel();
+}
+
+bool NotifyWatcherMac::Watch(const char* key, const CallbackType& callback) {
+ DCHECK(key);
+ DCHECK(!callback.is_null());
+ Cancel();
+ uint32_t status = notify_register_file_descriptor(
+ key, &notify_fd_, 0, &notify_token_);
+ if (status != NOTIFY_STATUS_OK)
+ return false;
+ DCHECK_GE(notify_fd_, 0);
+ if (!MessageLoopForIO::current()->WatchFileDescriptor(
+ notify_fd_,
+ true,
+ MessageLoopForIO::WATCH_READ,
+ &watcher_,
+ this)) {
+ Cancel();
+ return false;
+ }
+ callback_ = callback;
+ return true;
+}
+
+void NotifyWatcherMac::Cancel() {
+ if (notify_fd_ >= 0) {
+ notify_cancel(notify_token_); // Also closes |notify_fd_|.
+ notify_fd_ = -1;
+ callback_.Reset();
+ watcher_.StopWatchingFileDescriptor();
+ }
+}
+
+void NotifyWatcherMac::OnFileCanReadWithoutBlocking(int fd) {
+ int token;
+ int status = HANDLE_EINTR(read(notify_fd_, &token, sizeof(token)));
+ if (status != sizeof(token)) {
+ Cancel();
+ callback_.Run(false);
+ return;
+ }
+ // Ignoring |token| value to avoid possible endianness mismatch:
+ // http://openradar.appspot.com/8821081
+ callback_.Run(true);
+}
+
+} // namespace net
« no previous file with comments | « net/dns/notify_watcher_mac.h ('k') | net/net.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698