| OLD | NEW | 
|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be | 
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. | 
| 4 | 4 | 
| 5 #ifndef BASE_MESSAGE_LOOP_MESSAGE_PUMP_LIBEVENT_H_ | 5 #ifndef BASE_MESSAGE_LOOP_MESSAGE_PUMP_LIBEVENT_H_ | 
| 6 #define BASE_MESSAGE_LOOP_MESSAGE_PUMP_LIBEVENT_H_ | 6 #define BASE_MESSAGE_LOOP_MESSAGE_PUMP_LIBEVENT_H_ | 
| 7 | 7 | 
| 8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" | 
| 9 #include "base/macros.h" | 9 #include "base/macros.h" | 
| 10 #include "base/message_loop/message_pump.h" | 10 #include "base/message_loop/message_pump.h" | 
| 11 #include "base/observer_list.h" |  | 
| 12 #include "base/threading/thread_checker.h" | 11 #include "base/threading/thread_checker.h" | 
| 13 #include "base/time/time.h" | 12 #include "base/time/time.h" | 
| 14 | 13 | 
| 15 // Declare structs we need from libevent.h rather than including it | 14 // Declare structs we need from libevent.h rather than including it | 
| 16 struct event_base; | 15 struct event_base; | 
| 17 struct event; | 16 struct event; | 
| 18 | 17 | 
| 19 namespace base { | 18 namespace base { | 
| 20 | 19 | 
| 21 // Class to monitor sockets and issue callbacks when sockets are ready for I/O | 20 // Class to monitor sockets and issue callbacks when sockets are ready for I/O | 
| 22 // TODO(dkegel): add support for background file IO somehow | 21 // TODO(dkegel): add support for background file IO somehow | 
| 23 class BASE_EXPORT MessagePumpLibevent : public MessagePump { | 22 class BASE_EXPORT MessagePumpLibevent : public MessagePump { | 
| 24  public: | 23  public: | 
| 25   class IOObserver { |  | 
| 26    public: |  | 
| 27     IOObserver() {} |  | 
| 28 |  | 
| 29     // An IOObserver is an object that receives IO notifications from the |  | 
| 30     // MessagePump. |  | 
| 31     // |  | 
| 32     // NOTE: An IOObserver implementation should be extremely fast! |  | 
| 33     virtual void WillProcessIOEvent() = 0; |  | 
| 34     virtual void DidProcessIOEvent() = 0; |  | 
| 35 |  | 
| 36    protected: |  | 
| 37     virtual ~IOObserver() {} |  | 
| 38   }; |  | 
| 39 |  | 
| 40   // Used with WatchFileDescriptor to asynchronously monitor the I/O readiness | 24   // Used with WatchFileDescriptor to asynchronously monitor the I/O readiness | 
| 41   // of a file descriptor. | 25   // of a file descriptor. | 
| 42   class Watcher { | 26   class Watcher { | 
| 43    public: | 27    public: | 
| 44     // Called from MessageLoop::Run when an FD can be read from/written to | 28     // Called from MessageLoop::Run when an FD can be read from/written to | 
| 45     // without blocking | 29     // without blocking | 
| 46     virtual void OnFileCanReadWithoutBlocking(int fd) = 0; | 30     virtual void OnFileCanReadWithoutBlocking(int fd) = 0; | 
| 47     virtual void OnFileCanWriteWithoutBlocking(int fd) = 0; | 31     virtual void OnFileCanWriteWithoutBlocking(int fd) = 0; | 
| 48 | 32 | 
| 49    protected: | 33    protected: | 
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 112   // event previously attached to |controller| is aborted. | 96   // event previously attached to |controller| is aborted. | 
| 113   // Returns true on success. | 97   // Returns true on success. | 
| 114   // Must be called on the same thread the message_pump is running on. | 98   // Must be called on the same thread the message_pump is running on. | 
| 115   // TODO(dkegel): switch to edge-triggered readiness notification | 99   // TODO(dkegel): switch to edge-triggered readiness notification | 
| 116   bool WatchFileDescriptor(int fd, | 100   bool WatchFileDescriptor(int fd, | 
| 117                            bool persistent, | 101                            bool persistent, | 
| 118                            int mode, | 102                            int mode, | 
| 119                            FileDescriptorWatcher *controller, | 103                            FileDescriptorWatcher *controller, | 
| 120                            Watcher *delegate); | 104                            Watcher *delegate); | 
| 121 | 105 | 
| 122   void AddIOObserver(IOObserver* obs); |  | 
| 123   void RemoveIOObserver(IOObserver* obs); |  | 
| 124 |  | 
| 125   // MessagePump methods: | 106   // MessagePump methods: | 
| 126   void Run(Delegate* delegate) override; | 107   void Run(Delegate* delegate) override; | 
| 127   void Quit() override; | 108   void Quit() override; | 
| 128   void ScheduleWork() override; | 109   void ScheduleWork() override; | 
| 129   void ScheduleDelayedWork(const TimeTicks& delayed_work_time) override; | 110   void ScheduleDelayedWork(const TimeTicks& delayed_work_time) override; | 
| 130 | 111 | 
| 131  private: | 112  private: | 
| 132   friend class MessagePumpLibeventTest; | 113   friend class MessagePumpLibeventTest; | 
| 133 | 114 | 
| 134   void WillProcessIOEvent(); | 115   void WillProcessIOEvent(); | 
| (...skipping 26 matching lines...) Expand all  Loading... | 
| 161   // readiness callbacks when a socket is ready for I/O. | 142   // readiness callbacks when a socket is ready for I/O. | 
| 162   event_base* event_base_; | 143   event_base* event_base_; | 
| 163 | 144 | 
| 164   // ... write end; ScheduleWork() writes a single byte to it | 145   // ... write end; ScheduleWork() writes a single byte to it | 
| 165   int wakeup_pipe_in_; | 146   int wakeup_pipe_in_; | 
| 166   // ... read end; OnWakeup reads it and then breaks Run() out of its sleep | 147   // ... read end; OnWakeup reads it and then breaks Run() out of its sleep | 
| 167   int wakeup_pipe_out_; | 148   int wakeup_pipe_out_; | 
| 168   // ... libevent wrapper for read end | 149   // ... libevent wrapper for read end | 
| 169   event* wakeup_event_; | 150   event* wakeup_event_; | 
| 170 | 151 | 
| 171   ObserverList<IOObserver> io_observers_; |  | 
| 172   ThreadChecker watch_file_descriptor_caller_checker_; | 152   ThreadChecker watch_file_descriptor_caller_checker_; | 
| 173   DISALLOW_COPY_AND_ASSIGN(MessagePumpLibevent); | 153   DISALLOW_COPY_AND_ASSIGN(MessagePumpLibevent); | 
| 174 }; | 154 }; | 
| 175 | 155 | 
| 176 }  // namespace base | 156 }  // namespace base | 
| 177 | 157 | 
| 178 #endif  // BASE_MESSAGE_LOOP_MESSAGE_PUMP_LIBEVENT_H_ | 158 #endif  // BASE_MESSAGE_LOOP_MESSAGE_PUMP_LIBEVENT_H_ | 
| OLD | NEW | 
|---|