| 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.h" | 5 #include "device/generic_sensor/platform_sensor_provider.h" |
| 6 | 6 |
| 7 #include "base/android/context_utils.h" | 7 #include "base/android/context_utils.h" |
| 8 #include "base/android/scoped_java_ref.h" | 8 #include "base/android/scoped_java_ref.h" |
| 9 #include "base/memory/singleton.h" | 9 #include "base/memory/singleton.h" |
| 10 #include "device/generic_sensor/platform_sensor_android.h" | 10 #include "device/generic_sensor/platform_sensor_android.h" |
| 11 #include "jni/PlatformSensorProvider_jni.h" | 11 #include "jni/PlatformSensorProvider_jni.h" |
| 12 | 12 |
| 13 using base::android::AttachCurrentThread; | 13 using base::android::AttachCurrentThread; |
| 14 using base::android::ScopedJavaLocalRef; | 14 using base::android::ScopedJavaLocalRef; |
| 15 | 15 |
| 16 namespace device { | 16 namespace device { |
| 17 | 17 |
| 18 class PlatformSensorProviderAndroid : public PlatformSensorProvider { | 18 class PlatformSensorProviderAndroid : public PlatformSensorProvider { |
| 19 public: | 19 public: |
| 20 PlatformSensorProviderAndroid(); | 20 PlatformSensorProviderAndroid(); |
| 21 ~PlatformSensorProviderAndroid() override; | 21 ~PlatformSensorProviderAndroid() override; |
| 22 | 22 |
| 23 static PlatformSensorProviderAndroid* GetInstance(); | 23 static PlatformSensorProviderAndroid* GetInstance(); |
| 24 | 24 |
| 25 protected: | 25 protected: |
| 26 scoped_refptr<PlatformSensor> CreateSensorInternal( | 26 void CreateSensorInternal(mojom::SensorType type, |
| 27 mojom::SensorType type, | 27 mojo::ScopedSharedBufferMapping mapping, |
| 28 mojo::ScopedSharedBufferMapping mapping, | 28 uint64_t buffer_size, |
| 29 uint64_t buffer_size) override; | 29 const CreateSensorCallback& callback) override; |
| 30 | 30 |
| 31 private: | 31 private: |
| 32 // Java object org.chromium.device.sensors.PlatformSensorProvider | 32 // Java object org.chromium.device.sensors.PlatformSensorProvider |
| 33 base::android::ScopedJavaGlobalRef<jobject> j_object_; | 33 base::android::ScopedJavaGlobalRef<jobject> j_object_; |
| 34 | 34 |
| 35 DISALLOW_COPY_AND_ASSIGN(PlatformSensorProviderAndroid); | 35 DISALLOW_COPY_AND_ASSIGN(PlatformSensorProviderAndroid); |
| 36 }; | 36 }; |
| 37 | 37 |
| 38 // static | 38 // static |
| 39 PlatformSensorProvider* PlatformSensorProvider::GetInstance() { | 39 PlatformSensorProvider* PlatformSensorProvider::GetInstance() { |
| 40 return PlatformSensorProviderAndroid::GetInstance(); | 40 return PlatformSensorProviderAndroid::GetInstance(); |
| 41 } | 41 } |
| 42 | 42 |
| 43 // static | 43 // static |
| 44 PlatformSensorProviderAndroid* PlatformSensorProviderAndroid::GetInstance() { | 44 PlatformSensorProviderAndroid* PlatformSensorProviderAndroid::GetInstance() { |
| 45 return base::Singleton< | 45 return base::Singleton< |
| 46 PlatformSensorProviderAndroid, | 46 PlatformSensorProviderAndroid, |
| 47 base::LeakySingletonTraits<PlatformSensorProviderAndroid>>::get(); | 47 base::LeakySingletonTraits<PlatformSensorProviderAndroid>>::get(); |
| 48 } | 48 } |
| 49 | 49 |
| 50 PlatformSensorProviderAndroid::PlatformSensorProviderAndroid() { | 50 PlatformSensorProviderAndroid::PlatformSensorProviderAndroid() { |
| 51 JNIEnv* env = AttachCurrentThread(); | 51 JNIEnv* env = AttachCurrentThread(); |
| 52 j_object_.Reset(Java_PlatformSensorProvider_create( | 52 j_object_.Reset(Java_PlatformSensorProvider_create( |
| 53 env, base::android::GetApplicationContext())); | 53 env, base::android::GetApplicationContext())); |
| 54 } | 54 } |
| 55 | 55 |
| 56 PlatformSensorProviderAndroid::~PlatformSensorProviderAndroid() = default; | 56 PlatformSensorProviderAndroid::~PlatformSensorProviderAndroid() = default; |
| 57 | 57 |
| 58 scoped_refptr<PlatformSensor> | 58 void PlatformSensorProviderAndroid::CreateSensorInternal( |
| 59 PlatformSensorProviderAndroid::CreateSensorInternal( | |
| 60 mojom::SensorType type, | 59 mojom::SensorType type, |
| 61 mojo::ScopedSharedBufferMapping mapping, | 60 mojo::ScopedSharedBufferMapping mapping, |
| 62 uint64_t buffer_size) { | 61 uint64_t buffer_size, |
| 62 const CreateSensorCallback& callback) { |
| 63 JNIEnv* env = AttachCurrentThread(); | 63 JNIEnv* env = AttachCurrentThread(); |
| 64 ScopedJavaLocalRef<jobject> sensor = Java_PlatformSensorProvider_createSensor( | 64 ScopedJavaLocalRef<jobject> sensor = Java_PlatformSensorProvider_createSensor( |
| 65 env, j_object_.obj(), static_cast<jint>(type)); | 65 env, j_object_.obj(), static_cast<jint>(type)); |
| 66 | 66 |
| 67 if (!sensor.obj()) | 67 if (!sensor.obj()) |
| 68 return nullptr; | 68 callback.Run(nullptr); |
| 69 | 69 |
| 70 return new PlatformSensorAndroid(type, std::move(mapping), buffer_size, this, | 70 scoped_refptr<PlatformSensorAndroid> concrete_sensor = |
| 71 sensor); | 71 new PlatformSensorAndroid(type, std::move(mapping), buffer_size, this, |
| 72 sensor); |
| 73 callback.Run(concrete_sensor); |
| 72 } | 74 } |
| 73 | 75 |
| 74 } // namespace device | 76 } // namespace device |
| OLD | NEW |