OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/generic_sensor/platform_sensor_provider_android.h" | 5 #include "device/generic_sensor/platform_sensor_provider_android.h" |
6 | 6 |
7 #include "base/android/context_utils.h" | |
8 #include "base/android/scoped_java_ref.h" | 7 #include "base/android/scoped_java_ref.h" |
9 #include "base/memory/singleton.h" | 8 #include "base/memory/singleton.h" |
10 #include "device/generic_sensor/platform_sensor_android.h" | 9 #include "device/generic_sensor/platform_sensor_android.h" |
11 #include "jni/PlatformSensorProvider_jni.h" | 10 #include "jni/PlatformSensorProvider_jni.h" |
12 | 11 |
13 using base::android::AttachCurrentThread; | 12 using base::android::AttachCurrentThread; |
14 using base::android::ScopedJavaLocalRef; | 13 using base::android::ScopedJavaLocalRef; |
15 | 14 |
16 namespace device { | 15 namespace device { |
17 | 16 |
18 // static | 17 // static |
19 PlatformSensorProviderAndroid* PlatformSensorProviderAndroid::GetInstance() { | 18 PlatformSensorProviderAndroid* PlatformSensorProviderAndroid::GetInstance() { |
20 return base::Singleton< | 19 return base::Singleton< |
21 PlatformSensorProviderAndroid, | 20 PlatformSensorProviderAndroid, |
22 base::LeakySingletonTraits<PlatformSensorProviderAndroid>>::get(); | 21 base::LeakySingletonTraits<PlatformSensorProviderAndroid>>::get(); |
23 } | 22 } |
24 | 23 |
25 PlatformSensorProviderAndroid::PlatformSensorProviderAndroid() { | 24 PlatformSensorProviderAndroid::PlatformSensorProviderAndroid() { |
26 JNIEnv* env = AttachCurrentThread(); | 25 JNIEnv* env = AttachCurrentThread(); |
27 j_object_.Reset(Java_PlatformSensorProvider_create( | 26 j_object_.Reset(Java_PlatformSensorProvider_create(env)); |
28 env, base::android::GetApplicationContext())); | |
29 } | 27 } |
30 | 28 |
31 PlatformSensorProviderAndroid::~PlatformSensorProviderAndroid() = default; | 29 PlatformSensorProviderAndroid::~PlatformSensorProviderAndroid() = default; |
32 | 30 |
33 void PlatformSensorProviderAndroid::CreateSensorInternal( | 31 void PlatformSensorProviderAndroid::CreateSensorInternal( |
34 mojom::SensorType type, | 32 mojom::SensorType type, |
35 mojo::ScopedSharedBufferMapping mapping, | 33 mojo::ScopedSharedBufferMapping mapping, |
36 const CreateSensorCallback& callback) { | 34 const CreateSensorCallback& callback) { |
37 JNIEnv* env = AttachCurrentThread(); | 35 JNIEnv* env = AttachCurrentThread(); |
38 ScopedJavaLocalRef<jobject> sensor = Java_PlatformSensorProvider_createSensor( | 36 ScopedJavaLocalRef<jobject> sensor = Java_PlatformSensorProvider_createSensor( |
39 env, j_object_.obj(), static_cast<jint>(type)); | 37 env, j_object_.obj(), static_cast<jint>(type)); |
40 | 38 |
41 if (!sensor.obj()) | 39 if (!sensor.obj()) |
42 callback.Run(nullptr); | 40 callback.Run(nullptr); |
43 | 41 |
44 scoped_refptr<PlatformSensorAndroid> concrete_sensor = | 42 scoped_refptr<PlatformSensorAndroid> concrete_sensor = |
45 new PlatformSensorAndroid(type, std::move(mapping), this, sensor); | 43 new PlatformSensorAndroid(type, std::move(mapping), this, sensor); |
46 callback.Run(concrete_sensor); | 44 callback.Run(concrete_sensor); |
47 } | 45 } |
48 | 46 |
49 } // namespace device | 47 } // namespace device |
OLD | NEW |