- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / api / serial / serial_api.h
1 // Copyright (c) 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_API_H_
6 #define CHROME_BROWSER_EXTENSIONS_API_SERIAL_SERIAL_API_H_
7
8 #include <string>
9
10 #include "base/memory/ref_counted.h"
11 #include "chrome/browser/extensions/api/api_function.h"
12 #include "chrome/browser/extensions/api/api_resource_manager.h"
13 #include "chrome/common/extensions/api/serial.h"
14 #include "net/base/io_buffer.h"
15
16 namespace serial = extensions::api::serial;
17
18 namespace extensions {
19
20 class SerialConnection;
21
22 extern const char kConnectionIdKey[];
23
24 class SerialAsyncApiFunction : public AsyncApiFunction {
25  public:
26   SerialAsyncApiFunction();
27
28  protected:
29   virtual ~SerialAsyncApiFunction();
30
31   // AsyncApiFunction:
32   virtual bool PrePrepare() OVERRIDE;
33
34   SerialConnection* GetSerialConnection(int api_resource_id);
35   void RemoveSerialConnection(int api_resource_id);
36
37   ApiResourceManager<SerialConnection>* manager_;
38 };
39
40 class SerialGetPortsFunction : public SerialAsyncApiFunction {
41  public:
42   DECLARE_EXTENSION_FUNCTION("serial.getPorts", SERIAL_GETPORTS)
43
44   SerialGetPortsFunction();
45
46  protected:
47   virtual ~SerialGetPortsFunction() {}
48
49   // AsyncApiFunction:
50   virtual bool Prepare() OVERRIDE;
51   virtual void Work() OVERRIDE;
52   virtual bool Respond() OVERRIDE;
53 };
54
55 class SerialOpenFunction : public SerialAsyncApiFunction {
56  public:
57   DECLARE_EXTENSION_FUNCTION("serial.open", SERIAL_OPEN)
58
59   SerialOpenFunction();
60
61  protected:
62   virtual ~SerialOpenFunction();
63
64   // AsyncApiFunction:
65   virtual bool Prepare() OVERRIDE;
66   virtual void AsyncWorkStart() OVERRIDE;
67   virtual void Work() OVERRIDE;
68   virtual bool Respond() OVERRIDE;
69
70   // Overrideable for testing.
71   virtual SerialConnection* CreateSerialConnection(
72       const std::string& port,
73       int bitrate,
74       serial::DataBit databit,
75       serial::ParityBit parity,
76       serial::StopBit stopbit,
77       const std::string& owner_extension_id);
78
79   virtual bool DoesPortExist(const std::string& port);
80
81  private:
82   scoped_ptr<api::serial::Open::Params> params_;
83   int bitrate_;
84   api::serial::DataBit databit_;
85   api::serial::ParityBit parity_;
86   api::serial::StopBit stopbit_;
87 };
88
89 class SerialCloseFunction : public SerialAsyncApiFunction {
90  public:
91   DECLARE_EXTENSION_FUNCTION("serial.close", SERIAL_CLOSE)
92
93   SerialCloseFunction();
94
95  protected:
96   virtual ~SerialCloseFunction();
97
98   // AsyncApiFunction:
99   virtual bool Prepare() OVERRIDE;
100   virtual void Work() OVERRIDE;
101   virtual bool Respond() OVERRIDE;
102
103  private:
104   scoped_ptr<api::serial::Close::Params> params_;
105 };
106
107 class SerialReadFunction : public SerialAsyncApiFunction {
108  public:
109   DECLARE_EXTENSION_FUNCTION("serial.read", SERIAL_READ)
110
111   SerialReadFunction();
112
113  protected:
114   virtual ~SerialReadFunction();
115
116   // AsyncApiFunction:
117   virtual bool Prepare() OVERRIDE;
118   virtual void Work() OVERRIDE;
119   virtual bool Respond() OVERRIDE;
120
121  private:
122   scoped_ptr<api::serial::Read::Params> params_;
123 };
124
125 class SerialWriteFunction : public SerialAsyncApiFunction {
126  public:
127   DECLARE_EXTENSION_FUNCTION("serial.write", SERIAL_WRITE)
128
129   SerialWriteFunction();
130
131  protected:
132   virtual ~SerialWriteFunction();
133
134   // AsyncApiFunction:
135   virtual bool Prepare() OVERRIDE;
136   virtual void Work() OVERRIDE;
137   virtual bool Respond() OVERRIDE;
138
139  private:
140   scoped_ptr<api::serial::Write::Params> params_;
141   scoped_refptr<net::IOBuffer> io_buffer_;
142   size_t io_buffer_size_;
143 };
144
145 class SerialFlushFunction : public SerialAsyncApiFunction {
146  public:
147   DECLARE_EXTENSION_FUNCTION("serial.flush", SERIAL_FLUSH)
148
149   SerialFlushFunction();
150
151  protected:
152   virtual ~SerialFlushFunction();
153
154   // AsyncApiFunction:
155   virtual bool Prepare() OVERRIDE;
156   virtual void Work() OVERRIDE;
157   virtual bool Respond() OVERRIDE;
158
159  private:
160   scoped_ptr<api::serial::Flush::Params> params_;
161 };
162
163 class SerialGetControlSignalsFunction : public SerialAsyncApiFunction {
164  public:
165   DECLARE_EXTENSION_FUNCTION("serial.getControlSignals",
166                              SERIAL_GETCONTROLSIGNALS)
167
168   SerialGetControlSignalsFunction();
169
170  protected:
171   virtual ~SerialGetControlSignalsFunction();
172
173   // AsyncApiFunction:
174   virtual bool Prepare() OVERRIDE;
175   virtual void Work() OVERRIDE;
176   virtual bool Respond() OVERRIDE;
177
178  private:
179   scoped_ptr<api::serial::GetControlSignals::Params> params_;
180   bool api_response_;
181 };
182
183 class SerialSetControlSignalsFunction : public SerialAsyncApiFunction {
184  public:
185   DECLARE_EXTENSION_FUNCTION("serial.setControlSignals",
186                              SERIAL_SETCONTROLSIGNALS)
187
188   SerialSetControlSignalsFunction();
189
190  protected:
191   virtual ~SerialSetControlSignalsFunction();
192
193   // AsyncApiFunction:
194   virtual bool Prepare() OVERRIDE;
195   virtual void Work() OVERRIDE;
196   virtual bool Respond() OVERRIDE;
197
198  private:
199   scoped_ptr<api::serial::SetControlSignals::Params> params_;
200 };
201
202 }  // namespace extensions
203
204 #endif  // CHROME_BROWSER_EXTENSIONS_API_SERIAL_SERIAL_API_H_