| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/serial/serial_io_handler_posix.h" | 5 #include "chrome/browser/extensions/api/serial/serial_io_handler_posix.h" |
| 6 | 6 |
| 7 #include "base/posix/eintr_wrapper.h" | 7 #include "base/posix/eintr_wrapper.h" |
| 8 | 8 |
| 9 namespace { |
| 10 const base::PlatformFile kInvalidPlatformFileValue = -1; |
| 11 } // namespace |
| 12 |
| 9 namespace extensions { | 13 namespace extensions { |
| 10 | 14 |
| 11 // static | 15 // static |
| 12 scoped_refptr<SerialIoHandler> SerialIoHandler::Create() { | 16 scoped_refptr<SerialIoHandler> SerialIoHandler::Create() { |
| 13 return new SerialIoHandlerPosix(); | 17 return new SerialIoHandlerPosix(); |
| 14 } | 18 } |
| 15 | 19 |
| 16 void SerialIoHandlerPosix::ReadImpl() { | 20 void SerialIoHandlerPosix::ReadImpl() { |
| 17 DCHECK(CalledOnValidThread()); | 21 DCHECK(CalledOnValidThread()); |
| 18 DCHECK(pending_read_buffer()); | 22 DCHECK(pending_read_buffer()); |
| 19 DCHECK_NE(file(), base::kInvalidPlatformFileValue); | 23 DCHECK_NE(file(), kInvalidPlatformFileValue); |
| 20 | 24 |
| 21 EnsureWatchingReads(); | 25 EnsureWatchingReads(); |
| 22 } | 26 } |
| 23 | 27 |
| 24 void SerialIoHandlerPosix::WriteImpl() { | 28 void SerialIoHandlerPosix::WriteImpl() { |
| 25 DCHECK(CalledOnValidThread()); | 29 DCHECK(CalledOnValidThread()); |
| 26 DCHECK(pending_write_buffer()); | 30 DCHECK(pending_write_buffer()); |
| 27 DCHECK_NE(file(), base::kInvalidPlatformFileValue); | 31 DCHECK_NE(file(), kInvalidPlatformFileValue); |
| 28 | 32 |
| 29 EnsureWatchingWrites(); | 33 EnsureWatchingWrites(); |
| 30 } | 34 } |
| 31 | 35 |
| 32 void SerialIoHandlerPosix::CancelReadImpl() { | 36 void SerialIoHandlerPosix::CancelReadImpl() { |
| 33 DCHECK(CalledOnValidThread()); | 37 DCHECK(CalledOnValidThread()); |
| 34 is_watching_reads_ = false; | 38 is_watching_reads_ = false; |
| 35 file_read_watcher_.StopWatchingFileDescriptor(); | 39 file_read_watcher_.StopWatchingFileDescriptor(); |
| 36 } | 40 } |
| 37 | 41 |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 91 } else { | 95 } else { |
| 92 // Stop watching the fd if we get notifications with no pending | 96 // Stop watching the fd if we get notifications with no pending |
| 93 // writes to avoid starving the message loop. | 97 // writes to avoid starving the message loop. |
| 94 is_watching_writes_ = false; | 98 is_watching_writes_ = false; |
| 95 file_write_watcher_.StopWatchingFileDescriptor(); | 99 file_write_watcher_.StopWatchingFileDescriptor(); |
| 96 } | 100 } |
| 97 } | 101 } |
| 98 | 102 |
| 99 void SerialIoHandlerPosix::EnsureWatchingReads() { | 103 void SerialIoHandlerPosix::EnsureWatchingReads() { |
| 100 DCHECK(CalledOnValidThread()); | 104 DCHECK(CalledOnValidThread()); |
| 101 DCHECK_NE(file(), base::kInvalidPlatformFileValue); | 105 DCHECK_NE(file(), kInvalidPlatformFileValue); |
| 102 if (!is_watching_reads_) { | 106 if (!is_watching_reads_) { |
| 103 is_watching_reads_ = base::MessageLoopForIO::current()->WatchFileDescriptor( | 107 is_watching_reads_ = base::MessageLoopForIO::current()->WatchFileDescriptor( |
| 104 file(), true, base::MessageLoopForIO::WATCH_READ, | 108 file(), true, base::MessageLoopForIO::WATCH_READ, |
| 105 &file_read_watcher_, this); | 109 &file_read_watcher_, this); |
| 106 } | 110 } |
| 107 } | 111 } |
| 108 | 112 |
| 109 void SerialIoHandlerPosix::EnsureWatchingWrites() { | 113 void SerialIoHandlerPosix::EnsureWatchingWrites() { |
| 110 DCHECK(CalledOnValidThread()); | 114 DCHECK(CalledOnValidThread()); |
| 111 DCHECK_NE(file(), base::kInvalidPlatformFileValue); | 115 DCHECK_NE(file(), kInvalidPlatformFileValue); |
| 112 if (!is_watching_writes_) { | 116 if (!is_watching_writes_) { |
| 113 is_watching_writes_ = | 117 is_watching_writes_ = |
| 114 base::MessageLoopForIO::current()->WatchFileDescriptor( | 118 base::MessageLoopForIO::current()->WatchFileDescriptor( |
| 115 file(), true, base::MessageLoopForIO::WATCH_WRITE, | 119 file(), true, base::MessageLoopForIO::WATCH_WRITE, |
| 116 &file_write_watcher_, this); | 120 &file_write_watcher_, this); |
| 117 } | 121 } |
| 118 } | 122 } |
| 119 | 123 |
| 120 } // namespace extensions | 124 } // namespace extensions |
| OLD | NEW |