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