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 #include "base/message_pump_io_ios.h" |
| 6 |
| 7 namespace base { |
| 8 |
| 9 MessagePumpIOSForIO::FileDescriptorWatcher::FileDescriptorWatcher() |
| 10 : is_persistent_(false), |
| 11 fdref_(NULL), |
| 12 callback_types_(0), |
| 13 fd_source_(NULL), |
| 14 pump_(NULL), |
| 15 watcher_(NULL) { |
| 16 } |
| 17 |
| 18 MessagePumpIOSForIO::FileDescriptorWatcher::~FileDescriptorWatcher() { |
| 19 StopWatchingFileDescriptor(); |
| 20 } |
| 21 |
| 22 bool MessagePumpIOSForIO::FileDescriptorWatcher::StopWatchingFileDescriptor() { |
| 23 if (fdref_ == NULL) |
| 24 return true; |
| 25 |
| 26 CFFileDescriptorDisableCallBacks(fdref_, callback_types_); |
| 27 pump_->RemoveRunLoopSource(fd_source_); |
| 28 fd_source_.reset(); |
| 29 fdref_.reset(); |
| 30 callback_types_ = 0; |
| 31 pump_ = NULL; |
| 32 watcher_ = NULL; |
| 33 return true; |
| 34 } |
| 35 |
| 36 void MessagePumpIOSForIO::FileDescriptorWatcher::Init( |
| 37 CFFileDescriptorRef fdref, |
| 38 CFOptionFlags callback_types, |
| 39 CFRunLoopSourceRef fd_source, |
| 40 bool is_persistent) { |
| 41 DCHECK(fdref); |
| 42 DCHECK(!fdref_); |
| 43 |
| 44 is_persistent_ = is_persistent; |
| 45 fdref_.reset(fdref); |
| 46 callback_types_ = callback_types; |
| 47 fd_source_.reset(fd_source); |
| 48 } |
| 49 |
| 50 void MessagePumpIOSForIO::FileDescriptorWatcher::OnFileCanReadWithoutBlocking( |
| 51 int fd, |
| 52 MessagePumpIOSForIO* pump) { |
| 53 DCHECK(callback_types_ & kCFFileDescriptorReadCallBack); |
| 54 pump->WillProcessIOEvent(); |
| 55 watcher_->OnFileCanReadWithoutBlocking(fd); |
| 56 pump->DidProcessIOEvent(); |
| 57 } |
| 58 |
| 59 void MessagePumpIOSForIO::FileDescriptorWatcher::OnFileCanWriteWithoutBlocking( |
| 60 int fd, |
| 61 MessagePumpIOSForIO* pump) { |
| 62 DCHECK(callback_types_ & kCFFileDescriptorWriteCallBack); |
| 63 pump->WillProcessIOEvent(); |
| 64 watcher_->OnFileCanWriteWithoutBlocking(fd); |
| 65 pump->DidProcessIOEvent(); |
| 66 } |
| 67 |
| 68 MessagePumpIOSForIO::MessagePumpIOSForIO() { |
| 69 } |
| 70 |
| 71 MessagePumpIOSForIO::~MessagePumpIOSForIO() { |
| 72 } |
| 73 |
| 74 bool MessagePumpIOSForIO::WatchFileDescriptor( |
| 75 int fd, |
| 76 bool persistent, |
| 77 int mode, |
| 78 FileDescriptorWatcher *controller, |
| 79 Watcher *delegate) { |
| 80 DCHECK_GE(fd, 0); |
| 81 DCHECK(controller); |
| 82 DCHECK(delegate); |
| 83 DCHECK(mode == WATCH_READ || mode == WATCH_WRITE || |
| 84 mode == (WATCH_READ | WATCH_WRITE)); |
| 85 |
| 86 // WatchFileDescriptor should be called on the pump thread. It is not |
| 87 // threadsafe, and your watcher may never be registered. |
| 88 DCHECK(watch_file_descriptor_caller_checker_.CalledOnValidThread()); |
| 89 |
| 90 CFFileDescriptorContext source_context = {0}; |
| 91 source_context.info = controller; |
| 92 |
| 93 CFOptionFlags callback_types = 0; |
| 94 if (mode & WATCH_READ) { |
| 95 callback_types |= kCFFileDescriptorReadCallBack; |
| 96 } |
| 97 if (mode & WATCH_WRITE) { |
| 98 callback_types |= kCFFileDescriptorWriteCallBack; |
| 99 } |
| 100 |
| 101 CFFileDescriptorRef fdref = controller->fdref_; |
| 102 if (fdref == NULL) { |
| 103 base::mac::ScopedCFTypeRef<CFFileDescriptorRef> scoped_fdref( |
| 104 CFFileDescriptorCreate(kCFAllocatorDefault, fd, false, HandleFdIOEvent, |
| 105 &source_context)); |
| 106 if (scoped_fdref == NULL) { |
| 107 NOTREACHED() << "CFFileDescriptorCreate failed"; |
| 108 return false; |
| 109 } |
| 110 |
| 111 CFFileDescriptorEnableCallBacks(scoped_fdref, callback_types); |
| 112 |
| 113 // TODO(wtc): what should the 'order' argument be? |
| 114 base::mac::ScopedCFTypeRef<CFRunLoopSourceRef> scoped_fd_source( |
| 115 CFFileDescriptorCreateRunLoopSource(kCFAllocatorDefault, |
| 116 scoped_fdref, |
| 117 0)); |
| 118 if (scoped_fd_source == NULL) { |
| 119 NOTREACHED() << "CFFileDescriptorCreateRunLoopSource failed"; |
| 120 return false; |
| 121 } |
| 122 CFRunLoopAddSource(run_loop(), scoped_fd_source, kCFRunLoopCommonModes); |
| 123 |
| 124 // Transfer ownership of scoped_fdref and fd_source to controller. |
| 125 controller->Init(scoped_fdref.release(), callback_types, |
| 126 scoped_fd_source.release(), persistent); |
| 127 } else { |
| 128 // It's illegal to use this function to listen on 2 separate fds with the |
| 129 // same |controller|. |
| 130 if (CFFileDescriptorGetNativeDescriptor(fdref) != fd) { |
| 131 NOTREACHED() << "FDs don't match: " |
| 132 << CFFileDescriptorGetNativeDescriptor(fdref) |
| 133 << " != " << fd; |
| 134 return false; |
| 135 } |
| 136 if (persistent != controller->is_persistent_) { |
| 137 NOTREACHED() << "persistent doesn't match"; |
| 138 return false; |
| 139 } |
| 140 |
| 141 // Combine old/new event masks. |
| 142 CFFileDescriptorDisableCallBacks(fdref, controller->callback_types_); |
| 143 controller->callback_types_ |= callback_types; |
| 144 CFFileDescriptorEnableCallBacks(fdref, controller->callback_types_); |
| 145 } |
| 146 |
| 147 controller->set_watcher(delegate); |
| 148 controller->set_pump(this); |
| 149 |
| 150 return true; |
| 151 } |
| 152 |
| 153 void MessagePumpIOSForIO::RemoveRunLoopSource(CFRunLoopSourceRef source) { |
| 154 CFRunLoopRemoveSource(run_loop(), source, kCFRunLoopCommonModes); |
| 155 } |
| 156 |
| 157 void MessagePumpIOSForIO::AddIOObserver(IOObserver *obs) { |
| 158 io_observers_.AddObserver(obs); |
| 159 } |
| 160 |
| 161 void MessagePumpIOSForIO::RemoveIOObserver(IOObserver *obs) { |
| 162 io_observers_.RemoveObserver(obs); |
| 163 } |
| 164 |
| 165 void MessagePumpIOSForIO::WillProcessIOEvent() { |
| 166 FOR_EACH_OBSERVER(IOObserver, io_observers_, WillProcessIOEvent()); |
| 167 } |
| 168 |
| 169 void MessagePumpIOSForIO::DidProcessIOEvent() { |
| 170 FOR_EACH_OBSERVER(IOObserver, io_observers_, DidProcessIOEvent()); |
| 171 } |
| 172 |
| 173 // static |
| 174 void MessagePumpIOSForIO::HandleFdIOEvent(CFFileDescriptorRef fdref, |
| 175 CFOptionFlags callback_types, |
| 176 void* info) { |
| 177 int fd = CFFileDescriptorGetNativeDescriptor(fdref); |
| 178 |
| 179 FileDescriptorWatcher* controller = |
| 180 static_cast<FileDescriptorWatcher*>(info); |
| 181 |
| 182 DCHECK_EQ(fdref, controller->fdref_); |
| 183 bool persistent = controller->is_persistent_; |
| 184 |
| 185 MessagePumpIOSForIO* pump = controller->pump(); |
| 186 if (callback_types & kCFFileDescriptorWriteCallBack) { |
| 187 controller->OnFileCanWriteWithoutBlocking(fd, pump); |
| 188 } |
| 189 if (callback_types & kCFFileDescriptorReadCallBack) { |
| 190 controller->OnFileCanReadWithoutBlocking(fd, pump); |
| 191 } |
| 192 |
| 193 // Must read/write from the fd before re-enabling the callbacks. |
| 194 // |controller| may have been deleted, so we test a copy of |
| 195 // controller->persistent. |fdref| may have been invalidated. |
| 196 DCHECK_GT(CFGetRetainCount(fdref), 0); |
| 197 if (CFFileDescriptorIsValid(fdref) && persistent) |
| 198 CFFileDescriptorEnableCallBacks(fdref, callback_types); |
| 199 } |
| 200 |
| 201 } // namespace base |
OLD | NEW |