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 #include "device/usb/usb_device_handle_impl.h" | 5 #include "device/usb/usb_device_handle_impl.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <memory> | 8 #include <memory> |
9 #include <numeric> | 9 #include <numeric> |
10 #include <utility> | 10 #include <utility> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "base/bind.h" | 13 #include "base/bind.h" |
14 #include "base/location.h" | 14 #include "base/location.h" |
15 #include "base/macros.h" | 15 #include "base/macros.h" |
16 #include "base/sequence_checker.h" | |
16 #include "base/single_thread_task_runner.h" | 17 #include "base/single_thread_task_runner.h" |
17 #include "base/stl_util.h" | 18 #include "base/stl_util.h" |
18 #include "base/strings/string16.h" | 19 #include "base/strings/string16.h" |
19 #include "base/synchronization/lock.h" | |
20 #include "base/threading/thread_task_runner_handle.h" | 20 #include "base/threading/thread_task_runner_handle.h" |
21 #include "components/device_event_log/device_event_log.h" | 21 #include "components/device_event_log/device_event_log.h" |
22 #include "device/usb/usb_context.h" | 22 #include "device/usb/usb_context.h" |
23 #include "device/usb/usb_descriptors.h" | 23 #include "device/usb/usb_descriptors.h" |
24 #include "device/usb/usb_device_impl.h" | 24 #include "device/usb/usb_device_impl.h" |
25 #include "device/usb/usb_error.h" | 25 #include "device/usb/usb_error.h" |
26 #include "device/usb/usb_service.h" | 26 #include "device/usb/usb_service.h" |
27 #include "net/base/io_buffer.h" | 27 #include "net/base/io_buffer.h" |
28 #include "third_party/libusb/src/libusb/libusb.h" | 28 #include "third_party/libusb/src/libusb/libusb.h" |
29 | 29 |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
142 | 142 |
143 } // namespace | 143 } // namespace |
144 | 144 |
145 class UsbDeviceHandleImpl::InterfaceClaimer | 145 class UsbDeviceHandleImpl::InterfaceClaimer |
146 : public base::RefCountedThreadSafe<UsbDeviceHandleImpl::InterfaceClaimer> { | 146 : public base::RefCountedThreadSafe<UsbDeviceHandleImpl::InterfaceClaimer> { |
147 public: | 147 public: |
148 InterfaceClaimer(scoped_refptr<UsbDeviceHandleImpl> handle, | 148 InterfaceClaimer(scoped_refptr<UsbDeviceHandleImpl> handle, |
149 int interface_number, | 149 int interface_number, |
150 scoped_refptr<base::SingleThreadTaskRunner> task_runner); | 150 scoped_refptr<base::SingleThreadTaskRunner> task_runner); |
151 | 151 |
152 int interface_number() const { return interface_number_; } | 152 int interface_number() const { return interface_number_; } |
robliao
2017/05/02 14:32:59
Should these accessors also be DCHECK'ing the sequ
Reilly Grant (use Gerrit)
2017/05/02 23:41:44
DCHECKing in inline accessors is frowned upon. The
| |
153 int alternate_setting() const { return alternate_setting_; } | 153 int alternate_setting() const { return alternate_setting_; } |
154 void set_alternate_setting(const int alternate_setting) { | 154 void set_alternate_setting(const int alternate_setting) { |
155 alternate_setting_ = alternate_setting; | 155 alternate_setting_ = alternate_setting; |
156 } | 156 } |
157 | 157 |
158 void set_release_callback(const ResultCallback& callback) { | 158 void set_release_callback(const ResultCallback& callback) { |
159 release_callback_ = callback; | 159 release_callback_ = callback; |
160 } | 160 } |
161 | 161 |
162 private: | 162 private: |
163 friend class base::RefCountedThreadSafe<InterfaceClaimer>; | 163 friend class base::RefCountedThreadSafe<InterfaceClaimer>; |
164 ~InterfaceClaimer(); | 164 ~InterfaceClaimer(); |
165 | 165 |
166 const scoped_refptr<UsbDeviceHandleImpl> handle_; | 166 const scoped_refptr<UsbDeviceHandleImpl> handle_; |
167 const int interface_number_; | 167 const int interface_number_; |
168 int alternate_setting_; | 168 int alternate_setting_; |
169 const scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | 169 const scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
170 ResultCallback release_callback_; | 170 ResultCallback release_callback_; |
171 base::ThreadChecker thread_checker_; | 171 base::SequenceChecker sequence_checker_; |
172 | 172 |
173 DISALLOW_COPY_AND_ASSIGN(InterfaceClaimer); | 173 DISALLOW_COPY_AND_ASSIGN(InterfaceClaimer); |
174 }; | 174 }; |
175 | 175 |
176 UsbDeviceHandleImpl::InterfaceClaimer::InterfaceClaimer( | 176 UsbDeviceHandleImpl::InterfaceClaimer::InterfaceClaimer( |
177 scoped_refptr<UsbDeviceHandleImpl> handle, | 177 scoped_refptr<UsbDeviceHandleImpl> handle, |
178 int interface_number, | 178 int interface_number, |
179 scoped_refptr<base::SingleThreadTaskRunner> task_runner) | 179 scoped_refptr<base::SingleThreadTaskRunner> task_runner) |
180 : handle_(handle), | 180 : handle_(handle), |
181 interface_number_(interface_number), | 181 interface_number_(interface_number), |
182 alternate_setting_(0), | 182 alternate_setting_(0), |
183 task_runner_(task_runner) {} | 183 task_runner_(task_runner) {} |
184 | 184 |
185 UsbDeviceHandleImpl::InterfaceClaimer::~InterfaceClaimer() { | 185 UsbDeviceHandleImpl::InterfaceClaimer::~InterfaceClaimer() { |
186 DCHECK(thread_checker_.CalledOnValidThread()); | 186 DCHECK(sequence_checker_.CalledOnValidSequence()); |
187 int rc = libusb_release_interface(handle_->handle(), interface_number_); | 187 int rc = libusb_release_interface(handle_->handle(), interface_number_); |
188 if (rc != LIBUSB_SUCCESS) { | 188 if (rc != LIBUSB_SUCCESS) { |
189 USB_LOG(DEBUG) << "Failed to release interface: " | 189 USB_LOG(DEBUG) << "Failed to release interface: " |
190 << ConvertPlatformUsbErrorToString(rc); | 190 << ConvertPlatformUsbErrorToString(rc); |
191 } | 191 } |
192 if (!release_callback_.is_null()) { | 192 if (!release_callback_.is_null()) { |
193 task_runner_->PostTask(FROM_HERE, | 193 task_runner_->PostTask(FROM_HERE, |
194 base::Bind(release_callback_, rc == LIBUSB_SUCCESS)); | 194 base::Bind(release_callback_, rc == LIBUSB_SUCCESS)); |
195 } | 195 } |
196 } | 196 } |
(...skipping 948 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1145 } else { | 1145 } else { |
1146 transfer->callback_task_runner()->PostTask(FROM_HERE, callback); | 1146 transfer->callback_task_runner()->PostTask(FROM_HERE, callback); |
1147 } | 1147 } |
1148 | 1148 |
1149 // libusb_free_transfer races with libusb_submit_transfer and only work- | 1149 // libusb_free_transfer races with libusb_submit_transfer and only work- |
1150 // around is to make sure to call them on the same thread. | 1150 // around is to make sure to call them on the same thread. |
1151 blocking_task_runner_->DeleteSoon(FROM_HERE, transfer); | 1151 blocking_task_runner_->DeleteSoon(FROM_HERE, transfer); |
1152 } | 1152 } |
1153 | 1153 |
1154 } // namespace device | 1154 } // namespace device |
OLD | NEW |