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 watcher_(NULL) { | 14 watcher_(NULL) { |
15 } | 15 } |
16 | 16 |
17 MessagePumpIOSForIO::FileDescriptorWatcher::~FileDescriptorWatcher() { | 17 MessagePumpIOSForIO::FileDescriptorWatcher::~FileDescriptorWatcher() { |
18 StopWatchingFileDescriptor(); | 18 StopWatchingFileDescriptor(); |
19 } | 19 } |
20 | 20 |
21 bool MessagePumpIOSForIO::FileDescriptorWatcher::StopWatchingFileDescriptor() { | 21 bool MessagePumpIOSForIO::FileDescriptorWatcher::StopWatchingFileDescriptor() { |
22 if (fdref_ == NULL) | 22 if (fdref_ == NULL) |
23 return true; | 23 return true; |
24 | 24 |
25 CFFileDescriptorDisableCallBacks(fdref_, callback_types_); | 25 CFFileDescriptorDisableCallBacks(fdref_.get(), callback_types_); |
26 if (pump_) | 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_.reset(); |
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); |
42 DCHECK(!fdref_); | 42 DCHECK(!fdref_.is_valid()); |
43 | 43 |
44 is_persistent_ = is_persistent; | 44 is_persistent_ = is_persistent; |
45 fdref_.reset(fdref); | 45 fdref_.reset(fdref); |
46 callback_types_ = callback_types; | 46 callback_types_ = callback_types; |
47 fd_source_.reset(fd_source); | 47 fd_source_.reset(fd_source); |
48 } | 48 } |
49 | 49 |
50 void MessagePumpIOSForIO::FileDescriptorWatcher::OnFileCanReadWithoutBlocking( | 50 void MessagePumpIOSForIO::FileDescriptorWatcher::OnFileCanReadWithoutBlocking( |
51 int fd, | 51 int fd, |
52 MessagePumpIOSForIO* pump) { | 52 MessagePumpIOSForIO* pump) { |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 source_context.info = controller; | 90 source_context.info = controller; |
91 | 91 |
92 CFOptionFlags callback_types = 0; | 92 CFOptionFlags callback_types = 0; |
93 if (mode & WATCH_READ) { | 93 if (mode & WATCH_READ) { |
94 callback_types |= kCFFileDescriptorReadCallBack; | 94 callback_types |= kCFFileDescriptorReadCallBack; |
95 } | 95 } |
96 if (mode & WATCH_WRITE) { | 96 if (mode & WATCH_WRITE) { |
97 callback_types |= kCFFileDescriptorWriteCallBack; | 97 callback_types |= kCFFileDescriptorWriteCallBack; |
98 } | 98 } |
99 | 99 |
100 CFFileDescriptorRef fdref = controller->fdref_; | 100 CFFileDescriptorRef fdref = controller->fdref_.get(); |
101 if (fdref == NULL) { | 101 if (fdref == NULL) { |
102 base::ScopedCFTypeRef<CFFileDescriptorRef> scoped_fdref( | 102 base::ScopedCFTypeRef<CFFileDescriptorRef> scoped_fdref( |
103 CFFileDescriptorCreate( | 103 CFFileDescriptorCreate( |
104 kCFAllocatorDefault, fd, false, HandleFdIOEvent, &source_context)); | 104 kCFAllocatorDefault, fd, false, HandleFdIOEvent, &source_context)); |
105 if (scoped_fdref == NULL) { | 105 if (scoped_fdref == NULL) { |
106 NOTREACHED() << "CFFileDescriptorCreate failed"; | 106 NOTREACHED() << "CFFileDescriptorCreate failed"; |
107 return false; | 107 return false; |
108 } | 108 } |
109 | 109 |
110 CFFileDescriptorEnableCallBacks(scoped_fdref, callback_types); | 110 CFFileDescriptorEnableCallBacks(scoped_fdref, callback_types); |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
167 void MessagePumpIOSForIO::DidProcessIOEvent() { | 167 void MessagePumpIOSForIO::DidProcessIOEvent() { |
168 FOR_EACH_OBSERVER(IOObserver, io_observers_, DidProcessIOEvent()); | 168 FOR_EACH_OBSERVER(IOObserver, io_observers_, DidProcessIOEvent()); |
169 } | 169 } |
170 | 170 |
171 // static | 171 // static |
172 void MessagePumpIOSForIO::HandleFdIOEvent(CFFileDescriptorRef fdref, | 172 void MessagePumpIOSForIO::HandleFdIOEvent(CFFileDescriptorRef fdref, |
173 CFOptionFlags callback_types, | 173 CFOptionFlags callback_types, |
174 void* context) { | 174 void* context) { |
175 FileDescriptorWatcher* controller = | 175 FileDescriptorWatcher* controller = |
176 static_cast<FileDescriptorWatcher*>(context); | 176 static_cast<FileDescriptorWatcher*>(context); |
177 DCHECK_EQ(fdref, controller->fdref_); | 177 DCHECK_EQ(fdref, controller->fdref_.get()); |
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().get(); |
187 DCHECK(pump); | 187 DCHECK(pump); |
188 if (callback_types & kCFFileDescriptorWriteCallBack) | 188 if (callback_types & kCFFileDescriptorWriteCallBack) |
189 controller->OnFileCanWriteWithoutBlocking(fd, pump); | 189 controller->OnFileCanWriteWithoutBlocking(fd, pump); |
190 | 190 |
191 // Perform the read callback only if the file descriptor has not been | 191 // Perform the read callback only if the file descriptor has not been |
192 // invalidated in the write callback. As |FileDescriptorWatcher| invalidates | 192 // invalidated in the write callback. As |FileDescriptorWatcher| invalidates |
193 // its file descriptor on destruction, the file descriptor being valid also | 193 // its file descriptor on destruction, the file descriptor being valid also |
194 // guarantees that |controller| has not been deleted. | 194 // guarantees that |controller| has not been deleted. |
195 if (callback_types & kCFFileDescriptorReadCallBack && | 195 if (callback_types & kCFFileDescriptorReadCallBack && |
196 CFFileDescriptorIsValid(fdref)) { | 196 CFFileDescriptorIsValid(fdref)) { |
197 DCHECK_EQ(fdref, controller->fdref_); | 197 DCHECK_EQ(fdref, controller->fdref_.get()); |
198 controller->OnFileCanReadWithoutBlocking(fd, pump); | 198 controller->OnFileCanReadWithoutBlocking(fd, pump); |
199 } | 199 } |
200 | 200 |
201 // Re-enable callbacks after the read/write if the file descriptor is still | 201 // Re-enable callbacks after the read/write if the file descriptor is still |
202 // valid and the controller is persistent. | 202 // valid and the controller is persistent. |
203 if (CFFileDescriptorIsValid(fdref) && controller->is_persistent_) { | 203 if (CFFileDescriptorIsValid(fdref) && controller->is_persistent_) { |
204 DCHECK_EQ(fdref, controller->fdref_); | 204 DCHECK_EQ(fdref, controller->fdref_.get()); |
205 CFFileDescriptorEnableCallBacks(fdref, callback_types); | 205 CFFileDescriptorEnableCallBacks(fdref, callback_types); |
206 } | 206 } |
207 } | 207 } |
208 | 208 |
209 } // namespace base | 209 } // namespace base |
OLD | NEW |