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), | |
15 watcher_(NULL) { | 14 watcher_(NULL) { |
16 } | 15 } |
17 | 16 |
18 MessagePumpIOSForIO::FileDescriptorWatcher::~FileDescriptorWatcher() { | 17 MessagePumpIOSForIO::FileDescriptorWatcher::~FileDescriptorWatcher() { |
19 StopWatchingFileDescriptor(); | 18 StopWatchingFileDescriptor(); |
20 } | 19 } |
21 | 20 |
22 bool MessagePumpIOSForIO::FileDescriptorWatcher::StopWatchingFileDescriptor() { | 21 bool MessagePumpIOSForIO::FileDescriptorWatcher::StopWatchingFileDescriptor() { |
23 if (fdref_ == NULL) | 22 if (fdref_ == NULL) |
24 return true; | 23 return true; |
25 | 24 |
26 CFFileDescriptorDisableCallBacks(fdref_, callback_types_); | 25 CFFileDescriptorDisableCallBacks(fdref_, callback_types_); |
27 pump_->RemoveRunLoopSource(fd_source_); | 26 if (pump_) |
| 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_ = NULL; | 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); |
(...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() { | 68 MessagePumpIOSForIO::MessagePumpIOSForIO() : weak_factory_(this) { |
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(this); | 146 controller->set_pump(weak_factory_.GetWeakPtr()); |
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(); | 186 MessagePumpIOSForIO* pump = controller->pump().get(); |
| 187 DCHECK(pump); |
187 if (callback_types & kCFFileDescriptorWriteCallBack) | 188 if (callback_types & kCFFileDescriptorWriteCallBack) |
188 controller->OnFileCanWriteWithoutBlocking(fd, pump); | 189 controller->OnFileCanWriteWithoutBlocking(fd, pump); |
189 | 190 |
190 // 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 |
191 // invalidated in the write callback. As |FileDescriptorWatcher| invalidates | 192 // invalidated in the write callback. As |FileDescriptorWatcher| invalidates |
192 // its file descriptor on destruction, the file descriptor being valid also | 193 // its file descriptor on destruction, the file descriptor being valid also |
193 // guarantees that |controller| has not been deleted. | 194 // guarantees that |controller| has not been deleted. |
194 if (callback_types & kCFFileDescriptorReadCallBack && | 195 if (callback_types & kCFFileDescriptorReadCallBack && |
195 CFFileDescriptorIsValid(fdref)) { | 196 CFFileDescriptorIsValid(fdref)) { |
196 DCHECK_EQ(fdref, controller->fdref_); | 197 DCHECK_EQ(fdref, controller->fdref_); |
197 controller->OnFileCanReadWithoutBlocking(fd, pump); | 198 controller->OnFileCanReadWithoutBlocking(fd, pump); |
198 } | 199 } |
199 | 200 |
200 // 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 |
201 // valid and the controller is persistent. | 202 // valid and the controller is persistent. |
202 if (CFFileDescriptorIsValid(fdref) && controller->is_persistent_) { | 203 if (CFFileDescriptorIsValid(fdref) && controller->is_persistent_) { |
203 DCHECK_EQ(fdref, controller->fdref_); | 204 DCHECK_EQ(fdref, controller->fdref_); |
204 CFFileDescriptorEnableCallBacks(fdref, callback_types); | 205 CFFileDescriptorEnableCallBacks(fdref, callback_types); |
205 } | 206 } |
206 } | 207 } |
207 | 208 |
208 } // namespace base | 209 } // namespace base |
OLD | NEW |