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.
5 #include "base/message_loop/message_pump_io_ios.h"
9 MessagePumpIOSForIO::FileDescriptorWatcher::FileDescriptorWatcher()
10 : is_persistent_(false),
17 MessagePumpIOSForIO::FileDescriptorWatcher::~FileDescriptorWatcher() {
18 StopWatchingFileDescriptor();
21 bool MessagePumpIOSForIO::FileDescriptorWatcher::StopWatchingFileDescriptor() {
25 CFFileDescriptorDisableCallBacks(fdref_
, callback_types_
);
27 pump_
->RemoveRunLoopSource(fd_source_
);
36 void MessagePumpIOSForIO::FileDescriptorWatcher::Init(
37 CFFileDescriptorRef fdref
,
38 CFOptionFlags callback_types
,
39 CFRunLoopSourceRef fd_source
,
44 is_persistent_
= is_persistent
;
46 callback_types_
= callback_types
;
47 fd_source_
.reset(fd_source
);
50 void MessagePumpIOSForIO::FileDescriptorWatcher::OnFileCanReadWithoutBlocking(
52 MessagePumpIOSForIO
* pump
) {
53 DCHECK(callback_types_
& kCFFileDescriptorReadCallBack
);
54 pump
->WillProcessIOEvent();
55 watcher_
->OnFileCanReadWithoutBlocking(fd
);
56 pump
->DidProcessIOEvent();
59 void MessagePumpIOSForIO::FileDescriptorWatcher::OnFileCanWriteWithoutBlocking(
61 MessagePumpIOSForIO
* pump
) {
62 DCHECK(callback_types_
& kCFFileDescriptorWriteCallBack
);
63 pump
->WillProcessIOEvent();
64 watcher_
->OnFileCanWriteWithoutBlocking(fd
);
65 pump
->DidProcessIOEvent();
68 MessagePumpIOSForIO::MessagePumpIOSForIO() : weak_factory_(this) {
71 MessagePumpIOSForIO::~MessagePumpIOSForIO() {
74 bool MessagePumpIOSForIO::WatchFileDescriptor(
78 FileDescriptorWatcher
*controller
,
83 DCHECK(mode
== WATCH_READ
|| mode
== WATCH_WRITE
|| mode
== WATCH_READ_WRITE
);
85 // WatchFileDescriptor should be called on the pump thread. It is not
86 // threadsafe, and your watcher may never be registered.
87 DCHECK(watch_file_descriptor_caller_checker_
.CalledOnValidThread());
89 CFFileDescriptorContext source_context
= {0};
90 source_context
.info
= controller
;
92 CFOptionFlags callback_types
= 0;
93 if (mode
& WATCH_READ
) {
94 callback_types
|= kCFFileDescriptorReadCallBack
;
96 if (mode
& WATCH_WRITE
) {
97 callback_types
|= kCFFileDescriptorWriteCallBack
;
100 CFFileDescriptorRef fdref
= controller
->fdref_
;
102 base::ScopedCFTypeRef
<CFFileDescriptorRef
> scoped_fdref(
103 CFFileDescriptorCreate(
104 kCFAllocatorDefault
, fd
, false, HandleFdIOEvent
, &source_context
));
105 if (scoped_fdref
== NULL
) {
106 NOTREACHED() << "CFFileDescriptorCreate failed";
110 CFFileDescriptorEnableCallBacks(scoped_fdref
, callback_types
);
112 // TODO(wtc): what should the 'order' argument be?
113 base::ScopedCFTypeRef
<CFRunLoopSourceRef
> scoped_fd_source(
114 CFFileDescriptorCreateRunLoopSource(
115 kCFAllocatorDefault
, scoped_fdref
, 0));
116 if (scoped_fd_source
== NULL
) {
117 NOTREACHED() << "CFFileDescriptorCreateRunLoopSource failed";
120 CFRunLoopAddSource(run_loop(), scoped_fd_source
, kCFRunLoopCommonModes
);
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
);
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
)
134 if (persistent
!= controller
->is_persistent_
) {
135 NOTREACHED() << "persistent doesn't match";
139 // Combine old/new event masks.
140 CFFileDescriptorDisableCallBacks(fdref
, controller
->callback_types_
);
141 controller
->callback_types_
|= callback_types
;
142 CFFileDescriptorEnableCallBacks(fdref
, controller
->callback_types_
);
145 controller
->set_watcher(delegate
);
146 controller
->set_pump(weak_factory_
.GetWeakPtr());
151 void MessagePumpIOSForIO::RemoveRunLoopSource(CFRunLoopSourceRef source
) {
152 CFRunLoopRemoveSource(run_loop(), source
, kCFRunLoopCommonModes
);
155 void MessagePumpIOSForIO::AddIOObserver(IOObserver
*obs
) {
156 io_observers_
.AddObserver(obs
);
159 void MessagePumpIOSForIO::RemoveIOObserver(IOObserver
*obs
) {
160 io_observers_
.RemoveObserver(obs
);
163 void MessagePumpIOSForIO::WillProcessIOEvent() {
164 FOR_EACH_OBSERVER(IOObserver
, io_observers_
, WillProcessIOEvent());
167 void MessagePumpIOSForIO::DidProcessIOEvent() {
168 FOR_EACH_OBSERVER(IOObserver
, io_observers_
, DidProcessIOEvent());
172 void MessagePumpIOSForIO::HandleFdIOEvent(CFFileDescriptorRef fdref
,
173 CFOptionFlags callback_types
,
175 FileDescriptorWatcher
* controller
=
176 static_cast<FileDescriptorWatcher
*>(context
);
177 DCHECK_EQ(fdref
, controller
->fdref_
);
179 // Ensure that |fdref| will remain live for the duration of this function
180 // call even if |controller| is deleted or |StopWatchingFileDescriptor()| is
181 // called, either of which will cause |fdref| to be released.
182 ScopedCFTypeRef
<CFFileDescriptorRef
> scoped_fdref(
183 fdref
, base::scoped_policy::RETAIN
);
185 int fd
= CFFileDescriptorGetNativeDescriptor(fdref
);
186 MessagePumpIOSForIO
* pump
= controller
->pump().get();
188 if (callback_types
& kCFFileDescriptorWriteCallBack
)
189 controller
->OnFileCanWriteWithoutBlocking(fd
, pump
);
191 // Perform the read callback only if the file descriptor has not been
192 // invalidated in the write callback. As |FileDescriptorWatcher| invalidates
193 // its file descriptor on destruction, the file descriptor being valid also
194 // guarantees that |controller| has not been deleted.
195 if (callback_types
& kCFFileDescriptorReadCallBack
&&
196 CFFileDescriptorIsValid(fdref
)) {
197 DCHECK_EQ(fdref
, controller
->fdref_
);
198 controller
->OnFileCanReadWithoutBlocking(fd
, pump
);
201 // Re-enable callbacks after the read/write if the file descriptor is still
202 // valid and the controller is persistent.
203 if (CFFileDescriptorIsValid(fdref
) && controller
->is_persistent_
) {
204 DCHECK_EQ(fdref
, controller
->fdref_
);
205 CFFileDescriptorEnableCallBacks(fdref
, callback_types
);