| 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 mojo::Callback<void(TransferStatus, mojo::Array<uint8_t>)>; | 30 base::Callback<void(TransferStatus, mojo::Array<uint8_t>)>; |
| 31 | 31 |
| 32 using MojoTransferOutCallback = mojo::Callback<void(TransferStatus)>; | 32 using MojoTransferOutCallback = base::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 } | |
| 54 | 33 |
| 55 scoped_refptr<net::IOBuffer> CreateTransferBuffer(size_t size) { | 34 scoped_refptr<net::IOBuffer> CreateTransferBuffer(size_t size) { |
| 56 scoped_refptr<net::IOBuffer> buffer = new net::IOBuffer( | 35 scoped_refptr<net::IOBuffer> buffer = new net::IOBuffer( |
| 57 std::max(static_cast<size_t>(1u), static_cast<size_t>(size))); | 36 std::max(static_cast<size_t>(1u), static_cast<size_t>(size))); |
| 58 return buffer; | 37 return buffer; |
| 59 } | 38 } |
| 60 | 39 |
| 61 void OnTransferIn(std::unique_ptr<MojoTransferInCallback> callback, | 40 void OnTransferIn(std::unique_ptr<MojoTransferInCallback> callback, |
| 62 UsbTransferStatus status, | 41 UsbTransferStatus status, |
| 63 scoped_refptr<net::IOBuffer> buffer, | 42 scoped_refptr<net::IOBuffer> buffer, |
| (...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 250 | 229 |
| 251 void DeviceImpl::SetConfiguration(uint8_t value, | 230 void DeviceImpl::SetConfiguration(uint8_t value, |
| 252 const SetConfigurationCallback& callback) { | 231 const SetConfigurationCallback& callback) { |
| 253 if (!device_handle_) { | 232 if (!device_handle_) { |
| 254 callback.Run(false); | 233 callback.Run(false); |
| 255 return; | 234 return; |
| 256 } | 235 } |
| 257 | 236 |
| 258 if (permission_provider_ && | 237 if (permission_provider_ && |
| 259 permission_provider_->HasConfigurationPermission(value, device_)) { | 238 permission_provider_->HasConfigurationPermission(value, device_)) { |
| 260 device_handle_->SetConfiguration(value, WrapMojoCallback(callback)); | 239 device_handle_->SetConfiguration(value, callback); |
| 261 } else { | 240 } else { |
| 262 callback.Run(false); | 241 callback.Run(false); |
| 263 } | 242 } |
| 264 } | 243 } |
| 265 | 244 |
| 266 void DeviceImpl::ClaimInterface(uint8_t interface_number, | 245 void DeviceImpl::ClaimInterface(uint8_t interface_number, |
| 267 const ClaimInterfaceCallback& callback) { | 246 const ClaimInterfaceCallback& callback) { |
| 268 if (!device_handle_) { | 247 if (!device_handle_) { |
| 269 callback.Run(false); | 248 callback.Run(false); |
| 270 return; | 249 return; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 283 }); | 262 }); |
| 284 if (interface_it == config->interfaces.end()) { | 263 if (interface_it == config->interfaces.end()) { |
| 285 callback.Run(false); | 264 callback.Run(false); |
| 286 return; | 265 return; |
| 287 } | 266 } |
| 288 | 267 |
| 289 if (permission_provider_ && | 268 if (permission_provider_ && |
| 290 permission_provider_->HasFunctionPermission(interface_it->first_interface, | 269 permission_provider_->HasFunctionPermission(interface_it->first_interface, |
| 291 config->configuration_value, | 270 config->configuration_value, |
| 292 device_)) { | 271 device_)) { |
| 293 device_handle_->ClaimInterface(interface_number, | 272 device_handle_->ClaimInterface(interface_number, callback); |
| 294 WrapMojoCallback(callback)); | |
| 295 } else { | 273 } else { |
| 296 callback.Run(false); | 274 callback.Run(false); |
| 297 } | 275 } |
| 298 } | 276 } |
| 299 | 277 |
| 300 void DeviceImpl::ReleaseInterface(uint8_t interface_number, | 278 void DeviceImpl::ReleaseInterface(uint8_t interface_number, |
| 301 const ReleaseInterfaceCallback& callback) { | 279 const ReleaseInterfaceCallback& callback) { |
| 302 if (!device_handle_) { | 280 if (!device_handle_) { |
| 303 callback.Run(false); | 281 callback.Run(false); |
| 304 return; | 282 return; |
| 305 } | 283 } |
| 306 | 284 |
| 307 device_handle_->ReleaseInterface(interface_number, | 285 device_handle_->ReleaseInterface(interface_number, callback); |
| 308 WrapMojoCallback(callback)); | |
| 309 } | 286 } |
| 310 | 287 |
| 311 void DeviceImpl::SetInterfaceAlternateSetting( | 288 void DeviceImpl::SetInterfaceAlternateSetting( |
| 312 uint8_t interface_number, | 289 uint8_t interface_number, |
| 313 uint8_t alternate_setting, | 290 uint8_t alternate_setting, |
| 314 const SetInterfaceAlternateSettingCallback& callback) { | 291 const SetInterfaceAlternateSettingCallback& callback) { |
| 315 if (!device_handle_) { | 292 if (!device_handle_) { |
| 316 callback.Run(false); | 293 callback.Run(false); |
| 317 return; | 294 return; |
| 318 } | 295 } |
| 319 | 296 |
| 320 device_handle_->SetInterfaceAlternateSetting( | 297 device_handle_->SetInterfaceAlternateSetting(interface_number, |
| 321 interface_number, alternate_setting, WrapMojoCallback(callback)); | 298 alternate_setting, callback); |
| 322 } | 299 } |
| 323 | 300 |
| 324 void DeviceImpl::Reset(const ResetCallback& callback) { | 301 void DeviceImpl::Reset(const ResetCallback& callback) { |
| 325 if (!device_handle_) { | 302 if (!device_handle_) { |
| 326 callback.Run(false); | 303 callback.Run(false); |
| 327 return; | 304 return; |
| 328 } | 305 } |
| 329 | 306 |
| 330 device_handle_->ResetDevice(WrapMojoCallback(callback)); | 307 device_handle_->ResetDevice(callback); |
| 331 } | 308 } |
| 332 | 309 |
| 333 void DeviceImpl::ClearHalt(uint8_t endpoint, | 310 void DeviceImpl::ClearHalt(uint8_t endpoint, |
| 334 const ClearHaltCallback& callback) { | 311 const ClearHaltCallback& callback) { |
| 335 if (!device_handle_) { | 312 if (!device_handle_) { |
| 336 callback.Run(false); | 313 callback.Run(false); |
| 337 return; | 314 return; |
| 338 } | 315 } |
| 339 | 316 |
| 340 device_handle_->ClearHalt(endpoint, WrapMojoCallback(callback)); | 317 device_handle_->ClearHalt(endpoint, callback); |
| 341 } | 318 } |
| 342 | 319 |
| 343 void DeviceImpl::ControlTransferIn(ControlTransferParamsPtr params, | 320 void DeviceImpl::ControlTransferIn(ControlTransferParamsPtr params, |
| 344 uint32_t length, | 321 uint32_t length, |
| 345 uint32_t timeout, | 322 uint32_t timeout, |
| 346 const ControlTransferInCallback& callback) { | 323 const ControlTransferInCallback& callback) { |
| 347 if (!device_handle_) { | 324 if (!device_handle_) { |
| 348 callback.Run(TransferStatus::TRANSFER_ERROR, mojo::Array<uint8_t>()); | 325 callback.Run(TransferStatus::TRANSFER_ERROR, mojo::Array<uint8_t>()); |
| 349 return; | 326 return; |
| 350 } | 327 } |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 475 base::Bind(&OnIsochronousTransferOut, base::Passed(&callback_ptr))); | 452 base::Bind(&OnIsochronousTransferOut, base::Passed(&callback_ptr))); |
| 476 } | 453 } |
| 477 | 454 |
| 478 void DeviceImpl::OnDeviceRemoved(scoped_refptr<UsbDevice> device) { | 455 void DeviceImpl::OnDeviceRemoved(scoped_refptr<UsbDevice> device) { |
| 479 DCHECK_EQ(device_, device); | 456 DCHECK_EQ(device_, device); |
| 480 delete this; | 457 delete this; |
| 481 } | 458 } |
| 482 | 459 |
| 483 } // namespace usb | 460 } // namespace usb |
| 484 } // namespace device | 461 } // namespace device |
| OLD | NEW |