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" | |
11 #include "base/bind.h" | 7 #include "base/bind.h" |
12 #include "base/location.h" | 8 #include "base/location.h" |
13 #include "base/thread_task_runner_handle.h" | 9 #include "base/thread_task_runner_handle.h" |
14 #include "device/usb/usb_device_android.h" | 10 #include "device/usb/usb_device.h" |
15 #include "jni/ChromeUsbService_jni.h" | |
16 | |
17 using base::android::AttachCurrentThread; | |
18 using base::android::ScopedJavaLocalRef; | |
19 | 11 |
20 namespace device { | 12 namespace device { |
21 | 13 |
22 // static | 14 UsbServiceAndroid::UsbServiceAndroid() {} |
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 } | |
31 | 15 |
32 UsbServiceAndroid::~UsbServiceAndroid() {} | 16 UsbServiceAndroid::~UsbServiceAndroid() {} |
33 | 17 |
34 scoped_refptr<UsbDevice> UsbServiceAndroid::GetDevice(const std::string& guid) { | 18 scoped_refptr<UsbDevice> UsbServiceAndroid::GetDevice(const std::string& guid) { |
35 return nullptr; | 19 return nullptr; |
36 } | 20 } |
37 | 21 |
38 void UsbServiceAndroid::GetDevices(const GetDevicesCallback& callback) { | 22 void UsbServiceAndroid::GetDevices(const GetDevicesCallback& callback) { |
39 JNIEnv* env = AttachCurrentThread(); | 23 std::vector<scoped_refptr<UsbDevice>> empty; |
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 | |
51 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 24 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
52 base::Bind(callback, results)); | 25 base::Bind(callback, empty)); |
53 } | 26 } |
54 | 27 |
55 } // namespace device | 28 } // namespace device |
OLD | NEW |