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.
5 #ifndef CHROME_BROWSER_EXTENSIONS_API_SERIAL_SERIAL_API_H_
6 #define CHROME_BROWSER_EXTENSIONS_API_SERIAL_SERIAL_API_H_
10 #include "base/memory/ref_counted.h"
11 #include "chrome/common/extensions/api/serial.h"
12 #include "extensions/browser/api/api_resource_manager.h"
13 #include "extensions/browser/api/async_api_function.h"
15 namespace extensions
{
17 class SerialConnection
;
21 class SerialEventDispatcher
;
23 class SerialAsyncApiFunction
: public AsyncApiFunction
{
25 SerialAsyncApiFunction();
28 virtual ~SerialAsyncApiFunction();
31 virtual bool PrePrepare() OVERRIDE
;
32 virtual 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 virtual ~SerialGetDevicesFunction() {}
50 virtual bool Prepare() OVERRIDE
;
51 virtual void Work() OVERRIDE
;
54 class SerialConnectFunction
: public SerialAsyncApiFunction
{
56 DECLARE_EXTENSION_FUNCTION("serial.connect", SERIAL_CONNECT
)
58 SerialConnectFunction();
61 virtual ~SerialConnectFunction();
64 virtual bool Prepare() OVERRIDE
;
65 virtual 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 virtual ~SerialUpdateFunction();
97 virtual bool Prepare() OVERRIDE
;
98 virtual void Work() OVERRIDE
;
101 scoped_ptr
<serial::Update::Params
> params_
;
104 class SerialDisconnectFunction
: public SerialAsyncApiFunction
{
106 DECLARE_EXTENSION_FUNCTION("serial.disconnect", SERIAL_DISCONNECT
)
108 SerialDisconnectFunction();
111 virtual ~SerialDisconnectFunction();
114 virtual bool Prepare() OVERRIDE
;
115 virtual 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 virtual ~SerialSetPausedFunction();
131 virtual bool Prepare() OVERRIDE
;
132 virtual 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 virtual ~SerialGetInfoFunction();
149 virtual bool Prepare() OVERRIDE
;
150 virtual 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 virtual ~SerialGetConnectionsFunction();
166 virtual bool Prepare() OVERRIDE
;
167 virtual void Work() OVERRIDE
;
170 class SerialSendFunction
: public SerialAsyncApiFunction
{
172 DECLARE_EXTENSION_FUNCTION("serial.send", SERIAL_SEND
)
174 SerialSendFunction();
177 virtual ~SerialSendFunction();
180 virtual bool Prepare() OVERRIDE
;
181 virtual 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 virtual ~SerialFlushFunction();
199 virtual bool Prepare() OVERRIDE
;
200 virtual 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 virtual ~SerialGetControlSignalsFunction();
217 virtual bool Prepare() OVERRIDE
;
218 virtual 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 virtual ~SerialSetControlSignalsFunction();
235 virtual bool Prepare() OVERRIDE
;
236 virtual void Work() OVERRIDE
;
239 scoped_ptr
<serial::SetControlSignals::Params
> params_
;
244 } // namespace extensions
246 #endif // CHROME_BROWSER_EXTENSIONS_API_SERIAL_SERIAL_API_H_