1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "net/dns/notify_watcher_mac.h"
9 #include "base/logging.h"
10 #include "base/posix/eintr_wrapper.h"
14 NotifyWatcherMac::NotifyWatcherMac() : notify_fd_(-1), notify_token_(-1) {}
16 NotifyWatcherMac::~NotifyWatcherMac() {
20 bool NotifyWatcherMac::Watch(const char* key
, const CallbackType
& callback
) {
22 DCHECK(!callback
.is_null());
24 uint32_t status
= notify_register_file_descriptor(
25 key
, ¬ify_fd_
, 0, ¬ify_token_
);
26 if (status
!= NOTIFY_STATUS_OK
)
28 DCHECK_GE(notify_fd_
, 0);
29 if (!base::MessageLoopForIO::current()->WatchFileDescriptor(
32 base::MessageLoopForIO::WATCH_READ
,
42 void NotifyWatcherMac::Cancel() {
43 if (notify_fd_
>= 0) {
44 notify_cancel(notify_token_
); // Also closes |notify_fd_|.
47 watcher_
.StopWatchingFileDescriptor();
51 void NotifyWatcherMac::OnFileCanReadWithoutBlocking(int fd
) {
53 int status
= HANDLE_EINTR(read(notify_fd_
, &token
, sizeof(token
)));
54 if (status
!= sizeof(token
)) {
59 // Ignoring |token| value to avoid possible endianness mismatch:
60 // http://openradar.appspot.com/8821081