| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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/adb/android_usb_device.h" | 5 #include "chrome/browser/devtools/adb/android_usb_device.h" |
| 6 | 6 |
| 7 #include <set> | 7 #include <set> |
| 8 | 8 |
| 9 #include "base/base64.h" | 9 #include "base/base64.h" |
| 10 #include "base/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
| 11 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
| 12 #include "base/stl_util.h" |
| 12 #include "base/strings/stringprintf.h" | 13 #include "base/strings/stringprintf.h" |
| 13 #include "chrome/browser/devtools/adb/android_rsa.h" | 14 #include "chrome/browser/devtools/adb/android_rsa.h" |
| 14 #include "chrome/browser/devtools/adb/android_usb_socket.h" | 15 #include "chrome/browser/devtools/adb/android_usb_socket.h" |
| 15 #include "chrome/browser/usb/usb_interface.h" | 16 #include "chrome/browser/usb/usb_interface.h" |
| 16 #include "chrome/browser/usb/usb_service.h" | 17 #include "chrome/browser/usb/usb_service.h" |
| 17 #include "chrome/browser/usb/usb_service_factory.h" | 18 #include "chrome/browser/usb/usb_service_factory.h" |
| 18 #include "crypto/rsa_private_key.h" | 19 #include "crypto/rsa_private_key.h" |
| 19 #include "net/base/ip_endpoint.h" | 20 #include "net/base/ip_endpoint.h" |
| 20 #include "net/base/net_errors.h" | 21 #include "net/base/net_errors.h" |
| 21 #include "net/socket/stream_socket.h" | 22 #include "net/socket/stream_socket.h" |
| 22 | 23 |
| 23 namespace { | 24 namespace { |
| 24 | 25 |
| 25 void Noop() {} | 26 void Noop() {} |
| 26 void BoolNoop(bool success) {} | 27 void BoolNoop(bool success) {} |
| 27 | 28 |
| 28 const size_t kHeaderSize = 24; | 29 const size_t kHeaderSize = 24; |
| 29 | 30 |
| 30 const int kAdbClass = 0xff; | 31 const int kAdbClass = 0xff; |
| 31 const int kAdbSubclass = 0x42; | 32 const int kAdbSubclass = 0x42; |
| 32 const int kAdbProtocol = 0x1; | 33 const int kAdbProtocol = 0x1; |
| 33 | 34 |
| 34 const int kUsbTimeout = 0; | 35 const int kUsbTimeout = 0; |
| 35 | 36 |
| 36 const uint32 kMaxPayload = 4096; | 37 const uint32 kMaxPayload = 4096; |
| 37 const uint32 kVersion = 0x01000000; | 38 const uint32 kVersion = 0x01000000; |
| 38 | 39 |
| 39 static const char kHostConnectMessage[] = "host::"; | 40 static const char kHostConnectMessage[] = "host::"; |
| 40 | 41 |
| 41 typedef std::vector<scoped_refptr<UsbDevice> > UsbDevices; | |
| 42 | |
| 43 base::LazyInstance<AndroidUsbDevices>::Leaky g_devices = | 42 base::LazyInstance<AndroidUsbDevices>::Leaky g_devices = |
| 44 LAZY_INSTANCE_INITIALIZER; | 43 LAZY_INSTANCE_INITIALIZER; |
| 45 | 44 |
| 46 static std::string ReadSerialNumSync(libusb_device_handle* handle) { | 45 static std::string ReadSerialNumSync(libusb_device_handle* handle) { |
| 47 libusb_device* device = libusb_get_device(handle); | 46 libusb_device* device = libusb_get_device(handle); |
| 48 libusb_device_descriptor descriptor; | 47 libusb_device_descriptor descriptor; |
| 49 if (libusb_get_device_descriptor(device, &descriptor) != LIBUSB_SUCCESS) | 48 if (libusb_get_device_descriptor(device, &descriptor) != LIBUSB_SUCCESS) |
| 50 return std::string(); | 49 return std::string(); |
| 51 | 50 |
| 52 if (!descriptor.iSerialNumber) | 51 if (!descriptor.iSerialNumber) |
| (...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 205 | 204 |
| 206 AdbMessage::~AdbMessage() { | 205 AdbMessage::~AdbMessage() { |
| 207 } | 206 } |
| 208 | 207 |
| 209 // static | 208 // static |
| 210 void AndroidUsbDevice::Enumerate(Profile* profile, | 209 void AndroidUsbDevice::Enumerate(Profile* profile, |
| 211 crypto::RSAPrivateKey* rsa_key, | 210 crypto::RSAPrivateKey* rsa_key, |
| 212 AndroidUsbDevices* devices) { | 211 AndroidUsbDevices* devices) { |
| 213 UsbService* service = | 212 UsbService* service = |
| 214 UsbServiceFactory::GetInstance()->GetForProfile(profile); | 213 UsbServiceFactory::GetInstance()->GetForProfile(profile); |
| 215 UsbDevices usb_devices; | 214 |
| 216 service->EnumerateDevices(&usb_devices); | 215 std::vector<int> usb_devices; |
| 216 service->GetDevices(&usb_devices); |
| 217 | 217 |
| 218 // GC Android devices with no actual usb device. | 218 // GC Android devices with no actual usb device. |
| 219 AndroidUsbDevices::iterator it = g_devices.Get().begin(); | 219 AndroidUsbDevices::iterator it = g_devices.Get().begin(); |
| 220 std::set<UsbDevice*> claimed_devices; | 220 std::set<int> claimed_devices; |
| 221 while (it != g_devices.Get().end()) { | 221 while (it != g_devices.Get().end()) { |
| 222 bool found_device = false; | 222 bool found_device = false; |
| 223 for (UsbDevices::iterator it2 = usb_devices.begin(); | 223 for (size_t i = 0; i < usb_devices.size(); ++i) { |
| 224 it2 != usb_devices.end() && !found_device; ++it2) { | 224 int usb_device_id = usb_devices[i]; |
| 225 UsbDevice* usb_device = it2->get(); | |
| 226 AndroidUsbDevice* device = it->get(); | 225 AndroidUsbDevice* device = it->get(); |
| 227 if (usb_device == device->usb_device_) { | 226 if (usb_device_id == device->usb_device_->device()) { |
| 228 found_device = true; | 227 found_device = true; |
| 229 claimed_devices.insert(*it2); | 228 claimed_devices.insert(usb_device_id); |
| 230 } | 229 } |
| 231 } | 230 } |
| 232 | 231 |
| 233 if (!found_device) | 232 if (!found_device) |
| 234 it = g_devices.Get().erase(it); | 233 it = g_devices.Get().erase(it); |
| 235 else | 234 else |
| 236 ++it; | 235 ++it; |
| 237 } | 236 } |
| 238 | 237 // service->OpenDevice(usb_device_id) |
| 239 // Add new devices. | 238 // Add new devices. |
| 240 AndroidUsbDevices new_devices; | 239 AndroidUsbDevices new_devices; |
| 241 for (UsbDevices::iterator it = usb_devices.begin(); it != usb_devices.end(); | 240 for (size_t i = 0; i < usb_devices.size(); ++i) { |
| 242 ++it) { | 241 if (ContainsKey(claimed_devices, usb_devices[i])) |
| 243 UsbDevice* usb_device = *it; | |
| 244 if (claimed_devices.find(usb_device) != claimed_devices.end()) | |
| 245 continue; | 242 continue; |
| 243 scoped_refptr<UsbDevice> usb_device = service->OpenDevice(usb_devices[i]); |
| 246 scoped_refptr<UsbConfigDescriptor> config = new UsbConfigDescriptor(); | 244 scoped_refptr<UsbConfigDescriptor> config = new UsbConfigDescriptor(); |
| 247 usb_device->ListInterfaces(config.get(), base::Bind(&BoolNoop)); | 245 usb_device->ListInterfaces(config.get(), base::Bind(&BoolNoop)); |
| 248 for (size_t j = 0; j < config->GetNumInterfaces(); ++j) { | 246 for (size_t j = 0; j < config->GetNumInterfaces(); ++j) { |
| 249 ClaimInterface(rsa_key, usb_device, config->GetInterface(j), | 247 ClaimInterface(rsa_key, usb_device, config->GetInterface(j), |
| 250 &g_devices.Get()); | 248 &g_devices.Get()); |
| 251 } | 249 } |
| 252 } | 250 } |
| 253 | 251 |
| 254 *devices = g_devices.Get(); | 252 *devices = g_devices.Get(); |
| 255 } | 253 } |
| (...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 522 it->second->Terminated(); | 520 it->second->Terminated(); |
| 523 } | 521 } |
| 524 | 522 |
| 525 usb_device_->ReleaseInterface(1, base::Bind(&BoolNoop)); | 523 usb_device_->ReleaseInterface(1, base::Bind(&BoolNoop)); |
| 526 usb_device_->Close(base::Bind(&Noop)); | 524 usb_device_->Close(base::Bind(&Noop)); |
| 527 } | 525 } |
| 528 | 526 |
| 529 void AndroidUsbDevice::SocketDeleted(uint32 socket_id) { | 527 void AndroidUsbDevice::SocketDeleted(uint32 socket_id) { |
| 530 sockets_.erase(socket_id); | 528 sockets_.erase(socket_id); |
| 531 } | 529 } |
| OLD | NEW |