All instances of addresses as void* are changed to uintptr_t in
[chromium-blink-merge.git] / media / midi / usb_midi_device_factory_android.cc
blob636316c31836002692c85500be56909343f02eab
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "media/midi/usb_midi_device_factory_android.h"
8 #include "base/bind.h"
9 #include "base/containers/hash_tables.h"
10 #include "base/lazy_instance.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/synchronization/lock.h"
13 #include "jni/UsbMidiDeviceFactoryAndroid_jni.h"
14 #include "media/midi/usb_midi_device_android.h"
16 namespace media {
17 namespace midi {
19 namespace {
21 typedef UsbMidiDevice::Factory::Callback Callback;
23 } // namespace
25 UsbMidiDeviceFactoryAndroid::UsbMidiDeviceFactoryAndroid() : delegate_(NULL) {}
27 UsbMidiDeviceFactoryAndroid::~UsbMidiDeviceFactoryAndroid() {
28 JNIEnv* env = base::android::AttachCurrentThread();
29 if (!raw_factory_.is_null())
30 Java_UsbMidiDeviceFactoryAndroid_close(
31 env, raw_factory_.obj(), base::android::GetApplicationContext());
34 void UsbMidiDeviceFactoryAndroid::EnumerateDevices(
35 UsbMidiDeviceDelegate* delegate,
36 Callback callback) {
37 DCHECK(!delegate_);
38 JNIEnv* env = base::android::AttachCurrentThread();
39 uintptr_t pointer = reinterpret_cast<uintptr_t>(this);
40 raw_factory_.Reset(Java_UsbMidiDeviceFactoryAndroid_create(
41 env, base::android::GetApplicationContext(), pointer));
43 delegate_ = delegate;
44 callback_ = callback;
46 if (Java_UsbMidiDeviceFactoryAndroid_enumerateDevices(
47 env, raw_factory_.obj(), base::android::GetApplicationContext())) {
48 // Asynchronous operation.
49 return;
51 // No devices are found.
52 ScopedVector<UsbMidiDevice> devices;
53 callback.Run(true, &devices);
56 // Called from the Java world.
57 void UsbMidiDeviceFactoryAndroid::OnUsbMidiDeviceRequestDone(
58 JNIEnv* env,
59 jobject caller,
60 jobjectArray devices) {
61 size_t size = env->GetArrayLength(devices);
62 ScopedVector<UsbMidiDevice> devices_to_pass;
63 for (size_t i = 0; i < size; ++i) {
64 UsbMidiDeviceAndroid::ObjectRef raw_device(
65 env, env->GetObjectArrayElement(devices, i));
66 devices_to_pass.push_back(new UsbMidiDeviceAndroid(raw_device, delegate_));
69 callback_.Run(true, &devices_to_pass);
72 // Called from the Java world.
73 void UsbMidiDeviceFactoryAndroid::OnUsbMidiDeviceAttached(
74 JNIEnv* env,
75 jobject caller,
76 jobject device) {
77 UsbMidiDeviceAndroid::ObjectRef raw_device(env, device);
78 delegate_->OnDeviceAttached(
79 scoped_ptr<UsbMidiDevice>(
80 new UsbMidiDeviceAndroid(raw_device, delegate_)));
83 // Called from the Java world.
84 void UsbMidiDeviceFactoryAndroid::OnUsbMidiDeviceDetached(
85 JNIEnv* env,
86 jobject caller,
87 jint index) {
88 delegate_->OnDeviceDetached(index);
91 bool UsbMidiDeviceFactoryAndroid::RegisterUsbMidiDeviceFactory(JNIEnv* env) {
92 return RegisterNativesImpl(env);
95 } // namespace midi
96 } // namespace media