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