Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / device / serial / serial_io_handler.cc
blob452f91c198ced135a454f0b44734699e31780508
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
3 // found in the LICENSE file.
5 #include "device/serial/serial_io_handler.h"
7 #include "base/bind.h"
8 #include "base/files/file_path.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/strings/string_util.h"
12 #if defined(OS_CHROMEOS)
13 #include "chromeos/dbus/dbus_thread_manager.h"
14 #include "chromeos/dbus/permission_broker_client.h"
15 #include "dbus/file_descriptor.h"
16 #endif // defined(OS_CHROMEOS)
18 namespace device {
20 SerialIoHandler::SerialIoHandler(
21 scoped_refptr<base::SingleThreadTaskRunner> file_thread_task_runner,
22 scoped_refptr<base::SingleThreadTaskRunner> ui_thread_task_runner)
23 : file_thread_task_runner_(file_thread_task_runner),
24 ui_thread_task_runner_(ui_thread_task_runner) {
25 options_.bitrate = 9600;
26 options_.data_bits = serial::DATA_BITS_EIGHT;
27 options_.parity_bit = serial::PARITY_BIT_NO;
28 options_.stop_bits = serial::STOP_BITS_ONE;
29 options_.cts_flow_control = false;
30 options_.has_cts_flow_control = true;
33 SerialIoHandler::~SerialIoHandler() {
34 DCHECK(CalledOnValidThread());
35 Close();
38 void SerialIoHandler::Open(const std::string& port,
39 const serial::ConnectionOptions& options,
40 const OpenCompleteCallback& callback) {
41 DCHECK(CalledOnValidThread());
42 DCHECK(open_complete_.is_null());
43 open_complete_ = callback;
44 DCHECK(file_thread_task_runner_.get());
45 DCHECK(ui_thread_task_runner_.get());
46 MergeConnectionOptions(options);
48 #if defined(OS_CHROMEOS)
49 chromeos::PermissionBrokerClient* client =
50 chromeos::DBusThreadManager::Get()->GetPermissionBrokerClient();
51 DCHECK(client) << "Could not get permission_broker client.";
52 // PermissionBrokerClient should be called on the UI thread.
53 ui_thread_task_runner_->PostTask(
54 FROM_HERE, base::Bind(&chromeos::PermissionBrokerClient::OpenPath,
55 base::Unretained(client), port,
56 base::Bind(&SerialIoHandler::OnPathOpened, this,
57 file_thread_task_runner_,
58 base::ThreadTaskRunnerHandle::Get())));
59 #else
60 file_thread_task_runner_->PostTask(
61 FROM_HERE, base::Bind(&SerialIoHandler::StartOpen, this, port,
62 base::ThreadTaskRunnerHandle::Get()));
63 #endif // defined(OS_CHROMEOS)
66 #if defined(OS_CHROMEOS)
68 void SerialIoHandler::OnPathOpened(
69 scoped_refptr<base::SingleThreadTaskRunner> file_thread_task_runner,
70 scoped_refptr<base::SingleThreadTaskRunner> io_thread_task_runner,
71 dbus::FileDescriptor fd) {
72 DCHECK(CalledOnValidThread());
73 file_thread_task_runner->PostTask(
74 FROM_HERE, base::Bind(&SerialIoHandler::ValidateOpenPort, this,
75 io_thread_task_runner, base::Passed(&fd)));
78 void SerialIoHandler::ValidateOpenPort(
79 scoped_refptr<base::SingleThreadTaskRunner> io_thread_task_runner,
80 dbus::FileDescriptor fd) {
81 base::File file;
82 fd.CheckValidity();
83 if (fd.is_valid()) {
84 file = base::File(fd.TakeValue());
87 io_thread_task_runner->PostTask(
88 FROM_HERE,
89 base::Bind(&SerialIoHandler::FinishOpen, this, base::Passed(&file)));
92 #endif
94 void SerialIoHandler::MergeConnectionOptions(
95 const serial::ConnectionOptions& options) {
96 if (options.bitrate) {
97 options_.bitrate = options.bitrate;
99 if (options.data_bits != serial::DATA_BITS_NONE) {
100 options_.data_bits = options.data_bits;
102 if (options.parity_bit != serial::PARITY_BIT_NONE) {
103 options_.parity_bit = options.parity_bit;
105 if (options.stop_bits != serial::STOP_BITS_NONE) {
106 options_.stop_bits = options.stop_bits;
108 if (options.has_cts_flow_control) {
109 DCHECK(options_.has_cts_flow_control);
110 options_.cts_flow_control = options.cts_flow_control;
114 void SerialIoHandler::StartOpen(
115 const std::string& port,
116 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner) {
117 DCHECK(!open_complete_.is_null());
118 DCHECK(file_thread_task_runner_->RunsTasksOnCurrentThread());
119 DCHECK(!file_.IsValid());
120 // It's the responsibility of the API wrapper around SerialIoHandler to
121 // validate the supplied path against the set of valid port names, and
122 // it is a reasonable assumption that serial port names are ASCII.
123 DCHECK(base::IsStringASCII(port));
124 base::FilePath path(base::FilePath::FromUTF8Unsafe(MaybeFixUpPortName(port)));
125 int flags = base::File::FLAG_OPEN | base::File::FLAG_READ |
126 base::File::FLAG_EXCLUSIVE_READ | base::File::FLAG_WRITE |
127 base::File::FLAG_EXCLUSIVE_WRITE | base::File::FLAG_ASYNC |
128 base::File::FLAG_TERMINAL_DEVICE;
129 base::File file(path, flags);
130 io_task_runner->PostTask(FROM_HERE, base::Bind(&SerialIoHandler::FinishOpen,
131 this, base::Passed(&file)));
134 void SerialIoHandler::FinishOpen(base::File file) {
135 DCHECK(CalledOnValidThread());
136 DCHECK(!open_complete_.is_null());
137 OpenCompleteCallback callback = open_complete_;
138 open_complete_.Reset();
140 if (!file.IsValid()) {
141 LOG(ERROR) << "Failed to open serial port: "
142 << base::File::ErrorToString(file.error_details());
143 callback.Run(false);
144 return;
147 file_ = file.Pass();
149 bool success = PostOpen() && ConfigurePortImpl();
150 if (!success) {
151 Close();
154 callback.Run(success);
157 bool SerialIoHandler::PostOpen() {
158 return true;
161 void SerialIoHandler::Close() {
162 if (file_.IsValid()) {
163 DCHECK(file_thread_task_runner_.get());
164 file_thread_task_runner_->PostTask(
165 FROM_HERE, base::Bind(&SerialIoHandler::DoClose, Passed(file_.Pass())));
169 // static
170 void SerialIoHandler::DoClose(base::File port) {
171 // port closed by destructor.
174 void SerialIoHandler::Read(scoped_ptr<WritableBuffer> buffer) {
175 DCHECK(CalledOnValidThread());
176 DCHECK(!IsReadPending());
177 pending_read_buffer_ = buffer.Pass();
178 read_canceled_ = false;
179 AddRef();
180 ReadImpl();
183 void SerialIoHandler::Write(scoped_ptr<ReadOnlyBuffer> buffer) {
184 DCHECK(CalledOnValidThread());
185 DCHECK(!IsWritePending());
186 pending_write_buffer_ = buffer.Pass();
187 write_canceled_ = false;
188 AddRef();
189 WriteImpl();
192 void SerialIoHandler::ReadCompleted(int bytes_read,
193 serial::ReceiveError error) {
194 DCHECK(CalledOnValidThread());
195 DCHECK(IsReadPending());
196 scoped_ptr<WritableBuffer> pending_read_buffer = pending_read_buffer_.Pass();
197 if (error == serial::RECEIVE_ERROR_NONE) {
198 pending_read_buffer->Done(bytes_read);
199 } else {
200 pending_read_buffer->DoneWithError(bytes_read, error);
202 Release();
205 void SerialIoHandler::WriteCompleted(int bytes_written,
206 serial::SendError error) {
207 DCHECK(CalledOnValidThread());
208 DCHECK(IsWritePending());
209 scoped_ptr<ReadOnlyBuffer> pending_write_buffer =
210 pending_write_buffer_.Pass();
211 if (error == serial::SEND_ERROR_NONE) {
212 pending_write_buffer->Done(bytes_written);
213 } else {
214 pending_write_buffer->DoneWithError(bytes_written, error);
216 Release();
219 bool SerialIoHandler::IsReadPending() const {
220 DCHECK(CalledOnValidThread());
221 return pending_read_buffer_ != NULL;
224 bool SerialIoHandler::IsWritePending() const {
225 DCHECK(CalledOnValidThread());
226 return pending_write_buffer_ != NULL;
229 void SerialIoHandler::CancelRead(serial::ReceiveError reason) {
230 DCHECK(CalledOnValidThread());
231 if (IsReadPending() && !read_canceled_) {
232 read_canceled_ = true;
233 read_cancel_reason_ = reason;
234 CancelReadImpl();
238 void SerialIoHandler::CancelWrite(serial::SendError reason) {
239 DCHECK(CalledOnValidThread());
240 if (IsWritePending() && !write_canceled_) {
241 write_canceled_ = true;
242 write_cancel_reason_ = reason;
243 CancelWriteImpl();
247 bool SerialIoHandler::ConfigurePort(const serial::ConnectionOptions& options) {
248 MergeConnectionOptions(options);
249 return ConfigurePortImpl();
252 void SerialIoHandler::QueueReadCompleted(int bytes_read,
253 serial::ReceiveError error) {
254 base::MessageLoop::current()->PostTask(
255 FROM_HERE,
256 base::Bind(&SerialIoHandler::ReadCompleted, this, bytes_read, error));
259 void SerialIoHandler::QueueWriteCompleted(int bytes_written,
260 serial::SendError error) {
261 base::MessageLoop::current()->PostTask(
262 FROM_HERE,
263 base::Bind(&SerialIoHandler::WriteCompleted, this, bytes_written, error));
266 } // namespace device