platform_sensor.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. #ifndef SERVICES_DEVICE_GENERIC_SENSOR_PLATFORM_SENSOR_H_
  5. #define SERVICES_DEVICE_GENERIC_SENSOR_PLATFORM_SENSOR_H_
  6. #include <list>
  7. #include <map>
  8. #include <memory>
  9. #include "base/callback_forward.h"
  10. #include "base/location.h"
  11. #include "base/memory/raw_ptr.h"
  12. #include "base/memory/ref_counted.h"
  13. #include "base/memory/weak_ptr.h"
  14. #include "base/observer_list.h"
  15. #include "base/synchronization/lock.h"
  16. #include "base/task/sequenced_task_runner.h"
  17. #include "base/thread_annotations.h"
  18. #include "mojo/public/cpp/system/buffer.h"
  19. #include "services/device/public/cpp/generic_sensor/sensor_reading.h"
  20. #include "services/device/public/mojom/sensor.mojom.h"
  21. #include "third_party/abseil-cpp/absl/types/optional.h"
  22. namespace device {
  23. class PlatformSensorProvider;
  24. class PlatformSensorConfiguration;
  25. // Base class for the sensors provided by the platform. Concrete instances of
  26. // this class are created by platform specific PlatformSensorProvider.
  27. class PlatformSensor : public base::RefCountedThreadSafe<PlatformSensor> {
  28. public:
  29. // The interface that must be implemented by PlatformSensor clients.
  30. class Client {
  31. public:
  32. virtual void OnSensorReadingChanged(mojom::SensorType type) = 0;
  33. virtual void OnSensorError() = 0;
  34. virtual bool IsSuspended() = 0;
  35. protected:
  36. virtual ~Client() {}
  37. };
  38. PlatformSensor(const PlatformSensor&) = delete;
  39. PlatformSensor& operator=(const PlatformSensor&) = delete;
  40. virtual mojom::ReportingMode GetReportingMode() = 0;
  41. virtual PlatformSensorConfiguration GetDefaultConfiguration() = 0;
  42. virtual bool CheckSensorConfiguration(
  43. const PlatformSensorConfiguration& configuration) = 0;
  44. // Can be overridden to return the sensor maximum sampling frequency
  45. // value obtained from the platform if it is available. If platform
  46. // does not provide maximum sampling frequency this method must
  47. // return default frequency.
  48. // The default implementation returns default frequency.
  49. virtual double GetMaximumSupportedFrequency();
  50. // Can be overridden to return the sensor minimum sampling frequency.
  51. // The default implementation returns '1.0 / (60 * 60)', i.e. once per hour.
  52. virtual double GetMinimumSupportedFrequency();
  53. // Can be overridden to reset this sensor by the PlatformSensorProvider.
  54. virtual void SensorReplaced();
  55. // Checks if new value is significantly different than old value.
  56. // When the reading we get does not differ significantly from our current
  57. // value, we discard this reading and do not emit any events. This is a
  58. // privacy measure to avoid giving readings that are too specific.
  59. virtual bool IsSignificantlyDifferent(const SensorReading& lhs,
  60. const SensorReading& rhs,
  61. mojom::SensorType sensor_type);
  62. mojom::SensorType GetType() const;
  63. bool StartListening(Client* client,
  64. const PlatformSensorConfiguration& config);
  65. bool StopListening(Client* client, const PlatformSensorConfiguration& config);
  66. // Stops all the configurations tied to the |client|, but the |client| still
  67. // gets notification.
  68. bool StopListening(Client* client);
  69. void UpdateSensor();
  70. void AddClient(Client*);
  71. void RemoveClient(Client*);
  72. bool GetLatestReading(SensorReading* result);
  73. // Return the last raw (i.e. unrounded) sensor reading.
  74. bool GetLatestRawReading(SensorReading* result) const;
  75. // Returns 'true' if the sensor is started; returns 'false' otherwise.
  76. bool IsActiveForTesting() const;
  77. using ConfigMap = std::map<Client*, std::list<PlatformSensorConfiguration>>;
  78. const ConfigMap& GetConfigMapForTesting() const;
  79. // Called by API users to post a task on |main_task_runner_| when run from a
  80. // different sequence.
  81. void PostTaskToMainSequence(const base::Location& location,
  82. base::OnceClosure task);
  83. protected:
  84. virtual ~PlatformSensor();
  85. PlatformSensor(mojom::SensorType type,
  86. SensorReadingSharedBuffer* reading_buffer,
  87. PlatformSensorProvider* provider);
  88. using ReadingBuffer = SensorReadingSharedBuffer;
  89. virtual bool UpdateSensorInternal(const ConfigMap& configurations);
  90. virtual bool StartSensor(
  91. const PlatformSensorConfiguration& configuration) = 0;
  92. virtual void StopSensor() = 0;
  93. // Updates the shared buffer with new sensor reading data and posts a task to
  94. // invoke NotifySensorReadingChanged() on |main_task_runner_|.
  95. // Note: this method is thread-safe.
  96. void UpdateSharedBufferAndNotifyClients(const SensorReading& reading);
  97. void NotifySensorReadingChanged();
  98. void NotifySensorError();
  99. void ResetReadingBuffer();
  100. // Returns the task runner where this object has been created. Subclasses can
  101. // use it to post tasks to the right sequence when running on a different task
  102. // runner.
  103. scoped_refptr<base::SequencedTaskRunner> main_task_runner() const {
  104. return main_task_runner_;
  105. }
  106. base::ObserverList<Client, true>::Unchecked clients_;
  107. private:
  108. friend class base::RefCountedThreadSafe<PlatformSensor>;
  109. // Updates shared buffer with provided SensorReading. If
  110. // |do_significance_check| is true then |last_raw_reading_| and
  111. // |reading_buffer_| are only updated if |reading| is significantly different
  112. // from |last_raw_reading_|. Returns true if |reading_buffer_| has been
  113. // updated, and false otherwise.
  114. // Note: this method is thread-safe.
  115. bool UpdateSharedBuffer(const SensorReading& reading,
  116. bool do_significance_check);
  117. scoped_refptr<base::SequencedTaskRunner> main_task_runner_;
  118. raw_ptr<SensorReadingSharedBuffer>
  119. reading_buffer_; // NOTE: Owned by |provider_|.
  120. mojom::SensorType type_;
  121. ConfigMap config_map_;
  122. raw_ptr<PlatformSensorProvider> provider_;
  123. bool is_active_ = false;
  124. absl::optional<SensorReading> last_raw_reading_ GUARDED_BY(lock_);
  125. absl::optional<SensorReading> last_rounded_reading_ GUARDED_BY(lock_);
  126. // Protect last_raw_reading_ & last_rounded_reading_.
  127. mutable base::Lock lock_;
  128. base::WeakPtrFactory<PlatformSensor> weak_factory_{this};
  129. };
  130. } // namespace device
  131. #endif // SERVICES_DEVICE_GENERIC_SENSOR_PLATFORM_SENSOR_H_