| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef DEVICE_HID_HID_CONNECTION_H_ | 5 #ifndef DEVICE_HID_HID_CONNECTION_H_ |
| 6 #define DEVICE_HID_HID_CONNECTION_H_ | 6 #define DEVICE_HID_HID_CONNECTION_H_ |
| 7 | 7 |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include "base/callback.h" | 10 #include "base/callback.h" |
| 11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
| 12 #include "base/threading/thread_checker.h" |
| 12 #include "device/hid/hid_device_info.h" | 13 #include "device/hid/hid_device_info.h" |
| 13 #include "net/base/io_buffer.h" | 14 #include "net/base/io_buffer.h" |
| 14 | 15 |
| 15 namespace device { | 16 namespace device { |
| 16 | 17 |
| 17 class HidConnection : public base::RefCountedThreadSafe<HidConnection> { | 18 class HidConnection : public base::RefCountedThreadSafe<HidConnection> { |
| 18 public: | 19 public: |
| 20 enum SpecialReportIds { |
| 21 kNullReportId = 0x00, |
| 22 kAnyReportId = 0xFF, |
| 23 }; |
| 24 |
| 19 typedef base::Callback<void(bool success, size_t size)> IOCallback; | 25 typedef base::Callback<void(bool success, size_t size)> IOCallback; |
| 20 | 26 |
| 21 virtual void Read(scoped_refptr<net::IOBufferWithSize> buffer, | 27 const HidDeviceInfo& device_info() const { return device_info_; } |
| 22 const IOCallback& callback) = 0; | 28 bool has_protected_collection() const { return has_protected_collection_; } |
| 23 virtual void Write(uint8_t report_id, | 29 bool has_report_id() const { return has_report_id_; } |
| 24 scoped_refptr<net::IOBufferWithSize> buffer, | 30 const base::ThreadChecker& thread_checker() const { return thread_checker_; } |
| 25 const IOCallback& callback) = 0; | |
| 26 virtual void GetFeatureReport(uint8_t report_id, | |
| 27 scoped_refptr<net::IOBufferWithSize> buffer, | |
| 28 const IOCallback& callback) = 0; | |
| 29 virtual void SendFeatureReport(uint8_t report_id, | |
| 30 scoped_refptr<net::IOBufferWithSize> buffer, | |
| 31 const IOCallback& callback) = 0; | |
| 32 | 31 |
| 33 const HidDeviceInfo& device_info() const { return device_info_; } | 32 void Read(scoped_refptr<net::IOBufferWithSize> buffer, |
| 33 const IOCallback& callback); |
| 34 void Write(uint8_t report_id, |
| 35 scoped_refptr<net::IOBufferWithSize> buffer, |
| 36 const IOCallback& callback); |
| 37 void GetFeatureReport(uint8_t report_id, |
| 38 scoped_refptr<net::IOBufferWithSize> buffer, |
| 39 const IOCallback& callback); |
| 40 void SendFeatureReport(uint8_t report_id, |
| 41 scoped_refptr<net::IOBufferWithSize> buffer, |
| 42 const IOCallback& callback); |
| 34 | 43 |
| 35 protected: | 44 protected: |
| 36 friend class base::RefCountedThreadSafe<HidConnection>; | 45 friend class base::RefCountedThreadSafe<HidConnection>; |
| 37 friend struct HidDeviceInfo; | |
| 38 | 46 |
| 39 explicit HidConnection(const HidDeviceInfo& device_info); | 47 explicit HidConnection(const HidDeviceInfo& device_info); |
| 40 virtual ~HidConnection(); | 48 virtual ~HidConnection(); |
| 41 | 49 |
| 50 virtual void PlatformRead(scoped_refptr<net::IOBufferWithSize> buffer, |
| 51 const IOCallback& callback) = 0; |
| 52 virtual void PlatformWrite(uint8_t report_id, |
| 53 scoped_refptr<net::IOBufferWithSize> buffer, |
| 54 const IOCallback& callback) = 0; |
| 55 virtual void PlatformGetFeatureReport( |
| 56 uint8_t report_id, |
| 57 scoped_refptr<net::IOBufferWithSize> buffer, |
| 58 const IOCallback& callback) = 0; |
| 59 virtual void PlatformSendFeatureReport( |
| 60 uint8_t report_id, |
| 61 scoped_refptr<net::IOBufferWithSize> buffer, |
| 62 const IOCallback& callback) = 0; |
| 63 |
| 64 // PlatformRead implementation must call this method on read |
| 65 // success, rather than directly running the callback. |
| 66 // In case incoming buffer is empty or protected, it is filtered |
| 67 // and this method returns false. Otherwise it runs the callback |
| 68 // and returns true. |
| 69 bool CompleteRead(scoped_refptr<net::IOBufferWithSize> buffer, |
| 70 const IOCallback& callback); |
| 71 |
| 42 private: | 72 private: |
| 73 bool IsReportIdProtected(const uint8_t report_id); |
| 74 |
| 43 const HidDeviceInfo device_info_; | 75 const HidDeviceInfo device_info_; |
| 76 bool has_report_id_; |
| 77 bool has_protected_collection_; |
| 78 base::ThreadChecker thread_checker_; |
| 44 | 79 |
| 45 DISALLOW_COPY_AND_ASSIGN(HidConnection); | 80 DISALLOW_COPY_AND_ASSIGN(HidConnection); |
| 46 }; | 81 }; |
| 47 | 82 |
| 48 struct PendingHidReport { | 83 struct PendingHidReport { |
| 49 PendingHidReport(); | 84 PendingHidReport(); |
| 50 ~PendingHidReport(); | 85 ~PendingHidReport(); |
| 51 | 86 |
| 52 scoped_refptr<net::IOBufferWithSize> buffer; | 87 scoped_refptr<net::IOBufferWithSize> buffer; |
| 53 }; | 88 }; |
| 54 | 89 |
| 55 struct PendingHidRead { | 90 struct PendingHidRead { |
| 56 PendingHidRead(); | 91 PendingHidRead(); |
| 57 ~PendingHidRead(); | 92 ~PendingHidRead(); |
| 58 | 93 |
| 59 scoped_refptr<net::IOBufferWithSize> buffer; | 94 scoped_refptr<net::IOBufferWithSize> buffer; |
| 60 HidConnection::IOCallback callback; | 95 HidConnection::IOCallback callback; |
| 61 }; | 96 }; |
| 62 | 97 |
| 63 } // namespace device | 98 } // namespace device |
| 64 | 99 |
| 65 #endif // DEVICE_HID_HID_CONNECTION_H_ | 100 #endif // DEVICE_HID_HID_CONNECTION_H_ |
| OLD | NEW |