OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "device/generic_sensor/platform_sensor_provider.h" | |
6 | |
7 #include "base/android/context_utils.h" | |
8 #include "base/android/scoped_java_ref.h" | |
9 #include "base/memory/singleton.h" | |
10 #include "device/generic_sensor/platform_sensor_android.h" | |
11 #include "jni/PlatformSensorProvider_jni.h" | |
12 | |
13 using base::android::AttachCurrentThread; | |
14 using base::android::ScopedJavaLocalRef; | |
15 | |
16 namespace device { | |
17 | |
18 class PlatformSensorProviderAndroid : public PlatformSensorProvider { | |
19 public: | |
20 PlatformSensorProviderAndroid(); | |
21 ~PlatformSensorProviderAndroid() override; | |
22 | |
23 static PlatformSensorProviderAndroid* GetInstance(); | |
24 | |
25 protected: | |
26 scoped_refptr<PlatformSensor> CreateSensorInternal( | |
27 mojom::SensorType type, | |
28 mojo::ScopedSharedBufferMapping mapping, | |
29 uint64_t buffer_size) override; | |
30 | |
31 private: | |
32 // Java object org.chromium.device.sensors.PlatformSensorProvider | |
33 base::android::ScopedJavaGlobalRef<jobject> j_object_; | |
34 | |
35 DISALLOW_COPY_AND_ASSIGN(PlatformSensorProviderAndroid); | |
36 }; | |
37 | |
38 // static | |
39 PlatformSensorProvider* PlatformSensorProvider::GetInstance() { | |
40 return PlatformSensorProviderAndroid::GetInstance(); | |
timvolodine
2016/09/06 17:44:38
Looks a bit awkward, having two singletons.. It wo
shalamov
2016/09/07 13:53:42
I couldn't figure out how to return singleton from
| |
41 } | |
42 | |
43 // static | |
44 PlatformSensorProviderAndroid* PlatformSensorProviderAndroid::GetInstance() { | |
45 return base::Singleton< | |
46 PlatformSensorProviderAndroid, | |
47 base::LeakySingletonTraits<PlatformSensorProviderAndroid>>::get(); | |
48 } | |
49 | |
50 PlatformSensorProviderAndroid::PlatformSensorProviderAndroid() { | |
51 JNIEnv* env = AttachCurrentThread(); | |
52 j_object_.Reset(Java_PlatformSensorProvider_create( | |
53 env, base::android::GetApplicationContext())); | |
54 } | |
55 | |
56 PlatformSensorProviderAndroid::~PlatformSensorProviderAndroid() = default; | |
57 | |
58 scoped_refptr<PlatformSensor> | |
59 PlatformSensorProviderAndroid::CreateSensorInternal( | |
60 mojom::SensorType type, | |
61 mojo::ScopedSharedBufferMapping mapping, | |
62 uint64_t buffer_size) { | |
63 JNIEnv* env = AttachCurrentThread(); | |
64 ScopedJavaLocalRef<jobject> sensor = Java_PlatformSensorProvider_createSensor( | |
65 env, j_object_.obj(), static_cast<jint>(type)); | |
66 | |
67 if (!sensor.obj()) | |
68 return nullptr; | |
69 | |
70 return new PlatformSensorAndroid(type, std::move(mapping), buffer_size, this, | |
71 sensor); | |
72 } | |
73 | |
74 } // namespace device | |
OLD | NEW |