Vectorize website settings icons in omnibox
[chromium-blink-merge.git] / device / serial / serial_connection_factory.cc
blobf45b9a683a6b8c5de97541341893a247ae2db5d3
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_connection_factory.h"
7 #include "base/bind.h"
8 #include "base/location.h"
9 #include "device/serial/serial_connection.h"
10 #include "device/serial/serial_io_handler.h"
12 namespace device {
13 namespace {
15 void FillDefaultConnectionOptions(serial::ConnectionOptions* options) {
16 if (!options->bitrate)
17 options->bitrate = 9600;
18 if (options->data_bits == serial::DATA_BITS_NONE)
19 options->data_bits = serial::DATA_BITS_EIGHT;
20 if (options->stop_bits == serial::STOP_BITS_NONE)
21 options->stop_bits = serial::STOP_BITS_ONE;
22 if (options->parity_bit == serial::PARITY_BIT_NONE)
23 options->parity_bit = serial::PARITY_BIT_NO;
24 if (!options->has_cts_flow_control) {
25 options->has_cts_flow_control = true;
26 options->cts_flow_control = false;
30 } // namespace
32 class SerialConnectionFactory::ConnectTask
33 : public base::RefCountedThreadSafe<SerialConnectionFactory::ConnectTask> {
34 public:
35 ConnectTask(scoped_refptr<SerialConnectionFactory> factory,
36 const std::string& path,
37 serial::ConnectionOptionsPtr options,
38 mojo::InterfaceRequest<serial::Connection> connection_request,
39 mojo::InterfaceRequest<serial::DataSink> sink,
40 mojo::InterfaceRequest<serial::DataSource> source,
41 mojo::InterfacePtr<serial::DataSourceClient> source_client);
42 void Run();
44 private:
45 friend class base::RefCountedThreadSafe<SerialConnectionFactory::ConnectTask>;
46 virtual ~ConnectTask();
47 void Connect();
48 void OnConnected(bool success);
50 scoped_refptr<SerialConnectionFactory> factory_;
51 const std::string path_;
52 serial::ConnectionOptionsPtr options_;
53 mojo::InterfaceRequest<serial::Connection> connection_request_;
54 mojo::InterfaceRequest<serial::DataSink> sink_;
55 mojo::InterfaceRequest<serial::DataSource> source_;
56 mojo::InterfacePtr<serial::DataSourceClient> source_client_;
57 scoped_refptr<SerialIoHandler> io_handler_;
59 DISALLOW_COPY_AND_ASSIGN(ConnectTask);
62 SerialConnectionFactory::SerialConnectionFactory(
63 const IoHandlerFactory& io_handler_factory,
64 scoped_refptr<base::SingleThreadTaskRunner> connect_task_runner)
65 : io_handler_factory_(io_handler_factory),
66 connect_task_runner_(connect_task_runner) {
69 void SerialConnectionFactory::CreateConnection(
70 const std::string& path,
71 serial::ConnectionOptionsPtr options,
72 mojo::InterfaceRequest<serial::Connection> connection_request,
73 mojo::InterfaceRequest<serial::DataSink> sink,
74 mojo::InterfaceRequest<serial::DataSource> source,
75 mojo::InterfacePtr<serial::DataSourceClient> source_client) {
76 scoped_refptr<ConnectTask> task(
77 new ConnectTask(this, path, options.Pass(), connection_request.Pass(),
78 sink.Pass(), source.Pass(), source_client.Pass()));
79 task->Run();
82 SerialConnectionFactory::~SerialConnectionFactory() {
85 SerialConnectionFactory::ConnectTask::ConnectTask(
86 scoped_refptr<SerialConnectionFactory> factory,
87 const std::string& path,
88 serial::ConnectionOptionsPtr options,
89 mojo::InterfaceRequest<serial::Connection> connection_request,
90 mojo::InterfaceRequest<serial::DataSink> sink,
91 mojo::InterfaceRequest<serial::DataSource> source,
92 mojo::InterfacePtr<serial::DataSourceClient> source_client)
93 : factory_(factory),
94 path_(path),
95 options_(options.Pass()),
96 connection_request_(connection_request.Pass()),
97 sink_(sink.Pass()),
98 source_(source.Pass()),
99 source_client_(source_client.Pass()) {
100 if (!options_) {
101 options_ = serial::ConnectionOptions::New();
103 FillDefaultConnectionOptions(options_.get());
106 void SerialConnectionFactory::ConnectTask::Run() {
107 factory_->connect_task_runner_->PostTask(
108 FROM_HERE,
109 base::Bind(&SerialConnectionFactory::ConnectTask::Connect, this));
112 SerialConnectionFactory::ConnectTask::~ConnectTask() {
115 void SerialConnectionFactory::ConnectTask::Connect() {
116 io_handler_ = factory_->io_handler_factory_.Run();
117 io_handler_->Open(
118 path_, *options_,
119 base::Bind(&SerialConnectionFactory::ConnectTask::OnConnected, this));
122 void SerialConnectionFactory::ConnectTask::OnConnected(bool success) {
123 DCHECK(io_handler_.get());
124 if (!success) {
125 return;
128 new SerialConnection(io_handler_, sink_.Pass(), source_.Pass(),
129 source_client_.Pass(), connection_request_.Pass());
132 } // namespace device