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