xref: /aosp_15_r20/external/cronet/net/dns/notify_watcher_mac.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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, &notify_fd_, 0, &notify_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     CancelInternal();
42   }
43 }
44 
OnFileCanReadWithoutBlocking()45 void NotifyWatcherMac::OnFileCanReadWithoutBlocking() {
46   int token;
47   int status = HANDLE_EINTR(read(notify_fd_, &token, sizeof(token)));
48   if (status != sizeof(token)) {
49     CancelInternal().Run(false);
50     return;
51   }
52   // Ignoring |token| value to avoid possible endianness mismatch:
53   // https://openradar.appspot.com/8821081
54   callback_.Run(true);
55 }
56 
CancelInternal()57 NotifyWatcherMac::CallbackType NotifyWatcherMac::CancelInternal() {
58   DCHECK_GE(notify_fd_, 0);
59 
60   watcher_.reset();
61   notify_cancel(notify_token_);  // Also closes |notify_fd_|.
62   notify_fd_ = -1;
63 
64   return std::move(callback_);
65 }
66 
67 }  // namespace net
68