OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 #include "device/usb/usb_device_win.h" | 5 #include "device/usb/usb_device_win.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/memory/ptr_util.h" | 8 #include "base/memory/ptr_util.h" |
9 #include "base/sequenced_task_runner.h" | 9 #include "base/sequenced_task_runner.h" |
10 #include "base/single_thread_task_runner.h" | 10 #include "base/single_thread_task_runner.h" |
11 #include "base/threading/thread_task_runner_handle.h" | 11 #include "base/threading/thread_task_runner_handle.h" |
12 #include "components/device_event_log/device_event_log.h" | 12 #include "components/device_event_log/device_event_log.h" |
13 #include "device/usb/usb_device_handle.h" | 13 #include "device/usb/usb_device_handle_win.h" |
14 | 14 |
15 namespace device { | 15 namespace device { |
16 | 16 |
17 UsbDeviceWin::UsbDeviceWin( | 17 UsbDeviceWin::UsbDeviceWin( |
18 const std::string& device_path, | 18 const std::string& device_path, |
19 const std::string& hub_path, | 19 const std::string& hub_path, |
20 int port_number, | 20 int port_number, |
21 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner) | 21 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner) |
22 : device_path_(device_path), | 22 : device_path_(device_path), |
23 hub_path_(hub_path), | 23 hub_path_(hub_path), |
24 port_number_(port_number), | 24 port_number_(port_number), |
25 task_runner_(base::ThreadTaskRunnerHandle::Get()), | 25 task_runner_(base::ThreadTaskRunnerHandle::Get()), |
26 blocking_task_runner_(std::move(blocking_task_runner)) {} | 26 blocking_task_runner_(std::move(blocking_task_runner)) {} |
27 | 27 |
28 UsbDeviceWin::~UsbDeviceWin() {} | 28 UsbDeviceWin::~UsbDeviceWin() {} |
29 | 29 |
30 void UsbDeviceWin::Open(const OpenCallback& callback) { | 30 void UsbDeviceWin::Open(const OpenCallback& callback) { |
31 DCHECK(thread_checker_.CalledOnValidThread()); | 31 DCHECK(thread_checker_.CalledOnValidThread()); |
32 task_runner_->PostTask(FROM_HERE, base::Bind(callback, nullptr)); | 32 task_runner_->PostTask(FROM_HERE, base::Bind(callback, nullptr)); |
33 } | 33 } |
34 | 34 |
| 35 void UsbDeviceWin::ReadDescriptors(const base::Callback<void(bool)>& callback) { |
| 36 DCHECK(thread_checker_.CalledOnValidThread()); |
| 37 scoped_refptr<UsbDeviceHandle> device_handle; |
| 38 base::win::ScopedHandle handle( |
| 39 CreateFileA(hub_path_.c_str(), GENERIC_WRITE, FILE_SHARE_WRITE, nullptr, |
| 40 OPEN_EXISTING, FILE_FLAG_OVERLAPPED, nullptr)); |
| 41 if (handle.IsValid()) { |
| 42 device_handle = |
| 43 new UsbDeviceHandleWin(this, std::move(handle), blocking_task_runner_); |
| 44 } else { |
| 45 USB_PLOG(ERROR) << "Failed to open " << hub_path_; |
| 46 callback.Run(false); |
| 47 return; |
| 48 } |
| 49 |
| 50 ReadUsbDescriptors(device_handle, base::Bind(&UsbDeviceWin::OnReadDescriptors, |
| 51 this, callback, device_handle)); |
| 52 } |
| 53 |
| 54 void UsbDeviceWin::OnReadDescriptors( |
| 55 const base::Callback<void(bool)>& callback, |
| 56 scoped_refptr<UsbDeviceHandle> device_handle, |
| 57 std::unique_ptr<UsbDeviceDescriptor> descriptor) { |
| 58 DCHECK(thread_checker_.CalledOnValidThread()); |
| 59 if (!descriptor) { |
| 60 USB_LOG(ERROR) << "Failed to read descriptors from " << device_path_ << "."; |
| 61 device_handle->Close(); |
| 62 callback.Run(false); |
| 63 return; |
| 64 } |
| 65 |
| 66 descriptor_ = *descriptor; |
| 67 |
| 68 auto string_map = base::MakeUnique<std::map<uint8_t, base::string16>>(); |
| 69 if (descriptor_.i_manufacturer) |
| 70 (*string_map)[descriptor_.i_manufacturer] = base::string16(); |
| 71 if (descriptor_.i_product) |
| 72 (*string_map)[descriptor_.i_product] = base::string16(); |
| 73 if (descriptor_.i_serial_number) |
| 74 (*string_map)[descriptor_.i_serial_number] = base::string16(); |
| 75 |
| 76 ReadUsbStringDescriptors(device_handle, std::move(string_map), |
| 77 base::Bind(&UsbDeviceWin::OnReadStringDescriptors, |
| 78 this, callback, device_handle)); |
| 79 } |
| 80 |
| 81 void UsbDeviceWin::OnReadStringDescriptors( |
| 82 const base::Callback<void(bool)>& callback, |
| 83 scoped_refptr<UsbDeviceHandle> device_handle, |
| 84 std::unique_ptr<std::map<uint8_t, base::string16>> string_map) { |
| 85 DCHECK(thread_checker_.CalledOnValidThread()); |
| 86 device_handle->Close(); |
| 87 |
| 88 if (descriptor_.i_manufacturer) |
| 89 manufacturer_string_ = (*string_map)[descriptor_.i_manufacturer]; |
| 90 if (descriptor_.i_product) |
| 91 product_string_ = (*string_map)[descriptor_.i_product]; |
| 92 if (descriptor_.i_serial_number) |
| 93 serial_number_ = (*string_map)[descriptor_.i_serial_number]; |
| 94 |
| 95 callback.Run(true); |
| 96 } |
| 97 |
35 } // namespace device | 98 } // namespace device |
OLD | NEW |