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_pump_io_ios.h"
9 MessagePumpIOSForIO::FileDescriptorWatcher::FileDescriptorWatcher()
10 : is_persistent_(false),
18 MessagePumpIOSForIO::FileDescriptorWatcher::~FileDescriptorWatcher() {
19 StopWatchingFileDescriptor();
22 bool MessagePumpIOSForIO::FileDescriptorWatcher::StopWatchingFileDescriptor() {
26 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() {
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::mac::ScopedCFTypeRef
<CFFileDescriptorRef
> scoped_fdref(
103 CFFileDescriptorCreate(kCFAllocatorDefault
, fd
, false, HandleFdIOEvent
,
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::mac::ScopedCFTypeRef
<CFRunLoopSourceRef
> scoped_fd_source(
114 CFFileDescriptorCreateRunLoopSource(kCFAllocatorDefault
,
117 if (scoped_fd_source
== NULL
) {
118 NOTREACHED() << "CFFileDescriptorCreateRunLoopSource failed";
121 CFRunLoopAddSource(run_loop(), scoped_fd_source
, kCFRunLoopCommonModes
);
123 // Transfer ownership of scoped_fdref and fd_source to controller.
124 controller
->Init(scoped_fdref
.release(), callback_types
,
125 scoped_fd_source
.release(), persistent
);
127 // It's illegal to use this function to listen on 2 separate fds with the
128 // same |controller|.
129 if (CFFileDescriptorGetNativeDescriptor(fdref
) != fd
) {
130 NOTREACHED() << "FDs don't match: "
131 << CFFileDescriptorGetNativeDescriptor(fdref
)
135 if (persistent
!= controller
->is_persistent_
) {
136 NOTREACHED() << "persistent doesn't match";
140 // Combine old/new event masks.
141 CFFileDescriptorDisableCallBacks(fdref
, controller
->callback_types_
);
142 controller
->callback_types_
|= callback_types
;
143 CFFileDescriptorEnableCallBacks(fdref
, controller
->callback_types_
);
146 controller
->set_watcher(delegate
);
147 controller
->set_pump(this);
152 void MessagePumpIOSForIO::RemoveRunLoopSource(CFRunLoopSourceRef source
) {
153 CFRunLoopRemoveSource(run_loop(), source
, kCFRunLoopCommonModes
);
156 void MessagePumpIOSForIO::AddIOObserver(IOObserver
*obs
) {
157 io_observers_
.AddObserver(obs
);
160 void MessagePumpIOSForIO::RemoveIOObserver(IOObserver
*obs
) {
161 io_observers_
.RemoveObserver(obs
);
164 void MessagePumpIOSForIO::WillProcessIOEvent() {
165 FOR_EACH_OBSERVER(IOObserver
, io_observers_
, WillProcessIOEvent());
168 void MessagePumpIOSForIO::DidProcessIOEvent() {
169 FOR_EACH_OBSERVER(IOObserver
, io_observers_
, DidProcessIOEvent());
173 void MessagePumpIOSForIO::HandleFdIOEvent(CFFileDescriptorRef fdref
,
174 CFOptionFlags callback_types
,
176 FileDescriptorWatcher
* controller
=
177 static_cast<FileDescriptorWatcher
*>(context
);
178 DCHECK_EQ(fdref
, controller
->fdref_
);
180 // Ensure that |fdref| will remain live for the duration of this function
181 // call even if |controller| is deleted or |StopWatchingFileDescriptor()| is
182 // called, either of which will cause |fdref| to be released.
183 mac::ScopedCFTypeRef
<CFFileDescriptorRef
> scoped_fdref(
184 fdref
, base::scoped_policy::RETAIN
);
186 int fd
= CFFileDescriptorGetNativeDescriptor(fdref
);
187 MessagePumpIOSForIO
* pump
= controller
->pump();
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
);