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