Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / device / hid / hid_connection.h
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.
4
5 #ifndef DEVICE_HID_HID_CONNECTION_H_
6 #define DEVICE_HID_HID_CONNECTION_H_
7
8 #include <stdint.h>
9
10 #include "base/callback.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/threading/thread_checker.h"
13 #include "device/hid/hid_device_info.h"
14 #include "net/base/io_buffer.h"
15
16 namespace device {
17
18 class HidConnection : public base::RefCountedThreadSafe<HidConnection> {
19  public:
20   enum SpecialReportIds {
21     kNullReportId = 0x00,
22     kAnyReportId = 0xFF,
23   };
24
25   typedef base::Callback<
26       void(bool success, scoped_refptr<net::IOBuffer> buffer, size_t size)>
27       ReadCallback;
28   typedef base::Callback<void(bool success)> WriteCallback;
29
30   const HidDeviceInfo& device_info() const { return device_info_; }
31   bool has_protected_collection() const { return has_protected_collection_; }
32   const base::ThreadChecker& thread_checker() const { return thread_checker_; }
33   bool closed() const { return closed_; }
34
35   // Closes the connection. This must be called before the object is freed.
36   void Close();
37
38   // The report ID (or 0 if report IDs are not supported by the device) is
39   // always returned in the first byte of the buffer.
40   void Read(const ReadCallback& callback);
41
42   // The report ID (or 0 if report IDs are not supported by the device) is
43   // always expected in the first byte of the buffer.
44   void Write(scoped_refptr<net::IOBuffer> buffer,
45              size_t size,
46              const WriteCallback& callback);
47
48   // The buffer will contain whatever report data was received from the device.
49   // This may include the report ID. The report ID is not stripped because a
50   // device may respond with other data in place of the report ID.
51   void GetFeatureReport(uint8_t report_id, const ReadCallback& callback);
52
53   // The report ID (or 0 if report IDs are not supported by the device) is
54   // always expected in the first byte of the buffer.
55   void SendFeatureReport(scoped_refptr<net::IOBuffer> buffer,
56                          size_t size,
57                          const WriteCallback& callback);
58
59  protected:
60   friend class base::RefCountedThreadSafe<HidConnection>;
61
62   explicit HidConnection(const HidDeviceInfo& device_info);
63   virtual ~HidConnection();
64
65   virtual void PlatformClose() = 0;
66   virtual void PlatformRead(const ReadCallback& callback) = 0;
67   virtual void PlatformWrite(scoped_refptr<net::IOBuffer> buffer,
68                              size_t size,
69                              const WriteCallback& callback) = 0;
70   virtual void PlatformGetFeatureReport(uint8_t report_id,
71                                         const ReadCallback& callback) = 0;
72   virtual void PlatformSendFeatureReport(scoped_refptr<net::IOBuffer> buffer,
73                                          size_t size,
74                                          const WriteCallback& callback) = 0;
75
76   // PlatformRead implementation must call this method on read
77   // success, rather than directly running the callback.
78   // In case incoming buffer is empty or protected, it is filtered
79   // and this method returns false. Otherwise it runs the callback
80   // and returns true.
81   bool CompleteRead(scoped_refptr<net::IOBuffer> buffer,
82                     size_t size,
83                     const ReadCallback& callback);
84
85  private:
86   bool IsReportIdProtected(uint8_t report_id);
87
88   const HidDeviceInfo device_info_;
89   bool has_protected_collection_;
90   base::ThreadChecker thread_checker_;
91   bool closed_;
92
93   DISALLOW_COPY_AND_ASSIGN(HidConnection);
94 };
95
96 struct PendingHidReport {
97   PendingHidReport();
98   ~PendingHidReport();
99
100   scoped_refptr<net::IOBuffer> buffer;
101   size_t size;
102 };
103
104 struct PendingHidRead {
105   PendingHidRead();
106   ~PendingHidRead();
107
108   HidConnection::ReadCallback callback;
109 };
110
111 }  // namespace device
112
113 #endif  // DEVICE_HID_HID_CONNECTION_H_