Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(162)

Side by Side Diff: trunk/src/base/message_loop/message_pump_io_ios.cc

Issue 16897005: Revert 206507 "Move message_pump to base/message_loop." (Closed) Base URL: svn://svn.chromium.org/chrome/
Patch Set: Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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.
4
5 #include "base/message_loop/message_pump_io_ios.h"
6
7 namespace base {
8
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) {
16 }
17
18 MessagePumpIOSForIO::FileDescriptorWatcher::~FileDescriptorWatcher() {
19 StopWatchingFileDescriptor();
20 }
21
22 bool MessagePumpIOSForIO::FileDescriptorWatcher::StopWatchingFileDescriptor() {
23 if (fdref_ == NULL)
24 return true;
25
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;
34 }
35
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_);
43
44 is_persistent_ = is_persistent;
45 fdref_.reset(fdref);
46 callback_types_ = callback_types;
47 fd_source_.reset(fd_source);
48 }
49
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();
57 }
58
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();
66 }
67
68 MessagePumpIOSForIO::MessagePumpIOSForIO() {
69 }
70
71 MessagePumpIOSForIO::~MessagePumpIOSForIO() {
72 }
73
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);
84
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());
88
89 CFFileDescriptorContext source_context = {0};
90 source_context.info = controller;
91
92 CFOptionFlags callback_types = 0;
93 if (mode & WATCH_READ) {
94 callback_types |= kCFFileDescriptorReadCallBack;
95 }
96 if (mode & WATCH_WRITE) {
97 callback_types |= kCFFileDescriptorWriteCallBack;
98 }
99
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;
108 }
109
110 CFFileDescriptorEnableCallBacks(scoped_fdref, callback_types);
111
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;
120 }
121 CFRunLoopAddSource(run_loop(), scoped_fd_source, kCFRunLoopCommonModes);
122
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;
134 }
135 if (persistent != controller->is_persistent_) {
136 NOTREACHED() << "persistent doesn't match";
137 return false;
138 }
139
140 // Combine old/new event masks.
141 CFFileDescriptorDisableCallBacks(fdref, controller->callback_types_);
142 controller->callback_types_ |= callback_types;
143 CFFileDescriptorEnableCallBacks(fdref, controller->callback_types_);
144 }
145
146 controller->set_watcher(delegate);
147 controller->set_pump(this);
148
149 return true;
150 }
151
152 void MessagePumpIOSForIO::RemoveRunLoopSource(CFRunLoopSourceRef source) {
153 CFRunLoopRemoveSource(run_loop(), source, kCFRunLoopCommonModes);
154 }
155
156 void MessagePumpIOSForIO::AddIOObserver(IOObserver *obs) {
157 io_observers_.AddObserver(obs);
158 }
159
160 void MessagePumpIOSForIO::RemoveIOObserver(IOObserver *obs) {
161 io_observers_.RemoveObserver(obs);
162 }
163
164 void MessagePumpIOSForIO::WillProcessIOEvent() {
165 FOR_EACH_OBSERVER(IOObserver, io_observers_, WillProcessIOEvent());
166 }
167
168 void MessagePumpIOSForIO::DidProcessIOEvent() {
169 FOR_EACH_OBSERVER(IOObserver, io_observers_, DidProcessIOEvent());
170 }
171
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_);
179
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);
185
186 int fd = CFFileDescriptorGetNativeDescriptor(fdref);
187 MessagePumpIOSForIO* pump = controller->pump();
188 if (callback_types & kCFFileDescriptorWriteCallBack)
189 controller->OnFileCanWriteWithoutBlocking(fd, pump);
190
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);
199 }
200
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);
206 }
207 }
208
209 } // namespace base
OLDNEW
« no previous file with comments | « trunk/src/base/message_loop/message_pump_io_ios.h ('k') | trunk/src/base/message_loop/message_pump_io_ios_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698