OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef DEVICE_GENERIC_SENSOR_PLATFORM_SENSOR_READER_LINUX_H_ | 5 #ifndef DEVICE_GENERIC_SENSOR_PLATFORM_SENSOR_READER_LINUX_H_ |
6 #define DEVICE_GENERIC_SENSOR_PLATFORM_SENSOR_READER_LINUX_H_ | 6 #define DEVICE_GENERIC_SENSOR_PLATFORM_SENSOR_READER_LINUX_H_ |
7 | 7 |
| 8 #include "base/callback.h" |
8 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
| 10 #include "base/memory/weak_ptr.h" |
| 11 #include "base/threading/thread_checker.h" |
| 12 #include "device/generic_sensor/generic_sensor_export.h" |
9 | 13 |
10 namespace base { | 14 namespace base { |
11 class SingleThreadTaskRunner; | 15 class SingleThreadTaskRunner; |
12 } | 16 } |
13 | 17 |
14 namespace device { | 18 namespace device { |
15 | 19 |
| 20 class PlatformSensorConfiguration; |
16 class PlatformSensorLinux; | 21 class PlatformSensorLinux; |
17 class PlatformSensorConfiguration; | |
18 struct SensorInfoLinux; | 22 struct SensorInfoLinux; |
19 | 23 |
20 // A generic reader class that can be implemented with two different strategies: | 24 // A generic reader class that can be implemented with two different strategies: |
21 // polling and on trigger. | 25 // polling and on trigger. All methods are not thread-safe and must be called |
| 26 // on a polling thread that allows I/O. |
22 class SensorReader { | 27 class SensorReader { |
23 public: | 28 public: |
24 // Creates a new instance of SensorReader. At the moment, only polling | 29 // Creates a new instance of SensorReader. At the moment, only polling |
25 // reader is supported. | 30 // reader is supported. |
26 static std::unique_ptr<SensorReader> Create( | 31 static std::unique_ptr<SensorReader> Create( |
27 const SensorInfoLinux* sensor_device, | 32 const SensorInfoLinux* sensor_device, |
28 PlatformSensorLinux* sensor, | 33 base::WeakPtr<PlatformSensorLinux> sensor, |
29 scoped_refptr<base::SingleThreadTaskRunner> polling_thread_task_runner); | 34 scoped_refptr<base::SingleThreadTaskRunner> task_runner); |
30 | 35 |
31 virtual ~SensorReader(); | 36 virtual ~SensorReader(); |
32 | 37 |
33 // Starts fetching data based on strategy this reader has chosen. | 38 // Starts fetching data based on strategy this reader has chosen. |
34 // Only polling strategy is supported at the moment. Thread safe. | 39 // Only polling strategy is supported at the moment. |
35 virtual bool StartFetchingData( | 40 virtual void StartFetchingData( |
36 const PlatformSensorConfiguration& configuration) = 0; | 41 const PlatformSensorConfiguration& configuration) = 0; |
37 | 42 |
38 // Stops fetching data. Thread safe. | 43 // Stops fetching data. |
39 virtual void StopFetchingData() = 0; | 44 virtual void StopFetchingData() = 0; |
40 | 45 |
41 protected: | 46 protected: |
42 SensorReader(PlatformSensorLinux* sensor, | 47 SensorReader(base::WeakPtr<PlatformSensorLinux> sensor, |
43 scoped_refptr<base::SingleThreadTaskRunner> polling_task_runner); | 48 scoped_refptr<base::SingleThreadTaskRunner> task_runner); |
44 | 49 |
45 // Notifies |sensor_| about an error. | 50 // Notifies |sensor_| about an error. |
46 void NotifyReadError(); | 51 void NotifyReadError(); |
47 | 52 |
48 // Non-owned pointer to a sensor that created this reader. | 53 // In builds with DCHECK enabled checks that methods of this |
49 PlatformSensorLinux* sensor_; | 54 // and derived classes are called on a right thread. |
| 55 base::ThreadChecker thread_checker_; |
50 | 56 |
51 // A task runner that is used to poll data. | 57 // A sensor that this reader is owned by and notifies about errors and |
52 scoped_refptr<base::SingleThreadTaskRunner> polling_task_runner_; | 58 // readings to. |
| 59 base::WeakPtr<PlatformSensorLinux> sensor_; |
53 | 60 |
54 // A task runner that belongs to a thread this reader is created on. | 61 // A task runner that is used to report about new readings and errors |
| 62 // to a |sensor_|. |
55 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | 63 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
56 | 64 |
57 // Indicates if reading is active. | 65 // Indicates if reading is active. |
58 bool is_reading_active_; | 66 bool is_reading_active_; |
59 | 67 |
60 DISALLOW_COPY_AND_ASSIGN(SensorReader); | 68 DISALLOW_COPY_AND_ASSIGN(SensorReader); |
61 }; | 69 }; |
62 | 70 |
63 } // namespace device | 71 } // namespace device |
64 | 72 |
65 #endif // DEVICE_GENERIC_SENSOR_PLATFORM_SENSOR_READER_LINUX_H_ | 73 #endif // DEVICE_GENERIC_SENSOR_PLATFORM_SENSOR_READER_LINUX_H_ |
OLD | NEW |