1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "net/dns/notify_watcher_mac.h"
6
7 #include <notify.h>
8
9 #include "base/check_op.h"
10 #include "base/functional/bind.h"
11 #include "base/mac/mac_util.h"
12 #include "base/posix/eintr_wrapper.h"
13
14 namespace net {
15
NotifyWatcherMac()16 NotifyWatcherMac::NotifyWatcherMac() : notify_fd_(-1), notify_token_(-1) {}
17
~NotifyWatcherMac()18 NotifyWatcherMac::~NotifyWatcherMac() {
19 Cancel();
20 }
21
Watch(const char * key,const CallbackType & callback)22 bool NotifyWatcherMac::Watch(const char* key, const CallbackType& callback) {
23 DCHECK(key);
24 DCHECK(!callback.is_null());
25 Cancel();
26 uint32_t status = notify_register_file_descriptor(
27 key, ¬ify_fd_, 0, ¬ify_token_);
28 if (status != NOTIFY_STATUS_OK)
29 return false;
30 DCHECK_GE(notify_fd_, 0);
31 watcher_ = base::FileDescriptorWatcher::WatchReadable(
32 notify_fd_,
33 base::BindRepeating(&NotifyWatcherMac::OnFileCanReadWithoutBlocking,
34 base::Unretained(this)));
35 callback_ = callback;
36 return true;
37 }
38
Cancel()39 void NotifyWatcherMac::Cancel() {
40 if (notify_fd_ >= 0) {
41 watcher_.reset();
42 notify_cancel(notify_token_); // Also closes |notify_fd_|.
43 notify_fd_ = -1;
44 callback_.Reset();
45 }
46 }
47
OnFileCanReadWithoutBlocking()48 void NotifyWatcherMac::OnFileCanReadWithoutBlocking() {
49 int token;
50 int status = HANDLE_EINTR(read(notify_fd_, &token, sizeof(token)));
51 if (status != sizeof(token)) {
52 Cancel();
53 callback_.Run(false);
54 return;
55 }
56 // Ignoring |token| value to avoid possible endianness mismatch:
57 // https://openradar.appspot.com/8821081
58 callback_.Run(true);
59 }
60
61 } // namespace net
62