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 "chrome/browser/devtools/device/usb/android_usb_device.h" | 5 #include "chrome/browser/devtools/device/usb/android_usb_device.h" |
6 | 6 |
7 #include <set> | 7 #include <set> |
8 | 8 |
9 #include "base/barrier_closure.h" | 9 #include "base/barrier_closure.h" |
10 #include "base/base64.h" | 10 #include "base/base64.h" |
11 #include "base/lazy_instance.h" | 11 #include "base/lazy_instance.h" |
12 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
13 #include "base/stl_util.h" | 13 #include "base/stl_util.h" |
14 #include "base/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
15 #include "base/strings/stringprintf.h" | 15 #include "base/strings/stringprintf.h" |
16 #include "base/strings/utf_string_conversions.h" | 16 #include "base/strings/utf_string_conversions.h" |
17 #include "chrome/browser/devtools/device/usb/android_rsa.h" | 17 #include "chrome/browser/devtools/device/usb/android_rsa.h" |
18 #include "chrome/browser/devtools/device/usb/android_usb_socket.h" | 18 #include "chrome/browser/devtools/device/usb/android_usb_socket.h" |
19 #include "components/usb_service/usb_device.h" | |
20 #include "components/usb_service/usb_interface.h" | |
21 #include "components/usb_service/usb_service.h" | |
22 #include "content/public/browser/browser_thread.h" | 19 #include "content/public/browser/browser_thread.h" |
23 #include "crypto/rsa_private_key.h" | 20 #include "crypto/rsa_private_key.h" |
24 #include "device/core/device_client.h" | 21 #include "device/core/device_client.h" |
| 22 #include "device/usb/usb_device.h" |
| 23 #include "device/usb/usb_interface.h" |
| 24 #include "device/usb/usb_service.h" |
25 #include "net/base/ip_endpoint.h" | 25 #include "net/base/ip_endpoint.h" |
26 #include "net/base/net_errors.h" | 26 #include "net/base/net_errors.h" |
27 #include "net/socket/stream_socket.h" | 27 #include "net/socket/stream_socket.h" |
28 | 28 |
29 using usb_service::UsbConfigDescriptor; | 29 using device::UsbConfigDescriptor; |
30 using usb_service::UsbDevice; | 30 using device::UsbDevice; |
31 using usb_service::UsbDeviceHandle; | 31 using device::UsbDeviceHandle; |
32 using usb_service::UsbInterfaceAltSettingDescriptor; | 32 using device::UsbInterfaceAltSettingDescriptor; |
33 using usb_service::UsbInterfaceDescriptor; | 33 using device::UsbInterfaceDescriptor; |
34 using usb_service::UsbEndpointDescriptor; | 34 using device::UsbEndpointDescriptor; |
35 using usb_service::UsbService; | 35 using device::UsbService; |
36 using usb_service::UsbTransferStatus; | 36 using device::UsbTransferStatus; |
37 | 37 |
38 namespace { | 38 namespace { |
39 | 39 |
40 const size_t kHeaderSize = 24; | 40 const size_t kHeaderSize = 24; |
41 | 41 |
42 const int kAdbClass = 0xff; | 42 const int kAdbClass = 0xff; |
43 const int kAdbSubclass = 0x42; | 43 const int kAdbSubclass = 0x42; |
44 const int kAdbProtocol = 0x1; | 44 const int kAdbProtocol = 0x1; |
45 | 45 |
46 const int kUsbTimeout = 0; | 46 const int kUsbTimeout = 0; |
47 | 47 |
48 const uint32 kMaxPayload = 4096; | 48 const uint32 kMaxPayload = 4096; |
49 const uint32 kVersion = 0x01000000; | 49 const uint32 kVersion = 0x01000000; |
50 | 50 |
51 static const char kHostConnectMessage[] = "host::"; | 51 static const char kHostConnectMessage[] = "host::"; |
52 | 52 |
53 using content::BrowserThread; | 53 using content::BrowserThread; |
54 | 54 |
55 typedef std::vector<scoped_refptr<UsbDevice> > UsbDevices; | 55 typedef std::vector<scoped_refptr<UsbDevice> > UsbDevices; |
56 typedef std::set<scoped_refptr<UsbDevice> > UsbDeviceSet; | 56 typedef std::set<scoped_refptr<UsbDevice> > UsbDeviceSet; |
57 | 57 |
58 // Stores android wrappers around claimed usb devices on caller thread. | 58 // Stores android wrappers around claimed usb devices on caller thread. |
59 base::LazyInstance<std::vector<AndroidUsbDevice*> >::Leaky g_devices = | 59 base::LazyInstance<std::vector<AndroidUsbDevice*> >::Leaky g_devices = |
60 LAZY_INSTANCE_INITIALIZER; | 60 LAZY_INSTANCE_INITIALIZER; |
61 | 61 |
62 bool IsAndroidInterface( | 62 bool IsAndroidInterface(scoped_refptr<const UsbInterfaceDescriptor> interface) { |
63 scoped_refptr<const usb_service::UsbInterfaceDescriptor> interface) { | |
64 if (interface->GetNumAltSettings() == 0) | 63 if (interface->GetNumAltSettings() == 0) |
65 return false; | 64 return false; |
66 | 65 |
67 scoped_refptr<const UsbInterfaceAltSettingDescriptor> idesc = | 66 scoped_refptr<const UsbInterfaceAltSettingDescriptor> idesc = |
68 interface->GetAltSetting(0); | 67 interface->GetAltSetting(0); |
69 | 68 |
70 if (idesc->GetInterfaceClass() != kAdbClass || | 69 if (idesc->GetInterfaceClass() != kAdbClass || |
71 idesc->GetInterfaceSubclass() != kAdbSubclass || | 70 idesc->GetInterfaceSubclass() != kAdbSubclass || |
72 idesc->GetInterfaceProtocol() != kAdbProtocol || | 71 idesc->GetInterfaceProtocol() != kAdbProtocol || |
73 idesc->GetNumEndpoints() != 2) { | 72 idesc->GetNumEndpoints() != 2) { |
(...skipping 10 matching lines...) Expand all Loading... |
84 scoped_refptr<const UsbInterfaceAltSettingDescriptor> idesc = | 83 scoped_refptr<const UsbInterfaceAltSettingDescriptor> idesc = |
85 interface->GetAltSetting(0); | 84 interface->GetAltSetting(0); |
86 | 85 |
87 int inbound_address = 0; | 86 int inbound_address = 0; |
88 int outbound_address = 0; | 87 int outbound_address = 0; |
89 int zero_mask = 0; | 88 int zero_mask = 0; |
90 | 89 |
91 for (size_t i = 0; i < idesc->GetNumEndpoints(); ++i) { | 90 for (size_t i = 0; i < idesc->GetNumEndpoints(); ++i) { |
92 scoped_refptr<const UsbEndpointDescriptor> edesc = | 91 scoped_refptr<const UsbEndpointDescriptor> edesc = |
93 idesc->GetEndpoint(i); | 92 idesc->GetEndpoint(i); |
94 if (edesc->GetTransferType() != usb_service::USB_TRANSFER_BULK) | 93 if (edesc->GetTransferType() != device::USB_TRANSFER_BULK) |
95 continue; | 94 continue; |
96 if (edesc->GetDirection() == usb_service::USB_DIRECTION_INBOUND) | 95 if (edesc->GetDirection() == device::USB_DIRECTION_INBOUND) |
97 inbound_address = edesc->GetAddress(); | 96 inbound_address = edesc->GetAddress(); |
98 else | 97 else |
99 outbound_address = edesc->GetAddress(); | 98 outbound_address = edesc->GetAddress(); |
100 zero_mask = edesc->GetMaximumPacketSize() - 1; | 99 zero_mask = edesc->GetMaximumPacketSize() - 1; |
101 } | 100 } |
102 | 101 |
103 if (inbound_address == 0 || outbound_address == 0) | 102 if (inbound_address == 0 || outbound_address == 0) |
104 return NULL; | 103 return NULL; |
105 | 104 |
106 if (!usb_handle->ClaimInterface(interface_id)) | 105 if (!usb_handle->ClaimInterface(interface_id)) |
(...skipping 317 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
424 | 423 |
425 void AndroidUsbDevice::ProcessOutgoing() { | 424 void AndroidUsbDevice::ProcessOutgoing() { |
426 DCHECK(message_loop_ == base::MessageLoop::current()); | 425 DCHECK(message_loop_ == base::MessageLoop::current()); |
427 | 426 |
428 if (outgoing_queue_.empty() || !usb_handle_.get()) | 427 if (outgoing_queue_.empty() || !usb_handle_.get()) |
429 return; | 428 return; |
430 | 429 |
431 BulkMessage message = outgoing_queue_.front(); | 430 BulkMessage message = outgoing_queue_.front(); |
432 outgoing_queue_.pop(); | 431 outgoing_queue_.pop(); |
433 DumpMessage(true, message->data(), message->size()); | 432 DumpMessage(true, message->data(), message->size()); |
434 usb_handle_->BulkTransfer(usb_service::USB_DIRECTION_OUTBOUND, | 433 usb_handle_->BulkTransfer(device::USB_DIRECTION_OUTBOUND, |
435 outbound_address_, | 434 outbound_address_, |
436 message.get(), | 435 message.get(), |
437 message->size(), | 436 message->size(), |
438 kUsbTimeout, | 437 kUsbTimeout, |
439 base::Bind(&AndroidUsbDevice::OutgoingMessageSent, | 438 base::Bind(&AndroidUsbDevice::OutgoingMessageSent, |
440 weak_factory_.GetWeakPtr())); | 439 weak_factory_.GetWeakPtr())); |
441 } | 440 } |
442 | 441 |
443 void AndroidUsbDevice::OutgoingMessageSent(UsbTransferStatus status, | 442 void AndroidUsbDevice::OutgoingMessageSent(UsbTransferStatus status, |
444 scoped_refptr<net::IOBuffer> buffer, | 443 scoped_refptr<net::IOBuffer> buffer, |
445 size_t result) { | 444 size_t result) { |
446 DCHECK(message_loop_ == base::MessageLoop::current()); | 445 DCHECK(message_loop_ == base::MessageLoop::current()); |
447 | 446 |
448 if (status != usb_service::USB_TRANSFER_COMPLETED) | 447 if (status != device::USB_TRANSFER_COMPLETED) |
449 return; | 448 return; |
450 message_loop_->PostTask(FROM_HERE, | 449 message_loop_->PostTask(FROM_HERE, |
451 base::Bind(&AndroidUsbDevice::ProcessOutgoing, this)); | 450 base::Bind(&AndroidUsbDevice::ProcessOutgoing, this)); |
452 } | 451 } |
453 | 452 |
454 void AndroidUsbDevice::ReadHeader() { | 453 void AndroidUsbDevice::ReadHeader() { |
455 DCHECK(message_loop_ == base::MessageLoop::current()); | 454 DCHECK(message_loop_ == base::MessageLoop::current()); |
456 | 455 |
457 if (!usb_handle_.get()) | 456 if (!usb_handle_.get()) |
458 return; | 457 return; |
459 scoped_refptr<net::IOBuffer> buffer = new net::IOBuffer(kHeaderSize); | 458 scoped_refptr<net::IOBuffer> buffer = new net::IOBuffer(kHeaderSize); |
460 usb_handle_->BulkTransfer( | 459 usb_handle_->BulkTransfer( |
461 usb_service::USB_DIRECTION_INBOUND, | 460 device::USB_DIRECTION_INBOUND, |
462 inbound_address_, | 461 inbound_address_, |
463 buffer.get(), | 462 buffer.get(), |
464 kHeaderSize, | 463 kHeaderSize, |
465 kUsbTimeout, | 464 kUsbTimeout, |
466 base::Bind(&AndroidUsbDevice::ParseHeader, weak_factory_.GetWeakPtr())); | 465 base::Bind(&AndroidUsbDevice::ParseHeader, weak_factory_.GetWeakPtr())); |
467 } | 466 } |
468 | 467 |
469 void AndroidUsbDevice::ParseHeader(UsbTransferStatus status, | 468 void AndroidUsbDevice::ParseHeader(UsbTransferStatus status, |
470 scoped_refptr<net::IOBuffer> buffer, | 469 scoped_refptr<net::IOBuffer> buffer, |
471 size_t result) { | 470 size_t result) { |
472 DCHECK(message_loop_ == base::MessageLoop::current()); | 471 DCHECK(message_loop_ == base::MessageLoop::current()); |
473 | 472 |
474 if (status == usb_service::USB_TRANSFER_TIMEOUT) { | 473 if (status == device::USB_TRANSFER_TIMEOUT) { |
475 message_loop_->PostTask(FROM_HERE, | 474 message_loop_->PostTask(FROM_HERE, |
476 base::Bind(&AndroidUsbDevice::ReadHeader, this)); | 475 base::Bind(&AndroidUsbDevice::ReadHeader, this)); |
477 return; | 476 return; |
478 } | 477 } |
479 | 478 |
480 if (status != usb_service::USB_TRANSFER_COMPLETED || result != kHeaderSize) { | 479 if (status != device::USB_TRANSFER_COMPLETED || result != kHeaderSize) { |
481 TransferError(status); | 480 TransferError(status); |
482 return; | 481 return; |
483 } | 482 } |
484 | 483 |
485 DumpMessage(false, buffer->data(), result); | 484 DumpMessage(false, buffer->data(), result); |
486 std::vector<uint32> header(6); | 485 std::vector<uint32> header(6); |
487 memcpy(&header[0], buffer->data(), result); | 486 memcpy(&header[0], buffer->data(), result); |
488 scoped_refptr<AdbMessage> message = | 487 scoped_refptr<AdbMessage> message = |
489 new AdbMessage(header[0], header[1], header[2], ""); | 488 new AdbMessage(header[0], header[1], header[2], ""); |
490 uint32 data_length = header[3]; | 489 uint32 data_length = header[3]; |
491 uint32 data_check = header[4]; | 490 uint32 data_check = header[4]; |
492 uint32 magic = header[5]; | 491 uint32 magic = header[5]; |
493 if ((message->command ^ 0xffffffff) != magic) { | 492 if ((message->command ^ 0xffffffff) != magic) { |
494 TransferError(usb_service::USB_TRANSFER_ERROR); | 493 TransferError(device::USB_TRANSFER_ERROR); |
495 return; | 494 return; |
496 } | 495 } |
497 | 496 |
498 if (data_length == 0) { | 497 if (data_length == 0) { |
499 message_loop_->PostTask(FROM_HERE, | 498 message_loop_->PostTask(FROM_HERE, |
500 base::Bind(&AndroidUsbDevice::HandleIncoming, this, | 499 base::Bind(&AndroidUsbDevice::HandleIncoming, this, |
501 message)); | 500 message)); |
502 return; | 501 return; |
503 } | 502 } |
504 | 503 |
505 message_loop_->PostTask(FROM_HERE, | 504 message_loop_->PostTask(FROM_HERE, |
506 base::Bind(&AndroidUsbDevice::ReadBody, this, | 505 base::Bind(&AndroidUsbDevice::ReadBody, this, |
507 message, data_length, data_check)); | 506 message, data_length, data_check)); |
508 } | 507 } |
509 | 508 |
510 void AndroidUsbDevice::ReadBody(scoped_refptr<AdbMessage> message, | 509 void AndroidUsbDevice::ReadBody(scoped_refptr<AdbMessage> message, |
511 uint32 data_length, | 510 uint32 data_length, |
512 uint32 data_check) { | 511 uint32 data_check) { |
513 DCHECK(message_loop_ == base::MessageLoop::current()); | 512 DCHECK(message_loop_ == base::MessageLoop::current()); |
514 | 513 |
515 if (!usb_handle_.get()) | 514 if (!usb_handle_.get()) |
516 return; | 515 return; |
517 scoped_refptr<net::IOBuffer> buffer = new net::IOBuffer(data_length); | 516 scoped_refptr<net::IOBuffer> buffer = new net::IOBuffer(data_length); |
518 usb_handle_->BulkTransfer(usb_service::USB_DIRECTION_INBOUND, | 517 usb_handle_->BulkTransfer(device::USB_DIRECTION_INBOUND, |
519 inbound_address_, | 518 inbound_address_, |
520 buffer.get(), | 519 buffer.get(), |
521 data_length, | 520 data_length, |
522 kUsbTimeout, | 521 kUsbTimeout, |
523 base::Bind(&AndroidUsbDevice::ParseBody, | 522 base::Bind(&AndroidUsbDevice::ParseBody, |
524 weak_factory_.GetWeakPtr(), | 523 weak_factory_.GetWeakPtr(), |
525 message, | 524 message, |
526 data_length, | 525 data_length, |
527 data_check)); | 526 data_check)); |
528 } | 527 } |
529 | 528 |
530 void AndroidUsbDevice::ParseBody(scoped_refptr<AdbMessage> message, | 529 void AndroidUsbDevice::ParseBody(scoped_refptr<AdbMessage> message, |
531 uint32 data_length, | 530 uint32 data_length, |
532 uint32 data_check, | 531 uint32 data_check, |
533 UsbTransferStatus status, | 532 UsbTransferStatus status, |
534 scoped_refptr<net::IOBuffer> buffer, | 533 scoped_refptr<net::IOBuffer> buffer, |
535 size_t result) { | 534 size_t result) { |
536 DCHECK(message_loop_ == base::MessageLoop::current()); | 535 DCHECK(message_loop_ == base::MessageLoop::current()); |
537 | 536 |
538 if (status == usb_service::USB_TRANSFER_TIMEOUT) { | 537 if (status == device::USB_TRANSFER_TIMEOUT) { |
539 message_loop_->PostTask(FROM_HERE, | 538 message_loop_->PostTask(FROM_HERE, |
540 base::Bind(&AndroidUsbDevice::ReadBody, this, | 539 base::Bind(&AndroidUsbDevice::ReadBody, this, |
541 message, data_length, data_check)); | 540 message, data_length, data_check)); |
542 return; | 541 return; |
543 } | 542 } |
544 | 543 |
545 if (status != usb_service::USB_TRANSFER_COMPLETED || | 544 if (status != device::USB_TRANSFER_COMPLETED || |
546 static_cast<uint32>(result) != data_length) { | 545 static_cast<uint32>(result) != data_length) { |
547 TransferError(status); | 546 TransferError(status); |
548 return; | 547 return; |
549 } | 548 } |
550 | 549 |
551 DumpMessage(false, buffer->data(), data_length); | 550 DumpMessage(false, buffer->data(), data_length); |
552 message->body = std::string(buffer->data(), result); | 551 message->body = std::string(buffer->data(), result); |
553 if (Checksum(message->body) != data_check) { | 552 if (Checksum(message->body) != data_check) { |
554 TransferError(usb_service::USB_TRANSFER_ERROR); | 553 TransferError(device::USB_TRANSFER_ERROR); |
555 return; | 554 return; |
556 } | 555 } |
557 | 556 |
558 message_loop_->PostTask(FROM_HERE, | 557 message_loop_->PostTask(FROM_HERE, |
559 base::Bind(&AndroidUsbDevice::HandleIncoming, this, | 558 base::Bind(&AndroidUsbDevice::HandleIncoming, this, |
560 message)); | 559 message)); |
561 } | 560 } |
562 | 561 |
563 void AndroidUsbDevice::HandleIncoming(scoped_refptr<AdbMessage> message) { | 562 void AndroidUsbDevice::HandleIncoming(scoped_refptr<AdbMessage> message) { |
564 DCHECK(message_loop_ == base::MessageLoop::current()); | 563 DCHECK(message_loop_ == base::MessageLoop::current()); |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
655 BrowserThread::PostTask( | 654 BrowserThread::PostTask( |
656 BrowserThread::FILE, FROM_HERE, | 655 BrowserThread::FILE, FROM_HERE, |
657 base::Bind(&ReleaseInterface, usb_handle, interface_id_)); | 656 base::Bind(&ReleaseInterface, usb_handle, interface_id_)); |
658 } | 657 } |
659 | 658 |
660 void AndroidUsbDevice::SocketDeleted(uint32 socket_id) { | 659 void AndroidUsbDevice::SocketDeleted(uint32 socket_id) { |
661 DCHECK(message_loop_ == base::MessageLoop::current()); | 660 DCHECK(message_loop_ == base::MessageLoop::current()); |
662 | 661 |
663 sockets_.erase(socket_id); | 662 sockets_.erase(socket_id); |
664 } | 663 } |
OLD | NEW |