Add ICU message format support
[chromium-blink-merge.git] / extensions / browser / api / serial / serial_connection.h
blobe03d66ee0ef986bace1fc1f187899db508491396
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 #ifndef EXTENSIONS_BROWSER_API_SERIAL_SERIAL_CONNECTION_H_
6 #define EXTENSIONS_BROWSER_API_SERIAL_SERIAL_CONNECTION_H_
8 #include <string>
9 #include <vector>
11 #include "base/callback.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/time/time.h"
14 #include "content/public/browser/browser_thread.h"
15 #include "device/serial/serial_io_handler.h"
16 #include "extensions/browser/api/api_resource.h"
17 #include "extensions/browser/api/api_resource_manager.h"
18 #include "extensions/common/api/serial.h"
19 #include "net/base/io_buffer.h"
21 using content::BrowserThread;
23 namespace extensions {
25 // Encapsulates an open serial port.
26 // NOTE: Instances of this object should only be constructed on the IO thread,
27 // and all methods should only be called on the IO thread unless otherwise
28 // noted.
29 class SerialConnection : public ApiResource,
30 public base::SupportsWeakPtr<SerialConnection> {
31 public:
32 typedef device::SerialIoHandler::OpenCompleteCallback OpenCompleteCallback;
34 // This is the callback type expected by Receive. Note that an error result
35 // does not necessarily imply an empty |data| string, since a receive may
36 // complete partially before being interrupted by an error condition.
37 typedef base::Callback<void(const std::vector<char>& data,
38 api::serial::ReceiveError error)>
39 ReceiveCompleteCallback;
41 // This is the callback type expected by Send. Note that an error result
42 // does not necessarily imply 0 bytes sent, since a send may complete
43 // partially before being interrupted by an error condition.
44 typedef base::Callback<void(int bytes_sent, api::serial::SendError error)>
45 SendCompleteCallback;
47 SerialConnection(const std::string& port,
48 const std::string& owner_extension_id);
49 ~SerialConnection() override;
51 // ApiResource override.
52 bool IsPersistent() const override;
54 void set_persistent(bool persistent) { persistent_ = persistent; }
55 bool persistent() const { return persistent_; }
57 void set_name(const std::string& name) { name_ = name; }
58 const std::string& name() const { return name_; }
60 void set_buffer_size(int buffer_size);
61 int buffer_size() const { return buffer_size_; }
63 void set_receive_timeout(int receive_timeout);
64 int receive_timeout() const { return receive_timeout_; }
66 void set_send_timeout(int send_timeout);
67 int send_timeout() const { return send_timeout_; }
69 void set_paused(bool paused);
70 bool paused() const { return paused_; }
72 // Initiates an asynchronous Open of the device. It is the caller's
73 // responsibility to ensure that this SerialConnection stays alive
74 // until |callback| is run.
75 void Open(const api::serial::ConnectionOptions& options,
76 const OpenCompleteCallback& callback);
78 // Begins an asynchronous receive operation. Calling this while a Receive
79 // is already pending is a no-op and returns |false| without calling
80 // |callback|.
81 bool Receive(const ReceiveCompleteCallback& callback);
83 // Begins an asynchronous send operation. Calling this while a Send
84 // is already pending is a no-op and returns |false| without calling
85 // |callback|.
86 bool Send(const std::vector<char>& data,
87 const SendCompleteCallback& callback);
89 // Flushes input and output buffers.
90 bool Flush() const;
92 // Configures some subset of port options for this connection.
93 // Omitted options are unchanged. Returns |true| iff the configuration
94 // changes were successful.
95 bool Configure(const api::serial::ConnectionOptions& options);
97 // Connection configuration query. Fills values in an existing
98 // ConnectionInfo. Returns |true| iff the connection's information
99 // was successfully retrieved.
100 bool GetInfo(api::serial::ConnectionInfo* info) const;
102 // Reads current control signals (DCD, CTS, etc.) into an existing
103 // DeviceControlSignals structure. Returns |true| iff the signals were
104 // successfully read.
105 bool GetControlSignals(
106 api::serial::DeviceControlSignals* control_signals) const;
108 // Sets one or more control signals (DTR and/or RTS). Returns |true| iff
109 // the signals were successfully set. Unininitialized flags in the
110 // HostControlSignals structure are left unchanged.
111 bool SetControlSignals(
112 const api::serial::HostControlSignals& control_signals);
114 // Suspend character transmission. Known as setting/sending 'Break' signal.
115 bool SetBreak();
117 // Restore character transmission. Known as clear/stop sending 'Break' signal.
118 bool ClearBreak();
120 // Overrides |io_handler_| for testing.
121 void SetIoHandlerForTest(scoped_refptr<device::SerialIoHandler> handler);
123 static const BrowserThread::ID kThreadId = BrowserThread::IO;
125 private:
126 friend class ApiResourceManager<SerialConnection>;
127 static const char* service_name() { return "SerialConnectionManager"; }
129 // Encapsulates a cancelable, delayed timeout task. Posts a delayed
130 // task upon construction and implicitly cancels the task upon
131 // destruction if it hasn't run yet.
132 class TimeoutTask {
133 public:
134 TimeoutTask(const base::Closure& closure, const base::TimeDelta& delay);
135 ~TimeoutTask();
137 private:
138 void Run() const;
140 base::Closure closure_;
141 base::TimeDelta delay_;
142 base::WeakPtrFactory<TimeoutTask> weak_factory_;
145 // Handles a receive timeout.
146 void OnReceiveTimeout();
148 // Handles a send timeout.
149 void OnSendTimeout();
151 // Receives read completion notification from the |io_handler_|.
152 void OnAsyncReadComplete(int bytes_read, device::serial::ReceiveError error);
154 // Receives write completion notification from the |io_handler_|.
155 void OnAsyncWriteComplete(int bytes_sent, device::serial::SendError error);
157 // The pathname of the serial device.
158 std::string port_;
160 // Flag indicating whether or not the connection should persist when
161 // its host app is suspended.
162 bool persistent_;
164 // User-specified connection name.
165 std::string name_;
167 // Size of the receive buffer.
168 int buffer_size_;
170 // Amount of time (in ms) to wait for a Read to succeed before triggering a
171 // timeout response via onReceiveError.
172 int receive_timeout_;
174 // Amount of time (in ms) to wait for a Write to succeed before triggering
175 // a timeout response.
176 int send_timeout_;
178 // Flag indicating that the connection is paused. A paused connection will not
179 // raise new onReceive events.
180 bool paused_;
182 // Callback to handle the completion of a pending Receive() request.
183 ReceiveCompleteCallback receive_complete_;
185 // Callback to handle the completion of a pending Send() request.
186 SendCompleteCallback send_complete_;
188 // Closure which will trigger a receive timeout unless cancelled. Reset on
189 // initialization and after every successful Receive().
190 scoped_ptr<TimeoutTask> receive_timeout_task_;
192 // Write timeout closure. Reset on initialization and after every successful
193 // Send().
194 scoped_ptr<TimeoutTask> send_timeout_task_;
196 scoped_refptr<net::IOBuffer> receive_buffer_;
198 // Asynchronous I/O handler.
199 scoped_refptr<device::SerialIoHandler> io_handler_;
202 } // namespace extensions
204 namespace mojo {
206 template <>
207 struct TypeConverter<device::serial::HostControlSignalsPtr,
208 extensions::api::serial::HostControlSignals> {
209 static device::serial::HostControlSignalsPtr Convert(
210 const extensions::api::serial::HostControlSignals& input);
213 template <>
214 struct TypeConverter<device::serial::ConnectionOptionsPtr,
215 extensions::api::serial::ConnectionOptions> {
216 static device::serial::ConnectionOptionsPtr Convert(
217 const extensions::api::serial::ConnectionOptions& input);
220 } // namespace mojo
222 #endif // EXTENSIONS_BROWSER_API_SERIAL_SERIAL_CONNECTION_H_