OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "device/usb/usb_device_android.h" |
| 6 |
| 7 #include "base/android/build_info.h" |
| 8 #include "base/android/jni_string.h" |
| 9 #include "base/bind.h" |
| 10 #include "base/location.h" |
| 11 #include "base/thread_task_runner_handle.h" |
| 12 #include "device/usb/usb_configuration_android.h" |
| 13 #include "device/usb/usb_device_handle.h" |
| 14 #include "device/usb/usb_interface_android.h" |
| 15 #include "jni/ChromeUsbDevice_jni.h" |
| 16 |
| 17 using base::android::AttachCurrentThread; |
| 18 using base::android::ConvertJavaStringToUTF16; |
| 19 using base::android::JavaRef; |
| 20 using base::android::ScopedJavaLocalRef; |
| 21 |
| 22 namespace device { |
| 23 |
| 24 // static |
| 25 bool UsbDeviceAndroid::RegisterJNI(JNIEnv* env) { |
| 26 return RegisterNativesImpl(env); // Generated in ChromeUsbDevice_jni.h |
| 27 } |
| 28 |
| 29 // static |
| 30 scoped_refptr<UsbDeviceAndroid> UsbDeviceAndroid::Create( |
| 31 JNIEnv* env, |
| 32 const JavaRef<jobject>& usb_device) { |
| 33 ScopedJavaLocalRef<jobject> wrapper = |
| 34 Java_ChromeUsbDevice_create(env, usb_device.obj()); |
| 35 uint16_t vendor_id = Java_ChromeUsbDevice_getVendorId(env, wrapper.obj()); |
| 36 uint16_t product_id = Java_ChromeUsbDevice_getProductId(env, wrapper.obj()); |
| 37 ScopedJavaLocalRef<jstring> manufacturer_string = |
| 38 Java_ChromeUsbDevice_getManufacturerName(env, wrapper.obj()); |
| 39 ScopedJavaLocalRef<jstring> product_string = |
| 40 Java_ChromeUsbDevice_getProductName(env, wrapper.obj()); |
| 41 ScopedJavaLocalRef<jstring> serial_number = |
| 42 Java_ChromeUsbDevice_getSerialNumber(env, wrapper.obj()); |
| 43 return make_scoped_refptr(new UsbDeviceAndroid( |
| 44 env, vendor_id, product_id, |
| 45 ConvertJavaStringToUTF16(env, manufacturer_string), |
| 46 ConvertJavaStringToUTF16(env, product_string), |
| 47 ConvertJavaStringToUTF16(env, serial_number), wrapper)); |
| 48 } |
| 49 |
| 50 void UsbDeviceAndroid::Open(const OpenCallback& callback) { |
| 51 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 52 base::Bind(callback, nullptr)); |
| 53 } |
| 54 |
| 55 const UsbConfigDescriptor* UsbDeviceAndroid::GetActiveConfiguration() { |
| 56 return nullptr; |
| 57 } |
| 58 |
| 59 UsbDeviceAndroid::UsbDeviceAndroid(JNIEnv* env, |
| 60 uint16_t vendor_id, |
| 61 uint16_t product_id, |
| 62 const base::string16& manufacturer_string, |
| 63 const base::string16& product_string, |
| 64 const base::string16& serial_number, |
| 65 const JavaRef<jobject>& wrapper) |
| 66 : UsbDevice(vendor_id, |
| 67 product_id, |
| 68 manufacturer_string, |
| 69 product_string, |
| 70 serial_number) { |
| 71 j_object_.Reset(wrapper); |
| 72 |
| 73 if (base::android::BuildInfo::GetInstance()->sdk_int() >= 21) { |
| 74 ScopedJavaLocalRef<jobjectArray> configurations = |
| 75 Java_ChromeUsbDevice_getConfigurations(env, j_object_.obj()); |
| 76 jsize count = env->GetArrayLength(configurations.obj()); |
| 77 configurations_.reserve(count); |
| 78 for (jsize i = 0; i < count; ++i) { |
| 79 ScopedJavaLocalRef<jobject> config( |
| 80 env, env->GetObjectArrayElement(configurations.obj(), i)); |
| 81 configurations_.push_back(UsbConfigurationAndroid::Convert(env, config)); |
| 82 } |
| 83 } else { |
| 84 // Pre-lollipop only the first configuration was supported. Build a basic |
| 85 // configuration out of the available interfaces. |
| 86 UsbConfigDescriptor config; |
| 87 config.configuration_value = 1; // Reasonable guess. |
| 88 config.self_powered = false; // Arbitrary default. |
| 89 config.remote_wakeup = false; // Arbitrary default. |
| 90 config.maximum_power = 0; // Arbitrary default. |
| 91 |
| 92 ScopedJavaLocalRef<jobjectArray> interfaces = |
| 93 Java_ChromeUsbDevice_getInterfaces(env, wrapper.obj()); |
| 94 jsize count = env->GetArrayLength(interfaces.obj()); |
| 95 config.interfaces.reserve(count); |
| 96 for (jsize i = 0; i < count; ++i) { |
| 97 ScopedJavaLocalRef<jobject> interface( |
| 98 env, env->GetObjectArrayElement(interfaces.obj(), i)); |
| 99 config.interfaces.push_back(UsbInterfaceAndroid::Convert(env, interface)); |
| 100 } |
| 101 configurations_.push_back(config); |
| 102 } |
| 103 } |
| 104 |
| 105 UsbDeviceAndroid::~UsbDeviceAndroid() {} |
| 106 |
| 107 } // namespace device |
OLD | NEW |