1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
base / message_loop / message_pump_io_ios.cc [blame]
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/message_loop/message_pump_io_ios.h"
#include "base/notreached.h"
namespace base {
MessagePumpIOSForIO::FdWatchController::FdWatchController(
const Location& from_here)
: FdWatchControllerInterface(from_here) {}
MessagePumpIOSForIO::FdWatchController::~FdWatchController() {
StopWatchingFileDescriptor();
}
bool MessagePumpIOSForIO::FdWatchController::StopWatchingFileDescriptor() {
if (fdref_ == NULL)
return true;
CFFileDescriptorDisableCallBacks(fdref_.get(), callback_types_);
if (pump_)
pump_->RemoveRunLoopSource(fd_source_);
fd_source_.reset();
fdref_.reset();
callback_types_ = 0;
pump_.reset();
watcher_ = NULL;
return true;
}
void MessagePumpIOSForIO::FdWatchController::Init(CFFileDescriptorRef fdref,
CFOptionFlags callback_types,
CFRunLoopSourceRef fd_source,
bool is_persistent) {
DCHECK(fdref);
DCHECK(!fdref_.is_valid());
is_persistent_ = is_persistent;
fdref_.reset(fdref);
callback_types_ = callback_types;
fd_source_.reset(fd_source);
}
void MessagePumpIOSForIO::FdWatchController::OnFileCanReadWithoutBlocking(
int fd,
MessagePumpIOSForIO* pump) {
DCHECK(callback_types_ & kCFFileDescriptorReadCallBack);
watcher_->OnFileCanReadWithoutBlocking(fd);
}
void MessagePumpIOSForIO::FdWatchController::OnFileCanWriteWithoutBlocking(
int fd,
MessagePumpIOSForIO* pump) {
DCHECK(callback_types_ & kCFFileDescriptorWriteCallBack);
watcher_->OnFileCanWriteWithoutBlocking(fd);
}
MessagePumpIOSForIO::MessagePumpIOSForIO() = default;
MessagePumpIOSForIO::~MessagePumpIOSForIO() = default;
bool MessagePumpIOSForIO::WatchFileDescriptor(int fd,
bool persistent,
int mode,
FdWatchController* controller,
FdWatcher* delegate) {
DCHECK_GE(fd, 0);
DCHECK(controller);
DCHECK(delegate);
DCHECK(mode == WATCH_READ || mode == WATCH_WRITE || mode == WATCH_READ_WRITE);
// WatchFileDescriptor should be called on the pump thread. It is not
// threadsafe, and your watcher may never be registered.
DCHECK(watch_file_descriptor_caller_checker_.CalledOnValidThread());
CFFileDescriptorContext source_context = {0};
source_context.info = controller;
CFOptionFlags callback_types = 0;
if (mode & WATCH_READ) {
callback_types |= kCFFileDescriptorReadCallBack;
}
if (mode & WATCH_WRITE) {
callback_types |= kCFFileDescriptorWriteCallBack;
}
CFFileDescriptorRef fdref = controller->fdref_.get();
if (fdref == NULL) {
apple::ScopedCFTypeRef<CFFileDescriptorRef> scoped_fdref(
CFFileDescriptorCreate(kCFAllocatorDefault, fd, false, HandleFdIOEvent,
&source_context));
if (scoped_fdref == NULL) {
NOTREACHED() << "CFFileDescriptorCreate failed";
}
CFFileDescriptorEnableCallBacks(scoped_fdref, callback_types);
// TODO(wtc): what should the 'order' argument be?
apple::ScopedCFTypeRef<CFRunLoopSourceRef> scoped_fd_source(
CFFileDescriptorCreateRunLoopSource(kCFAllocatorDefault, scoped_fdref,
0));
if (scoped_fd_source == NULL) {
NOTREACHED() << "CFFileDescriptorCreateRunLoopSource failed";
}
CFRunLoopAddSource(run_loop(), scoped_fd_source, kCFRunLoopCommonModes);
// Transfer ownership of scoped_fdref and fd_source to controller.
controller->Init(scoped_fdref.release(), callback_types,
scoped_fd_source.release(), persistent);
} else {
// It's illegal to use this function to listen on 2 separate fds with the
// same |controller|.
if (CFFileDescriptorGetNativeDescriptor(fdref) != fd) {
NOTREACHED() << "FDs don't match: "
<< CFFileDescriptorGetNativeDescriptor(fdref)
<< " != " << fd;
}
if (persistent != controller->is_persistent_) {
NOTREACHED() << "persistent doesn't match";
}
// Combine old/new event masks.
CFFileDescriptorDisableCallBacks(fdref, controller->callback_types_);
controller->callback_types_ |= callback_types;
CFFileDescriptorEnableCallBacks(fdref, controller->callback_types_);
}
controller->set_watcher(delegate);
controller->set_pump(weak_factory_.GetWeakPtr());
return true;
}
void MessagePumpIOSForIO::RemoveRunLoopSource(CFRunLoopSourceRef source) {
CFRunLoopRemoveSource(run_loop(), source, kCFRunLoopCommonModes);
}
// static
void MessagePumpIOSForIO::HandleFdIOEvent(CFFileDescriptorRef fdref,
CFOptionFlags callback_types,
void* context) {
FdWatchController* controller = static_cast<FdWatchController*>(context);
DCHECK_EQ(fdref, controller->fdref_.get());
// Ensure that |fdref| will remain live for the duration of this function
// call even if |controller| is deleted or |StopWatchingFileDescriptor()| is
// called, either of which will cause |fdref| to be released.
apple::ScopedCFTypeRef<CFFileDescriptorRef> scoped_fdref(
fdref, base::scoped_policy::RETAIN);
int fd = CFFileDescriptorGetNativeDescriptor(fdref);
MessagePumpIOSForIO* pump = controller->pump().get();
DCHECK(pump);
// Inform ThreadController of this native work item for tracking and tracing
// purposes.
Delegate::ScopedDoWorkItem scoped_do_work_item;
if (pump->delegate()) {
scoped_do_work_item = pump->delegate()->BeginWorkItem();
}
if (callback_types & kCFFileDescriptorWriteCallBack)
controller->OnFileCanWriteWithoutBlocking(fd, pump);
// Perform the read callback only if the file descriptor has not been
// invalidated in the write callback. As |FdWatchController| invalidates
// its file descriptor on destruction, the file descriptor being valid also
// guarantees that |controller| has not been deleted.
if (callback_types & kCFFileDescriptorReadCallBack &&
CFFileDescriptorIsValid(fdref)) {
DCHECK_EQ(fdref, controller->fdref_.get());
controller->OnFileCanReadWithoutBlocking(fd, pump);
}
// Re-enable callbacks after the read/write if the file descriptor is still
// valid and the controller is persistent.
if (CFFileDescriptorIsValid(fdref) && controller->is_persistent_) {
DCHECK_EQ(fdref, controller->fdref_.get());
CFFileDescriptorEnableCallBacks(fdref, callback_types);
}
}
} // namespace base