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