| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 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_PUMP_LIBEVENT_H_ | 5 #ifndef BASE_MESSAGE_PUMP_IO_IOS_H_ |
| 6 #define BASE_MESSAGE_PUMP_LIBEVENT_H_ | 6 #define BASE_MESSAGE_PUMP_IO_IOS_H_ |
| 7 | 7 |
| 8 #include "base/basictypes.h" | 8 #include "base/base_export.h" |
| 9 #include "base/compiler_specific.h" | 9 #include "base/mac/scoped_cffiledescriptorref.h" |
| 10 #include "base/memory/weak_ptr.h" | 10 #include "base/mac/scoped_cftyperef.h" |
| 11 #include "base/message_pump.h" | 11 #include "base/message_pump_mac.h" |
| 12 #include "base/observer_list.h" | 12 #include "base/observer_list.h" |
| 13 #include "base/threading/thread_checker.h" | |
| 14 #include "base/time.h" | |
| 15 | |
| 16 // Declare structs we need from libevent.h rather than including it | |
| 17 struct event_base; | |
| 18 struct event; | |
| 19 | 13 |
| 20 namespace base { | 14 namespace base { |
| 21 | 15 |
| 22 // Class to monitor sockets and issue callbacks when sockets are ready for I/O | 16 // This file introduces a class to monitor sockets and issue callbacks when |
| 23 // TODO(dkegel): add support for background file IO somehow | 17 // sockets are ready for I/O on iOS. |
| 24 class BASE_EXPORT MessagePumpLibevent : public MessagePump { | 18 class BASE_EXPORT MessagePumpIOSForIO : public MessagePumpNSRunLoop { |
| 25 public: | 19 public: |
| 26 class IOObserver { | 20 class IOObserver { |
| 27 public: | 21 public: |
| 28 IOObserver() {} | 22 IOObserver() {} |
| 29 | 23 |
| 30 // An IOObserver is an object that receives IO notifications from the | 24 // An IOObserver is an object that receives IO notifications from the |
| 31 // MessagePump. | 25 // MessagePump. |
| 32 // | 26 // |
| 33 // NOTE: An IOObserver implementation should be extremely fast! | 27 // NOTE: An IOObserver implementation should be extremely fast! |
| 34 virtual void WillProcessIOEvent() = 0; | 28 virtual void WillProcessIOEvent() = 0; |
| 35 virtual void DidProcessIOEvent() = 0; | 29 virtual void DidProcessIOEvent() = 0; |
| 36 | 30 |
| 37 protected: | 31 protected: |
| 38 virtual ~IOObserver() {} | 32 virtual ~IOObserver() {} |
| 39 }; | 33 }; |
| 40 | 34 |
| 41 class FileDescriptorWatcher; | |
| 42 | |
| 43 // Used with WatchFileDescriptor to asynchronously monitor the I/O readiness | 35 // Used with WatchFileDescriptor to asynchronously monitor the I/O readiness |
| 44 // of a file descriptor. | 36 // of a file descriptor. |
| 45 class Watcher { | 37 class Watcher { |
| 46 public: | 38 public: |
| 47 // Called from MessageLoop::Run when an FD can be read from/written to | 39 // Called from MessageLoop::Run when an FD can be read from/written to |
| 48 // without blocking | 40 // without blocking |
| 49 virtual void OnFileCanReadWithoutBlocking(int fd) = 0; | 41 virtual void OnFileCanReadWithoutBlocking(int fd) = 0; |
| 50 virtual void OnFileCanWriteWithoutBlocking(int fd) = 0; | 42 virtual void OnFileCanWriteWithoutBlocking(int fd) = 0; |
| 51 | 43 |
| 52 protected: | 44 protected: |
| 53 virtual ~Watcher() {} | 45 virtual ~Watcher() {} |
| 54 }; | 46 }; |
| 55 | 47 |
| 56 // Object returned by WatchFileDescriptor to manage further watching. | 48 // Object returned by WatchFileDescriptor to manage further watching. |
| 57 class FileDescriptorWatcher { | 49 class FileDescriptorWatcher { |
| 58 public: | 50 public: |
| 59 FileDescriptorWatcher(); | 51 FileDescriptorWatcher(); |
| 60 ~FileDescriptorWatcher(); // Implicitly calls StopWatchingFileDescriptor. | 52 ~FileDescriptorWatcher(); // Implicitly calls StopWatchingFileDescriptor. |
| 61 | 53 |
| 62 // NOTE: These methods aren't called StartWatching()/StopWatching() to | 54 // NOTE: These methods aren't called StartWatching()/StopWatching() to |
| 63 // avoid confusion with the win32 ObjectWatcher class. | 55 // avoid confusion with the win32 ObjectWatcher class. |
| 64 | 56 |
| 65 // Stop watching the FD, always safe to call. No-op if there's nothing | 57 // Stop watching the FD, always safe to call. No-op if there's nothing |
| 66 // to do. | 58 // to do. |
| 67 bool StopWatchingFileDescriptor(); | 59 bool StopWatchingFileDescriptor(); |
| 68 | 60 |
| 69 private: | 61 private: |
| 70 friend class MessagePumpLibevent; | 62 friend class MessagePumpIOSForIO; |
| 71 friend class MessagePumpLibeventTest; | 63 friend class MessagePumpIOSForIOTest; |
| 72 | 64 |
| 73 // Called by MessagePumpLibevent, ownership of |e| is transferred to this | 65 // Called by MessagePumpIOSForIO, ownership of |fdref| and |fd_source| |
| 74 // object. | 66 // is transferred to this object. |
| 75 void Init(event* e); | 67 void Init(CFFileDescriptorRef fdref, |
| 68 CFOptionFlags callback_types, |
| 69 CFRunLoopSourceRef fd_source, |
| 70 bool is_persistent); |
| 76 | 71 |
| 77 // Used by MessagePumpLibevent to take ownership of event_. | 72 void set_pump(MessagePumpIOSForIO* pump) { pump_ = pump; } |
| 78 event* ReleaseEvent(); | 73 MessagePumpIOSForIO* pump() const { return pump_; } |
| 79 | |
| 80 void set_pump(MessagePumpLibevent* pump) { pump_ = pump; } | |
| 81 MessagePumpLibevent* pump() { return pump_; } | |
| 82 | 74 |
| 83 void set_watcher(Watcher* watcher) { watcher_ = watcher; } | 75 void set_watcher(Watcher* watcher) { watcher_ = watcher; } |
| 84 | 76 |
| 85 void OnFileCanReadWithoutBlocking(int fd, MessagePumpLibevent* pump); | 77 void OnFileCanReadWithoutBlocking(int fd, MessagePumpIOSForIO* pump); |
| 86 void OnFileCanWriteWithoutBlocking(int fd, MessagePumpLibevent* pump); | 78 void OnFileCanWriteWithoutBlocking(int fd, MessagePumpIOSForIO* pump); |
| 87 | 79 |
| 88 event* event_; | 80 bool is_persistent_; // false if this event is one-shot. |
| 89 MessagePumpLibevent* pump_; | 81 base::mac::ScopedCFFileDescriptorRef fdref_; |
| 82 CFOptionFlags callback_types_; |
| 83 base::mac::ScopedCFTypeRef<CFRunLoopSourceRef> fd_source_; |
| 84 MessagePumpIOSForIO* pump_; |
| 90 Watcher* watcher_; | 85 Watcher* watcher_; |
| 91 base::WeakPtrFactory<FileDescriptorWatcher> weak_factory_; | |
| 92 | 86 |
| 93 DISALLOW_COPY_AND_ASSIGN(FileDescriptorWatcher); | 87 DISALLOW_COPY_AND_ASSIGN(FileDescriptorWatcher); |
| 94 }; | 88 }; |
| 95 | 89 |
| 96 enum Mode { | 90 enum Mode { |
| 97 WATCH_READ = 1 << 0, | 91 WATCH_READ = 1 << 0, |
| 98 WATCH_WRITE = 1 << 1, | 92 WATCH_WRITE = 1 << 1, |
| 99 WATCH_READ_WRITE = WATCH_READ | WATCH_WRITE | 93 WATCH_READ_WRITE = WATCH_READ | WATCH_WRITE |
| 100 }; | 94 }; |
| 101 | 95 |
| 102 MessagePumpLibevent(); | 96 MessagePumpIOSForIO(); |
| 103 | 97 |
| 104 // Have the current thread's message loop watch for a a situation in which | 98 // Have the current thread's message loop watch for a a situation in which |
| 105 // reading/writing to the FD can be performed without blocking. | 99 // reading/writing to the FD can be performed without blocking. |
| 106 // Callers must provide a preallocated FileDescriptorWatcher object which | 100 // Callers must provide a preallocated FileDescriptorWatcher object which |
| 107 // can later be used to manage the lifetime of this event. | 101 // can later be used to manage the lifetime of this event. |
| 108 // If a FileDescriptorWatcher is passed in which is already attached to | 102 // If a FileDescriptorWatcher is passed in which is already attached to |
| 109 // an event, then the effect is cumulative i.e. after the call |controller| | 103 // an event, then the effect is cumulative i.e. after the call |controller| |
| 110 // will watch both the previous event and the new one. | 104 // will watch both the previous event and the new one. |
| 111 // If an error occurs while calling this method in a cumulative fashion, the | 105 // If an error occurs while calling this method in a cumulative fashion, the |
| 112 // event previously attached to |controller| is aborted. | 106 // event previously attached to |controller| is aborted. |
| 113 // Returns true on success. | 107 // Returns true on success. |
| 114 // Must be called on the same thread the message_pump is running on. | 108 // Must be called on the same thread the message_pump is running on. |
| 115 // TODO(dkegel): switch to edge-triggered readiness notification | |
| 116 bool WatchFileDescriptor(int fd, | 109 bool WatchFileDescriptor(int fd, |
| 117 bool persistent, | 110 bool persistent, |
| 118 Mode mode, | 111 int mode, |
| 119 FileDescriptorWatcher *controller, | 112 FileDescriptorWatcher *controller, |
| 120 Watcher *delegate); | 113 Watcher *delegate); |
| 121 | 114 |
| 115 void RemoveRunLoopSource(CFRunLoopSourceRef source); |
| 116 |
| 122 void AddIOObserver(IOObserver* obs); | 117 void AddIOObserver(IOObserver* obs); |
| 123 void RemoveIOObserver(IOObserver* obs); | 118 void RemoveIOObserver(IOObserver* obs); |
| 124 | 119 |
| 125 // MessagePump methods: | |
| 126 virtual void Run(Delegate* delegate) OVERRIDE; | |
| 127 virtual void Quit() OVERRIDE; | |
| 128 virtual void ScheduleWork() OVERRIDE; | |
| 129 virtual void ScheduleDelayedWork(const TimeTicks& delayed_work_time) OVERRIDE; | |
| 130 | |
| 131 protected: | 120 protected: |
| 132 virtual ~MessagePumpLibevent(); | 121 virtual ~MessagePumpIOSForIO(); |
| 133 | 122 |
| 134 private: | 123 private: |
| 135 friend class MessagePumpLibeventTest; | 124 friend class MessagePumpIOSForIOTest; |
| 136 | 125 |
| 137 void WillProcessIOEvent(); | 126 void WillProcessIOEvent(); |
| 138 void DidProcessIOEvent(); | 127 void DidProcessIOEvent(); |
| 139 | 128 |
| 140 // Risky part of constructor. Returns true on success. | 129 static void HandleFdIOEvent(CFFileDescriptorRef fdref, |
| 141 bool Init(); | 130 CFOptionFlags callback_types, |
| 142 | 131 void* context); |
| 143 // Called by libevent to tell us a registered FD can be read/written to. | |
| 144 static void OnLibeventNotification(int fd, short flags, | |
| 145 void* context); | |
| 146 | |
| 147 // Unix pipe used to implement ScheduleWork() | |
| 148 // ... callback; called by libevent inside Run() when pipe is ready to read | |
| 149 static void OnWakeup(int socket, short flags, void* context); | |
| 150 | |
| 151 // This flag is set to false when Run should return. | |
| 152 bool keep_running_; | |
| 153 | |
| 154 // This flag is set when inside Run. | |
| 155 bool in_run_; | |
| 156 | |
| 157 // This flag is set if libevent has processed I/O events. | |
| 158 bool processed_io_events_; | |
| 159 | |
| 160 // The time at which we should call DoDelayedWork. | |
| 161 TimeTicks delayed_work_time_; | |
| 162 | |
| 163 // Libevent dispatcher. Watches all sockets registered with it, and sends | |
| 164 // readiness callbacks when a socket is ready for I/O. | |
| 165 event_base* event_base_; | |
| 166 | |
| 167 // ... write end; ScheduleWork() writes a single byte to it | |
| 168 int wakeup_pipe_in_; | |
| 169 // ... read end; OnWakeup reads it and then breaks Run() out of its sleep | |
| 170 int wakeup_pipe_out_; | |
| 171 // ... libevent wrapper for read end | |
| 172 event* wakeup_event_; | |
| 173 | 132 |
| 174 ObserverList<IOObserver> io_observers_; | 133 ObserverList<IOObserver> io_observers_; |
| 175 ThreadChecker watch_file_descriptor_caller_checker_; | 134 ThreadChecker watch_file_descriptor_caller_checker_; |
| 176 DISALLOW_COPY_AND_ASSIGN(MessagePumpLibevent); | 135 |
| 136 DISALLOW_COPY_AND_ASSIGN(MessagePumpIOSForIO); |
| 177 }; | 137 }; |
| 178 | 138 |
| 179 } // namespace base | 139 } // namespace base |
| 180 | 140 |
| 181 #endif // BASE_MESSAGE_PUMP_LIBEVENT_H_ | 141 #endif // BASE_MESSAGE_PUMP_IO_IOS_H_ |
| OLD | NEW |