Revert of Instrumented libraries: update the libnspr4 build script. (patchset #1...
[chromium-blink-merge.git] / device / hid / hid_connection.h
blob9d2503c275285c8e528b60dd7fd8ac7e0803006e
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 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_; }
34 // The report ID (or 0 if report IDs are not supported by the device) is
35 // always returned in the first byte of the buffer.
36 void Read(const ReadCallback& callback);
38 // The report ID (or 0 if report IDs are not supported by the device) is
39 // always expected in the first byte of the buffer.
40 void Write(scoped_refptr<net::IOBuffer> buffer,
41 size_t size,
42 const WriteCallback& callback);
44 // The report ID is not returned in the buffer.
45 void GetFeatureReport(uint8_t report_id, const ReadCallback& callback);
47 // The report ID (or 0 if report IDs are not supported by the device) is
48 // always expected in the first byte of the buffer.
49 void SendFeatureReport(scoped_refptr<net::IOBuffer> buffer,
50 size_t size,
51 const WriteCallback& callback);
53 protected:
54 friend class base::RefCountedThreadSafe<HidConnection>;
56 explicit HidConnection(const HidDeviceInfo& device_info);
57 virtual ~HidConnection();
59 virtual void PlatformRead(const ReadCallback& callback) = 0;
60 virtual void PlatformWrite(scoped_refptr<net::IOBuffer> buffer,
61 size_t size,
62 const WriteCallback& callback) = 0;
63 virtual void PlatformGetFeatureReport(uint8_t report_id,
64 const ReadCallback& callback) = 0;
65 virtual void PlatformSendFeatureReport(scoped_refptr<net::IOBuffer> buffer,
66 size_t size,
67 const WriteCallback& callback) = 0;
69 // PlatformRead implementation must call this method on read
70 // success, rather than directly running the callback.
71 // In case incoming buffer is empty or protected, it is filtered
72 // and this method returns false. Otherwise it runs the callback
73 // and returns true.
74 bool CompleteRead(scoped_refptr<net::IOBuffer> buffer,
75 size_t size,
76 const ReadCallback& callback);
78 private:
79 bool IsReportIdProtected(uint8_t report_id);
81 const HidDeviceInfo device_info_;
82 bool has_protected_collection_;
83 base::ThreadChecker thread_checker_;
85 DISALLOW_COPY_AND_ASSIGN(HidConnection);
88 struct PendingHidReport {
89 PendingHidReport();
90 ~PendingHidReport();
92 scoped_refptr<net::IOBuffer> buffer;
93 size_t size;
96 struct PendingHidRead {
97 PendingHidRead();
98 ~PendingHidRead();
100 HidConnection::ReadCallback callback;
103 } // namespace device
105 #endif // DEVICE_HID_HID_CONNECTION_H_