message_pump_io_ios.cc 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. #include "base/message_loop/message_pump_io_ios.h"
  5. #include "base/notreached.h"
  6. namespace base {
  7. MessagePumpIOSForIO::FdWatchController::FdWatchController(
  8. const Location& from_here)
  9. : FdWatchControllerInterface(from_here) {}
  10. MessagePumpIOSForIO::FdWatchController::~FdWatchController() {
  11. StopWatchingFileDescriptor();
  12. }
  13. bool MessagePumpIOSForIO::FdWatchController::StopWatchingFileDescriptor() {
  14. if (fdref_ == NULL)
  15. return true;
  16. CFFileDescriptorDisableCallBacks(fdref_.get(), callback_types_);
  17. if (pump_)
  18. pump_->RemoveRunLoopSource(fd_source_);
  19. fd_source_.reset();
  20. fdref_.reset();
  21. callback_types_ = 0;
  22. pump_.reset();
  23. watcher_ = NULL;
  24. return true;
  25. }
  26. void MessagePumpIOSForIO::FdWatchController::Init(CFFileDescriptorRef fdref,
  27. CFOptionFlags callback_types,
  28. CFRunLoopSourceRef fd_source,
  29. bool is_persistent) {
  30. DCHECK(fdref);
  31. DCHECK(!fdref_.is_valid());
  32. is_persistent_ = is_persistent;
  33. fdref_.reset(fdref);
  34. callback_types_ = callback_types;
  35. fd_source_.reset(fd_source);
  36. }
  37. void MessagePumpIOSForIO::FdWatchController::OnFileCanReadWithoutBlocking(
  38. int fd,
  39. MessagePumpIOSForIO* pump) {
  40. DCHECK(callback_types_ & kCFFileDescriptorReadCallBack);
  41. watcher_->OnFileCanReadWithoutBlocking(fd);
  42. }
  43. void MessagePumpIOSForIO::FdWatchController::OnFileCanWriteWithoutBlocking(
  44. int fd,
  45. MessagePumpIOSForIO* pump) {
  46. DCHECK(callback_types_ & kCFFileDescriptorWriteCallBack);
  47. watcher_->OnFileCanWriteWithoutBlocking(fd);
  48. }
  49. MessagePumpIOSForIO::MessagePumpIOSForIO() : weak_factory_(this) {
  50. }
  51. MessagePumpIOSForIO::~MessagePumpIOSForIO() {
  52. }
  53. bool MessagePumpIOSForIO::WatchFileDescriptor(int fd,
  54. bool persistent,
  55. int mode,
  56. FdWatchController* controller,
  57. FdWatcher* delegate) {
  58. DCHECK_GE(fd, 0);
  59. DCHECK(controller);
  60. DCHECK(delegate);
  61. DCHECK(mode == WATCH_READ || mode == WATCH_WRITE || mode == WATCH_READ_WRITE);
  62. // WatchFileDescriptor should be called on the pump thread. It is not
  63. // threadsafe, and your watcher may never be registered.
  64. DCHECK(watch_file_descriptor_caller_checker_.CalledOnValidThread());
  65. CFFileDescriptorContext source_context = {0};
  66. source_context.info = controller;
  67. CFOptionFlags callback_types = 0;
  68. if (mode & WATCH_READ) {
  69. callback_types |= kCFFileDescriptorReadCallBack;
  70. }
  71. if (mode & WATCH_WRITE) {
  72. callback_types |= kCFFileDescriptorWriteCallBack;
  73. }
  74. CFFileDescriptorRef fdref = controller->fdref_.get();
  75. if (fdref == NULL) {
  76. base::ScopedCFTypeRef<CFFileDescriptorRef> scoped_fdref(
  77. CFFileDescriptorCreate(
  78. kCFAllocatorDefault, fd, false, HandleFdIOEvent, &source_context));
  79. if (scoped_fdref == NULL) {
  80. NOTREACHED() << "CFFileDescriptorCreate failed";
  81. return false;
  82. }
  83. CFFileDescriptorEnableCallBacks(scoped_fdref, callback_types);
  84. // TODO(wtc): what should the 'order' argument be?
  85. base::ScopedCFTypeRef<CFRunLoopSourceRef> scoped_fd_source(
  86. CFFileDescriptorCreateRunLoopSource(
  87. kCFAllocatorDefault, scoped_fdref, 0));
  88. if (scoped_fd_source == NULL) {
  89. NOTREACHED() << "CFFileDescriptorCreateRunLoopSource failed";
  90. return false;
  91. }
  92. CFRunLoopAddSource(run_loop(), scoped_fd_source, kCFRunLoopCommonModes);
  93. // Transfer ownership of scoped_fdref and fd_source to controller.
  94. controller->Init(scoped_fdref.release(), callback_types,
  95. scoped_fd_source.release(), persistent);
  96. } else {
  97. // It's illegal to use this function to listen on 2 separate fds with the
  98. // same |controller|.
  99. if (CFFileDescriptorGetNativeDescriptor(fdref) != fd) {
  100. NOTREACHED() << "FDs don't match: "
  101. << CFFileDescriptorGetNativeDescriptor(fdref)
  102. << " != " << fd;
  103. return false;
  104. }
  105. if (persistent != controller->is_persistent_) {
  106. NOTREACHED() << "persistent doesn't match";
  107. return false;
  108. }
  109. // Combine old/new event masks.
  110. CFFileDescriptorDisableCallBacks(fdref, controller->callback_types_);
  111. controller->callback_types_ |= callback_types;
  112. CFFileDescriptorEnableCallBacks(fdref, controller->callback_types_);
  113. }
  114. controller->set_watcher(delegate);
  115. controller->set_pump(weak_factory_.GetWeakPtr());
  116. return true;
  117. }
  118. void MessagePumpIOSForIO::RemoveRunLoopSource(CFRunLoopSourceRef source) {
  119. CFRunLoopRemoveSource(run_loop(), source, kCFRunLoopCommonModes);
  120. }
  121. // static
  122. void MessagePumpIOSForIO::HandleFdIOEvent(CFFileDescriptorRef fdref,
  123. CFOptionFlags callback_types,
  124. void* context) {
  125. FdWatchController* controller = static_cast<FdWatchController*>(context);
  126. DCHECK_EQ(fdref, controller->fdref_.get());
  127. // Ensure that |fdref| will remain live for the duration of this function
  128. // call even if |controller| is deleted or |StopWatchingFileDescriptor()| is
  129. // called, either of which will cause |fdref| to be released.
  130. ScopedCFTypeRef<CFFileDescriptorRef> scoped_fdref(
  131. fdref, base::scoped_policy::RETAIN);
  132. int fd = CFFileDescriptorGetNativeDescriptor(fdref);
  133. MessagePumpIOSForIO* pump = controller->pump().get();
  134. DCHECK(pump);
  135. if (callback_types & kCFFileDescriptorWriteCallBack)
  136. controller->OnFileCanWriteWithoutBlocking(fd, pump);
  137. // Perform the read callback only if the file descriptor has not been
  138. // invalidated in the write callback. As |FdWatchController| invalidates
  139. // its file descriptor on destruction, the file descriptor being valid also
  140. // guarantees that |controller| has not been deleted.
  141. if (callback_types & kCFFileDescriptorReadCallBack &&
  142. CFFileDescriptorIsValid(fdref)) {
  143. DCHECK_EQ(fdref, controller->fdref_.get());
  144. controller->OnFileCanReadWithoutBlocking(fd, pump);
  145. }
  146. // Re-enable callbacks after the read/write if the file descriptor is still
  147. // valid and the controller is persistent.
  148. if (CFFileDescriptorIsValid(fdref) && controller->is_persistent_) {
  149. DCHECK_EQ(fdref, controller->fdref_.get());
  150. CFFileDescriptorEnableCallBacks(fdref, callback_types);
  151. }
  152. }
  153. } // namespace base