message_pump_io_ios.h 3.2 KB

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