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