accelerometer_reader.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // Copyright 2014 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 ASH_ACCELEROMETER_ACCELEROMETER_READER_H_
  5. #define ASH_ACCELEROMETER_ACCELEROMETER_READER_H_
  6. #include "ash/accelerometer/accelerometer_types.h"
  7. #include "ash/ash_export.h"
  8. #include "ash/public/cpp/tablet_mode_observer.h"
  9. #include "base/memory/ref_counted.h"
  10. #include "base/no_destructor.h"
  11. #include "base/observer_list.h"
  12. namespace base {
  13. class SequencedTaskRunner;
  14. } // namespace base
  15. namespace ash {
  16. enum class ECLidAngleDriverStatus { UNKNOWN, SUPPORTED, NOT_SUPPORTED };
  17. class AccelerometerProviderInterface;
  18. // AccelerometerReader should only be used on the UI thread.
  19. // It notifies observers if EC Lid Angle Driver is supported, and provides
  20. // accelerometers' (lid and base) samples.
  21. // The current usages of accelerometers' samples are for calculating the angle
  22. // between the lid and the base, which can be substituted by EC Lid Angle
  23. // Driver, if it exists, and the auto rotation, which only needs
  24. // lid-accelerometer's data.
  25. // Therefore, if EC Lid Angle Driver is present, base-accelerometer's samples
  26. // may be ignored and not sent to the observers.
  27. class ASH_EXPORT AccelerometerReader {
  28. public:
  29. // An interface to receive data from the AccelerometerReader.
  30. class Observer {
  31. public:
  32. // Normally called only once, when
  33. // |AcceleromterProviderInterface::ec_lid_angle_driver_status_| is set to
  34. // either SUPPORTED or NOT_SUPPORTED, unless the lid angle driver is
  35. // late-present after timed out, which will be called twice with
  36. // |is_supported| being false and true respectively.
  37. // It's guaranteed to be called before |OnAccelerometerUpdated|.
  38. virtual void OnECLidAngleDriverStatusChanged(bool is_supported) = 0;
  39. virtual void OnAccelerometerUpdated(const AccelerometerUpdate& update) = 0;
  40. protected:
  41. virtual ~Observer() {}
  42. };
  43. static AccelerometerReader* GetInstance();
  44. void Initialize();
  45. // Add/Remove observers.
  46. void AddObserver(Observer* observer);
  47. void RemoveObserver(Observer* observer);
  48. // Accelerometer file reader starts/stops listening to tablet mode controller.
  49. void StartListenToTabletModeController();
  50. void StopListenToTabletModeController();
  51. // Controls the availability of emitting acccelerometer reader events to
  52. // its observers. This shouldn't be called normally, but Tast tests should
  53. // be able to control the accelerometer feature.
  54. void SetEnabled(bool enabled);
  55. void SetECLidAngleDriverStatusForTesting(
  56. ECLidAngleDriverStatus ec_lid_angle_driver_status);
  57. protected:
  58. AccelerometerReader();
  59. AccelerometerReader(const AccelerometerReader&) = delete;
  60. AccelerometerReader& operator=(const AccelerometerReader&) = delete;
  61. virtual ~AccelerometerReader();
  62. private:
  63. friend class base::NoDestructor<AccelerometerReader>;
  64. // Worker that will run on the base::SequencedTaskRunner provided to
  65. // Initialize. It will determine accelerometer configuration, read the data,
  66. // and notify observers.
  67. scoped_refptr<AccelerometerProviderInterface> accelerometer_provider_;
  68. };
  69. class ASH_EXPORT AccelerometerProviderInterface
  70. : public base::RefCountedThreadSafe<AccelerometerProviderInterface>,
  71. public TabletModeObserver {
  72. public:
  73. // Prepare and start async initialization.
  74. virtual void PrepareAndInitialize() = 0;
  75. // With ChromeOS EC lid angle driver present, it's triggered when the device
  76. // is physically used as a tablet (even thought its UI might be in clamshell
  77. // mode), cancelled otherwise.
  78. virtual void TriggerRead() = 0;
  79. virtual void CancelRead() = 0;
  80. // TabletModeObserver:
  81. void OnTabletPhysicalStateChanged() override;
  82. // Add/Remove observers.
  83. void AddObserver(AccelerometerReader::Observer* observer);
  84. void RemoveObserver(AccelerometerReader::Observer* observer);
  85. // Start/Stop listening to tablet mode controller.
  86. void StartListenToTabletModeController();
  87. void StopListenToTabletModeController();
  88. // Set emitting events (samples) to observers or not.
  89. void SetEmitEvents(bool emit_events);
  90. void SetECLidAngleDriverStatusForTesting(ECLidAngleDriverStatus status);
  91. protected:
  92. AccelerometerProviderInterface();
  93. ~AccelerometerProviderInterface() override;
  94. // Used in |OnTabletPhysicalStateChanged()|. As there might be
  95. // initialization steps, each implementation can override this function to
  96. // determine if this class is ready to process the state changed.
  97. // If returns true, |OnTabletPhysicalStateChanged()| will be skipped, and it's
  98. // the implementation's responsibility to call it again when the class is
  99. // ready. If returns false, |OnTabletPhysicalStateChanged()| will be processed
  100. // as usual.
  101. // Default to return false.
  102. virtual bool ShouldDelayOnTabletPhysicalStateChanged();
  103. void SetECLidAngleDriverStatus(ECLidAngleDriverStatus status);
  104. ECLidAngleDriverStatus GetECLidAngleDriverStatus() const;
  105. void NotifyAccelerometerUpdated(const AccelerometerUpdate& update);
  106. // Set in the constructor.
  107. scoped_refptr<base::SequencedTaskRunner> ui_task_runner_;
  108. private:
  109. // State of ChromeOS EC lid angle driver, if SUPPORTED, it means EC can handle
  110. // lid angle calculation.
  111. ECLidAngleDriverStatus ec_lid_angle_driver_status_ =
  112. ECLidAngleDriverStatus::UNKNOWN;
  113. bool emit_events_ = true;
  114. // The observers to notify of accelerometer updates.
  115. // Bound to the UI thread.
  116. base::ObserverList<AccelerometerReader::Observer>::Unchecked observers_;
  117. friend class base::RefCountedThreadSafe<AccelerometerProviderInterface>;
  118. };
  119. } // namespace ash
  120. #endif // ASH_ACCELEROMETER_ACCELEROMETER_READER_H_