Adding owners for third_party
[chromium-blink-merge.git] / base / message_pump_io_ios.cc
blobca800ffaa5aaba38250ec001128cfa0f090376fb
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"
7 namespace base {
9 MessagePumpIOSForIO::FileDescriptorWatcher::FileDescriptorWatcher()
10 : is_persistent_(false),
11 fdref_(NULL),
12 callback_types_(0),
13 fd_source_(NULL),
14 pump_(NULL),
15 watcher_(NULL) {
18 MessagePumpIOSForIO::FileDescriptorWatcher::~FileDescriptorWatcher() {
19 StopWatchingFileDescriptor();
22 bool MessagePumpIOSForIO::FileDescriptorWatcher::StopWatchingFileDescriptor() {
23 if (fdref_ == NULL)
24 return true;
26 CFFileDescriptorDisableCallBacks(fdref_, callback_types_);
27 pump_->RemoveRunLoopSource(fd_source_);
28 fd_source_.reset();
29 fdref_.reset();
30 callback_types_ = 0;
31 pump_ = NULL;
32 watcher_ = NULL;
33 return true;
36 void MessagePumpIOSForIO::FileDescriptorWatcher::Init(
37 CFFileDescriptorRef fdref,
38 CFOptionFlags callback_types,
39 CFRunLoopSourceRef fd_source,
40 bool is_persistent) {
41 DCHECK(fdref);
42 DCHECK(!fdref_);
44 is_persistent_ = is_persistent;
45 fdref_.reset(fdref);
46 callback_types_ = callback_types;
47 fd_source_.reset(fd_source);
50 void MessagePumpIOSForIO::FileDescriptorWatcher::OnFileCanReadWithoutBlocking(
51 int fd,
52 MessagePumpIOSForIO* pump) {
53 DCHECK(callback_types_ & kCFFileDescriptorReadCallBack);
54 pump->WillProcessIOEvent();
55 watcher_->OnFileCanReadWithoutBlocking(fd);
56 pump->DidProcessIOEvent();
59 void MessagePumpIOSForIO::FileDescriptorWatcher::OnFileCanWriteWithoutBlocking(
60 int fd,
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(
75 int fd,
76 bool persistent,
77 int mode,
78 FileDescriptorWatcher *controller,
79 Watcher *delegate) {
80 DCHECK_GE(fd, 0);
81 DCHECK(controller);
82 DCHECK(delegate);
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_;
101 if (fdref == NULL) {
102 base::mac::ScopedCFTypeRef<CFFileDescriptorRef> scoped_fdref(
103 CFFileDescriptorCreate(kCFAllocatorDefault, fd, false, HandleFdIOEvent,
104 &source_context));
105 if (scoped_fdref == NULL) {
106 NOTREACHED() << "CFFileDescriptorCreate failed";
107 return false;
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,
115 scoped_fdref,
116 0));
117 if (scoped_fd_source == NULL) {
118 NOTREACHED() << "CFFileDescriptorCreateRunLoopSource failed";
119 return false;
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);
126 } else {
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)
132 << " != " << fd;
133 return false;
135 if (persistent != controller->is_persistent_) {
136 NOTREACHED() << "persistent doesn't match";
137 return false;
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);
149 return true;
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());
172 // static
173 void MessagePumpIOSForIO::HandleFdIOEvent(CFFileDescriptorRef fdref,
174 CFOptionFlags callback_types,
175 void* context) {
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);
209 } // namespace base