| 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/usb_device_android.h" | 5 #include "device/usb/usb_device_android.h" |
| 6 | 6 |
| 7 #include "base/android/build_info.h" | 7 #include "base/android/build_info.h" |
| 8 #include "base/android/jni_string.h" | 8 #include "base/android/jni_string.h" |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/location.h" | 10 #include "base/location.h" |
| 11 #include "base/task_scheduler/post_task.h" |
| 11 #include "base/threading/thread_task_runner_handle.h" | 12 #include "base/threading/thread_task_runner_handle.h" |
| 12 #include "device/usb/usb_configuration_android.h" | 13 #include "device/usb/usb_configuration_android.h" |
| 13 #include "device/usb/usb_descriptors.h" | 14 #include "device/usb/usb_descriptors.h" |
| 14 #include "device/usb/usb_device_handle_android.h" | 15 #include "device/usb/usb_device_handle_android.h" |
| 15 #include "device/usb/usb_interface_android.h" | 16 #include "device/usb/usb_interface_android.h" |
| 16 #include "device/usb/usb_service_android.h" | 17 #include "device/usb/usb_service_android.h" |
| 17 #include "device/usb/webusb_descriptors.h" | 18 #include "device/usb/webusb_descriptors.h" |
| 18 #include "jni/ChromeUsbDevice_jni.h" | 19 #include "jni/ChromeUsbDevice_jni.h" |
| 19 | 20 |
| 20 using base::android::ConvertJavaStringToUTF16; | 21 using base::android::ConvertJavaStringToUTF16; |
| 21 using base::android::JavaRef; | 22 using base::android::JavaRef; |
| 22 using base::android::ScopedJavaLocalRef; | 23 using base::android::ScopedJavaLocalRef; |
| 23 | 24 |
| 24 namespace device { | 25 namespace device { |
| 25 | 26 |
| 27 namespace { |
| 28 |
| 29 scoped_refptr<base::SequencedTaskRunner> CreateBlockingTaskRunner() { |
| 30 return base::CreateSequencedTaskRunnerWithTraits( |
| 31 base::TaskTraits() |
| 32 .MayBlock() |
| 33 .WithPriority(base::TaskPriority::USER_VISIBLE) |
| 34 .WithShutdownBehavior( |
| 35 base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN)); |
| 36 } |
| 37 |
| 38 } // namespace |
| 39 |
| 26 // static | 40 // static |
| 27 scoped_refptr<UsbDeviceAndroid> UsbDeviceAndroid::Create( | 41 scoped_refptr<UsbDeviceAndroid> UsbDeviceAndroid::Create( |
| 28 JNIEnv* env, | 42 JNIEnv* env, |
| 29 base::WeakPtr<UsbServiceAndroid> service, | 43 base::WeakPtr<UsbServiceAndroid> service, |
| 30 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner, | |
| 31 const JavaRef<jobject>& usb_device) { | 44 const JavaRef<jobject>& usb_device) { |
| 32 ScopedJavaLocalRef<jobject> wrapper = | 45 ScopedJavaLocalRef<jobject> wrapper = |
| 33 Java_ChromeUsbDevice_create(env, usb_device); | 46 Java_ChromeUsbDevice_create(env, usb_device); |
| 34 uint16_t device_version = 0; | 47 uint16_t device_version = 0; |
| 35 if (base::android::BuildInfo::GetInstance()->sdk_int() >= 23) | 48 if (base::android::BuildInfo::GetInstance()->sdk_int() >= 23) |
| 36 device_version = Java_ChromeUsbDevice_getDeviceVersion(env, wrapper); | 49 device_version = Java_ChromeUsbDevice_getDeviceVersion(env, wrapper); |
| 37 base::string16 manufacturer_string, product_string, serial_number; | 50 base::string16 manufacturer_string, product_string, serial_number; |
| 38 if (base::android::BuildInfo::GetInstance()->sdk_int() >= 21) { | 51 if (base::android::BuildInfo::GetInstance()->sdk_int() >= 21) { |
| 39 manufacturer_string = ConvertJavaStringToUTF16( | 52 manufacturer_string = ConvertJavaStringToUTF16( |
| 40 env, Java_ChromeUsbDevice_getManufacturerName(env, wrapper)); | 53 env, Java_ChromeUsbDevice_getManufacturerName(env, wrapper)); |
| 41 product_string = ConvertJavaStringToUTF16( | 54 product_string = ConvertJavaStringToUTF16( |
| 42 env, Java_ChromeUsbDevice_getProductName(env, wrapper)); | 55 env, Java_ChromeUsbDevice_getProductName(env, wrapper)); |
| 43 serial_number = ConvertJavaStringToUTF16( | 56 serial_number = ConvertJavaStringToUTF16( |
| 44 env, Java_ChromeUsbDevice_getSerialNumber(env, wrapper)); | 57 env, Java_ChromeUsbDevice_getSerialNumber(env, wrapper)); |
| 45 } | 58 } |
| 46 return make_scoped_refptr(new UsbDeviceAndroid( | 59 return make_scoped_refptr(new UsbDeviceAndroid( |
| 47 env, service, | 60 env, service, |
| 48 0x0200, // USB protocol version, not provided by the Android API. | 61 0x0200, // USB protocol version, not provided by the Android API. |
| 49 Java_ChromeUsbDevice_getDeviceClass(env, wrapper), | 62 Java_ChromeUsbDevice_getDeviceClass(env, wrapper), |
| 50 Java_ChromeUsbDevice_getDeviceSubclass(env, wrapper), | 63 Java_ChromeUsbDevice_getDeviceSubclass(env, wrapper), |
| 51 Java_ChromeUsbDevice_getDeviceProtocol(env, wrapper), | 64 Java_ChromeUsbDevice_getDeviceProtocol(env, wrapper), |
| 52 Java_ChromeUsbDevice_getVendorId(env, wrapper), | 65 Java_ChromeUsbDevice_getVendorId(env, wrapper), |
| 53 Java_ChromeUsbDevice_getProductId(env, wrapper), device_version, | 66 Java_ChromeUsbDevice_getProductId(env, wrapper), device_version, |
| 54 manufacturer_string, product_string, serial_number, blocking_task_runner, | 67 manufacturer_string, product_string, serial_number, wrapper)); |
| 55 wrapper)); | |
| 56 } | 68 } |
| 57 | 69 |
| 58 void UsbDeviceAndroid::RequestPermission(const ResultCallback& callback) { | 70 void UsbDeviceAndroid::RequestPermission(const ResultCallback& callback) { |
| 59 if (!permission_granted_ && service_) { | 71 if (!permission_granted_ && service_) { |
| 60 request_permission_callbacks_.push_back(callback); | 72 request_permission_callbacks_.push_back(callback); |
| 61 service_->RequestDevicePermission(j_object_, device_id_); | 73 service_->RequestDevicePermission(j_object_, device_id_); |
| 62 } else { | 74 } else { |
| 63 base::ThreadTaskRunnerHandle::Get()->PostTask( | 75 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 64 FROM_HERE, base::Bind(callback, permission_granted_)); | 76 FROM_HERE, base::Bind(callback, permission_granted_)); |
| 65 } | 77 } |
| 66 } | 78 } |
| 67 | 79 |
| 68 void UsbDeviceAndroid::Open(const OpenCallback& callback) { | 80 void UsbDeviceAndroid::Open(const OpenCallback& callback) { |
| 69 scoped_refptr<UsbDeviceHandle> device_handle; | 81 scoped_refptr<UsbDeviceHandle> device_handle; |
| 70 if (service_) { | 82 if (service_) { |
| 71 JNIEnv* env = base::android::AttachCurrentThread(); | 83 JNIEnv* env = base::android::AttachCurrentThread(); |
| 72 ScopedJavaLocalRef<jobject> connection = | 84 ScopedJavaLocalRef<jobject> connection = |
| 73 service_->OpenDevice(env, j_object_); | 85 service_->OpenDevice(env, j_object_); |
| 74 if (!connection.is_null()) { | 86 if (!connection.is_null()) { |
| 75 device_handle = UsbDeviceHandleAndroid::Create( | 87 device_handle = UsbDeviceHandleAndroid::Create( |
| 76 env, this, blocking_task_runner_, connection); | 88 env, this, CreateBlockingTaskRunner(), connection); |
| 77 handles().push_back(device_handle.get()); | 89 handles().push_back(device_handle.get()); |
| 78 } | 90 } |
| 79 } | 91 } |
| 80 base::ThreadTaskRunnerHandle::Get()->PostTask( | 92 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 81 FROM_HERE, base::Bind(callback, device_handle)); | 93 FROM_HERE, base::Bind(callback, device_handle)); |
| 82 } | 94 } |
| 83 | 95 |
| 84 bool UsbDeviceAndroid::permission_granted() const { | 96 bool UsbDeviceAndroid::permission_granted() const { |
| 85 return permission_granted_; | 97 return permission_granted_; |
| 86 } | 98 } |
| 87 | 99 |
| 88 UsbDeviceAndroid::UsbDeviceAndroid( | 100 UsbDeviceAndroid::UsbDeviceAndroid( |
| 89 JNIEnv* env, | 101 JNIEnv* env, |
| 90 base::WeakPtr<UsbServiceAndroid> service, | 102 base::WeakPtr<UsbServiceAndroid> service, |
| 91 uint16_t usb_version, | 103 uint16_t usb_version, |
| 92 uint8_t device_class, | 104 uint8_t device_class, |
| 93 uint8_t device_subclass, | 105 uint8_t device_subclass, |
| 94 uint8_t device_protocol, | 106 uint8_t device_protocol, |
| 95 uint16_t vendor_id, | 107 uint16_t vendor_id, |
| 96 uint16_t product_id, | 108 uint16_t product_id, |
| 97 uint16_t device_version, | 109 uint16_t device_version, |
| 98 const base::string16& manufacturer_string, | 110 const base::string16& manufacturer_string, |
| 99 const base::string16& product_string, | 111 const base::string16& product_string, |
| 100 const base::string16& serial_number, | 112 const base::string16& serial_number, |
| 101 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner, | |
| 102 const JavaRef<jobject>& wrapper) | 113 const JavaRef<jobject>& wrapper) |
| 103 : UsbDevice(usb_version, | 114 : UsbDevice(usb_version, |
| 104 device_class, | 115 device_class, |
| 105 device_subclass, | 116 device_subclass, |
| 106 device_protocol, | 117 device_protocol, |
| 107 vendor_id, | 118 vendor_id, |
| 108 product_id, | 119 product_id, |
| 109 device_version, | 120 device_version, |
| 110 manufacturer_string, | 121 manufacturer_string, |
| 111 product_string, | 122 product_string, |
| 112 serial_number), | 123 serial_number), |
| 113 blocking_task_runner_(blocking_task_runner), | |
| 114 device_id_(Java_ChromeUsbDevice_getDeviceId(env, wrapper)), | 124 device_id_(Java_ChromeUsbDevice_getDeviceId(env, wrapper)), |
| 115 service_(service), | 125 service_(service), |
| 116 j_object_(wrapper) { | 126 j_object_(wrapper) { |
| 117 if (base::android::BuildInfo::GetInstance()->sdk_int() >= 21) { | 127 if (base::android::BuildInfo::GetInstance()->sdk_int() >= 21) { |
| 118 ScopedJavaLocalRef<jobjectArray> configurations = | 128 ScopedJavaLocalRef<jobjectArray> configurations = |
| 119 Java_ChromeUsbDevice_getConfigurations(env, j_object_); | 129 Java_ChromeUsbDevice_getConfigurations(env, j_object_); |
| 120 jsize count = env->GetArrayLength(configurations.obj()); | 130 jsize count = env->GetArrayLength(configurations.obj()); |
| 121 descriptor_.configurations.reserve(count); | 131 descriptor_.configurations.reserve(count); |
| 122 for (jsize i = 0; i < count; ++i) { | 132 for (jsize i = 0; i < count; ++i) { |
| 123 ScopedJavaLocalRef<jobject> config( | 133 ScopedJavaLocalRef<jobject> config( |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 205 if (allowed_origins) | 215 if (allowed_origins) |
| 206 webusb_allowed_origins_ = std::move(allowed_origins); | 216 webusb_allowed_origins_ = std::move(allowed_origins); |
| 207 if (landing_page.is_valid()) | 217 if (landing_page.is_valid()) |
| 208 webusb_landing_page_ = landing_page; | 218 webusb_landing_page_ = landing_page; |
| 209 | 219 |
| 210 device_handle->Close(); | 220 device_handle->Close(); |
| 211 CallRequestPermissionCallbacks(true); | 221 CallRequestPermissionCallbacks(true); |
| 212 } | 222 } |
| 213 | 223 |
| 214 } // namespace device | 224 } // namespace device |
| OLD | NEW |