OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/mojo/device_impl.h" | 5 #include "device/usb/mojo/device_impl.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <memory> | 10 #include <memory> |
11 #include <numeric> | 11 #include <numeric> |
12 #include <utility> | 12 #include <utility> |
13 #include <vector> | 13 #include <vector> |
14 | 14 |
15 #include "base/bind.h" | 15 #include "base/bind.h" |
16 #include "base/callback.h" | 16 #include "base/callback.h" |
17 #include "base/memory/ptr_util.h" | 17 #include "base/memory/ptr_util.h" |
18 #include "base/stl_util.h" | 18 #include "base/stl_util.h" |
19 #include "device/usb/mojo/type_converters.h" | 19 #include "device/usb/mojo/type_converters.h" |
20 #include "device/usb/usb_descriptors.h" | 20 #include "device/usb/usb_descriptors.h" |
21 #include "device/usb/usb_device.h" | 21 #include "device/usb/usb_device.h" |
22 #include "net/base/io_buffer.h" | 22 #include "net/base/io_buffer.h" |
23 | 23 |
24 namespace device { | 24 namespace device { |
25 namespace usb { | 25 namespace usb { |
26 | 26 |
27 namespace { | 27 namespace { |
28 | 28 |
29 using MojoTransferInCallback = | 29 using MojoTransferInCallback = |
30 base::Callback<void(TransferStatus, mojo::Array<uint8_t>)>; | 30 mojo::Callback<void(TransferStatus, mojo::Array<uint8_t>)>; |
31 | 31 |
32 using MojoTransferOutCallback = base::Callback<void(TransferStatus)>; | 32 using MojoTransferOutCallback = mojo::Callback<void(TransferStatus)>; |
| 33 |
| 34 template <typename... Args> |
| 35 void CallMojoCallback(std::unique_ptr<mojo::Callback<void(Args...)>> callback, |
| 36 Args... args) { |
| 37 callback->Run(args...); |
| 38 } |
| 39 |
| 40 // Generic wrapper to convert a Mojo callback to something we can rebind and |
| 41 // pass around. This is only usable for callbacks with no move-only arguments. |
| 42 template <typename... Args> |
| 43 base::Callback<void(Args...)> WrapMojoCallback( |
| 44 const mojo::Callback<void(Args...)>& callback) { |
| 45 // mojo::Callback is not thread safe. By wrapping |callback| in a scoped_ptr |
| 46 // we guarantee that it will be freed when CallMojoCallback is run and not |
| 47 // retained until the base::Callback is destroyed, which could happen on any |
| 48 // thread. This pattern is also used below in places where this generic |
| 49 // wrapper is not used. |
| 50 auto callback_ptr = |
| 51 base::WrapUnique(new mojo::Callback<void(Args...)>(callback)); |
| 52 return base::Bind(&CallMojoCallback<Args...>, base::Passed(&callback_ptr)); |
| 53 } |
33 | 54 |
34 scoped_refptr<net::IOBuffer> CreateTransferBuffer(size_t size) { | 55 scoped_refptr<net::IOBuffer> CreateTransferBuffer(size_t size) { |
35 scoped_refptr<net::IOBuffer> buffer = new net::IOBuffer( | 56 scoped_refptr<net::IOBuffer> buffer = new net::IOBuffer( |
36 std::max(static_cast<size_t>(1u), static_cast<size_t>(size))); | 57 std::max(static_cast<size_t>(1u), static_cast<size_t>(size))); |
37 return buffer; | 58 return buffer; |
38 } | 59 } |
39 | 60 |
40 void OnTransferIn(std::unique_ptr<MojoTransferInCallback> callback, | 61 void OnTransferIn(std::unique_ptr<MojoTransferInCallback> callback, |
41 UsbTransferStatus status, | 62 UsbTransferStatus status, |
42 scoped_refptr<net::IOBuffer> buffer, | 63 scoped_refptr<net::IOBuffer> buffer, |
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
229 | 250 |
230 void DeviceImpl::SetConfiguration(uint8_t value, | 251 void DeviceImpl::SetConfiguration(uint8_t value, |
231 const SetConfigurationCallback& callback) { | 252 const SetConfigurationCallback& callback) { |
232 if (!device_handle_) { | 253 if (!device_handle_) { |
233 callback.Run(false); | 254 callback.Run(false); |
234 return; | 255 return; |
235 } | 256 } |
236 | 257 |
237 if (permission_provider_ && | 258 if (permission_provider_ && |
238 permission_provider_->HasConfigurationPermission(value, device_)) { | 259 permission_provider_->HasConfigurationPermission(value, device_)) { |
239 device_handle_->SetConfiguration(value, callback); | 260 device_handle_->SetConfiguration(value, WrapMojoCallback(callback)); |
240 } else { | 261 } else { |
241 callback.Run(false); | 262 callback.Run(false); |
242 } | 263 } |
243 } | 264 } |
244 | 265 |
245 void DeviceImpl::ClaimInterface(uint8_t interface_number, | 266 void DeviceImpl::ClaimInterface(uint8_t interface_number, |
246 const ClaimInterfaceCallback& callback) { | 267 const ClaimInterfaceCallback& callback) { |
247 if (!device_handle_) { | 268 if (!device_handle_) { |
248 callback.Run(false); | 269 callback.Run(false); |
249 return; | 270 return; |
(...skipping 12 matching lines...) Expand all Loading... |
262 }); | 283 }); |
263 if (interface_it == config->interfaces.end()) { | 284 if (interface_it == config->interfaces.end()) { |
264 callback.Run(false); | 285 callback.Run(false); |
265 return; | 286 return; |
266 } | 287 } |
267 | 288 |
268 if (permission_provider_ && | 289 if (permission_provider_ && |
269 permission_provider_->HasFunctionPermission(interface_it->first_interface, | 290 permission_provider_->HasFunctionPermission(interface_it->first_interface, |
270 config->configuration_value, | 291 config->configuration_value, |
271 device_)) { | 292 device_)) { |
272 device_handle_->ClaimInterface(interface_number, callback); | 293 device_handle_->ClaimInterface(interface_number, |
| 294 WrapMojoCallback(callback)); |
273 } else { | 295 } else { |
274 callback.Run(false); | 296 callback.Run(false); |
275 } | 297 } |
276 } | 298 } |
277 | 299 |
278 void DeviceImpl::ReleaseInterface(uint8_t interface_number, | 300 void DeviceImpl::ReleaseInterface(uint8_t interface_number, |
279 const ReleaseInterfaceCallback& callback) { | 301 const ReleaseInterfaceCallback& callback) { |
280 if (!device_handle_) { | 302 if (!device_handle_) { |
281 callback.Run(false); | 303 callback.Run(false); |
282 return; | 304 return; |
283 } | 305 } |
284 | 306 |
285 device_handle_->ReleaseInterface(interface_number, callback); | 307 device_handle_->ReleaseInterface(interface_number, |
| 308 WrapMojoCallback(callback)); |
286 } | 309 } |
287 | 310 |
288 void DeviceImpl::SetInterfaceAlternateSetting( | 311 void DeviceImpl::SetInterfaceAlternateSetting( |
289 uint8_t interface_number, | 312 uint8_t interface_number, |
290 uint8_t alternate_setting, | 313 uint8_t alternate_setting, |
291 const SetInterfaceAlternateSettingCallback& callback) { | 314 const SetInterfaceAlternateSettingCallback& callback) { |
292 if (!device_handle_) { | 315 if (!device_handle_) { |
293 callback.Run(false); | 316 callback.Run(false); |
294 return; | 317 return; |
295 } | 318 } |
296 | 319 |
297 device_handle_->SetInterfaceAlternateSetting(interface_number, | 320 device_handle_->SetInterfaceAlternateSetting( |
298 alternate_setting, callback); | 321 interface_number, alternate_setting, WrapMojoCallback(callback)); |
299 } | 322 } |
300 | 323 |
301 void DeviceImpl::Reset(const ResetCallback& callback) { | 324 void DeviceImpl::Reset(const ResetCallback& callback) { |
302 if (!device_handle_) { | 325 if (!device_handle_) { |
303 callback.Run(false); | 326 callback.Run(false); |
304 return; | 327 return; |
305 } | 328 } |
306 | 329 |
307 device_handle_->ResetDevice(callback); | 330 device_handle_->ResetDevice(WrapMojoCallback(callback)); |
308 } | 331 } |
309 | 332 |
310 void DeviceImpl::ClearHalt(uint8_t endpoint, | 333 void DeviceImpl::ClearHalt(uint8_t endpoint, |
311 const ClearHaltCallback& callback) { | 334 const ClearHaltCallback& callback) { |
312 if (!device_handle_) { | 335 if (!device_handle_) { |
313 callback.Run(false); | 336 callback.Run(false); |
314 return; | 337 return; |
315 } | 338 } |
316 | 339 |
317 device_handle_->ClearHalt(endpoint, callback); | 340 device_handle_->ClearHalt(endpoint, WrapMojoCallback(callback)); |
318 } | 341 } |
319 | 342 |
320 void DeviceImpl::ControlTransferIn(ControlTransferParamsPtr params, | 343 void DeviceImpl::ControlTransferIn(ControlTransferParamsPtr params, |
321 uint32_t length, | 344 uint32_t length, |
322 uint32_t timeout, | 345 uint32_t timeout, |
323 const ControlTransferInCallback& callback) { | 346 const ControlTransferInCallback& callback) { |
324 if (!device_handle_) { | 347 if (!device_handle_) { |
325 callback.Run(TransferStatus::TRANSFER_ERROR, mojo::Array<uint8_t>()); | 348 callback.Run(TransferStatus::TRANSFER_ERROR, mojo::Array<uint8_t>()); |
326 return; | 349 return; |
327 } | 350 } |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
452 base::Bind(&OnIsochronousTransferOut, base::Passed(&callback_ptr))); | 475 base::Bind(&OnIsochronousTransferOut, base::Passed(&callback_ptr))); |
453 } | 476 } |
454 | 477 |
455 void DeviceImpl::OnDeviceRemoved(scoped_refptr<UsbDevice> device) { | 478 void DeviceImpl::OnDeviceRemoved(scoped_refptr<UsbDevice> device) { |
456 DCHECK_EQ(device_, device); | 479 DCHECK_EQ(device_, device); |
457 delete this; | 480 delete this; |
458 } | 481 } |
459 | 482 |
460 } // namespace usb | 483 } // namespace usb |
461 } // namespace device | 484 } // namespace device |
OLD | NEW |