| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "extensions/browser/api/serial/serial_connection.h" | 5 #include "extensions/browser/api/serial/serial_connection.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> |
| 8 | 9 |
| 9 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| 10 #include "base/lazy_instance.h" | 11 #include "base/lazy_instance.h" |
| 11 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
| 13 #include "base/stl_util.h" |
| 12 #include "extensions/browser/api/api_resource_manager.h" | 14 #include "extensions/browser/api/api_resource_manager.h" |
| 13 #include "extensions/common/api/serial.h" | 15 #include "extensions/common/api/serial.h" |
| 14 | 16 |
| 15 namespace extensions { | 17 namespace extensions { |
| 16 | 18 |
| 17 namespace { | 19 namespace { |
| 18 | 20 |
| 19 const int kDefaultBufferSize = 4096; | 21 const int kDefaultBufferSize = 4096; |
| 20 | 22 |
| 21 core_api::serial::SendError ConvertSendErrorFromMojo( | 23 core_api::serial::SendError ConvertSendErrorFromMojo( |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 130 return device::serial::STOP_BITS_ONE; | 132 return device::serial::STOP_BITS_ONE; |
| 131 case core_api::serial::STOP_BITS_TWO: | 133 case core_api::serial::STOP_BITS_TWO: |
| 132 return device::serial::STOP_BITS_TWO; | 134 return device::serial::STOP_BITS_TWO; |
| 133 } | 135 } |
| 134 return device::serial::STOP_BITS_NONE; | 136 return device::serial::STOP_BITS_NONE; |
| 135 } | 137 } |
| 136 | 138 |
| 137 class SendBuffer : public device::ReadOnlyBuffer { | 139 class SendBuffer : public device::ReadOnlyBuffer { |
| 138 public: | 140 public: |
| 139 SendBuffer( | 141 SendBuffer( |
| 140 const std::string& data, | 142 const std::vector<char>& data, |
| 141 const base::Callback<void(int, device::serial::SendError)>& callback) | 143 const base::Callback<void(int, device::serial::SendError)>& callback) |
| 142 : data_(data), callback_(callback) {} | 144 : data_(data), callback_(callback) {} |
| 143 ~SendBuffer() override {} | 145 ~SendBuffer() override {} |
| 144 const char* GetData() override { return data_.c_str(); } | 146 const char* GetData() override { return vector_as_array(&data_); } |
| 145 uint32_t GetSize() override { return static_cast<uint32_t>(data_.size()); } | 147 uint32_t GetSize() override { return static_cast<uint32_t>(data_.size()); } |
| 146 void Done(uint32_t bytes_read) override { | 148 void Done(uint32_t bytes_read) override { |
| 147 callback_.Run(bytes_read, device::serial::SEND_ERROR_NONE); | 149 callback_.Run(bytes_read, device::serial::SEND_ERROR_NONE); |
| 148 } | 150 } |
| 149 void DoneWithError(uint32_t bytes_read, int32_t error) override { | 151 void DoneWithError(uint32_t bytes_read, int32_t error) override { |
| 150 callback_.Run(bytes_read, static_cast<device::serial::SendError>(error)); | 152 callback_.Run(bytes_read, static_cast<device::serial::SendError>(error)); |
| 151 } | 153 } |
| 152 | 154 |
| 153 private: | 155 private: |
| 154 const std::string data_; | 156 const std::vector<char> data_; |
| 155 const base::Callback<void(int, device::serial::SendError)> callback_; | 157 const base::Callback<void(int, device::serial::SendError)> callback_; |
| 156 }; | 158 }; |
| 157 | 159 |
| 158 class ReceiveBuffer : public device::WritableBuffer { | 160 class ReceiveBuffer : public device::WritableBuffer { |
| 159 public: | 161 public: |
| 160 ReceiveBuffer( | 162 ReceiveBuffer( |
| 161 scoped_refptr<net::IOBuffer> buffer, | 163 scoped_refptr<net::IOBuffer> buffer, |
| 162 uint32_t size, | 164 uint32_t size, |
| 163 const base::Callback<void(int, device::serial::ReceiveError)>& callback) | 165 const base::Callback<void(int, device::serial::ReceiveError)>& callback) |
| 164 : buffer_(buffer), size_(size), callback_(callback) {} | 166 : buffer_(buffer), size_(size), callback_(callback) {} |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 254 base::Bind(&SerialConnection::OnAsyncReadComplete, AsWeakPtr())))); | 256 base::Bind(&SerialConnection::OnAsyncReadComplete, AsWeakPtr())))); |
| 255 receive_timeout_task_.reset(); | 257 receive_timeout_task_.reset(); |
| 256 if (receive_timeout_ > 0) { | 258 if (receive_timeout_ > 0) { |
| 257 receive_timeout_task_.reset(new TimeoutTask( | 259 receive_timeout_task_.reset(new TimeoutTask( |
| 258 base::Bind(&SerialConnection::OnReceiveTimeout, AsWeakPtr()), | 260 base::Bind(&SerialConnection::OnReceiveTimeout, AsWeakPtr()), |
| 259 base::TimeDelta::FromMilliseconds(receive_timeout_))); | 261 base::TimeDelta::FromMilliseconds(receive_timeout_))); |
| 260 } | 262 } |
| 261 return true; | 263 return true; |
| 262 } | 264 } |
| 263 | 265 |
| 264 bool SerialConnection::Send(const std::string& data, | 266 bool SerialConnection::Send(const std::vector<char>& data, |
| 265 const SendCompleteCallback& callback) { | 267 const SendCompleteCallback& callback) { |
| 266 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 268 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 267 if (!send_complete_.is_null()) | 269 if (!send_complete_.is_null()) |
| 268 return false; | 270 return false; |
| 269 send_complete_ = callback; | 271 send_complete_ = callback; |
| 270 io_handler_->Write(scoped_ptr<device::ReadOnlyBuffer>(new SendBuffer( | 272 io_handler_->Write(scoped_ptr<device::ReadOnlyBuffer>(new SendBuffer( |
| 271 data, base::Bind(&SerialConnection::OnAsyncWriteComplete, AsWeakPtr())))); | 273 data, base::Bind(&SerialConnection::OnAsyncWriteComplete, AsWeakPtr())))); |
| 272 send_timeout_task_.reset(); | 274 send_timeout_task_.reset(); |
| 273 if (send_timeout_ > 0) { | 275 if (send_timeout_ > 0) { |
| 274 send_timeout_task_.reset(new TimeoutTask( | 276 send_timeout_task_.reset(new TimeoutTask( |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 356 io_handler_->CancelWrite(device::serial::SEND_ERROR_TIMEOUT); | 358 io_handler_->CancelWrite(device::serial::SEND_ERROR_TIMEOUT); |
| 357 } | 359 } |
| 358 | 360 |
| 359 void SerialConnection::OnAsyncReadComplete(int bytes_read, | 361 void SerialConnection::OnAsyncReadComplete(int bytes_read, |
| 360 device::serial::ReceiveError error) { | 362 device::serial::ReceiveError error) { |
| 361 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 363 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 362 DCHECK(!receive_complete_.is_null()); | 364 DCHECK(!receive_complete_.is_null()); |
| 363 ReceiveCompleteCallback callback = receive_complete_; | 365 ReceiveCompleteCallback callback = receive_complete_; |
| 364 receive_complete_.Reset(); | 366 receive_complete_.Reset(); |
| 365 receive_timeout_task_.reset(); | 367 receive_timeout_task_.reset(); |
| 366 callback.Run(std::string(receive_buffer_->data(), bytes_read), | 368 callback.Run(std::vector<char>(receive_buffer_->data(), |
| 369 receive_buffer_->data() + bytes_read), |
| 367 ConvertReceiveErrorFromMojo(error)); | 370 ConvertReceiveErrorFromMojo(error)); |
| 368 receive_buffer_ = NULL; | 371 receive_buffer_ = NULL; |
| 369 } | 372 } |
| 370 | 373 |
| 371 void SerialConnection::OnAsyncWriteComplete(int bytes_sent, | 374 void SerialConnection::OnAsyncWriteComplete(int bytes_sent, |
| 372 device::serial::SendError error) { | 375 device::serial::SendError error) { |
| 373 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 376 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 374 DCHECK(!send_complete_.is_null()); | 377 DCHECK(!send_complete_.is_null()); |
| 375 SendCompleteCallback callback = send_complete_; | 378 SendCompleteCallback callback = send_complete_; |
| 376 send_complete_.Reset(); | 379 send_complete_.Reset(); |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 429 output->parity_bit = extensions::ConvertParityBitToMojo(input.parity_bit); | 432 output->parity_bit = extensions::ConvertParityBitToMojo(input.parity_bit); |
| 430 output->stop_bits = extensions::ConvertStopBitsToMojo(input.stop_bits); | 433 output->stop_bits = extensions::ConvertStopBitsToMojo(input.stop_bits); |
| 431 if (input.cts_flow_control.get()) { | 434 if (input.cts_flow_control.get()) { |
| 432 output->has_cts_flow_control = true; | 435 output->has_cts_flow_control = true; |
| 433 output->cts_flow_control = *input.cts_flow_control; | 436 output->cts_flow_control = *input.cts_flow_control; |
| 434 } | 437 } |
| 435 return output.Pass(); | 438 return output.Pass(); |
| 436 } | 439 } |
| 437 | 440 |
| 438 } // namespace mojo | 441 } // namespace mojo |
| OLD | NEW |