Reland: Move more tests from ChromeShellTest to ChromePublicTest
[chromium-blink-merge.git] / device / hid / hid_connection.h
blob1c7107580e994c1fff827bff7a4c24598fb6b38d
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.
5 #ifndef DEVICE_HID_HID_CONNECTION_H_
6 #define DEVICE_HID_HID_CONNECTION_H_
8 #include <stdint.h>
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"
16 namespace device {
18 class HidConnection : public base::RefCountedThreadSafe<HidConnection> {
19 public:
20 enum SpecialReportIds {
21 kNullReportId = 0x00,
22 kAnyReportId = 0xFF,
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;
30 scoped_refptr<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_; }
35 // Closes the connection. This must be called before the object is freed.
36 void Close();
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);
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);
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);
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);
59 protected:
60 friend class base::RefCountedThreadSafe<HidConnection>;
62 explicit HidConnection(scoped_refptr<HidDeviceInfo> device_info);
63 virtual ~HidConnection();
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;
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);
85 private:
86 bool IsReportIdProtected(uint8_t report_id);
88 scoped_refptr<HidDeviceInfo> device_info_;
89 bool has_protected_collection_;
90 base::ThreadChecker thread_checker_;
91 bool closed_;
93 DISALLOW_COPY_AND_ASSIGN(HidConnection);
96 struct PendingHidReport {
97 PendingHidReport();
98 ~PendingHidReport();
100 scoped_refptr<net::IOBuffer> buffer;
101 size_t size;
104 struct PendingHidRead {
105 PendingHidRead();
106 ~PendingHidRead();
108 HidConnection::ReadCallback callback;
111 } // namespace device
113 #endif // DEVICE_HID_HID_CONNECTION_H_