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_service_android.h" | 5 #include "device/usb/usb_service_android.h" |
6 | 6 |
| 7 #include <string> |
| 8 #include <vector> |
| 9 |
| 10 #include "base/android/context_utils.h" |
7 #include "base/bind.h" | 11 #include "base/bind.h" |
8 #include "base/location.h" | 12 #include "base/location.h" |
9 #include "base/thread_task_runner_handle.h" | 13 #include "base/thread_task_runner_handle.h" |
10 #include "device/usb/usb_device.h" | 14 #include "device/usb/usb_device_android.h" |
| 15 #include "jni/ChromeUsbService_jni.h" |
| 16 |
| 17 using base::android::AttachCurrentThread; |
| 18 using base::android::ScopedJavaLocalRef; |
11 | 19 |
12 namespace device { | 20 namespace device { |
13 | 21 |
14 UsbServiceAndroid::UsbServiceAndroid() {} | 22 // static |
| 23 bool UsbServiceAndroid::RegisterJNI(JNIEnv* env) { |
| 24 return RegisterNativesImpl(env); // Generated in ChromeUsbService_jni.h |
| 25 } |
| 26 |
| 27 UsbServiceAndroid::UsbServiceAndroid() { |
| 28 j_object_.Reset(Java_ChromeUsbService_create( |
| 29 AttachCurrentThread(), base::android::GetApplicationContext())); |
| 30 } |
15 | 31 |
16 UsbServiceAndroid::~UsbServiceAndroid() {} | 32 UsbServiceAndroid::~UsbServiceAndroid() {} |
17 | 33 |
18 scoped_refptr<UsbDevice> UsbServiceAndroid::GetDevice(const std::string& guid) { | 34 scoped_refptr<UsbDevice> UsbServiceAndroid::GetDevice(const std::string& guid) { |
19 return nullptr; | 35 return nullptr; |
20 } | 36 } |
21 | 37 |
22 void UsbServiceAndroid::GetDevices(const GetDevicesCallback& callback) { | 38 void UsbServiceAndroid::GetDevices(const GetDevicesCallback& callback) { |
23 std::vector<scoped_refptr<UsbDevice>> empty; | 39 JNIEnv* env = AttachCurrentThread(); |
| 40 ScopedJavaLocalRef<jobjectArray> devices = |
| 41 Java_ChromeUsbService_getDevices(env, j_object_.obj()); |
| 42 jsize length = env->GetArrayLength(devices.obj()); |
| 43 |
| 44 std::vector<scoped_refptr<UsbDevice>> results; |
| 45 for (jsize i = 0; i < length; ++i) { |
| 46 ScopedJavaLocalRef<jobject> raw_device( |
| 47 env, env->GetObjectArrayElement(devices.obj(), i)); |
| 48 results.push_back(UsbDeviceAndroid::Create(env, raw_device)); |
| 49 } |
| 50 |
24 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 51 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
25 base::Bind(callback, empty)); | 52 base::Bind(callback, results)); |
26 } | 53 } |
27 | 54 |
28 } // namespace device | 55 } // namespace device |
OLD | NEW |