Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef BASE_MESSAGE_PUMP_IO_IOS_H_ | |
| 6 #define BASE_MESSAGE_PUMP_IO_IOS_H_ | |
| 7 | |
| 8 #include "base/mac/scoped_cffiledescriptorref.h" | |
| 9 #include "base/mac/scoped_cftyperef.h" | |
| 10 #include "base/message_pump_mac.h" | |
| 11 #include "base/observer_list.h" | |
| 12 | |
| 13 namespace base { | |
| 14 | |
| 15 // This file introduces a class to monitor sockets and issue callbacks when | |
| 16 // sockets are ready for I/O on iOS. | |
| 17 class MessagePumpIOSForIO : public MessagePumpNSRunLoop { | |
| 18 public: | |
| 19 class IOObserver { | |
| 20 public: | |
| 21 IOObserver() {} | |
| 22 | |
| 23 // An IOObserver is an object that receives IO notifications from the | |
| 24 // MessagePump. | |
| 25 // | |
| 26 // NOTE: An IOObserver implementation should be extremely fast! | |
| 27 virtual void WillProcessIOEvent() = 0; | |
| 28 virtual void DidProcessIOEvent() = 0; | |
| 29 | |
| 30 protected: | |
| 31 virtual ~IOObserver() {} | |
| 32 }; | |
| 33 | |
| 34 // Used with WatchFileDescptor to asynchronously monitor the I/O readiness of | |
| 35 // a File Descriptor. | |
| 36 class Watcher { | |
| 37 public: | |
| 38 virtual ~Watcher() {} | |
| 39 | |
| 40 // Called from MessageLoop::Run when an FD can be read from/written to | |
| 41 // without blocking | |
| 42 virtual void OnFileCanReadWithoutBlocking(int fd) = 0; | |
| 43 virtual void OnFileCanWriteWithoutBlocking(int fd) = 0; | |
| 44 }; | |
| 45 | |
| 46 // Object returned by WatchFileDescriptor to manage further watching. | |
| 47 class FileDescriptorWatcher { | |
| 48 public: | |
| 49 FileDescriptorWatcher(); | |
| 50 ~FileDescriptorWatcher(); // Implicitly calls StopWatchingFileDescriptor. | |
| 51 | |
| 52 // NOTE: These methods aren't called StartWatching()/StopWatching() to | |
| 53 // avoid confusion with the win32 ObjectWatcher class. | |
| 54 | |
| 55 // Stop watching the FD, always safe to call. No-op if there's nothing | |
| 56 // to do. | |
| 57 bool StopWatchingFileDescriptor(); | |
| 58 | |
| 59 private: | |
| 60 friend class MessagePumpIOSForIO; | |
| 61 | |
| 62 // Called by MessagePumpIOSForIO, ownership of |fdref| and |fd_source| | |
| 63 // is transferred to this object. | |
| 64 void Init(CFFileDescriptorRef fdref, | |
| 65 CFOptionFlags callback_types, | |
| 66 CFRunLoopSourceRef fd_source, | |
| 67 bool is_persistent); | |
| 68 | |
| 69 void set_pump(MessagePumpIOSForIO* pump) { pump_ = pump; } | |
| 70 MessagePumpIOSForIO* pump() const { return pump_; } | |
| 71 | |
| 72 void set_watcher(Watcher* watcher) { watcher_ = watcher; } | |
| 73 | |
| 74 void OnFileCanReadWithoutBlocking(int fd, MessagePumpIOSForIO* pump); | |
| 75 void OnFileCanWriteWithoutBlocking(int fd, MessagePumpIOSForIO* pump); | |
| 76 | |
| 77 bool is_persistent_; // false if this event is one-shot. | |
| 78 base::mac::ScopedCFFileDescriptorRef fdref_; | |
| 79 CFOptionFlags callback_types_; | |
| 80 base::mac::ScopedCFTypeRef<CFRunLoopSourceRef> fd_source_; | |
| 81 MessagePumpIOSForIO* pump_; | |
| 82 Watcher* watcher_; | |
| 83 | |
| 84 DISALLOW_COPY_AND_ASSIGN(FileDescriptorWatcher); | |
| 85 }; | |
| 86 | |
| 87 enum Mode { | |
| 88 WATCH_READ = 1 << 0, | |
| 89 WATCH_WRITE = 1 << 1 | |
| 90 }; | |
| 91 | |
| 92 MessagePumpIOSForIO(); | |
| 93 | |
| 94 // Have the current thread's message loop watch for a a situation in which | |
| 95 // reading/writing to the FD can be performed without blocking. | |
| 96 // Callers must provide a preallocated FileDescriptorWatcher object which | |
| 97 // can later be used to manage the lifetime of this event. | |
| 98 // If a FileDescriptorWatcher is passed in which is already attached to | |
| 99 // an event, then the effect is cumulative i.e. after the call |controller| | |
| 100 // will watch both the previous event and the new one. | |
| 101 // If an error occurs while calling this method in a cumulative fashion, the | |
| 102 // event previously attached to |controller| is aborted. | |
| 103 // Returns true on success. | |
| 104 // Must be called on the same thread the message_pump is running on. | |
| 105 bool WatchFileDescriptor(int fd, | |
| 106 bool persistent, | |
| 107 Mode mode, | |
|
Mark Mentovai
2012/11/20 22:29:27
This should be an int argument. A variable of enum
blundell
2012/11/21 16:56:52
Thanks for the catch! Done.
On 2012/11/20 22:29:2
| |
| 108 FileDescriptorWatcher *controller, | |
| 109 Watcher *delegate); | |
| 110 | |
| 111 void RemoveRunLoopSource(CFRunLoopSourceRef source); | |
| 112 | |
| 113 void AddIOObserver(IOObserver* obs); | |
| 114 void RemoveIOObserver(IOObserver* obs); | |
| 115 | |
| 116 protected: | |
| 117 virtual ~MessagePumpIOSForIO(); | |
| 118 | |
| 119 private: | |
| 120 void WillProcessIOEvent(); | |
| 121 void DidProcessIOEvent(); | |
| 122 | |
| 123 static void HandleFdIOEvent(CFFileDescriptorRef fdref, | |
| 124 CFOptionFlags callback_types, | |
| 125 void* context); | |
| 126 | |
| 127 ObserverList<IOObserver> io_observers_; | |
| 128 ThreadChecker watch_file_descriptor_caller_checker_; | |
| 129 | |
| 130 DISALLOW_COPY_AND_ASSIGN(MessagePumpIOSForIO); | |
| 131 }; | |
| 132 | |
| 133 } // namespace base | |
| 134 | |
| 135 #endif // BASE_MESSAGE_PUMP_IO_IOS_H_ | |
| OLD | NEW |