OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/extensions/api/messaging/native_message_process_host.h" | 5 #include "chrome/browser/extensions/api/messaging/native_message_process_host.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
192 FROM_HERE, | 192 FROM_HERE, |
193 base::Bind(&NativeMessageProcessHost::LaunchHostProcess, | 193 base::Bind(&NativeMessageProcessHost::LaunchHostProcess, |
194 weak_factory_.GetWeakPtr())); | 194 weak_factory_.GetWeakPtr())); |
195 } | 195 } |
196 | 196 |
197 scoped_refptr<base::SingleThreadTaskRunner> | 197 scoped_refptr<base::SingleThreadTaskRunner> |
198 NativeMessageProcessHost::task_runner() const { | 198 NativeMessageProcessHost::task_runner() const { |
199 return task_runner_; | 199 return task_runner_; |
200 } | 200 } |
201 | 201 |
202 #if defined(OS_POSIX) | |
203 void NativeMessageProcessHost::OnFileCanReadWithoutBlocking(int fd) { | |
204 DCHECK_EQ(fd, read_file_); | |
205 DoRead(); | |
206 } | |
207 | |
208 void NativeMessageProcessHost::OnFileCanWriteWithoutBlocking(int fd) { | |
209 NOTREACHED(); | |
210 } | |
211 #endif // !defined(OS_POSIX) | |
212 | |
213 void NativeMessageProcessHost::ReadNowForTesting() { | 202 void NativeMessageProcessHost::ReadNowForTesting() { |
214 DoRead(); | 203 DoRead(); |
215 } | 204 } |
216 | 205 |
217 void NativeMessageProcessHost::WaitRead() { | 206 void NativeMessageProcessHost::WaitRead() { |
218 if (closed_) | 207 if (closed_) |
219 return; | 208 return; |
220 | 209 |
221 DCHECK(!read_pending_); | 210 DCHECK(!read_pending_); |
222 | 211 |
223 // On POSIX FileStream::Read() uses blocking thread pool, so it's better to | 212 // On POSIX FileStream::Read() uses blocking thread pool, so it's better to |
224 // wait for the file to become readable before calling DoRead(). Otherwise it | 213 // wait for the file to become readable before calling DoRead(). Otherwise it |
225 // would always be consuming one thread in the thread pool. On Windows | 214 // would always be consuming one thread in the thread pool. On Windows |
226 // FileStream uses overlapped IO, so that optimization isn't necessary there. | 215 // FileStream uses overlapped IO, so that optimization isn't necessary there. |
227 #if defined(OS_POSIX) | 216 #if defined(OS_POSIX) |
228 base::MessageLoopForIO::current()->WatchFileDescriptor( | 217 read_controller_ = base::FileDescriptorWatcher::WatchReadable( |
229 read_file_, false /* persistent */, | 218 read_file_, |
230 base::MessageLoopForIO::WATCH_READ, &read_watcher_, this); | 219 base::Bind(&NativeMessageProcessHost::DoRead, base::Unretained(this))); |
231 #else // defined(OS_POSIX) | 220 #else // defined(OS_POSIX) |
232 DoRead(); | 221 DoRead(); |
233 #endif // defined(!OS_POSIX) | 222 #endif // defined(!OS_POSIX) |
234 } | 223 } |
235 | 224 |
236 void NativeMessageProcessHost::DoRead() { | 225 void NativeMessageProcessHost::DoRead() { |
237 DCHECK(task_runner_->BelongsToCurrentThread()); | 226 DCHECK(task_runner_->BelongsToCurrentThread()); |
238 | 227 |
| 228 #if defined(OS_POSIX) |
| 229 read_controller_.reset(); |
| 230 #endif |
| 231 |
239 while (!closed_ && !read_pending_) { | 232 while (!closed_ && !read_pending_) { |
240 read_buffer_ = new net::IOBuffer(kReadBufferSize); | 233 read_buffer_ = new net::IOBuffer(kReadBufferSize); |
241 int result = | 234 int result = |
242 read_stream_->Read(read_buffer_.get(), kReadBufferSize, | 235 read_stream_->Read(read_buffer_.get(), kReadBufferSize, |
243 base::Bind(&NativeMessageProcessHost::OnRead, | 236 base::Bind(&NativeMessageProcessHost::OnRead, |
244 weak_factory_.GetWeakPtr())); | 237 weak_factory_.GetWeakPtr())); |
245 HandleReadResult(result); | 238 HandleReadResult(result); |
246 } | 239 } |
247 } | 240 } |
248 | 241 |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
358 | 351 |
359 if (!closed_) { | 352 if (!closed_) { |
360 closed_ = true; | 353 closed_ = true; |
361 read_stream_.reset(); | 354 read_stream_.reset(); |
362 write_stream_.reset(); | 355 write_stream_.reset(); |
363 client_->CloseChannel(error_message); | 356 client_->CloseChannel(error_message); |
364 } | 357 } |
365 } | 358 } |
366 | 359 |
367 } // namespace extensions | 360 } // namespace extensions |
OLD | NEW |