[MemSheriff] Exclude crashing test under Dr. Memory
[chromium-blink-merge.git] / device / serial / serial_connection_factory.cc
blob7d7e04f93e76db534b98e244d0f4048d8e6c3c86
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 void Run();
41 private:
42 friend class base::RefCountedThreadSafe<SerialConnectionFactory::ConnectTask>;
43 virtual ~ConnectTask();
44 void Connect();
45 void OnConnected(bool success);
47 scoped_refptr<SerialConnectionFactory> factory_;
48 const std::string path_;
49 serial::ConnectionOptionsPtr options_;
50 mojo::InterfaceRequest<serial::Connection> connection_request_;
51 scoped_refptr<SerialIoHandler> io_handler_;
53 DISALLOW_COPY_AND_ASSIGN(ConnectTask);
56 SerialConnectionFactory::SerialConnectionFactory(
57 const IoHandlerFactory& io_handler_factory,
58 scoped_refptr<base::MessageLoopProxy> connect_message_loop)
59 : io_handler_factory_(io_handler_factory),
60 connect_message_loop_(connect_message_loop) {
63 void SerialConnectionFactory::CreateConnection(
64 const std::string& path,
65 serial::ConnectionOptionsPtr options,
66 mojo::InterfaceRequest<serial::Connection> connection_request) {
67 scoped_refptr<ConnectTask> task(
68 new ConnectTask(this, path, options.Pass(), connection_request.Pass()));
69 task->Run();
72 SerialConnectionFactory::~SerialConnectionFactory() {
75 SerialConnectionFactory::ConnectTask::ConnectTask(
76 scoped_refptr<SerialConnectionFactory> factory,
77 const std::string& path,
78 serial::ConnectionOptionsPtr options,
79 mojo::InterfaceRequest<serial::Connection> connection_request)
80 : factory_(factory),
81 path_(path),
82 options_(options.Pass()),
83 connection_request_(connection_request.Pass()) {
86 void SerialConnectionFactory::ConnectTask::Run() {
87 factory_->connect_message_loop_->PostTask(
88 FROM_HERE,
89 base::Bind(&SerialConnectionFactory::ConnectTask::Connect, this));
92 SerialConnectionFactory::ConnectTask::~ConnectTask() {
95 void SerialConnectionFactory::ConnectTask::Connect() {
96 io_handler_ = factory_->io_handler_factory_.Run();
97 io_handler_->Open(
98 path_,
99 base::Bind(&SerialConnectionFactory::ConnectTask::OnConnected, this));
102 void SerialConnectionFactory::ConnectTask::OnConnected(bool success) {
103 DCHECK(io_handler_);
104 if (!success)
105 return;
106 if (!options_)
107 options_ = serial::ConnectionOptions::New();
108 FillDefaultConnectionOptions(options_.get());
109 if (!io_handler_->ConfigurePort(*options_))
110 return;
111 mojo::BindToRequest(new SerialConnection(io_handler_), &connection_request_);
114 } // namespace device