Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 CHROMEOS_ACCELEROMETER_ACCELEROMETER_READER_H_ | 5 #ifndef CHROMEOS_ACCELEROMETER_ACCELEROMETER_READER_H_ |
| 6 #define CHROMEOS_ACCELEROMETER_ACCELEROMETER_READER_H_ | 6 #define CHROMEOS_ACCELEROMETER_ACCELEROMETER_READER_H_ |
| 7 | 7 |
| 8 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
| 9 #include "base/memory/weak_ptr.h" | 9 #include "base/memory/weak_ptr.h" |
| 10 #include "base/observer_list.h" | 10 #include "base/observer_list_threadsafe.h" |
| 11 #include "chromeos/accelerometer/accelerometer_types.h" | 11 #include "chromeos/accelerometer/accelerometer_types.h" |
| 12 #include "chromeos/chromeos_export.h" | 12 #include "chromeos/chromeos_export.h" |
| 13 | 13 |
| 14 template <typename T> | 14 template <typename T> |
| 15 struct DefaultSingletonTraits; | 15 struct DefaultSingletonTraits; |
| 16 | 16 |
| 17 namespace base { | 17 namespace base { |
| 18 class TaskRunner; | 18 class TaskRunner; |
| 19 } | 19 } |
| 20 | 20 |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 43 | 43 |
| 44 // Index of each accelerometer axis in data stream. | 44 // Index of each accelerometer axis in data stream. |
| 45 int index[ACCELEROMETER_SOURCE_COUNT][3]; | 45 int index[ACCELEROMETER_SOURCE_COUNT][3]; |
| 46 }; | 46 }; |
| 47 typedef base::RefCountedData<ConfigurationData> Configuration; | 47 typedef base::RefCountedData<ConfigurationData> Configuration; |
| 48 typedef base::RefCountedData<char[12]> Reading; | 48 typedef base::RefCountedData<char[12]> Reading; |
| 49 | 49 |
| 50 // An interface to receive data from the AccelerometerReader. | 50 // An interface to receive data from the AccelerometerReader. |
| 51 class Observer { | 51 class Observer { |
| 52 public: | 52 public: |
| 53 virtual void OnAccelerometerUpdated(const AccelerometerUpdate& update) = 0; | 53 virtual void OnAccelerometerUpdated(const AccelerometerUpdate* update) = 0; |
|
flackr
2015/03/02 15:46:40
I think this should pass a scoped_refptr<const chr
jonross
2015/03/04 21:54:50
Done.
| |
| 54 | 54 |
| 55 protected: | 55 protected: |
| 56 virtual ~Observer() {} | 56 virtual ~Observer() {} |
| 57 }; | 57 }; |
| 58 | 58 |
| 59 static AccelerometerReader* GetInstance(); | 59 static AccelerometerReader* GetInstance(); |
| 60 | 60 |
| 61 void Initialize(scoped_refptr<base::TaskRunner> blocking_task_runner); | 61 void Initialize(scoped_refptr<base::TaskRunner> blocking_task_runner); |
| 62 | 62 |
| 63 // Add/Remove observers. | 63 // Add/Remove observers. |
| 64 void AddObserver(Observer* observer); | 64 void AddObserver(Observer* observer); |
| 65 void RemoveObserver(Observer* observer); | 65 void RemoveObserver(Observer* observer); |
| 66 bool HasObserver(Observer* observer); | |
| 66 | 67 |
| 67 protected: | 68 protected: |
| 68 AccelerometerReader(); | 69 AccelerometerReader(); |
| 69 virtual ~AccelerometerReader(); | 70 virtual ~AccelerometerReader(); |
| 70 | 71 |
| 71 private: | 72 private: |
| 72 friend struct DefaultSingletonTraits<AccelerometerReader>; | 73 friend struct DefaultSingletonTraits<AccelerometerReader>; |
| 73 | 74 |
| 74 // Dispatched when initialization is complete. If |success|, |configuration| | 75 // Dispatched when initialization is complete. If |success|, |configuration| |
| 75 // provides the details of the detected accelerometer. | 76 // provides the details of the detected accelerometer. |
| 76 void OnInitialized(scoped_refptr<Configuration> configuration, bool success); | 77 void OnInitialized(scoped_refptr<Configuration> configuration, bool success); |
| 77 | 78 |
| 78 // Triggers an asynchronous read from the accelerometer, signalling | 79 // Triggers an asynchronous read from the accelerometer, signalling |
| 79 // OnDataRead with the result. | 80 // OnDataRead with the result. |
| 80 void TriggerRead(); | 81 void TriggerRead(); |
| 81 | 82 |
| 82 // If |success|, converts the raw reading to an AccelerometerUpdate | 83 // If |success|, converts the raw reading to an AccelerometerUpdate |
| 83 // message and notifies the |delegate_| with the new readings. | 84 // message and notifies the |delegate_| with the new readings. |
| 84 // Triggers another read from the accelerometer at the current sampling rate. | 85 // Triggers another read from the accelerometer at the current sampling rate. |
| 85 void OnDataRead(scoped_refptr<Reading> reading, bool success); | 86 void OnDataRead(scoped_refptr<Reading> reading, bool success); |
| 86 | 87 |
| 87 // The task runner to use for blocking tasks. | 88 // The task runner to use for blocking tasks. |
| 88 scoped_refptr<base::TaskRunner> task_runner_; | 89 scoped_refptr<base::TaskRunner> task_runner_; |
| 89 | 90 |
| 90 // The last seen accelerometer data. | 91 // The last seen accelerometer data. |
| 91 AccelerometerUpdate update_; | 92 scoped_refptr<AccelerometerUpdate> update_; |
| 92 | |
| 93 // True if a valid accelerometer update is available. | |
| 94 bool has_update_; | |
| 95 | 93 |
| 96 // The accelerometer configuration. | 94 // The accelerometer configuration. |
| 97 scoped_refptr<Configuration> configuration_; | 95 scoped_refptr<Configuration> configuration_; |
| 98 | 96 |
| 99 ObserverList<Observer, true> observers_; | 97 scoped_refptr<ObserverListThreadSafe<Observer>> observers_; |
| 100 | 98 |
| 101 base::WeakPtrFactory<AccelerometerReader> weak_factory_; | 99 base::WeakPtrFactory<AccelerometerReader> weak_factory_; |
| 102 | 100 |
| 103 DISALLOW_COPY_AND_ASSIGN(AccelerometerReader); | 101 DISALLOW_COPY_AND_ASSIGN(AccelerometerReader); |
| 104 }; | 102 }; |
| 105 | 103 |
| 106 } // namespace chromeos | 104 } // namespace chromeos |
| 107 | 105 |
| 108 #endif // CHROMEOS_ACCELEROMETER_ACCELEROMETER_READER_H_ | 106 #endif // CHROMEOS_ACCELEROMETER_ACCELEROMETER_READER_H_ |
| OLD | NEW |