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