xref: /aosp_15_r20/external/cronet/net/dns/notify_watcher_mac.h (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 #ifndef NET_DNS_NOTIFY_WATCHER_MAC_H_
6 #define NET_DNS_NOTIFY_WATCHER_MAC_H_
7 
8 #include <memory>
9 
10 #include "base/files/file_descriptor_watcher_posix.h"
11 #include "base/functional/callback.h"
12 
13 namespace net {
14 
15 // Watches for notifications from Libnotify and delivers them to a Callback.
16 // After failure the watch is cancelled and will have to be restarted.
17 class NotifyWatcherMac {
18  public:
19   // Called on received notification with true on success and false on error.
20   typedef base::RepeatingCallback<void(bool succeeded)> CallbackType;
21 
22   NotifyWatcherMac();
23 
24   NotifyWatcherMac(const NotifyWatcherMac&) = delete;
25   NotifyWatcherMac& operator=(const NotifyWatcherMac&) = delete;
26 
27   // When deleted, automatically cancels.
28   virtual ~NotifyWatcherMac();
29 
30   // Registers for notifications for |key|. Returns true if succeeds. If so,
31   // will deliver asynchronous notifications and errors to |callback|.
32   bool Watch(const char* key, const CallbackType& callback);
33 
34   // Cancels the watch.
35   void Cancel();
36 
37  private:
38   // Called by |watcher_| when |notify_fd_| can be read without blocking.
39   void OnFileCanReadWithoutBlocking();
40 
41   CallbackType CancelInternal();
42 
43   int notify_fd_;
44   int notify_token_;
45   CallbackType callback_;
46   std::unique_ptr<base::FileDescriptorWatcher::Controller> watcher_;
47 };
48 
49 }  // namespace net
50 
51 #endif  // NET_DNS_NOTIFY_WATCHER_MAC_H_
52