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.h" |
(...skipping 27 matching lines...) Expand all Loading... |
38 // Scale of accelerometers (i.e. raw value * scale = m/s^2). | 38 // Scale of accelerometers (i.e. raw value * scale = m/s^2). |
39 float scale[ui::ACCELEROMETER_SOURCE_COUNT][3]; | 39 float scale[ui::ACCELEROMETER_SOURCE_COUNT][3]; |
40 | 40 |
41 // Index of each accelerometer axis in data stream. | 41 // Index of each accelerometer axis in data stream. |
42 int index[ui::ACCELEROMETER_SOURCE_COUNT][3]; | 42 int index[ui::ACCELEROMETER_SOURCE_COUNT][3]; |
43 }; | 43 }; |
44 typedef base::RefCountedData<ConfigurationData> Configuration; | 44 typedef base::RefCountedData<ConfigurationData> Configuration; |
45 typedef base::RefCountedData<char[12]> Reading; | 45 typedef base::RefCountedData<char[12]> Reading; |
46 | 46 |
47 // An interface to receive data from the AccelerometerReader. | 47 // An interface to receive data from the AccelerometerReader. |
48 class Delegate { | 48 class Observer { |
49 public: | 49 public: |
50 virtual ~Delegate() {} | 50 virtual void OnAccelerometerUpdated( |
| 51 const ui::AccelerometerUpdate& update) = 0; |
51 | 52 |
52 virtual void HandleAccelerometerUpdate( | 53 protected: |
53 const ui::AccelerometerUpdate& update) = 0; | 54 virtual ~Observer() {} |
54 }; | 55 }; |
55 | 56 |
56 AccelerometerReader(scoped_refptr<base::TaskRunner> blocking_task_runner, | 57 AccelerometerReader(); |
57 Delegate* delegate); | |
58 ~AccelerometerReader(); | 58 ~AccelerometerReader(); |
59 | 59 |
| 60 void Initialize(scoped_refptr<base::TaskRunner> blocking_task_runner); |
| 61 |
| 62 // Add/Remove observers. |
| 63 void AddObserver(Observer* observer); |
| 64 void RemoveObserver(Observer* observer); |
| 65 |
60 private: | 66 private: |
61 // Dispatched when initialization is complete. If |success|, |configuration| | 67 // Dispatched when initialization is complete. If |success|, |configuration| |
62 // provides the details of the detected accelerometer. | 68 // provides the details of the detected accelerometer. |
63 void OnInitialized(scoped_refptr<Configuration> configuration, bool success); | 69 void OnInitialized(scoped_refptr<Configuration> configuration, bool success); |
64 | 70 |
65 // Triggers an asynchronous read from the accelerometer, signalling | 71 // Triggers an asynchronous read from the accelerometer, signalling |
66 // OnDataRead with the result. | 72 // OnDataRead with the result. |
67 void TriggerRead(); | 73 void TriggerRead(); |
68 | 74 |
69 // If |success|, converts the raw reading to an AccelerometerUpdate | 75 // If |success|, converts the raw reading to an AccelerometerUpdate |
70 // message and notifies the |delegate_| with the new readings. | 76 // message and notifies the |delegate_| with the new readings. |
71 // Triggers another read from the accelerometer at the current sampling rate. | 77 // Triggers another read from the accelerometer at the current sampling rate. |
72 void OnDataRead(scoped_refptr<Reading> reading, bool success); | 78 void OnDataRead(scoped_refptr<Reading> reading, bool success); |
73 | 79 |
74 // The task runner to use for blocking tasks. | 80 // The task runner to use for blocking tasks. |
75 scoped_refptr<base::TaskRunner> task_runner_; | 81 scoped_refptr<base::TaskRunner> task_runner_; |
76 | 82 |
77 // A weak pointer to the delegate to send accelerometer readings to. | |
78 Delegate* delegate_; | |
79 | |
80 // The last seen accelerometer data. | 83 // The last seen accelerometer data. |
81 ui::AccelerometerUpdate update_; | 84 ui::AccelerometerUpdate update_; |
82 | 85 |
83 // The accelerometer configuration. | 86 // The accelerometer configuration. |
84 scoped_refptr<Configuration> configuration_; | 87 scoped_refptr<Configuration> configuration_; |
85 | 88 |
| 89 ObserverList<Observer, true> observers_; |
| 90 |
86 base::WeakPtrFactory<AccelerometerReader> weak_factory_; | 91 base::WeakPtrFactory<AccelerometerReader> weak_factory_; |
87 | 92 |
88 DISALLOW_COPY_AND_ASSIGN(AccelerometerReader); | 93 DISALLOW_COPY_AND_ASSIGN(AccelerometerReader); |
89 }; | 94 }; |
90 | 95 |
91 } // namespace chromeos | 96 } // namespace chromeos |
92 | 97 |
93 #endif // CHROMEOS_ACCELEROMETER_ACCELEROMETER_READER_H_ | 98 #endif // CHROMEOS_ACCELEROMETER_ACCELEROMETER_READER_H_ |
OLD | NEW |