| OLD | NEW |
| 1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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_LINUX_H_ | 5 #ifndef DEVICE_HID_HID_CONNECTION_LINUX_H_ |
| 6 #define DEVICE_HID_HID_CONNECTION_LINUX_H_ | 6 #define DEVICE_HID_HID_CONNECTION_LINUX_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 #include <stdint.h> | 9 #include <stdint.h> |
| 10 | 10 |
| 11 #include <queue> | 11 #include <queue> |
| 12 | 12 |
| 13 #include "base/files/file.h" | 13 #include "base/files/scoped_file.h" |
| 14 #include "base/macros.h" | 14 #include "base/macros.h" |
| 15 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
| 16 #include "base/sequence_checker.h" |
| 16 #include "device/hid/hid_connection.h" | 17 #include "device/hid/hid_connection.h" |
| 17 | 18 |
| 18 namespace base { | 19 namespace base { |
| 19 class SingleThreadTaskRunner; | 20 class SequencedTaskRunner; |
| 20 } | 21 } |
| 21 | 22 |
| 22 namespace device { | 23 namespace device { |
| 23 | 24 |
| 24 class HidConnectionLinux : public HidConnection { | 25 class HidConnectionLinux : public HidConnection { |
| 25 public: | 26 public: |
| 26 HidConnectionLinux( | 27 HidConnectionLinux( |
| 27 scoped_refptr<HidDeviceInfo> device_info, | 28 scoped_refptr<HidDeviceInfo> device_info, |
| 28 base::File device_file, | 29 base::ScopedFD fd, |
| 29 scoped_refptr<base::SingleThreadTaskRunner> file_thread_runner); | 30 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner); |
| 30 | 31 |
| 31 private: | 32 private: |
| 32 friend class base::RefCountedThreadSafe<HidConnectionLinux>; | 33 friend class base::RefCountedThreadSafe<HidConnectionLinux>; |
| 33 class FileThreadHelper; | 34 class FileThreadHelper; |
| 34 | 35 |
| 35 typedef base::Callback<void(ssize_t)> InternalWriteCallback; | |
| 36 typedef base::Callback<void(int)> IoctlCallback; | |
| 37 | |
| 38 ~HidConnectionLinux() override; | 36 ~HidConnectionLinux() override; |
| 39 | 37 |
| 40 // HidConnection implementation. | 38 // HidConnection implementation. |
| 41 void PlatformClose() override; | 39 void PlatformClose() override; |
| 42 void PlatformRead(const ReadCallback& callback) override; | 40 void PlatformRead(const ReadCallback& callback) override; |
| 43 void PlatformWrite(scoped_refptr<net::IOBuffer> buffer, | 41 void PlatformWrite(scoped_refptr<net::IOBuffer> buffer, |
| 44 size_t size, | 42 size_t size, |
| 45 const WriteCallback& callback) override; | 43 const WriteCallback& callback) override; |
| 46 void PlatformGetFeatureReport(uint8_t report_id, | 44 void PlatformGetFeatureReport(uint8_t report_id, |
| 47 const ReadCallback& callback) override; | 45 const ReadCallback& callback) override; |
| 48 void PlatformSendFeatureReport(scoped_refptr<net::IOBuffer> buffer, | 46 void PlatformSendFeatureReport(scoped_refptr<net::IOBuffer> buffer, |
| 49 size_t size, | 47 size_t size, |
| 50 const WriteCallback& callback) override; | 48 const WriteCallback& callback) override; |
| 51 | 49 |
| 52 // Callbacks for blocking operations run on the FILE thread. | |
| 53 void FinishWrite(size_t expected_size, | |
| 54 const WriteCallback& callback, | |
| 55 ssize_t result); | |
| 56 void FinishGetFeatureReport(uint8_t report_id, | |
| 57 scoped_refptr<net::IOBuffer> buffer, | |
| 58 const ReadCallback& callback, | |
| 59 int result); | |
| 60 void FinishSendFeatureReport(const WriteCallback& callback, int result); | |
| 61 | |
| 62 // Writes to the device. This operation may block. | |
| 63 static void BlockingWrite( | |
| 64 base::PlatformFile platform_file, | |
| 65 scoped_refptr<net::IOBuffer> buffer, | |
| 66 size_t size, | |
| 67 const InternalWriteCallback& callback, | |
| 68 scoped_refptr<base::SingleThreadTaskRunner> task_runner); | |
| 69 // Performs an ioctl on the device. This operation may block. | |
| 70 static void BlockingIoctl( | |
| 71 base::PlatformFile platform_file, | |
| 72 int request, | |
| 73 scoped_refptr<net::IOBuffer> buffer, | |
| 74 const IoctlCallback& callback, | |
| 75 scoped_refptr<base::SingleThreadTaskRunner> task_runner); | |
| 76 | |
| 77 // Closes the device file descriptor. Must be called on the FILE thread. | |
| 78 static void CloseDevice(base::File device_file); | |
| 79 | |
| 80 void ProcessInputReport(scoped_refptr<net::IOBuffer> buffer, size_t size); | 50 void ProcessInputReport(scoped_refptr<net::IOBuffer> buffer, size_t size); |
| 81 void ProcessReadQueue(); | 51 void ProcessReadQueue(); |
| 82 | 52 |
| 83 base::File device_file_; | 53 // This object lives on the sequence to which |blocking_task_runner_| posts |
| 84 FileThreadHelper* helper_; | 54 // tasks so all calls must be posted there including this object's |
| 55 // destruction. |
| 56 std::unique_ptr<FileThreadHelper> helper_; |
| 85 | 57 |
| 86 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | 58 const scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_; |
| 87 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner_; | |
| 88 | 59 |
| 89 std::queue<PendingHidReport> pending_reports_; | 60 std::queue<PendingHidReport> pending_reports_; |
| 90 std::queue<PendingHidRead> pending_reads_; | 61 std::queue<PendingHidRead> pending_reads_; |
| 91 | 62 |
| 63 base::SequenceChecker sequence_checker_; |
| 64 |
| 92 base::WeakPtrFactory<HidConnectionLinux> weak_factory_; | 65 base::WeakPtrFactory<HidConnectionLinux> weak_factory_; |
| 93 | 66 |
| 94 DISALLOW_COPY_AND_ASSIGN(HidConnectionLinux); | 67 DISALLOW_COPY_AND_ASSIGN(HidConnectionLinux); |
| 95 }; | 68 }; |
| 96 | 69 |
| 97 } // namespace device | 70 } // namespace device |
| 98 | 71 |
| 99 #endif // DEVICE_HID_HID_CONNECTION_LINUX_H_ | 72 #endif // DEVICE_HID_HID_CONNECTION_LINUX_H_ |
| OLD | NEW |