File: usb_device_android.cc

package info (click to toggle)
chromium 145.0.7632.159-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 5,976,224 kB
  • sloc: cpp: 36,198,469; ansic: 7,634,080; javascript: 3,564,060; python: 1,649,622; xml: 838,470; asm: 717,087; pascal: 185,708; sh: 88,786; perl: 88,718; objc: 79,984; sql: 59,811; cs: 42,452; fortran: 24,101; makefile: 21,144; tcl: 15,277; php: 14,022; yacc: 9,066; ruby: 7,553; awk: 3,720; lisp: 3,233; lex: 1,328; ada: 727; jsp: 228; sed: 36
file content (228 lines) | stat: -rw-r--r-- 8,453 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "services/device/usb/usb_device_android.h"

#include <list>
#include <memory>
#include <utility>

#include "base/android/android_info.h"
#include "base/android/jni_string.h"
#include "base/functional/bind.h"
#include "base/location.h"
#include "base/task/single_thread_task_runner.h"
#include "services/device/usb/usb_configuration_android.h"
#include "services/device/usb/usb_descriptors.h"
#include "services/device/usb/usb_device_handle_android.h"
#include "services/device/usb/usb_interface_android.h"
#include "services/device/usb/usb_service_android.h"
#include "services/device/usb/webusb_descriptors.h"

// Must come after all headers that specialize FromJniType() / ToJniType().
#include "services/device/usb/jni_headers/ChromeUsbDevice_jni.h"

using base::android::ConvertJavaStringToUTF16;
using base::android::JavaObjectArrayReader;
using base::android::JavaRef;
using base::android::ScopedJavaLocalRef;

namespace device {

// static
scoped_refptr<UsbDeviceAndroid> UsbDeviceAndroid::Create(
    JNIEnv* env,
    base::WeakPtr<UsbServiceAndroid> service,
    const JavaRef<jobject>& usb_device) {
  ScopedJavaLocalRef<jobject> wrapper =
      Java_ChromeUsbDevice_create(env, usb_device);

  std::u16string manufacturer_string;
  ScopedJavaLocalRef<jstring> manufacturer_jstring =
      Java_ChromeUsbDevice_getManufacturerName(env, wrapper);
  if (!manufacturer_jstring.is_null())
    manufacturer_string = ConvertJavaStringToUTF16(env, manufacturer_jstring);

  std::u16string product_string;
  ScopedJavaLocalRef<jstring> product_jstring =
      Java_ChromeUsbDevice_getProductName(env, wrapper);
  if (!product_jstring.is_null())
    product_string = ConvertJavaStringToUTF16(env, product_jstring);

  // Reading the serial number requires device access permission when
  // targeting the Q SDK.
  std::u16string serial_number;
  if (service->HasDevicePermission(wrapper) ||
      base::android::android_info::sdk_int() <
          base::android::android_info::SDK_VERSION_Q) {
    ScopedJavaLocalRef<jstring> serial_jstring =
        Java_ChromeUsbDevice_getSerialNumber(env, wrapper);
    if (!serial_jstring.is_null())
      serial_number = ConvertJavaStringToUTF16(env, serial_jstring);
  }

  return base::WrapRefCounted(new UsbDeviceAndroid(
      env, service,
      0x0200,  // USB protocol version, not provided by the Android API.
      Java_ChromeUsbDevice_getDeviceClass(env, wrapper),
      Java_ChromeUsbDevice_getDeviceSubclass(env, wrapper),
      Java_ChromeUsbDevice_getDeviceProtocol(env, wrapper),
      Java_ChromeUsbDevice_getVendorId(env, wrapper),
      Java_ChromeUsbDevice_getProductId(env, wrapper),
      Java_ChromeUsbDevice_getDeviceVersion(env, wrapper),
      manufacturer_string, product_string, serial_number, wrapper));
}

void UsbDeviceAndroid::RequestPermission(ResultCallback callback) {
  if (!permission_granted_ && service_) {
    request_permission_callbacks_.push_back(std::move(callback));
    service_->RequestDevicePermission(j_object_);
  } else {
    base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
        FROM_HERE, base::BindOnce(std::move(callback), permission_granted_));
  }
}

void UsbDeviceAndroid::Open(OpenCallback callback) {
  scoped_refptr<UsbDeviceHandle> device_handle;
  if (service_) {
    JNIEnv* env = jni_zero::AttachCurrentThread();
    ScopedJavaLocalRef<jobject> connection =
        service_->OpenDevice(env, j_object_);
    if (!connection.is_null()) {
      device_handle = UsbDeviceHandleAndroid::Create(env, this, connection);
      handles().push_back(device_handle.get());
    }
  }
  base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
      FROM_HERE, base::BindOnce(std::move(callback), device_handle));
}

bool UsbDeviceAndroid::permission_granted() const {
  return permission_granted_;
}

UsbDeviceAndroid::UsbDeviceAndroid(JNIEnv* env,
                                   base::WeakPtr<UsbServiceAndroid> service,
                                   uint16_t usb_version,
                                   uint8_t device_class,
                                   uint8_t device_subclass,
                                   uint8_t device_protocol,
                                   uint16_t vendor_id,
                                   uint16_t product_id,
                                   uint16_t device_version,
                                   const std::u16string& manufacturer_string,
                                   const std::u16string& product_string,
                                   const std::u16string& serial_number,
                                   const JavaRef<jobject>& wrapper)
    : UsbDevice(usb_version,
                device_class,
                device_subclass,
                device_protocol,
                vendor_id,
                product_id,
                device_version,
                manufacturer_string,
                product_string,
                serial_number,
                // We fake the bus and port number, because the underlying Java
                // UsbDevice class doesn't offer an interface for getting these
                // values, and nothing on Android seems to require them at this
                // time (23-Nov-2018)
                0,
                0),
      device_id_(Java_ChromeUsbDevice_getDeviceId(env, wrapper)),
      service_(service),
      j_object_(wrapper) {
  JavaObjectArrayReader<jobject> configs(
      Java_ChromeUsbDevice_getConfigurations(env, j_object_));
  device_info_->configurations.reserve(configs.size());
  for (auto config : configs) {
    device_info_->configurations.push_back(
        UsbConfigurationAndroid::Convert(env, config));
  }

  if (configurations().size() > 0)
    ActiveConfigurationChanged(configurations()[0]->configuration_value);
}

UsbDeviceAndroid::~UsbDeviceAndroid() {}

void UsbDeviceAndroid::PermissionGranted(JNIEnv* env, bool granted) {
  if (!granted) {
    CallRequestPermissionCallbacks(false);
    return;
  }

  ScopedJavaLocalRef<jstring> serial_jstring =
      Java_ChromeUsbDevice_getSerialNumber(env, j_object_);
  if (!serial_jstring.is_null())
    device_info_->serial_number = ConvertJavaStringToUTF16(env, serial_jstring);

  Open(
      base::BindOnce(&UsbDeviceAndroid::OnDeviceOpenedToReadDescriptors, this));
}

void UsbDeviceAndroid::CallRequestPermissionCallbacks(bool granted) {
  permission_granted_ = granted;
  std::list<ResultCallback> callbacks;
  callbacks.swap(request_permission_callbacks_);
  for (auto& callback : callbacks)
    std::move(callback).Run(granted);
}

void UsbDeviceAndroid::OnDeviceOpenedToReadDescriptors(
    scoped_refptr<UsbDeviceHandle> device_handle) {
  if (!device_handle) {
    CallRequestPermissionCallbacks(false);
    return;
  }

  ReadUsbDescriptors(device_handle,
                     base::BindOnce(&UsbDeviceAndroid::OnReadDescriptors, this,
                                    device_handle));
}

void UsbDeviceAndroid::OnReadDescriptors(
    scoped_refptr<UsbDeviceHandle> device_handle,
    std::unique_ptr<UsbDeviceDescriptor> descriptor) {
  if (!descriptor) {
    device_handle->Close();
    CallRequestPermissionCallbacks(false);
    return;
  }

  // ReadUsbDescriptors() is called to read properties which aren't always
  // available from the Android OS. The other properties should be left alone.
  device_info_->usb_version_major = descriptor->device_info->usb_version_major;
  device_info_->usb_version_minor = descriptor->device_info->usb_version_minor;
  device_info_->usb_version_subminor =
      descriptor->device_info->usb_version_subminor;
  device_info_->configurations =
      std::move(descriptor->device_info->configurations);

  if (usb_version() >= 0x0210) {
    ReadWebUsbDescriptors(
        device_handle,
        base::BindOnce(&UsbDeviceAndroid::OnReadWebUsbDescriptors, this,
                       device_handle));
  } else {
    device_handle->Close();
    CallRequestPermissionCallbacks(true);
  }
}

void UsbDeviceAndroid::OnReadWebUsbDescriptors(
    scoped_refptr<UsbDeviceHandle> device_handle,
    const GURL& landing_page) {
  if (landing_page.is_valid())
    device_info_->webusb_landing_page = landing_page;

  device_handle->Close();
  CallRequestPermissionCallbacks(true);
}

}  // namespace device

DEFINE_JNI(ChromeUsbDevice)