Chromium Code Reviews| 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 "content/renderer/usb/web_usb_device_impl.h" | 5 #include "content/renderer/usb/web_usb_device_impl.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/callback.h" | 10 #include "base/callback.h" |
| (...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 227 } | 227 } |
| 228 scoped_callbacks->onSuccess(adoptWebPtr(info.release())); | 228 scoped_callbacks->onSuccess(adoptWebPtr(info.release())); |
| 229 } | 229 } |
| 230 | 230 |
| 231 } // namespace | 231 } // namespace |
| 232 | 232 |
| 233 WebUSBDeviceImpl::WebUSBDeviceImpl(device::usb::DevicePtr device, | 233 WebUSBDeviceImpl::WebUSBDeviceImpl(device::usb::DevicePtr device, |
| 234 const blink::WebUSBDeviceInfo& device_info) | 234 const blink::WebUSBDeviceInfo& device_info) |
| 235 : device_(std::move(device)), | 235 : device_(std::move(device)), |
| 236 device_info_(device_info), | 236 device_info_(device_info), |
| 237 weak_factory_(this) {} | 237 weak_factory_(this) { |
| 238 device_.set_connection_error_handler([this]() { device_.reset(); }); | |
|
Ken Rockot(use gerrit already)
2016/03/02 01:54:19
BTW after thinking about our previous discussion m
| |
| 239 } | |
| 238 | 240 |
| 239 WebUSBDeviceImpl::~WebUSBDeviceImpl() {} | 241 WebUSBDeviceImpl::~WebUSBDeviceImpl() {} |
| 240 | 242 |
| 241 const blink::WebUSBDeviceInfo& WebUSBDeviceImpl::info() const { | 243 const blink::WebUSBDeviceInfo& WebUSBDeviceImpl::info() const { |
| 242 return device_info_; | 244 return device_info_; |
| 243 } | 245 } |
| 244 | 246 |
| 245 void WebUSBDeviceImpl::open(blink::WebUSBDeviceOpenCallbacks* callbacks) { | 247 void WebUSBDeviceImpl::open(blink::WebUSBDeviceOpenCallbacks* callbacks) { |
| 246 auto scoped_callbacks = MakeScopedUSBCallbacks(callbacks); | 248 auto scoped_callbacks = MakeScopedUSBCallbacks(callbacks); |
| 247 device_->Open(base::Bind(&OnOpenDevice, base::Passed(&scoped_callbacks))); | 249 if (device_) |
| 250 device_->Open(base::Bind(&OnOpenDevice, base::Passed(&scoped_callbacks))); | |
| 248 } | 251 } |
| 249 | 252 |
| 250 void WebUSBDeviceImpl::close(blink::WebUSBDeviceCloseCallbacks* callbacks) { | 253 void WebUSBDeviceImpl::close(blink::WebUSBDeviceCloseCallbacks* callbacks) { |
| 251 auto scoped_callbacks = MakeScopedUSBCallbacks(callbacks); | 254 auto scoped_callbacks = MakeScopedUSBCallbacks(callbacks); |
| 252 device_->Close(base::Bind(&OnDeviceClosed, base::Passed(&scoped_callbacks))); | 255 if (device_) |
| 256 device_->Close( | |
| 257 base::Bind(&OnDeviceClosed, base::Passed(&scoped_callbacks))); | |
| 253 } | 258 } |
| 254 | 259 |
| 255 void WebUSBDeviceImpl::getConfiguration( | 260 void WebUSBDeviceImpl::getConfiguration( |
| 256 blink::WebUSBDeviceGetConfigurationCallbacks* callbacks) { | 261 blink::WebUSBDeviceGetConfigurationCallbacks* callbacks) { |
| 257 auto scoped_callbacks = MakeScopedUSBCallbacks(callbacks); | 262 auto scoped_callbacks = MakeScopedUSBCallbacks(callbacks); |
| 258 device_->GetConfiguration( | 263 if (device_) |
| 259 base::Bind(&OnGetConfiguration, base::Passed(&scoped_callbacks))); | 264 device_->GetConfiguration( |
| 265 base::Bind(&OnGetConfiguration, base::Passed(&scoped_callbacks))); | |
| 260 } | 266 } |
| 261 | 267 |
| 262 void WebUSBDeviceImpl::setConfiguration( | 268 void WebUSBDeviceImpl::setConfiguration( |
| 263 uint8_t configuration_value, | 269 uint8_t configuration_value, |
| 264 blink::WebUSBDeviceSetConfigurationCallbacks* callbacks) { | 270 blink::WebUSBDeviceSetConfigurationCallbacks* callbacks) { |
| 265 auto scoped_callbacks = MakeScopedUSBCallbacks(callbacks); | 271 auto scoped_callbacks = MakeScopedUSBCallbacks(callbacks); |
| 266 device_->SetConfiguration( | 272 if (device_) |
| 267 configuration_value, | 273 device_->SetConfiguration( |
| 268 base::Bind(&HandlePassFailDeviceOperation, | 274 configuration_value, |
| 269 base::Passed(&scoped_callbacks), kSetConfigurationFailed)); | 275 base::Bind(&HandlePassFailDeviceOperation, |
| 276 base::Passed(&scoped_callbacks), kSetConfigurationFailed)); | |
| 270 } | 277 } |
| 271 | 278 |
| 272 void WebUSBDeviceImpl::claimInterface( | 279 void WebUSBDeviceImpl::claimInterface( |
| 273 uint8_t interface_number, | 280 uint8_t interface_number, |
| 274 blink::WebUSBDeviceClaimInterfaceCallbacks* callbacks) { | 281 blink::WebUSBDeviceClaimInterfaceCallbacks* callbacks) { |
| 275 auto scoped_callbacks = MakeScopedUSBCallbacks(callbacks); | 282 auto scoped_callbacks = MakeScopedUSBCallbacks(callbacks); |
| 276 device_->ClaimInterface( | 283 if (device_) |
| 277 interface_number, | 284 device_->ClaimInterface( |
| 278 base::Bind(&HandlePassFailDeviceOperation, | 285 interface_number, |
| 279 base::Passed(&scoped_callbacks), kClaimInterfaceFailed)); | 286 base::Bind(&HandlePassFailDeviceOperation, |
| 287 base::Passed(&scoped_callbacks), kClaimInterfaceFailed)); | |
| 280 } | 288 } |
| 281 | 289 |
| 282 void WebUSBDeviceImpl::releaseInterface( | 290 void WebUSBDeviceImpl::releaseInterface( |
| 283 uint8_t interface_number, | 291 uint8_t interface_number, |
| 284 blink::WebUSBDeviceReleaseInterfaceCallbacks* callbacks) { | 292 blink::WebUSBDeviceReleaseInterfaceCallbacks* callbacks) { |
| 285 auto scoped_callbacks = MakeScopedUSBCallbacks(callbacks); | 293 auto scoped_callbacks = MakeScopedUSBCallbacks(callbacks); |
| 286 device_->ReleaseInterface( | 294 if (device_) |
| 287 interface_number, | 295 device_->ReleaseInterface( |
| 288 base::Bind(&HandlePassFailDeviceOperation, | 296 interface_number, |
| 289 base::Passed(&scoped_callbacks), kReleaseInterfaceFailed)); | 297 base::Bind(&HandlePassFailDeviceOperation, |
| 298 base::Passed(&scoped_callbacks), kReleaseInterfaceFailed)); | |
| 290 } | 299 } |
| 291 | 300 |
| 292 void WebUSBDeviceImpl::setInterface( | 301 void WebUSBDeviceImpl::setInterface( |
| 293 uint8_t interface_number, | 302 uint8_t interface_number, |
| 294 uint8_t alternate_setting, | 303 uint8_t alternate_setting, |
| 295 blink::WebUSBDeviceSetInterfaceAlternateSettingCallbacks* callbacks) { | 304 blink::WebUSBDeviceSetInterfaceAlternateSettingCallbacks* callbacks) { |
| 296 auto scoped_callbacks = MakeScopedUSBCallbacks(callbacks); | 305 auto scoped_callbacks = MakeScopedUSBCallbacks(callbacks); |
| 297 device_->SetInterfaceAlternateSetting( | 306 if (device_) |
| 298 interface_number, alternate_setting, | 307 device_->SetInterfaceAlternateSetting( |
| 299 base::Bind(&HandlePassFailDeviceOperation, | 308 interface_number, alternate_setting, |
| 300 base::Passed(&scoped_callbacks), kSetInterfaceFailed)); | 309 base::Bind(&HandlePassFailDeviceOperation, |
| 310 base::Passed(&scoped_callbacks), kSetInterfaceFailed)); | |
| 301 } | 311 } |
| 302 | 312 |
| 303 void WebUSBDeviceImpl::clearHalt( | 313 void WebUSBDeviceImpl::clearHalt( |
| 304 uint8_t endpoint_number, | 314 uint8_t endpoint_number, |
| 305 blink::WebUSBDeviceClearHaltCallbacks* callbacks) { | 315 blink::WebUSBDeviceClearHaltCallbacks* callbacks) { |
| 306 auto scoped_callbacks = MakeScopedUSBCallbacks(callbacks); | 316 auto scoped_callbacks = MakeScopedUSBCallbacks(callbacks); |
| 307 device_->ClearHalt( | 317 if (device_) |
| 308 endpoint_number, | 318 device_->ClearHalt( |
| 309 base::Bind(&HandlePassFailDeviceOperation, | 319 endpoint_number, |
| 310 base::Passed(&scoped_callbacks), kClearHaltFailed)); | 320 base::Bind(&HandlePassFailDeviceOperation, |
| 321 base::Passed(&scoped_callbacks), kClearHaltFailed)); | |
| 311 } | 322 } |
| 312 | 323 |
| 313 void WebUSBDeviceImpl::controlTransfer( | 324 void WebUSBDeviceImpl::controlTransfer( |
| 314 const blink::WebUSBDevice::ControlTransferParameters& parameters, | 325 const blink::WebUSBDevice::ControlTransferParameters& parameters, |
| 315 uint8_t* data, | 326 uint8_t* data, |
| 316 size_t data_size, | 327 size_t data_size, |
| 317 unsigned int timeout, | 328 unsigned int timeout, |
| 318 blink::WebUSBDeviceTransferCallbacks* callbacks) { | 329 blink::WebUSBDeviceTransferCallbacks* callbacks) { |
| 319 auto scoped_callbacks = MakeScopedUSBCallbacks(callbacks); | 330 auto scoped_callbacks = MakeScopedUSBCallbacks(callbacks); |
| 331 if (!device_) | |
| 332 return; | |
| 333 | |
| 320 device::usb::ControlTransferParamsPtr params = | 334 device::usb::ControlTransferParamsPtr params = |
| 321 device::usb::ControlTransferParams::From(parameters); | 335 device::usb::ControlTransferParams::From(parameters); |
| 322 switch (parameters.direction) { | 336 switch (parameters.direction) { |
| 323 case WebUSBDevice::TransferDirection::In: | 337 case WebUSBDevice::TransferDirection::In: |
| 324 device_->ControlTransferIn( | 338 device_->ControlTransferIn( |
| 325 std::move(params), data_size, timeout, | 339 std::move(params), data_size, timeout, |
| 326 base::Bind(&OnTransferIn, base::Passed(&scoped_callbacks))); | 340 base::Bind(&OnTransferIn, base::Passed(&scoped_callbacks))); |
| 327 break; | 341 break; |
| 328 case WebUSBDevice::TransferDirection::Out: { | 342 case WebUSBDevice::TransferDirection::Out: { |
| 329 std::vector<uint8_t> bytes; | 343 std::vector<uint8_t> bytes; |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 343 } | 357 } |
| 344 | 358 |
| 345 void WebUSBDeviceImpl::transfer( | 359 void WebUSBDeviceImpl::transfer( |
| 346 blink::WebUSBDevice::TransferDirection direction, | 360 blink::WebUSBDevice::TransferDirection direction, |
| 347 uint8_t endpoint_number, | 361 uint8_t endpoint_number, |
| 348 uint8_t* data, | 362 uint8_t* data, |
| 349 size_t data_size, | 363 size_t data_size, |
| 350 unsigned int timeout, | 364 unsigned int timeout, |
| 351 blink::WebUSBDeviceTransferCallbacks* callbacks) { | 365 blink::WebUSBDeviceTransferCallbacks* callbacks) { |
| 352 auto scoped_callbacks = MakeScopedUSBCallbacks(callbacks); | 366 auto scoped_callbacks = MakeScopedUSBCallbacks(callbacks); |
| 367 if (!device_) | |
| 368 return; | |
| 369 | |
| 353 switch (direction) { | 370 switch (direction) { |
| 354 case WebUSBDevice::TransferDirection::In: | 371 case WebUSBDevice::TransferDirection::In: |
| 355 device_->GenericTransferIn( | 372 device_->GenericTransferIn( |
| 356 endpoint_number, data_size, timeout, | 373 endpoint_number, data_size, timeout, |
| 357 base::Bind(&OnTransferIn, base::Passed(&scoped_callbacks))); | 374 base::Bind(&OnTransferIn, base::Passed(&scoped_callbacks))); |
| 358 break; | 375 break; |
| 359 case WebUSBDevice::TransferDirection::Out: { | 376 case WebUSBDevice::TransferDirection::Out: { |
| 360 std::vector<uint8_t> bytes; | 377 std::vector<uint8_t> bytes; |
| 361 if (data) | 378 if (data) |
| 362 bytes.assign(data, data + data_size); | 379 bytes.assign(data, data + data_size); |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 375 | 392 |
| 376 void WebUSBDeviceImpl::isochronousTransfer( | 393 void WebUSBDeviceImpl::isochronousTransfer( |
| 377 blink::WebUSBDevice::TransferDirection direction, | 394 blink::WebUSBDevice::TransferDirection direction, |
| 378 uint8_t endpoint_number, | 395 uint8_t endpoint_number, |
| 379 uint8_t* data, | 396 uint8_t* data, |
| 380 size_t data_size, | 397 size_t data_size, |
| 381 blink::WebVector<uint32_t> packet_lengths, | 398 blink::WebVector<uint32_t> packet_lengths, |
| 382 unsigned int timeout, | 399 unsigned int timeout, |
| 383 blink::WebUSBDeviceTransferCallbacks* callbacks) { | 400 blink::WebUSBDeviceTransferCallbacks* callbacks) { |
| 384 auto scoped_callbacks = MakeScopedUSBCallbacks(callbacks); | 401 auto scoped_callbacks = MakeScopedUSBCallbacks(callbacks); |
| 402 if (!device_) | |
| 403 return; | |
| 404 | |
| 385 switch (direction) { | 405 switch (direction) { |
| 386 case WebUSBDevice::TransferDirection::In: | 406 case WebUSBDevice::TransferDirection::In: |
| 387 device_->IsochronousTransferIn( | 407 device_->IsochronousTransferIn( |
| 388 endpoint_number, mojo::Array<uint32_t>::From(packet_lengths), timeout, | 408 endpoint_number, mojo::Array<uint32_t>::From(packet_lengths), timeout, |
| 389 base::Bind(&OnIsochronousTransferIn, | 409 base::Bind(&OnIsochronousTransferIn, |
| 390 base::Passed(&scoped_callbacks))); | 410 base::Passed(&scoped_callbacks))); |
| 391 break; | 411 break; |
| 392 case WebUSBDevice::TransferDirection::Out: { | 412 case WebUSBDevice::TransferDirection::Out: { |
| 393 std::vector<uint8_t> bytes; | 413 std::vector<uint8_t> bytes; |
| 394 if (data) | 414 if (data) |
| 395 bytes.assign(data, data + data_size); | 415 bytes.assign(data, data + data_size); |
| 396 mojo::Array<uint8_t> mojo_bytes; | 416 mojo::Array<uint8_t> mojo_bytes; |
| 397 mojo_bytes.Swap(&bytes); | 417 mojo_bytes.Swap(&bytes); |
| 398 device_->IsochronousTransferOut( | 418 device_->IsochronousTransferOut( |
| 399 endpoint_number, std::move(mojo_bytes), | 419 endpoint_number, std::move(mojo_bytes), |
| 400 mojo::Array<uint32_t>::From(packet_lengths), timeout, | 420 mojo::Array<uint32_t>::From(packet_lengths), timeout, |
| 401 base::Bind(&OnIsochronousTransferOut, | 421 base::Bind(&OnIsochronousTransferOut, |
| 402 base::Passed(&scoped_callbacks))); | 422 base::Passed(&scoped_callbacks))); |
| 403 break; | 423 break; |
| 404 } | 424 } |
| 405 default: | 425 default: |
| 406 NOTREACHED(); | 426 NOTREACHED(); |
| 407 } | 427 } |
| 408 } | 428 } |
| 409 | 429 |
| 410 void WebUSBDeviceImpl::reset(blink::WebUSBDeviceResetCallbacks* callbacks) { | 430 void WebUSBDeviceImpl::reset(blink::WebUSBDeviceResetCallbacks* callbacks) { |
| 411 auto scoped_callbacks = MakeScopedUSBCallbacks(callbacks); | 431 auto scoped_callbacks = MakeScopedUSBCallbacks(callbacks); |
| 412 device_->Reset(base::Bind(&HandlePassFailDeviceOperation, | 432 if (device_) |
| 413 base::Passed(&scoped_callbacks), | 433 device_->Reset(base::Bind(&HandlePassFailDeviceOperation, |
| 414 kDeviceResetFailed)); | 434 base::Passed(&scoped_callbacks), |
| 435 kDeviceResetFailed)); | |
| 415 } | 436 } |
| 416 | 437 |
| 417 } // namespace content | 438 } // namespace content |
| OLD | NEW |