OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 "media/midi/usb_midi_device_factory_android.h" | 5 #include "media/midi/usb_midi_device_factory_android.h" |
6 | 6 |
7 #include <jni.h> | 7 #include <jni.h> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/android/scoped_java_ref.h" | 10 #include "base/android/scoped_java_ref.h" |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/containers/hash_tables.h" | 12 #include "base/containers/hash_tables.h" |
13 #include "base/lazy_instance.h" | 13 #include "base/lazy_instance.h" |
14 #include "base/memory/scoped_vector.h" | 14 #include "base/memory/scoped_vector.h" |
15 #include "base/message_loop/message_loop.h" | 15 #include "base/message_loop/message_loop.h" |
16 #include "base/synchronization/lock.h" | 16 #include "base/synchronization/lock.h" |
17 #include "jni/UsbMidiDeviceFactoryAndroid_jni.h" | 17 #include "jni/UsbMidiDeviceFactoryAndroid_jni.h" |
18 #include "media/midi/usb_midi_device_android.h" | 18 #include "media/midi/usb_midi_device_android.h" |
19 | 19 |
20 namespace media { | 20 namespace media { |
| 21 namespace midi { |
21 | 22 |
22 namespace { | 23 namespace { |
23 | 24 |
24 typedef UsbMidiDevice::Factory::Callback Callback; | 25 typedef UsbMidiDevice::Factory::Callback Callback; |
25 | 26 |
26 } // namespace | 27 } // namespace |
27 | 28 |
28 UsbMidiDeviceFactoryAndroid::UsbMidiDeviceFactoryAndroid() : delegate_(NULL) {} | 29 UsbMidiDeviceFactoryAndroid::UsbMidiDeviceFactoryAndroid() : delegate_(NULL) {} |
29 | 30 |
30 UsbMidiDeviceFactoryAndroid::~UsbMidiDeviceFactoryAndroid() { | 31 UsbMidiDeviceFactoryAndroid::~UsbMidiDeviceFactoryAndroid() { |
31 JNIEnv* env = base::android::AttachCurrentThread(); | 32 JNIEnv* env = base::android::AttachCurrentThread(); |
32 if (!raw_factory_.is_null()) | 33 if (!raw_factory_.is_null()) |
33 midi::Java_UsbMidiDeviceFactoryAndroid_close( | 34 Java_UsbMidiDeviceFactoryAndroid_close( |
34 env, raw_factory_.obj(), base::android::GetApplicationContext()); | 35 env, raw_factory_.obj(), base::android::GetApplicationContext()); |
35 } | 36 } |
36 | 37 |
37 void UsbMidiDeviceFactoryAndroid::EnumerateDevices( | 38 void UsbMidiDeviceFactoryAndroid::EnumerateDevices( |
38 UsbMidiDeviceDelegate* delegate, | 39 UsbMidiDeviceDelegate* delegate, |
39 Callback callback) { | 40 Callback callback) { |
40 DCHECK(!delegate_); | 41 DCHECK(!delegate_); |
41 JNIEnv* env = base::android::AttachCurrentThread(); | 42 JNIEnv* env = base::android::AttachCurrentThread(); |
42 uintptr_t pointer = reinterpret_cast<uintptr_t>(this); | 43 uintptr_t pointer = reinterpret_cast<uintptr_t>(this); |
43 raw_factory_.Reset(midi::Java_UsbMidiDeviceFactoryAndroid_create( | 44 raw_factory_.Reset(Java_UsbMidiDeviceFactoryAndroid_create( |
44 env, base::android::GetApplicationContext(), pointer)); | 45 env, base::android::GetApplicationContext(), pointer)); |
45 | 46 |
46 delegate_ = delegate; | 47 delegate_ = delegate; |
47 callback_ = callback; | 48 callback_ = callback; |
48 | 49 |
49 if (midi::Java_UsbMidiDeviceFactoryAndroid_enumerateDevices( | 50 if (Java_UsbMidiDeviceFactoryAndroid_enumerateDevices( |
50 env, raw_factory_.obj(), base::android::GetApplicationContext())) { | 51 env, raw_factory_.obj(), base::android::GetApplicationContext())) { |
51 // Asynchronous operation. | 52 // Asynchronous operation. |
52 return; | 53 return; |
53 } | 54 } |
54 // No devices are found. | 55 // No devices are found. |
55 ScopedVector<UsbMidiDevice> devices; | 56 ScopedVector<UsbMidiDevice> devices; |
56 callback.Run(true, &devices); | 57 callback.Run(true, &devices); |
57 } | 58 } |
58 | 59 |
59 // Called from the Java world. | 60 // Called from the Java world. |
(...skipping 25 matching lines...) Expand all Loading... |
85 | 86 |
86 // Called from the Java world. | 87 // Called from the Java world. |
87 void UsbMidiDeviceFactoryAndroid::OnUsbMidiDeviceDetached( | 88 void UsbMidiDeviceFactoryAndroid::OnUsbMidiDeviceDetached( |
88 JNIEnv* env, | 89 JNIEnv* env, |
89 jobject caller, | 90 jobject caller, |
90 jint index) { | 91 jint index) { |
91 delegate_->OnDeviceDetached(index); | 92 delegate_->OnDeviceDetached(index); |
92 } | 93 } |
93 | 94 |
94 bool UsbMidiDeviceFactoryAndroid::RegisterUsbMidiDeviceFactory(JNIEnv* env) { | 95 bool UsbMidiDeviceFactoryAndroid::RegisterUsbMidiDeviceFactory(JNIEnv* env) { |
95 return midi::RegisterNativesImpl(env); | 96 return RegisterNativesImpl(env); |
96 } | 97 } |
97 | 98 |
| 99 } // namespace midi |
98 } // namespace media | 100 } // namespace media |
OLD | NEW |