Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | |
|
wtc
2012/11/21 18:31:05
This file is named message_pump_io_ios.mm, but I t
stuartmorgan
2012/11/21 21:16:13
AFAIK our policy has always been to name Mac (and
| |
| 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) { | |
|
Mark Mentovai
2012/11/20 22:29:27
Do you want to DCHECK that you’re really listening
blundell
2012/11/21 16:56:52
Done.
| |
| 53 pump->WillProcessIOEvent(); | |
| 54 watcher_->OnFileCanReadWithoutBlocking(fd); | |
| 55 pump->DidProcessIOEvent(); | |
| 56 } | |
| 57 | |
| 58 void MessagePumpIOSForIO::FileDescriptorWatcher::OnFileCanWriteWithoutBlocking( | |
| 59 int fd, | |
| 60 MessagePumpIOSForIO* pump) { | |
| 61 pump->WillProcessIOEvent(); | |
| 62 watcher_->OnFileCanWriteWithoutBlocking(fd); | |
| 63 pump->DidProcessIOEvent(); | |
| 64 } | |
| 65 | |
| 66 MessagePumpIOSForIO::MessagePumpIOSForIO() { | |
| 67 } | |
| 68 | |
| 69 MessagePumpIOSForIO::~MessagePumpIOSForIO() { | |
| 70 } | |
| 71 | |
| 72 bool MessagePumpIOSForIO::WatchFileDescriptor( | |
| 73 int fd, | |
| 74 bool persistent, | |
| 75 Mode mode, | |
| 76 FileDescriptorWatcher *controller, | |
| 77 Watcher *delegate) { | |
| 78 DCHECK_GE(fd, 0); | |
| 79 DCHECK(controller); | |
| 80 DCHECK(delegate); | |
| 81 DCHECK(mode == WATCH_READ || mode == WATCH_WRITE || | |
| 82 mode == (WATCH_READ | WATCH_WRITE)); | |
| 83 | |
| 84 // WatchFileDescriptor should be called on the pump thread. It is not | |
| 85 // threadsafe, and your watcher may never be registered. | |
| 86 DCHECK(watch_file_descriptor_caller_checker_.CalledOnValidThread()); | |
| 87 | |
| 88 CFFileDescriptorContext source_context = {0}; | |
| 89 source_context.info = controller; | |
| 90 | |
| 91 CFOptionFlags callback_types = 0; | |
| 92 if ((mode & WATCH_READ) != 0) { | |
| 93 callback_types |= kCFFileDescriptorReadCallBack; | |
| 94 } | |
| 95 if ((mode & WATCH_WRITE) != 0) { | |
| 96 callback_types |= kCFFileDescriptorWriteCallBack; | |
| 97 } | |
| 98 | |
| 99 CFFileDescriptorRef fdref = controller->fdref_; | |
| 100 if (fdref == NULL) { | |
| 101 base::mac::ScopedCFTypeRef<CFFileDescriptorRef> scoped_fdref( | |
| 102 CFFileDescriptorCreate(kCFAllocatorDefault, fd, false, HandleFdIOEvent, | |
| 103 &source_context)); | |
| 104 if (scoped_fdref == NULL) { | |
| 105 NOTREACHED() << "CFFileDescriptorCreate failed"; | |
| 106 return false; | |
| 107 } | |
| 108 | |
| 109 CFFileDescriptorEnableCallBacks(scoped_fdref, callback_types); | |
| 110 | |
| 111 // TODO(wtc): what should the 'order' argument be? | |
| 112 base::mac::ScopedCFTypeRef<CFRunLoopSourceRef> scoped_fd_source( | |
| 113 CFFileDescriptorCreateRunLoopSource(kCFAllocatorDefault, | |
| 114 scoped_fdref, | |
| 115 0)); | |
| 116 if (scoped_fd_source == NULL) { | |
| 117 NOTREACHED() << "CFFileDescriptorCreateRunLoopSource failed"; | |
| 118 return false; | |
| 119 } | |
| 120 CFRunLoopAddSource(run_loop(), scoped_fd_source, kCFRunLoopCommonModes); | |
| 121 | |
| 122 // Transfer ownership of scoped_fdref and fd_source to controller. | |
| 123 controller->Init(scoped_fdref.release(), callback_types, | |
| 124 scoped_fd_source.release(), persistent); | |
| 125 } else { | |
| 126 // It's illegal to use this function to listen on 2 separate fds with the | |
| 127 // same |controller|. | |
| 128 if (CFFileDescriptorGetNativeDescriptor(fdref) != fd) { | |
| 129 NOTREACHED() << "FDs don't match: " | |
| 130 << CFFileDescriptorGetNativeDescriptor(fdref) | |
| 131 << " != " << fd; | |
| 132 return false; | |
| 133 } | |
| 134 if (persistent != controller->is_persistent_) { | |
| 135 NOTREACHED() << "persistent doesn't match"; | |
| 136 return false; | |
| 137 } | |
| 138 | |
| 139 // Combine old/new event masks. | |
| 140 CFFileDescriptorDisableCallBacks(fdref, controller->callback_types_); | |
| 141 controller->callback_types_ |= callback_types; | |
|
Mark Mentovai
2012/11/20 22:29:27
Does this “else” block ever get entered in practic
blundell
2012/11/21 16:56:52
I don't know whether it ever gets entered in pract
| |
| 142 CFFileDescriptorEnableCallBacks(fdref, controller->callback_types_); | |
| 143 } | |
| 144 | |
| 145 controller->set_watcher(delegate); | |
| 146 controller->set_pump(this); | |
| 147 | |
| 148 return true; | |
| 149 } | |
| 150 | |
| 151 void MessagePumpIOSForIO::RemoveRunLoopSource(CFRunLoopSourceRef source) { | |
| 152 CFRunLoopRemoveSource(run_loop(), source, kCFRunLoopCommonModes); | |
| 153 } | |
| 154 | |
| 155 void MessagePumpIOSForIO::AddIOObserver(IOObserver *obs) { | |
| 156 io_observers_.AddObserver(obs); | |
| 157 } | |
| 158 | |
| 159 void MessagePumpIOSForIO::RemoveIOObserver(IOObserver *obs) { | |
| 160 io_observers_.RemoveObserver(obs); | |
| 161 } | |
| 162 | |
| 163 void MessagePumpIOSForIO::WillProcessIOEvent() { | |
| 164 FOR_EACH_OBSERVER(IOObserver, io_observers_, WillProcessIOEvent()); | |
| 165 } | |
| 166 | |
| 167 void MessagePumpIOSForIO::DidProcessIOEvent() { | |
| 168 FOR_EACH_OBSERVER(IOObserver, io_observers_, DidProcessIOEvent()); | |
| 169 } | |
| 170 | |
| 171 // static | |
| 172 void MessagePumpIOSForIO::HandleFdIOEvent(CFFileDescriptorRef fdref, | |
| 173 CFOptionFlags callback_types, | |
| 174 void* info) { | |
| 175 int fd = CFFileDescriptorGetNativeDescriptor(fdref); | |
| 176 | |
| 177 FileDescriptorWatcher* controller = | |
| 178 static_cast<FileDescriptorWatcher*>(info); | |
| 179 | |
| 180 CHECK_EQ(fdref, controller->fdref_); | |
|
Mark Mentovai
2012/11/20 22:29:27
What justifies this being a CHECK and not a DCHECK
blundell
2012/11/21 16:56:52
Changed.
On 2012/11/20 22:29:27, Mark Mentovai wr
wtc
2012/11/21 18:31:05
I may have used a CHECK to try to expose bugs usin
| |
| 181 bool persistent = controller->is_persistent_; | |
| 182 | |
| 183 MessagePumpIOSForIO* pump = controller->pump(); | |
| 184 if (callback_types & kCFFileDescriptorWriteCallBack) { | |
|
Mark Mentovai
2012/11/20 22:29:27
Your style in this file has varied between (v & k)
blundell
2012/11/21 16:56:52
Changed to consistently use the (v & k) form both
| |
| 185 controller->OnFileCanWriteWithoutBlocking(fd, pump); | |
| 186 } | |
| 187 if (callback_types & kCFFileDescriptorReadCallBack) { | |
| 188 controller->OnFileCanReadWithoutBlocking(fd, pump); | |
|
wtc
2012/11/21 18:31:05
I may have filed a bug report about this. |control
| |
| 189 } | |
| 190 | |
| 191 // Must read/write from the fd before re-enabling the callbacks. | |
| 192 // |controller| may have been deleted, so we test a copy of | |
| 193 // controller->persistent. |fdref| may have been invalidated. | |
|
Mark Mentovai
2012/11/20 22:29:27
If you’re concerned that controller may be gone bu
blundell
2012/11/21 16:56:52
My understanding is that the |persistent| argument
wtc
2012/11/21 18:31:05
At this point, |controller| may have been destroye
| |
| 194 CHECK_GT(CFGetRetainCount(fdref), 0); | |
| 195 if (CFFileDescriptorIsValid(fdref) && persistent) | |
| 196 CFFileDescriptorEnableCallBacks(fdref, callback_types); | |
| 197 } | |
| 198 | |
| 199 } // namespace base | |
| OLD | NEW |