| OLD | NEW |
| (Empty) |
| 1 // Copyright 2012 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. | |
| 4 | |
| 5 #ifndef CHROME_BROWSER_EXTENSIONS_API_SERIAL_SERIAL_CONNECTION_H_ | |
| 6 #define CHROME_BROWSER_EXTENSIONS_API_SERIAL_SERIAL_CONNECTION_H_ | |
| 7 | |
| 8 #include <set> | |
| 9 #include <string> | |
| 10 | |
| 11 #include "base/callback.h" | |
| 12 #include "base/memory/ref_counted.h" | |
| 13 #include "base/memory/weak_ptr.h" | |
| 14 #include "base/message_loop/message_loop.h" | |
| 15 #include "base/time/time.h" | |
| 16 #include "chrome/common/extensions/api/serial.h" | |
| 17 #include "content/public/browser/browser_thread.h" | |
| 18 #include "device/serial/serial_io_handler.h" | |
| 19 #include "extensions/browser/api/api_resource.h" | |
| 20 #include "extensions/browser/api/api_resource_manager.h" | |
| 21 | |
| 22 using content::BrowserThread; | |
| 23 | |
| 24 namespace extensions { | |
| 25 | |
| 26 // Encapsulates an open serial port. | |
| 27 // NOTE: Instances of this object should only be constructed on the IO thread, | |
| 28 // and all methods should only be called on the IO thread unless otherwise | |
| 29 // noted. | |
| 30 class SerialConnection : public ApiResource, | |
| 31 public base::SupportsWeakPtr<SerialConnection> { | |
| 32 public: | |
| 33 typedef device::SerialIoHandler::OpenCompleteCallback OpenCompleteCallback; | |
| 34 | |
| 35 // This is the callback type expected by Receive. Note that an error result | |
| 36 // does not necessarily imply an empty |data| string, since a receive may | |
| 37 // complete partially before being interrupted by an error condition. | |
| 38 typedef base::Callback< | |
| 39 void(const std::string& data, api::serial::ReceiveError error)> | |
| 40 ReceiveCompleteCallback; | |
| 41 | |
| 42 // This is the callback type expected by Send. Note that an error result | |
| 43 // does not necessarily imply 0 bytes sent, since a send may complete | |
| 44 // partially before being interrupted by an error condition. | |
| 45 typedef base::Callback<void(int bytes_sent, api::serial::SendError error)> | |
| 46 SendCompleteCallback; | |
| 47 | |
| 48 SerialConnection(const std::string& port, | |
| 49 const std::string& owner_extension_id); | |
| 50 virtual ~SerialConnection(); | |
| 51 | |
| 52 // ApiResource override. | |
| 53 virtual bool IsPersistent() const OVERRIDE; | |
| 54 | |
| 55 void set_persistent(bool persistent) { persistent_ = persistent; } | |
| 56 bool persistent() const { return persistent_; } | |
| 57 | |
| 58 void set_name(const std::string& name) { name_ = name; } | |
| 59 const std::string& name() const { return name_; } | |
| 60 | |
| 61 void set_buffer_size(int buffer_size); | |
| 62 int buffer_size() const { return buffer_size_; } | |
| 63 | |
| 64 void set_receive_timeout(int receive_timeout); | |
| 65 int receive_timeout() const { return receive_timeout_; } | |
| 66 | |
| 67 void set_send_timeout(int send_timeout); | |
| 68 int send_timeout() const { return send_timeout_; } | |
| 69 | |
| 70 void set_paused(bool paused); | |
| 71 bool paused() const { return paused_; } | |
| 72 | |
| 73 // Initiates an asynchronous Open of the device. It is the caller's | |
| 74 // responsibility to ensure that this SerialConnection stays alive | |
| 75 // until |callback| is run. | |
| 76 void Open(const OpenCompleteCallback& callback); | |
| 77 | |
| 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); | |
| 82 | |
| 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::string& data, const SendCompleteCallback& callback); | |
| 87 | |
| 88 // Flushes input and output buffers. | |
| 89 bool Flush() const; | |
| 90 | |
| 91 // Configures some subset of port options for this connection. | |
| 92 // Omitted options are unchanged. Returns |true| iff the configuration | |
| 93 // changes were successful. | |
| 94 bool Configure(const api::serial::ConnectionOptions& options); | |
| 95 | |
| 96 // Connection configuration query. Fills values in an existing | |
| 97 // ConnectionInfo. Returns |true| iff the connection's information | |
| 98 // was successfully retrieved. | |
| 99 bool GetInfo(api::serial::ConnectionInfo* info) const; | |
| 100 | |
| 101 // Reads current control signals (DCD, CTS, etc.) into an existing | |
| 102 // DeviceControlSignals structure. Returns |true| iff the signals were | |
| 103 // successfully read. | |
| 104 bool GetControlSignals( | |
| 105 api::serial::DeviceControlSignals* control_signals) const; | |
| 106 | |
| 107 // Sets one or more control signals (DTR and/or RTS). Returns |true| iff | |
| 108 // the signals were successfully set. Unininitialized flags in the | |
| 109 // HostControlSignals structure are left unchanged. | |
| 110 bool SetControlSignals( | |
| 111 const api::serial::HostControlSignals& control_signals); | |
| 112 | |
| 113 // Overrides |io_handler_| for testing. | |
| 114 void SetIoHandlerForTest(scoped_refptr<device::SerialIoHandler> handler); | |
| 115 | |
| 116 static const BrowserThread::ID kThreadId = BrowserThread::IO; | |
| 117 | |
| 118 private: | |
| 119 friend class ApiResourceManager<SerialConnection>; | |
| 120 static const char* service_name() { return "SerialConnectionManager"; } | |
| 121 | |
| 122 // Encapsulates a cancelable, delayed timeout task. Posts a delayed | |
| 123 // task upon construction and implicitly cancels the task upon | |
| 124 // destruction if it hasn't run yet. | |
| 125 class TimeoutTask { | |
| 126 public: | |
| 127 TimeoutTask(const base::Closure& closure, const base::TimeDelta& delay); | |
| 128 ~TimeoutTask(); | |
| 129 | |
| 130 private: | |
| 131 void Run() const; | |
| 132 | |
| 133 base::WeakPtrFactory<TimeoutTask> weak_factory_; | |
| 134 base::Closure closure_; | |
| 135 base::TimeDelta delay_; | |
| 136 }; | |
| 137 | |
| 138 // Handles a receive timeout. | |
| 139 void OnReceiveTimeout(); | |
| 140 | |
| 141 // Handles a send timeout. | |
| 142 void OnSendTimeout(); | |
| 143 | |
| 144 // Receives read completion notification from the |io_handler_|. | |
| 145 void OnAsyncReadComplete(const std::string& data, | |
| 146 device::serial::ReceiveError error); | |
| 147 | |
| 148 // Receives write completion notification from the |io_handler_|. | |
| 149 void OnAsyncWriteComplete(int bytes_sent, device::serial::SendError error); | |
| 150 | |
| 151 // The pathname of the serial device. | |
| 152 std::string port_; | |
| 153 | |
| 154 // Flag indicating whether or not the connection should persist when | |
| 155 // its host app is suspended. | |
| 156 bool persistent_; | |
| 157 | |
| 158 // User-specified connection name. | |
| 159 std::string name_; | |
| 160 | |
| 161 // Size of the receive buffer. | |
| 162 int buffer_size_; | |
| 163 | |
| 164 // Amount of time (in ms) to wait for a Read to succeed before triggering a | |
| 165 // timeout response via onReceiveError. | |
| 166 int receive_timeout_; | |
| 167 | |
| 168 // Amount of time (in ms) to wait for a Write to succeed before triggering | |
| 169 // a timeout response. | |
| 170 int send_timeout_; | |
| 171 | |
| 172 // Flag indicating that the connection is paused. A paused connection will not | |
| 173 // raise new onReceive events. | |
| 174 bool paused_; | |
| 175 | |
| 176 // Callback to handle the completion of a pending Receive() request. | |
| 177 ReceiveCompleteCallback receive_complete_; | |
| 178 | |
| 179 // Callback to handle the completion of a pending Send() request. | |
| 180 SendCompleteCallback send_complete_; | |
| 181 | |
| 182 // Closure which will trigger a receive timeout unless cancelled. Reset on | |
| 183 // initialization and after every successful Receive(). | |
| 184 scoped_ptr<TimeoutTask> receive_timeout_task_; | |
| 185 | |
| 186 // Write timeout closure. Reset on initialization and after every successful | |
| 187 // Send(). | |
| 188 scoped_ptr<TimeoutTask> send_timeout_task_; | |
| 189 | |
| 190 // Asynchronous I/O handler. | |
| 191 scoped_refptr<device::SerialIoHandler> io_handler_; | |
| 192 }; | |
| 193 | |
| 194 } // namespace extensions | |
| 195 | |
| 196 namespace mojo { | |
| 197 | |
| 198 template <> | |
| 199 class TypeConverter<device::serial::HostControlSignalsPtr, | |
| 200 extensions::api::serial::HostControlSignals> { | |
| 201 public: | |
| 202 static device::serial::HostControlSignalsPtr ConvertFrom( | |
| 203 const extensions::api::serial::HostControlSignals& input); | |
| 204 }; | |
| 205 | |
| 206 template <> | |
| 207 class TypeConverter<device::serial::ConnectionOptionsPtr, | |
| 208 extensions::api::serial::ConnectionOptions> { | |
| 209 public: | |
| 210 static device::serial::ConnectionOptionsPtr ConvertFrom( | |
| 211 const extensions::api::serial::ConnectionOptions& input); | |
| 212 }; | |
| 213 | |
| 214 } // namespace mojo | |
| 215 | |
| 216 #endif // CHROME_BROWSER_EXTENSIONS_API_SERIAL_SERIAL_CONNECTION_H_ | |
| OLD | NEW |