| OLD | NEW |
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/message_loop/message_pump_io_ios.h" | 5 #include "base/message_loop/message_pump_io_ios.h" |
| 6 | 6 |
| 7 namespace base { | 7 namespace base { |
| 8 | 8 |
| 9 MessagePumpIOSForIO::FileDescriptorWatcher::FileDescriptorWatcher() | 9 MessagePumpIOSForIO::FileDescriptorWatcher::FileDescriptorWatcher() |
| 10 : is_persistent_(false), | 10 : is_persistent_(false), |
| 11 fdref_(NULL), | 11 fdref_(NULL), |
| 12 callback_types_(0), | 12 callback_types_(0), |
| 13 fd_source_(NULL), | 13 fd_source_(NULL), |
| 14 pump_(NULL), |
| 14 watcher_(NULL) { | 15 watcher_(NULL) { |
| 15 } | 16 } |
| 16 | 17 |
| 17 MessagePumpIOSForIO::FileDescriptorWatcher::~FileDescriptorWatcher() { | 18 MessagePumpIOSForIO::FileDescriptorWatcher::~FileDescriptorWatcher() { |
| 18 StopWatchingFileDescriptor(); | 19 StopWatchingFileDescriptor(); |
| 19 } | 20 } |
| 20 | 21 |
| 21 bool MessagePumpIOSForIO::FileDescriptorWatcher::StopWatchingFileDescriptor() { | 22 bool MessagePumpIOSForIO::FileDescriptorWatcher::StopWatchingFileDescriptor() { |
| 22 if (fdref_ == NULL) | 23 if (fdref_ == NULL) |
| 23 return true; | 24 return true; |
| 24 | 25 |
| 25 CFFileDescriptorDisableCallBacks(fdref_, callback_types_); | 26 CFFileDescriptorDisableCallBacks(fdref_, callback_types_); |
| 26 if (pump_) | 27 pump_->RemoveRunLoopSource(fd_source_); |
| 27 pump_->RemoveRunLoopSource(fd_source_); | |
| 28 fd_source_.reset(); | 28 fd_source_.reset(); |
| 29 fdref_.reset(); | 29 fdref_.reset(); |
| 30 callback_types_ = 0; | 30 callback_types_ = 0; |
| 31 pump_.reset(); | 31 pump_ = NULL; |
| 32 watcher_ = NULL; | 32 watcher_ = NULL; |
| 33 return true; | 33 return true; |
| 34 } | 34 } |
| 35 | 35 |
| 36 void MessagePumpIOSForIO::FileDescriptorWatcher::Init( | 36 void MessagePumpIOSForIO::FileDescriptorWatcher::Init( |
| 37 CFFileDescriptorRef fdref, | 37 CFFileDescriptorRef fdref, |
| 38 CFOptionFlags callback_types, | 38 CFOptionFlags callback_types, |
| 39 CFRunLoopSourceRef fd_source, | 39 CFRunLoopSourceRef fd_source, |
| 40 bool is_persistent) { | 40 bool is_persistent) { |
| 41 DCHECK(fdref); | 41 DCHECK(fdref); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 58 | 58 |
| 59 void MessagePumpIOSForIO::FileDescriptorWatcher::OnFileCanWriteWithoutBlocking( | 59 void MessagePumpIOSForIO::FileDescriptorWatcher::OnFileCanWriteWithoutBlocking( |
| 60 int fd, | 60 int fd, |
| 61 MessagePumpIOSForIO* pump) { | 61 MessagePumpIOSForIO* pump) { |
| 62 DCHECK(callback_types_ & kCFFileDescriptorWriteCallBack); | 62 DCHECK(callback_types_ & kCFFileDescriptorWriteCallBack); |
| 63 pump->WillProcessIOEvent(); | 63 pump->WillProcessIOEvent(); |
| 64 watcher_->OnFileCanWriteWithoutBlocking(fd); | 64 watcher_->OnFileCanWriteWithoutBlocking(fd); |
| 65 pump->DidProcessIOEvent(); | 65 pump->DidProcessIOEvent(); |
| 66 } | 66 } |
| 67 | 67 |
| 68 MessagePumpIOSForIO::MessagePumpIOSForIO() : weak_factory_(this) { | 68 MessagePumpIOSForIO::MessagePumpIOSForIO() { |
| 69 } | 69 } |
| 70 | 70 |
| 71 MessagePumpIOSForIO::~MessagePumpIOSForIO() { | 71 MessagePumpIOSForIO::~MessagePumpIOSForIO() { |
| 72 } | 72 } |
| 73 | 73 |
| 74 bool MessagePumpIOSForIO::WatchFileDescriptor( | 74 bool MessagePumpIOSForIO::WatchFileDescriptor( |
| 75 int fd, | 75 int fd, |
| 76 bool persistent, | 76 bool persistent, |
| 77 int mode, | 77 int mode, |
| 78 FileDescriptorWatcher *controller, | 78 FileDescriptorWatcher *controller, |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 136 return false; | 136 return false; |
| 137 } | 137 } |
| 138 | 138 |
| 139 // Combine old/new event masks. | 139 // Combine old/new event masks. |
| 140 CFFileDescriptorDisableCallBacks(fdref, controller->callback_types_); | 140 CFFileDescriptorDisableCallBacks(fdref, controller->callback_types_); |
| 141 controller->callback_types_ |= callback_types; | 141 controller->callback_types_ |= callback_types; |
| 142 CFFileDescriptorEnableCallBacks(fdref, controller->callback_types_); | 142 CFFileDescriptorEnableCallBacks(fdref, controller->callback_types_); |
| 143 } | 143 } |
| 144 | 144 |
| 145 controller->set_watcher(delegate); | 145 controller->set_watcher(delegate); |
| 146 controller->set_pump(weak_factory_.GetWeakPtr()); | 146 controller->set_pump(this); |
| 147 | 147 |
| 148 return true; | 148 return true; |
| 149 } | 149 } |
| 150 | 150 |
| 151 void MessagePumpIOSForIO::RemoveRunLoopSource(CFRunLoopSourceRef source) { | 151 void MessagePumpIOSForIO::RemoveRunLoopSource(CFRunLoopSourceRef source) { |
| 152 CFRunLoopRemoveSource(run_loop(), source, kCFRunLoopCommonModes); | 152 CFRunLoopRemoveSource(run_loop(), source, kCFRunLoopCommonModes); |
| 153 } | 153 } |
| 154 | 154 |
| 155 void MessagePumpIOSForIO::AddIOObserver(IOObserver *obs) { | 155 void MessagePumpIOSForIO::AddIOObserver(IOObserver *obs) { |
| 156 io_observers_.AddObserver(obs); | 156 io_observers_.AddObserver(obs); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 176 static_cast<FileDescriptorWatcher*>(context); | 176 static_cast<FileDescriptorWatcher*>(context); |
| 177 DCHECK_EQ(fdref, controller->fdref_); | 177 DCHECK_EQ(fdref, controller->fdref_); |
| 178 | 178 |
| 179 // Ensure that |fdref| will remain live for the duration of this function | 179 // Ensure that |fdref| will remain live for the duration of this function |
| 180 // call even if |controller| is deleted or |StopWatchingFileDescriptor()| is | 180 // call even if |controller| is deleted or |StopWatchingFileDescriptor()| is |
| 181 // called, either of which will cause |fdref| to be released. | 181 // called, either of which will cause |fdref| to be released. |
| 182 ScopedCFTypeRef<CFFileDescriptorRef> scoped_fdref( | 182 ScopedCFTypeRef<CFFileDescriptorRef> scoped_fdref( |
| 183 fdref, base::scoped_policy::RETAIN); | 183 fdref, base::scoped_policy::RETAIN); |
| 184 | 184 |
| 185 int fd = CFFileDescriptorGetNativeDescriptor(fdref); | 185 int fd = CFFileDescriptorGetNativeDescriptor(fdref); |
| 186 MessagePumpIOSForIO* pump = controller->pump().get(); | 186 MessagePumpIOSForIO* pump = controller->pump(); |
| 187 DCHECK(pump); | |
| 188 if (callback_types & kCFFileDescriptorWriteCallBack) | 187 if (callback_types & kCFFileDescriptorWriteCallBack) |
| 189 controller->OnFileCanWriteWithoutBlocking(fd, pump); | 188 controller->OnFileCanWriteWithoutBlocking(fd, pump); |
| 190 | 189 |
| 191 // Perform the read callback only if the file descriptor has not been | 190 // Perform the read callback only if the file descriptor has not been |
| 192 // invalidated in the write callback. As |FileDescriptorWatcher| invalidates | 191 // invalidated in the write callback. As |FileDescriptorWatcher| invalidates |
| 193 // its file descriptor on destruction, the file descriptor being valid also | 192 // its file descriptor on destruction, the file descriptor being valid also |
| 194 // guarantees that |controller| has not been deleted. | 193 // guarantees that |controller| has not been deleted. |
| 195 if (callback_types & kCFFileDescriptorReadCallBack && | 194 if (callback_types & kCFFileDescriptorReadCallBack && |
| 196 CFFileDescriptorIsValid(fdref)) { | 195 CFFileDescriptorIsValid(fdref)) { |
| 197 DCHECK_EQ(fdref, controller->fdref_); | 196 DCHECK_EQ(fdref, controller->fdref_); |
| 198 controller->OnFileCanReadWithoutBlocking(fd, pump); | 197 controller->OnFileCanReadWithoutBlocking(fd, pump); |
| 199 } | 198 } |
| 200 | 199 |
| 201 // Re-enable callbacks after the read/write if the file descriptor is still | 200 // Re-enable callbacks after the read/write if the file descriptor is still |
| 202 // valid and the controller is persistent. | 201 // valid and the controller is persistent. |
| 203 if (CFFileDescriptorIsValid(fdref) && controller->is_persistent_) { | 202 if (CFFileDescriptorIsValid(fdref) && controller->is_persistent_) { |
| 204 DCHECK_EQ(fdref, controller->fdref_); | 203 DCHECK_EQ(fdref, controller->fdref_); |
| 205 CFFileDescriptorEnableCallBacks(fdref, callback_types); | 204 CFFileDescriptorEnableCallBacks(fdref, callback_types); |
| 206 } | 205 } |
| 207 } | 206 } |
| 208 | 207 |
| 209 } // namespace base | 208 } // namespace base |
| OLD | NEW |