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_API_H_
6 #define EXTENSIONS_BROWSER_API_SERIAL_SERIAL_API_H_
10 #include "device/serial/serial.mojom.h"
11 #include "extensions/browser/api/api_resource_manager.h"
12 #include "extensions/browser/api/async_api_function.h"
13 #include "extensions/common/api/serial.h"
15 namespace extensions {
17 class SerialConnection;
21 class SerialEventDispatcher;
23 class SerialAsyncApiFunction : public AsyncApiFunction {
25 SerialAsyncApiFunction();
28 ~SerialAsyncApiFunction() override;
31 bool PrePrepare() override;
32 bool Respond() override;
34 SerialConnection* GetSerialConnection(int api_resource_id);
35 void RemoveSerialConnection(int api_resource_id);
37 ApiResourceManager<SerialConnection>* manager_;
40 class SerialGetDevicesFunction : public SerialAsyncApiFunction {
42 DECLARE_EXTENSION_FUNCTION("serial.getDevices", SERIAL_GETDEVICES)
44 SerialGetDevicesFunction();
47 ~SerialGetDevicesFunction() override {}
50 bool Prepare() override;
54 class SerialConnectFunction : public SerialAsyncApiFunction {
56 DECLARE_EXTENSION_FUNCTION("serial.connect", SERIAL_CONNECT)
58 SerialConnectFunction();
61 ~SerialConnectFunction() override;
64 bool Prepare() override;
65 void AsyncWorkStart() override;
67 virtual SerialConnection* CreateSerialConnection(
68 const std::string& port,
69 const std::string& extension_id) const;
72 void OnConnected(bool success);
75 scoped_ptr<serial::Connect::Params> params_;
77 // SerialEventDispatcher is owned by a BrowserContext.
78 SerialEventDispatcher* serial_event_dispatcher_;
80 // This connection is created within SerialConnectFunction.
81 // From there it is either destroyed in OnConnected (upon failure)
82 // or its ownership is transferred to the
83 // ApiResourceManager<SerialConnection>.
84 SerialConnection* connection_;
87 class SerialUpdateFunction : public SerialAsyncApiFunction {
89 DECLARE_EXTENSION_FUNCTION("serial.update", SERIAL_UPDATE);
91 SerialUpdateFunction();
94 ~SerialUpdateFunction() override;
97 bool Prepare() override;
101 scoped_ptr<serial::Update::Params> params_;
104 class SerialDisconnectFunction : public SerialAsyncApiFunction {
106 DECLARE_EXTENSION_FUNCTION("serial.disconnect", SERIAL_DISCONNECT)
108 SerialDisconnectFunction();
111 ~SerialDisconnectFunction() override;
114 bool Prepare() override;
115 void Work() override;
118 scoped_ptr<serial::Disconnect::Params> params_;
121 class SerialSetPausedFunction : public SerialAsyncApiFunction {
123 DECLARE_EXTENSION_FUNCTION("serial.setPaused", SERIAL_SETPAUSED)
125 SerialSetPausedFunction();
128 ~SerialSetPausedFunction() override;
131 bool Prepare() override;
132 void Work() override;
135 scoped_ptr<serial::SetPaused::Params> params_;
136 SerialEventDispatcher* serial_event_dispatcher_;
139 class SerialGetInfoFunction : public SerialAsyncApiFunction {
141 DECLARE_EXTENSION_FUNCTION("serial.getInfo", SERIAL_GETINFO)
143 SerialGetInfoFunction();
146 ~SerialGetInfoFunction() override;
149 bool Prepare() override;
150 void Work() override;
153 scoped_ptr<serial::GetInfo::Params> params_;
156 class SerialGetConnectionsFunction : public SerialAsyncApiFunction {
158 DECLARE_EXTENSION_FUNCTION("serial.getConnections", SERIAL_GETCONNECTIONS);
160 SerialGetConnectionsFunction();
163 ~SerialGetConnectionsFunction() override;
166 bool Prepare() override;
167 void Work() override;
170 class SerialSendFunction : public SerialAsyncApiFunction {
172 DECLARE_EXTENSION_FUNCTION("serial.send", SERIAL_SEND)
174 SerialSendFunction();
177 ~SerialSendFunction() override;
180 bool Prepare() override;
181 void AsyncWorkStart() override;
184 void OnSendComplete(int bytes_sent, serial::SendError error);
186 scoped_ptr<serial::Send::Params> params_;
189 class SerialFlushFunction : public SerialAsyncApiFunction {
191 DECLARE_EXTENSION_FUNCTION("serial.flush", SERIAL_FLUSH)
193 SerialFlushFunction();
196 ~SerialFlushFunction() override;
199 bool Prepare() override;
200 void Work() override;
203 scoped_ptr<serial::Flush::Params> params_;
206 class SerialGetControlSignalsFunction : public SerialAsyncApiFunction {
208 DECLARE_EXTENSION_FUNCTION("serial.getControlSignals",
209 SERIAL_GETCONTROLSIGNALS)
211 SerialGetControlSignalsFunction();
214 ~SerialGetControlSignalsFunction() override;
217 bool Prepare() override;
218 void Work() override;
221 scoped_ptr<serial::GetControlSignals::Params> params_;
224 class SerialSetControlSignalsFunction : public SerialAsyncApiFunction {
226 DECLARE_EXTENSION_FUNCTION("serial.setControlSignals",
227 SERIAL_SETCONTROLSIGNALS)
229 SerialSetControlSignalsFunction();
232 ~SerialSetControlSignalsFunction() override;
235 bool Prepare() override;
236 void Work() override;
239 scoped_ptr<serial::SetControlSignals::Params> params_;
242 } // namespace core_api
244 } // namespace extensions
249 struct TypeConverter<linked_ptr<extensions::core_api::serial::DeviceInfo>,
250 device::serial::DeviceInfoPtr> {
251 static linked_ptr<extensions::core_api::serial::DeviceInfo> Convert(
252 const device::serial::DeviceInfoPtr& input);
257 #endif // EXTENSIONS_BROWSER_API_SERIAL_SERIAL_API_H_