Chromium Code Reviews| 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..02bd4e4b04cc1835345142627c51f52830c050eb |
| --- /dev/null |
| +++ b/net/dns/notify_watcher_mac.cc |
| @@ -0,0 +1,61 @@ |
| +// 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> |
| + |
| +namespace net { |
| + |
| +NotifyWatcherMac::NotifyWatcherMac() : notify_fd_(-1), notify_token_(-1) {} |
| + |
| +NotifyWatcherMac::~NotifyWatcherMac() { |
| + Cancel(); |
| +} |
| + |
| +bool NotifyWatcherMac::Watch(const char* key, const CallbackType& callback) { |
| + DCHECK(key); |
|
Mark Mentovai
2012/04/17 16:27:43
#include "base/logging.h".
|
| + DCHECK(!callback.is_null()); |
| + Cancel(); |
| + uint32_t status = notify_register_file_descriptor( |
| + key, ¬ify_fd_, 0, ¬ify_token_); |
| + if (status != NOTIFY_STATUS_OK) |
| + return false; |
| + 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(); |
|
Mark Mentovai
2012/04/17 16:27:43
Would you need to reset this even if notify_fd_ <=
szym
2012/04/17 19:39:31
This is set in line 34, only after status == NOTIF
|
| + watcher_.StopWatchingFileDescriptor(); |
|
Mark Mentovai
2012/04/17 16:27:43
Is this safe if you got to Cancel() because WatchF
szym
2012/04/17 19:39:31
This is always safe to call, see message_pump_libe
|
| + } |
| +} |
| + |
| +void NotifyWatcherMac::OnFileCanReadWithoutBlocking(int /* fd */) { |
|
Mark Mentovai
2012/04/17 16:27:43
No /* */ necessary
|
| + int token; |
| + int status = read(notify_fd_, &token, sizeof(token)); |
|
Mark Mentovai
2012/04/17 16:27:43
Always write HANDLE_EINTR around read. #include "b
|
| + if (status < 0) { |
| + Cancel(); |
| + callback_.Run(false); |
| + return; |
| + } |
| + // Ignoring |token| read to avoid possible endianness mismatch: |
|
Mark Mentovai
2012/04/17 16:27:43
read -> value
|
| + // http://openradar.appspot.com/8821081 |
| + callback_.Run(true); |
| +} |
| + |
| +} // namespace net |
| + |
|
Mark Mentovai
2012/04/17 16:27:43
No blank line at EOF.
szym
2012/04/17 19:39:31
As before. Lint complained about that.
|