| 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/threading/thread_checker.h" |
| 9 #include "device/generic_sensor/generic_sensor_export.h" | 11 #include "device/generic_sensor/generic_sensor_export.h" |
| 10 | 12 |
| 11 namespace base { | 13 namespace base { |
| 12 class SingleThreadTaskRunner; | 14 class SingleThreadTaskRunner; |
| 13 } | 15 } |
| 14 | 16 |
| 15 namespace device { | 17 namespace device { |
| 16 | 18 |
| 17 class PlatformSensorLinux; | |
| 18 class PlatformSensorConfiguration; | 19 class PlatformSensorConfiguration; |
| 19 struct SensorInfoLinux; | 20 struct SensorInfoLinux; |
| 21 struct SensorReading; |
| 20 | 22 |
| 21 // A generic reader class that can be implemented with two different strategies: | 23 // A generic reader class that can be implemented with two different strategies: |
| 22 // polling and on trigger. | 24 // polling and on trigger. All methods are not thread-safe and must be called |
| 25 // on a polling thread that allows I/O. |
| 23 class SensorReader { | 26 class SensorReader { |
| 24 public: | 27 public: |
| 28 using UpdateSensorReadingCallback = base::Callback<void(SensorReading)>; |
| 29 using NotifyReadErrorCallback = base::Callback<void()>; |
| 30 |
| 25 // Creates a new instance of SensorReader. At the moment, only polling | 31 // Creates a new instance of SensorReader. At the moment, only polling |
| 26 // reader is supported. | 32 // reader is supported. |
| 27 static std::unique_ptr<SensorReader> Create( | 33 static std::unique_ptr<SensorReader> Create( |
| 28 const SensorInfoLinux* sensor_device, | 34 const SensorInfoLinux* sensor_device, |
| 29 PlatformSensorLinux* sensor, | 35 scoped_refptr<base::SingleThreadTaskRunner> task_runner, |
| 30 scoped_refptr<base::SingleThreadTaskRunner> polling_thread_task_runner); | 36 const UpdateSensorReadingCallback& notify_new_readings_cb, |
| 37 const NotifyReadErrorCallback& notify_read_error_cb); |
| 31 | 38 |
| 32 virtual ~SensorReader(); | 39 virtual ~SensorReader(); |
| 33 | 40 |
| 34 // Starts fetching data based on strategy this reader has chosen. | 41 // Starts fetching data based on strategy this reader has chosen. |
| 35 // Only polling strategy is supported at the moment. Thread safe. | 42 // Only polling strategy is supported at the moment. |
| 36 virtual bool StartFetchingData( | 43 virtual void StartFetchingData( |
| 37 const PlatformSensorConfiguration& configuration) = 0; | 44 const PlatformSensorConfiguration& configuration) = 0; |
| 38 | 45 |
| 39 // Stops fetching data. Thread safe. | 46 // Stops fetching data. |
| 40 virtual void StopFetchingData() = 0; | 47 virtual void StopFetchingData() = 0; |
| 41 | 48 |
| 42 protected: | 49 protected: |
| 43 SensorReader(PlatformSensorLinux* sensor, | 50 SensorReader(scoped_refptr<base::SingleThreadTaskRunner> task_runner, |
| 44 scoped_refptr<base::SingleThreadTaskRunner> polling_task_runner); | 51 const UpdateSensorReadingCallback& notify_new_readings_cb, |
| 52 const NotifyReadErrorCallback& notify_read_error_cb); |
| 45 | 53 |
| 46 // Notifies |sensor_| about an error. | 54 // Notifies |sensor_| about an error. |
| 47 void NotifyReadError(); | 55 void NotifyReadError(); |
| 48 | 56 |
| 49 // Non-owned pointer to a sensor that created this reader. | 57 // In builds with DCHECK enabled checks that methods of this |
| 50 PlatformSensorLinux* sensor_; | 58 // and derived classes are called on a right thread. |
| 59 base::ThreadChecker thread_checker_; |
| 51 | 60 |
| 52 // A task runner that is used to poll data. | 61 // Used for notifications about new readings and errors. |
| 62 // Can be run on any thread, because this callbacks are attached |
| 63 // to the same MessageLoop where they are created. |
| 64 UpdateSensorReadingCallback notify_new_readings_cb_; |
| 65 NotifyReadErrorCallback notify_read_error_cb_; |
| 66 |
| 67 // A task runner that belongs to a thread this reader is created on. |
| 53 scoped_refptr<base::SingleThreadTaskRunner> polling_task_runner_; | 68 scoped_refptr<base::SingleThreadTaskRunner> polling_task_runner_; |
| 54 | 69 |
| 55 // A task runner that belongs to a thread this reader is created on. | 70 // A task runner that is used to report reading updates and errors |
| 56 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | 71 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| 57 | 72 |
| 58 // Indicates if reading is active. | 73 // Indicates if reading is active. |
| 59 bool is_reading_active_; | 74 bool is_reading_active_; |
| 60 | 75 |
| 61 DISALLOW_COPY_AND_ASSIGN(SensorReader); | 76 DISALLOW_COPY_AND_ASSIGN(SensorReader); |
| 62 }; | 77 }; |
| 63 | 78 |
| 64 } // namespace device | 79 } // namespace device |
| 65 | 80 |
| 66 #endif // DEVICE_GENERIC_SENSOR_PLATFORM_SENSOR_READER_LINUX_H_ | 81 #endif // DEVICE_GENERIC_SENSOR_PLATFORM_SENSOR_READER_LINUX_H_ |
| OLD | NEW |