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_H_ |
| 6 #define DEVICE_GENERIC_SENSOR_PLATFORM_SENSOR_H_ |
| 7 |
| 8 #include <list> |
| 9 #include <map> |
| 10 |
| 11 #include "base/macros.h" |
| 12 #include "base/memory/ref_counted.h" |
| 13 #include "base/memory/weak_ptr.h" |
| 14 #include "base/observer_list.h" |
| 15 #include "base/single_thread_task_runner.h" |
| 16 #include "device/generic_sensor/generic_sensor_export.h" |
| 17 #include "device/generic_sensor/public/cpp/sensor_reading.h" |
| 18 #include "device/generic_sensor/public/interfaces/sensor.mojom.h" |
| 19 #include "mojo/public/cpp/system/buffer.h" |
| 20 |
| 21 namespace device { |
| 22 |
| 23 class PlatformSensorProvider; |
| 24 class PlatformSensorConfiguration; |
| 25 |
| 26 // Base class for the sensors provided by the platform. Concrete instances of |
| 27 // this class are created by platform specific PlatformSensorProvider. |
| 28 class DEVICE_GENERIC_SENSOR_EXPORT PlatformSensor |
| 29 : public base::RefCountedThreadSafe<PlatformSensor> { |
| 30 public: |
| 31 // The interface that must be implemented by PlatformSensor clients. |
| 32 class Client { |
| 33 public: |
| 34 virtual void OnSensorReadingChanged() = 0; |
| 35 virtual void OnSensorError() = 0; |
| 36 virtual bool IsNotificationSuspended() = 0; |
| 37 |
| 38 protected: |
| 39 virtual ~Client() {} |
| 40 }; |
| 41 |
| 42 virtual mojom::ReportingMode GetReportingMode() = 0; |
| 43 virtual PlatformSensorConfiguration GetDefaultConfiguration() = 0; |
| 44 |
| 45 // Can be overriden to return the sensor maximum sampling frequency |
| 46 // value obtained from the platform if it is available. If platfrom |
| 47 // does not provide maximum sampling frequency this method must |
| 48 // return default frequency. |
| 49 // The default implementation returns default frequency. |
| 50 virtual double GetMaximumSupportedFrequency(); |
| 51 |
| 52 // Can be overriden to return the sensor minimum sampling frequency. |
| 53 // The default implementation returns '1.0 / (60 * 60)', i.e. once per hour. |
| 54 virtual double GetMinimumSupportedFrequency(); |
| 55 |
| 56 mojom::SensorType GetType() const; |
| 57 |
| 58 bool StartListening(Client* client, |
| 59 const PlatformSensorConfiguration& config); |
| 60 bool StopListening(Client* client, const PlatformSensorConfiguration& config); |
| 61 |
| 62 void UpdateSensor(); |
| 63 |
| 64 void AddClient(Client*); |
| 65 void RemoveClient(Client*); |
| 66 |
| 67 protected: |
| 68 virtual ~PlatformSensor(); |
| 69 PlatformSensor(mojom::SensorType type, |
| 70 mojo::ScopedSharedBufferMapping mapping, |
| 71 PlatformSensorProvider* provider); |
| 72 |
| 73 using ConfigMap = std::map<Client*, std::list<PlatformSensorConfiguration>>; |
| 74 using ReadingBuffer = SensorReadingSharedBuffer; |
| 75 |
| 76 virtual bool UpdateSensorInternal(const ConfigMap& configurations); |
| 77 virtual bool StartSensor( |
| 78 const PlatformSensorConfiguration& configuration) = 0; |
| 79 virtual void StopSensor() = 0; |
| 80 virtual bool CheckSensorConfiguration( |
| 81 const PlatformSensorConfiguration& configuration) = 0; |
| 82 |
| 83 // Updates shared buffer with new sensor reading data. |
| 84 // Note: this method is thread-safe. |
| 85 void UpdateSensorReading(const SensorReading& reading, bool notify_clients); |
| 86 |
| 87 void NotifySensorReadingChanged(); |
| 88 void NotifySensorError(); |
| 89 |
| 90 // For testing purposes. |
| 91 const ConfigMap& config_map() const { return config_map_; } |
| 92 |
| 93 // Task runner that is used by mojo objects for the IPC. |
| 94 // If platfrom sensor events are processed on a different |
| 95 // thread, notifications are forwarded to |task_runner_|. |
| 96 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| 97 |
| 98 private: |
| 99 friend class base::RefCountedThreadSafe<PlatformSensor>; |
| 100 const mojo::ScopedSharedBufferMapping shared_buffer_mapping_; |
| 101 mojom::SensorType type_; |
| 102 base::ObserverList<Client, true> clients_; |
| 103 ConfigMap config_map_; |
| 104 PlatformSensorProvider* provider_; |
| 105 base::WeakPtrFactory<PlatformSensor> weak_factory_; |
| 106 DISALLOW_COPY_AND_ASSIGN(PlatformSensor); |
| 107 }; |
| 108 |
| 109 } // namespace device |
| 110 |
| 111 #endif // DEVICE_GENERIC_SENSOR_PLATFORM_SENSOR_H_ |
OLD | NEW |