Home | History | Annotate | Download | only in serial
      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 
     15 namespace extensions {
     16 
     17 class SerialConnection;
     18 
     19 namespace api {
     20 
     21 class SerialEventDispatcher;
     22 
     23 class SerialAsyncApiFunction : public AsyncApiFunction {
     24  public:
     25   SerialAsyncApiFunction();
     26 
     27  protected:
     28   virtual ~SerialAsyncApiFunction();
     29 
     30   // AsyncApiFunction:
     31   virtual bool PrePrepare() OVERRIDE;
     32   virtual bool Respond() 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 SerialGetDevicesFunction : public SerialAsyncApiFunction {
     41  public:
     42   DECLARE_EXTENSION_FUNCTION("serial.getDevices", SERIAL_GETDEVICES)
     43 
     44   SerialGetDevicesFunction();
     45 
     46  protected:
     47   virtual ~SerialGetDevicesFunction() {}
     48 
     49   // AsyncApiFunction:
     50   virtual bool Prepare() OVERRIDE;
     51   virtual void Work() OVERRIDE;
     52 };
     53 
     54 class SerialConnectFunction : public SerialAsyncApiFunction {
     55  public:
     56   DECLARE_EXTENSION_FUNCTION("serial.connect", SERIAL_CONNECT)
     57 
     58   SerialConnectFunction();
     59 
     60  protected:
     61   virtual ~SerialConnectFunction();
     62 
     63   // AsyncApiFunction:
     64   virtual bool Prepare() OVERRIDE;
     65   virtual void AsyncWorkStart() OVERRIDE;
     66 
     67   virtual SerialConnection* CreateSerialConnection(
     68       const std::string& port,
     69       const std::string& extension_id) const;
     70 
     71  private:
     72   void OnConnected(bool success);
     73   void FinishConnect();
     74 
     75   scoped_ptr<serial::Connect::Params> params_;
     76 
     77   // SerialEventDispatcher is owned by a Profile.
     78   SerialEventDispatcher* serial_event_dispatcher_;
     79 
     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 profile's.
     83   // ApiResourceManager<SerialConnection>.
     84   SerialConnection* connection_;
     85 };
     86 
     87 class SerialUpdateFunction : public SerialAsyncApiFunction {
     88  public:
     89   DECLARE_EXTENSION_FUNCTION("serial.update", SERIAL_UPDATE);
     90 
     91   SerialUpdateFunction();
     92 
     93  protected:
     94   virtual ~SerialUpdateFunction();
     95 
     96   // AsyncApiFunction:
     97   virtual bool Prepare() OVERRIDE;
     98   virtual void Work() OVERRIDE;
     99 
    100  private:
    101   scoped_ptr<serial::Update::Params> params_;
    102 };
    103 
    104 class SerialDisconnectFunction : public SerialAsyncApiFunction {
    105  public:
    106   DECLARE_EXTENSION_FUNCTION("serial.disconnect", SERIAL_DISCONNECT)
    107 
    108   SerialDisconnectFunction();
    109 
    110  protected:
    111   virtual ~SerialDisconnectFunction();
    112 
    113   // AsyncApiFunction:
    114   virtual bool Prepare() OVERRIDE;
    115   virtual void Work() OVERRIDE;
    116 
    117  private:
    118   scoped_ptr<serial::Disconnect::Params> params_;
    119 };
    120 
    121 class SerialSetPausedFunction : public SerialAsyncApiFunction {
    122  public:
    123   DECLARE_EXTENSION_FUNCTION("serial.setPaused", SERIAL_SETPAUSED)
    124 
    125   SerialSetPausedFunction();
    126 
    127  protected:
    128   virtual ~SerialSetPausedFunction();
    129 
    130   // AsyncApiFunction:
    131   virtual bool Prepare() OVERRIDE;
    132   virtual void Work() OVERRIDE;
    133 
    134  private:
    135   scoped_ptr<serial::SetPaused::Params> params_;
    136   SerialEventDispatcher* serial_event_dispatcher_;
    137 };
    138 
    139 class SerialGetInfoFunction : public SerialAsyncApiFunction {
    140  public:
    141   DECLARE_EXTENSION_FUNCTION("serial.getInfo", SERIAL_GETINFO)
    142 
    143   SerialGetInfoFunction();
    144 
    145  protected:
    146   virtual ~SerialGetInfoFunction();
    147 
    148   // AsyncApiFunction:
    149   virtual bool Prepare() OVERRIDE;
    150   virtual void Work() OVERRIDE;
    151 
    152  private:
    153   scoped_ptr<serial::GetInfo::Params> params_;
    154 };
    155 
    156 class SerialGetConnectionsFunction : public SerialAsyncApiFunction {
    157  public:
    158   DECLARE_EXTENSION_FUNCTION("serial.getConnections", SERIAL_GETCONNECTIONS);
    159 
    160   SerialGetConnectionsFunction();
    161 
    162  protected:
    163   virtual ~SerialGetConnectionsFunction();
    164 
    165   // AsyncApiFunction:
    166   virtual bool Prepare() OVERRIDE;
    167   virtual void Work() OVERRIDE;
    168 };
    169 
    170 class SerialSendFunction : public SerialAsyncApiFunction {
    171  public:
    172   DECLARE_EXTENSION_FUNCTION("serial.send", SERIAL_SEND)
    173 
    174   SerialSendFunction();
    175 
    176  protected:
    177   virtual ~SerialSendFunction();
    178 
    179   // AsyncApiFunction:
    180   virtual bool Prepare() OVERRIDE;
    181   virtual void AsyncWorkStart() OVERRIDE;
    182 
    183  private:
    184   void OnSendComplete(int bytes_sent, serial::SendError error);
    185 
    186   scoped_ptr<serial::Send::Params> params_;
    187 };
    188 
    189 class SerialFlushFunction : public SerialAsyncApiFunction {
    190  public:
    191   DECLARE_EXTENSION_FUNCTION("serial.flush", SERIAL_FLUSH)
    192 
    193   SerialFlushFunction();
    194 
    195  protected:
    196   virtual ~SerialFlushFunction();
    197 
    198   // AsyncApiFunction:
    199   virtual bool Prepare() OVERRIDE;
    200   virtual void Work() OVERRIDE;
    201 
    202  private:
    203   scoped_ptr<serial::Flush::Params> params_;
    204 };
    205 
    206 class SerialGetControlSignalsFunction : public SerialAsyncApiFunction {
    207  public:
    208   DECLARE_EXTENSION_FUNCTION("serial.getControlSignals",
    209                              SERIAL_GETCONTROLSIGNALS)
    210 
    211   SerialGetControlSignalsFunction();
    212 
    213  protected:
    214   virtual ~SerialGetControlSignalsFunction();
    215 
    216   // AsyncApiFunction:
    217   virtual bool Prepare() OVERRIDE;
    218   virtual void Work() OVERRIDE;
    219 
    220  private:
    221   scoped_ptr<serial::GetControlSignals::Params> params_;
    222 };
    223 
    224 class SerialSetControlSignalsFunction : public SerialAsyncApiFunction {
    225  public:
    226   DECLARE_EXTENSION_FUNCTION("serial.setControlSignals",
    227                              SERIAL_SETCONTROLSIGNALS)
    228 
    229   SerialSetControlSignalsFunction();
    230 
    231  protected:
    232   virtual ~SerialSetControlSignalsFunction();
    233 
    234   // AsyncApiFunction:
    235   virtual bool Prepare() OVERRIDE;
    236   virtual void Work() OVERRIDE;
    237 
    238  private:
    239   scoped_ptr<serial::SetControlSignals::Params> params_;
    240 };
    241 
    242 }  // namespace api
    243 
    244 }  // namespace extensions
    245 
    246 #endif  // CHROME_BROWSER_EXTENSIONS_API_SERIAL_SERIAL_API_H_
    247