1 // Copyright 2012 The Chromium Authors 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/apple/scoped_cffiledescriptorref.h" 9 #include "base/apple/scoped_cftyperef.h" 10 #include "base/base_export.h" 11 #include "base/memory/weak_ptr.h" 12 #include "base/message_loop/message_pump_apple.h" 13 #include "base/message_loop/watchable_io_message_pump_posix.h" 14 #include "base/threading/thread_checker.h" 15 16 namespace base { 17 18 // This file introduces a class to monitor sockets and issue callbacks when 19 // sockets are ready for I/O on iOS. 20 class BASE_EXPORT MessagePumpIOSForIO : public MessagePumpNSRunLoop, 21 public WatchableIOMessagePumpPosix { 22 public: 23 class FdWatchController : public FdWatchControllerInterface { 24 public: 25 explicit FdWatchController(const Location& from_here); 26 27 FdWatchController(const FdWatchController&) = delete; 28 FdWatchController& operator=(const FdWatchController&) = delete; 29 30 // Implicitly calls StopWatchingFileDescriptor. 31 ~FdWatchController() override; 32 33 // FdWatchControllerInterface: 34 bool StopWatchingFileDescriptor() override; 35 36 private: 37 friend class MessagePumpIOSForIO; 38 friend class MessagePumpIOSForIOTest; 39 40 // Called by MessagePumpIOSForIO, ownership of |fdref| and |fd_source| 41 // is transferred to this object. 42 void Init(CFFileDescriptorRef fdref, 43 CFOptionFlags callback_types, 44 CFRunLoopSourceRef fd_source, 45 bool is_persistent); 46 set_pump(base::WeakPtr<MessagePumpIOSForIO> pump)47 void set_pump(base::WeakPtr<MessagePumpIOSForIO> pump) { pump_ = pump; } pump()48 const base::WeakPtr<MessagePumpIOSForIO>& pump() const { return pump_; } 49 set_watcher(FdWatcher * watcher)50 void set_watcher(FdWatcher* watcher) { watcher_ = watcher; } 51 52 void OnFileCanReadWithoutBlocking(int fd, MessagePumpIOSForIO* pump); 53 void OnFileCanWriteWithoutBlocking(int fd, MessagePumpIOSForIO* pump); 54 55 bool is_persistent_ = false; // false if this event is one-shot. 56 apple::ScopedCFFileDescriptorRef fdref_; 57 CFOptionFlags callback_types_ = 0; 58 apple::ScopedCFTypeRef<CFRunLoopSourceRef> fd_source_; 59 WeakPtr<MessagePumpIOSForIO> pump_; 60 FdWatcher* watcher_ = nullptr; 61 }; 62 63 MessagePumpIOSForIO(); 64 65 MessagePumpIOSForIO(const MessagePumpIOSForIO&) = delete; 66 MessagePumpIOSForIO& operator=(const MessagePumpIOSForIO&) = delete; 67 68 ~MessagePumpIOSForIO() override; 69 70 bool WatchFileDescriptor(int fd, 71 bool persistent, 72 int mode, 73 FdWatchController* controller, 74 FdWatcher* delegate); 75 76 void RemoveRunLoopSource(CFRunLoopSourceRef source); 77 78 private: 79 friend class MessagePumpIOSForIOTest; 80 81 static void HandleFdIOEvent(CFFileDescriptorRef fdref, 82 CFOptionFlags callback_types, 83 void* context); 84 85 ThreadChecker watch_file_descriptor_caller_checker_; 86 87 base::WeakPtrFactory<MessagePumpIOSForIO> weak_factory_; 88 }; 89 90 } // namespace base 91 92 #endif // BASE_MESSAGE_LOOP_MESSAGE_PUMP_IO_IOS_H_ 93