message_pump_io_ios.h 3.0 KB

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