Index: device/sensors/platform_sensor.h |
diff --git a/device/sensors/platform_sensor.h b/device/sensors/platform_sensor.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..cd8e0252ffd77403d9e297464a80b335b9d3b975 |
--- /dev/null |
+++ b/device/sensors/platform_sensor.h |
@@ -0,0 +1,96 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#ifndef DEVICE_SENSORS_PLATFORM_SENSOR_H_ |
+#define DEVICE_SENSORS_PLATFORM_SENSOR_H_ |
+ |
+#include <list> |
+#include <map> |
+ |
+#include "base/macros.h" |
+#include "base/memory/ref_counted.h" |
+#include "base/memory/weak_ptr.h" |
+#include "base/observer_list.h" |
+#include "mojo/public/cpp/system/buffer.h" |
+ |
+namespace device { |
+ |
+class PlatformSensorProvider; |
+class PlatformSensorConfiguration; |
+ |
+// Note: this enum must be in sync with mojo enum SensorType that is defined |
+// in public/interfaces/sensor.mojom. |
+enum class SensorType : int32_t { |
+ FIRST = 1, |
+ AMBIENT_LIGHT = FIRST, |
+ PROXIMITY, |
+ LINEAR_ACCELERATION, |
+ GYROSCOPE, |
+ PRESSURE, |
+ LAST = PRESSURE, |
+}; |
+ |
+// Note: this enum must be in sync with mojo enum ReportingMode that is defined |
+// in public/interfaces/sensor.mojom. |
+enum class ReportingMode : int32_t { |
+ ON_CHANGE, |
+ CONTINUOUS, |
+}; |
+ |
+// Base class for the sensors provided by the platform. Concrete instances of |
+// this class are created by platform specific PlatformSensorProvider. |
+class PlatformSensor : public base::RefCountedThreadSafe<PlatformSensor> { |
+ public: |
+ // The interface that must be implemented by PlatformSensor clients. |
+ class Client { |
+ public: |
+ virtual void OnSensorReadingChanged() {} |
+ virtual void OnSensorError() {} |
+ |
+ protected: |
+ virtual ~Client() {} |
+ }; |
+ |
+ virtual ReportingMode GetReportingMode() = 0; |
+ |
+ SensorType GetType() const; |
+ |
+ bool StartListening(Client* client, |
+ const PlatformSensorConfiguration& config); |
+ bool StopListening(Client* client, const PlatformSensorConfiguration& config); |
+ |
+ void AddClient(Client*); |
+ void RemoveClient(Client*); |
+ |
+ protected: |
+ virtual ~PlatformSensor(); |
+ PlatformSensor(SensorType type, |
+ mojo::ScopedSharedBufferMapping mapping, |
+ PlatformSensorProvider* provider); |
+ |
+ using ConfigMap = std::map<Client*, std::list<PlatformSensorConfiguration>>; |
+ |
+ virtual bool UpdateSensor(const ConfigMap& configurations) = 0; |
+ virtual bool CheckSensorConfiguration( |
+ const PlatformSensorConfiguration& configuration) = 0; |
+ |
+ void NotifySensorReadingChanged(); |
+ void NotifySensorError(); |
+ |
+ mojo::ScopedSharedBufferMapping shared_buffer_mapping_; |
+ |
+ private: |
+ friend class base::RefCountedThreadSafe<PlatformSensor>; |
+ |
+ SensorType type_; |
+ base::ObserverList<Client, true> clients_; |
+ ConfigMap config_map_; |
+ PlatformSensorProvider* provider_; |
+ base::WeakPtrFactory<PlatformSensor> weak_factory_; |
+ DISALLOW_COPY_AND_ASSIGN(PlatformSensor); |
+}; |
+ |
+} // namespace device |
+ |
+#endif // DEVICE_SENSORS_PLATFORM_SENSOR_H_ |