| 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.h" | 5 #include "chrome/browser/extensions/api/serial/serial_io_handler.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
| 9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 112 DCHECK(!IsWritePending()); | 112 DCHECK(!IsWritePending()); |
| 113 pending_write_buffer_ = new net::IOBuffer(data.length()); | 113 pending_write_buffer_ = new net::IOBuffer(data.length()); |
| 114 pending_write_buffer_len_ = data.length(); | 114 pending_write_buffer_len_ = data.length(); |
| 115 memcpy(pending_write_buffer_->data(), data.data(), pending_write_buffer_len_); | 115 memcpy(pending_write_buffer_->data(), data.data(), pending_write_buffer_len_); |
| 116 write_canceled_ = false; | 116 write_canceled_ = false; |
| 117 AddRef(); | 117 AddRef(); |
| 118 WriteImpl(); | 118 WriteImpl(); |
| 119 } | 119 } |
| 120 | 120 |
| 121 void SerialIoHandler::ReadCompleted(int bytes_read, | 121 void SerialIoHandler::ReadCompleted(int bytes_read, |
| 122 api::serial::ReceiveError error) { | 122 device::ReceiveError error) { |
| 123 DCHECK(CalledOnValidThread()); | 123 DCHECK(CalledOnValidThread()); |
| 124 DCHECK(IsReadPending()); | 124 DCHECK(IsReadPending()); |
| 125 read_complete_.Run(std::string(pending_read_buffer_->data(), bytes_read), | 125 read_complete_.Run(std::string(pending_read_buffer_->data(), bytes_read), |
| 126 error); | 126 error); |
| 127 pending_read_buffer_ = NULL; | 127 pending_read_buffer_ = NULL; |
| 128 pending_read_buffer_len_ = 0; | 128 pending_read_buffer_len_ = 0; |
| 129 Release(); | 129 Release(); |
| 130 } | 130 } |
| 131 | 131 |
| 132 void SerialIoHandler::WriteCompleted(int bytes_written, | 132 void SerialIoHandler::WriteCompleted(int bytes_written, |
| 133 api::serial::SendError error) { | 133 device::SendError error) { |
| 134 DCHECK(CalledOnValidThread()); | 134 DCHECK(CalledOnValidThread()); |
| 135 DCHECK(IsWritePending()); | 135 DCHECK(IsWritePending()); |
| 136 write_complete_.Run(bytes_written, error); | 136 write_complete_.Run(bytes_written, error); |
| 137 pending_write_buffer_ = NULL; | 137 pending_write_buffer_ = NULL; |
| 138 pending_write_buffer_len_ = 0; | 138 pending_write_buffer_len_ = 0; |
| 139 Release(); | 139 Release(); |
| 140 } | 140 } |
| 141 | 141 |
| 142 bool SerialIoHandler::IsReadPending() const { | 142 bool SerialIoHandler::IsReadPending() const { |
| 143 DCHECK(CalledOnValidThread()); | 143 DCHECK(CalledOnValidThread()); |
| 144 return pending_read_buffer_ != NULL; | 144 return pending_read_buffer_ != NULL; |
| 145 } | 145 } |
| 146 | 146 |
| 147 bool SerialIoHandler::IsWritePending() const { | 147 bool SerialIoHandler::IsWritePending() const { |
| 148 DCHECK(CalledOnValidThread()); | 148 DCHECK(CalledOnValidThread()); |
| 149 return pending_write_buffer_ != NULL; | 149 return pending_write_buffer_ != NULL; |
| 150 } | 150 } |
| 151 | 151 |
| 152 void SerialIoHandler::CancelRead(api::serial::ReceiveError reason) { | 152 void SerialIoHandler::CancelRead(device::ReceiveError reason) { |
| 153 DCHECK(CalledOnValidThread()); | 153 DCHECK(CalledOnValidThread()); |
| 154 if (IsReadPending()) { | 154 if (IsReadPending()) { |
| 155 read_canceled_ = true; | 155 read_canceled_ = true; |
| 156 read_cancel_reason_ = reason; | 156 read_cancel_reason_ = reason; |
| 157 CancelReadImpl(); | 157 CancelReadImpl(); |
| 158 } | 158 } |
| 159 } | 159 } |
| 160 | 160 |
| 161 void SerialIoHandler::CancelWrite(api::serial::SendError reason) { | 161 void SerialIoHandler::CancelWrite(device::SendError reason) { |
| 162 DCHECK(CalledOnValidThread()); | 162 DCHECK(CalledOnValidThread()); |
| 163 if (IsWritePending()) { | 163 if (IsWritePending()) { |
| 164 write_canceled_ = true; | 164 write_canceled_ = true; |
| 165 write_cancel_reason_ = reason; | 165 write_cancel_reason_ = reason; |
| 166 CancelWriteImpl(); | 166 CancelWriteImpl(); |
| 167 } | 167 } |
| 168 } | 168 } |
| 169 | 169 |
| 170 void SerialIoHandler::QueueReadCompleted(int bytes_read, | 170 void SerialIoHandler::QueueReadCompleted(int bytes_read, |
| 171 api::serial::ReceiveError error) { | 171 device::ReceiveError error) { |
| 172 base::MessageLoop::current()->PostTask( | 172 base::MessageLoop::current()->PostTask( |
| 173 FROM_HERE, base::Bind(&SerialIoHandler::ReadCompleted, this, | 173 FROM_HERE, base::Bind(&SerialIoHandler::ReadCompleted, this, |
| 174 bytes_read, error)); | 174 bytes_read, error)); |
| 175 } | 175 } |
| 176 | 176 |
| 177 void SerialIoHandler::QueueWriteCompleted(int bytes_written, | 177 void SerialIoHandler::QueueWriteCompleted(int bytes_written, |
| 178 api::serial::SendError error) { | 178 device::SendError error) { |
| 179 base::MessageLoop::current()->PostTask( | 179 base::MessageLoop::current()->PostTask( |
| 180 FROM_HERE, base::Bind(&SerialIoHandler::WriteCompleted, this, | 180 FROM_HERE, base::Bind(&SerialIoHandler::WriteCompleted, this, |
| 181 bytes_written, error)); | 181 bytes_written, error)); |
| 182 } | 182 } |
| 183 | 183 |
| 184 } // namespace extensions | 184 } // namespace extensions |
| 185 | 185 |
| OLD | NEW |