notify_watcher_mac.h 1.5 KB

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