watchable_io_message_pump_posix.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // Copyright 2018 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 BASE_MESSAGE_LOOP_WATCHABLE_IO_MESSAGE_PUMP_POSIX_H_
  5. #define BASE_MESSAGE_LOOP_WATCHABLE_IO_MESSAGE_PUMP_POSIX_H_
  6. #include "base/location.h"
  7. namespace base {
  8. class WatchableIOMessagePumpPosix {
  9. public:
  10. // Used with WatchFileDescriptor to asynchronously monitor the I/O readiness
  11. // of a file descriptor.
  12. class FdWatcher {
  13. public:
  14. virtual void OnFileCanReadWithoutBlocking(int fd) = 0;
  15. virtual void OnFileCanWriteWithoutBlocking(int fd) = 0;
  16. protected:
  17. virtual ~FdWatcher() = default;
  18. };
  19. class FdWatchControllerInterface {
  20. public:
  21. explicit FdWatchControllerInterface(const Location& from_here);
  22. FdWatchControllerInterface(const FdWatchControllerInterface&) = delete;
  23. FdWatchControllerInterface& operator=(const FdWatchControllerInterface&) =
  24. delete;
  25. // Subclasses must call StopWatchingFileDescriptor() in their destructor
  26. // (this parent class cannot generically do it for them as it must usually
  27. // be invoked before they destroy their state which happens before the
  28. // parent destructor is invoked).
  29. virtual ~FdWatchControllerInterface();
  30. // NOTE: This method isn't called StopWatching() to avoid confusion with the
  31. // win32 ObjectWatcher class. While this doesn't really need to be virtual
  32. // as there's only one impl per platform and users don't use pointers to the
  33. // base class. Having this interface forces implementers to share similar
  34. // implementations (a problem in the past).
  35. // Stop watching the FD, always safe to call. No-op if there's nothing to
  36. // do.
  37. virtual bool StopWatchingFileDescriptor() = 0;
  38. const Location& created_from_location() const {
  39. return created_from_location_;
  40. }
  41. private:
  42. const Location created_from_location_;
  43. };
  44. enum Mode {
  45. WATCH_READ = 1 << 0,
  46. WATCH_WRITE = 1 << 1,
  47. WATCH_READ_WRITE = WATCH_READ | WATCH_WRITE
  48. };
  49. // Every subclass of WatchableIOMessagePumpPosix must provide a
  50. // WatchFileDescriptor() which has the following signature where
  51. // |FdWatchController| must be the complete type based on
  52. // FdWatchControllerInterface.
  53. // Registers |delegate| with the current thread's message loop so that its
  54. // methods are invoked when file descriptor |fd| becomes ready for reading or
  55. // writing (or both) without blocking. |mode| selects ready for reading, for
  56. // writing, or both. See "enum Mode" above. |controller| manages the
  57. // lifetime of registrations. ("Registrations" are also ambiguously called
  58. // "events" in many places, for instance in libevent.) It is an error to use
  59. // the same |controller| for different file descriptors; however, the same
  60. // controller can be reused to add registrations with a different |mode|. If
  61. // |controller| is already attached to one or more registrations, the new
  62. // registration is added onto those. If an error occurs while calling this
  63. // method, any registration previously attached to |controller| is removed.
  64. // Returns true on success. Must be called on the same thread the MessagePump
  65. // is running on.
  66. // bool WatchFileDescriptor(int fd,
  67. // bool persistent,
  68. // int mode,
  69. // FdWatchController* controller,
  70. // FdWatcher* delegate) = 0;
  71. };
  72. } // namespace base
  73. #endif // BASE_MESSAGE_LOOP_WATCHABLE_IO_MESSAGE_PUMP_POSIX_H_