| 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 #ifndef DEVICE_GENERIC_SENSOR_PLATFORM_SENSOR_PROVIDER_BASE_H_ | |
| 6 #define DEVICE_GENERIC_SENSOR_PLATFORM_SENSOR_PROVIDER_BASE_H_ | |
| 7 | |
| 8 #include "base/macros.h" | |
| 9 | |
| 10 #include "base/single_thread_task_runner.h" | |
| 11 #include "base/threading/thread_checker.h" | |
| 12 #include "device/generic_sensor/generic_sensor_export.h" | |
| 13 #include "device/generic_sensor/platform_sensor.h" | |
| 14 | |
| 15 namespace device { | |
| 16 | |
| 17 // Base class that defines factory methods for PlatformSensor creation. | |
| 18 // Its implementations must be accessed via GetInstance() method. | |
| 19 class DEVICE_GENERIC_SENSOR_EXPORT PlatformSensorProviderBase { | |
| 20 public: | |
| 21 using CreateSensorCallback = | |
| 22 base::Callback<void(scoped_refptr<PlatformSensor>)>; | |
| 23 | |
| 24 // Creates new instance of PlatformSensor. | |
| 25 void CreateSensor(mojom::SensorType type, | |
| 26 const CreateSensorCallback& callback); | |
| 27 | |
| 28 // Gets previously created instance of PlatformSensor by sensor type |type|. | |
| 29 scoped_refptr<PlatformSensor> GetSensor(mojom::SensorType type); | |
| 30 | |
| 31 // Shared buffer getters. | |
| 32 mojo::ScopedSharedBufferHandle CloneSharedBufferHandle(); | |
| 33 | |
| 34 // Returns 'true' if some of sensor instances produced by this provider are | |
| 35 // alive; 'false' otherwise. | |
| 36 bool HasSensors() const; | |
| 37 | |
| 38 // Implementations might want to override this in order to be able | |
| 39 // to read from sensor files. For example, linux does so. | |
| 40 virtual void SetFileTaskRunner( | |
| 41 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner) {} | |
| 42 | |
| 43 protected: | |
| 44 PlatformSensorProviderBase(); | |
| 45 virtual ~PlatformSensorProviderBase(); | |
| 46 | |
| 47 // Method that must be implemented by platform specific classes. | |
| 48 virtual void CreateSensorInternal(mojom::SensorType type, | |
| 49 mojo::ScopedSharedBufferMapping mapping, | |
| 50 const CreateSensorCallback& callback) = 0; | |
| 51 | |
| 52 // Implementations might override this method to free resources when there | |
| 53 // are no sensors left. | |
| 54 virtual void AllSensorsRemoved() {} | |
| 55 | |
| 56 void NotifySensorCreated(mojom::SensorType type, | |
| 57 scoped_refptr<PlatformSensor> sensor); | |
| 58 | |
| 59 std::vector<mojom::SensorType> GetPendingRequestTypes(); | |
| 60 | |
| 61 mojo::ScopedSharedBufferMapping MapSharedBufferForType( | |
| 62 mojom::SensorType type); | |
| 63 | |
| 64 THREAD_CHECKER(thread_checker_); | |
| 65 | |
| 66 private: | |
| 67 friend class PlatformSensor; // To call RemoveSensor(); | |
| 68 | |
| 69 bool CreateSharedBufferIfNeeded(); | |
| 70 void RemoveSensor(mojom::SensorType type); | |
| 71 | |
| 72 private: | |
| 73 using CallbackQueue = std::vector<CreateSensorCallback>; | |
| 74 | |
| 75 std::map<mojom::SensorType, PlatformSensor*> sensor_map_; | |
| 76 std::map<mojom::SensorType, CallbackQueue> requests_map_; | |
| 77 mojo::ScopedSharedBufferHandle shared_buffer_handle_; | |
| 78 | |
| 79 DISALLOW_COPY_AND_ASSIGN(PlatformSensorProviderBase); | |
| 80 }; | |
| 81 | |
| 82 } // namespace device | |
| 83 | |
| 84 #endif // DEVICE_GENERIC_SENSOR_PLATFORM_SENSOR_PROVIDER_BASE_H_ | |
| OLD | NEW |