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 #include "ui/events/devices/device_data_manager.h" | 5 #include "ui/events/devices/device_data_manager.h" |
6 | 6 |
7 #include "base/at_exit.h" | 7 #include "base/at_exit.h" |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "ui/events/devices/input_device_event_observer.h" | 10 #include "ui/events/devices/input_device_event_observer.h" |
11 #include "ui/gfx/display.h" | 11 #include "ui/gfx/display.h" |
12 #include "ui/gfx/geometry/point3_f.h" | 12 #include "ui/gfx/geometry/point3_f.h" |
13 | 13 |
| 14 // This macro provides the implementation for the observer notification methods. |
| 15 #define NOTIFY_OBSERVERS(method_name, observer_method) \ |
| 16 void DeviceDataManager::method_name() { \ |
| 17 FOR_EACH_OBSERVER(InputDeviceEventObserver, observers_, \ |
| 18 observer_method()); \ |
| 19 } |
| 20 |
14 namespace ui { | 21 namespace ui { |
15 | 22 |
16 namespace { | 23 namespace { |
17 | 24 |
18 bool InputDeviceEquals(const ui::InputDevice& a, const ui::InputDevice& b) { | 25 bool InputDeviceEquals(const ui::InputDevice& a, const ui::InputDevice& b) { |
19 return a.id == b.id; | 26 return a.id == b.id; |
20 } | 27 } |
21 | 28 |
22 } // namespace | 29 } // namespace |
23 | 30 |
24 // static | 31 // static |
25 DeviceDataManager* DeviceDataManager::instance_ = NULL; | 32 DeviceDataManager* DeviceDataManager::instance_ = NULL; |
26 | 33 |
27 DeviceDataManager::DeviceDataManager() { | 34 DeviceDataManager::DeviceDataManager() { |
28 ClearTouchDeviceAssociations(); | 35 ClearTouchDeviceAssociations(); |
29 } | 36 } |
30 | 37 |
31 DeviceDataManager::~DeviceDataManager() { | 38 DeviceDataManager::~DeviceDataManager() { |
32 } | 39 } |
33 | 40 |
34 // static | 41 // static |
35 DeviceDataManager* DeviceDataManager::instance() { return instance_; } | 42 DeviceDataManager* DeviceDataManager::instance() { return instance_; } |
36 | 43 |
37 void DeviceDataManager::set_instance(DeviceDataManager* instance) { | 44 void DeviceDataManager::set_instance(DeviceDataManager* instance) { |
38 CHECK(!instance_) << "Can not set multiple instances of DeviceDataManager"; | 45 DCHECK(instance) |
| 46 << "Must reset the DeviceDataManager using DeleteInstance()."; |
| 47 DCHECK(!instance_) "Can not set multiple instances of DeviceDataManager."; |
39 instance_ = instance; | 48 instance_ = instance; |
40 } | 49 } |
41 | 50 |
42 // static | 51 // static |
43 void DeviceDataManager::CreateInstance() { | 52 void DeviceDataManager::CreateInstance() { |
44 if (instance()) | 53 if (instance()) |
45 return; | 54 return; |
46 | 55 |
47 set_instance(new DeviceDataManager()); | 56 set_instance(new DeviceDataManager()); |
48 | 57 |
| 58 // TODO(bruthig): Replace the DeleteInstance callbacks with explicit calls. |
49 base::AtExitManager::RegisterTask(base::Bind(DeleteInstance)); | 59 base::AtExitManager::RegisterTask(base::Bind(DeleteInstance)); |
50 } | 60 } |
51 | 61 |
52 void DeviceDataManager::DeleteInstance() { | 62 void DeviceDataManager::DeleteInstance() { |
53 if (instance_) { | 63 if (instance_) { |
54 delete instance_; | 64 delete instance_; |
55 instance_ = NULL; | 65 instance_ = NULL; |
56 } | 66 } |
57 } | 67 } |
58 | 68 |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
125 void DeviceDataManager::OnTouchscreenDevicesUpdated( | 135 void DeviceDataManager::OnTouchscreenDevicesUpdated( |
126 const std::vector<TouchscreenDevice>& devices) { | 136 const std::vector<TouchscreenDevice>& devices) { |
127 if (devices.size() == touchscreen_devices_.size() && | 137 if (devices.size() == touchscreen_devices_.size() && |
128 std::equal(devices.begin(), | 138 std::equal(devices.begin(), |
129 devices.end(), | 139 devices.end(), |
130 touchscreen_devices_.begin(), | 140 touchscreen_devices_.begin(), |
131 InputDeviceEquals)) { | 141 InputDeviceEquals)) { |
132 return; | 142 return; |
133 } | 143 } |
134 touchscreen_devices_ = devices; | 144 touchscreen_devices_ = devices; |
135 FOR_EACH_OBSERVER(InputDeviceEventObserver, | 145 NotifyObserversTouchscreenDeviceConfigurationChanged(); |
136 observers_, | |
137 OnTouchscreenDeviceConfigurationChanged()); | |
138 } | 146 } |
139 | 147 |
140 void DeviceDataManager::OnKeyboardDevicesUpdated( | 148 void DeviceDataManager::OnKeyboardDevicesUpdated( |
141 const std::vector<KeyboardDevice>& devices) { | 149 const std::vector<KeyboardDevice>& devices) { |
142 if (devices.size() == keyboard_devices_.size() && | 150 if (devices.size() == keyboard_devices_.size() && |
143 std::equal(devices.begin(), | 151 std::equal(devices.begin(), |
144 devices.end(), | 152 devices.end(), |
145 keyboard_devices_.begin(), | 153 keyboard_devices_.begin(), |
146 InputDeviceEquals)) { | 154 InputDeviceEquals)) { |
147 return; | 155 return; |
148 } | 156 } |
149 keyboard_devices_ = devices; | 157 keyboard_devices_ = devices; |
150 FOR_EACH_OBSERVER(InputDeviceEventObserver, | 158 NotifyObserversKeyboardDeviceConfigurationChanged(); |
151 observers_, | |
152 OnKeyboardDeviceConfigurationChanged()); | |
153 } | 159 } |
154 | 160 |
155 void DeviceDataManager::OnMouseDevicesUpdated( | 161 void DeviceDataManager::OnMouseDevicesUpdated( |
156 const std::vector<InputDevice>& devices) { | 162 const std::vector<InputDevice>& devices) { |
157 if (devices.size() == mouse_devices_.size() && | 163 if (devices.size() == mouse_devices_.size() && |
158 std::equal(devices.begin(), | 164 std::equal(devices.begin(), |
159 devices.end(), | 165 devices.end(), |
160 mouse_devices_.begin(), | 166 mouse_devices_.begin(), |
161 InputDeviceEquals)) { | 167 InputDeviceEquals)) { |
162 return; | 168 return; |
163 } | 169 } |
164 mouse_devices_ = devices; | 170 mouse_devices_ = devices; |
165 FOR_EACH_OBSERVER(InputDeviceEventObserver, | 171 NotifyObserversMouseDeviceConfigurationChanged(); |
166 observers_, | |
167 OnMouseDeviceConfigurationChanged()); | |
168 } | 172 } |
169 | 173 |
170 void DeviceDataManager::OnTouchpadDevicesUpdated( | 174 void DeviceDataManager::OnTouchpadDevicesUpdated( |
171 const std::vector<InputDevice>& devices) { | 175 const std::vector<InputDevice>& devices) { |
172 if (devices.size() == touchpad_devices_.size() && | 176 if (devices.size() == touchpad_devices_.size() && |
173 std::equal(devices.begin(), | 177 std::equal(devices.begin(), |
174 devices.end(), | 178 devices.end(), |
175 touchpad_devices_.begin(), | 179 touchpad_devices_.begin(), |
176 InputDeviceEquals)) { | 180 InputDeviceEquals)) { |
177 return; | 181 return; |
178 } | 182 } |
179 touchpad_devices_ = devices; | 183 touchpad_devices_ = devices; |
180 FOR_EACH_OBSERVER(InputDeviceEventObserver, | 184 NotifyObserversTouchpadDeviceConfigurationChanged(); |
181 observers_, | |
182 OnTouchpadDeviceConfigurationChanged()); | |
183 } | 185 } |
184 | 186 |
185 void DeviceDataManager::OnDeviceListsComplete() { | 187 void DeviceDataManager::OnDeviceListsComplete() { |
186 if (!device_lists_complete_) { | 188 if (!device_lists_complete_) { |
187 device_lists_complete_ = true; | 189 device_lists_complete_ = true; |
188 FOR_EACH_OBSERVER(InputDeviceEventObserver, observers_, | 190 NotifyObserversDeviceListsComplete(); |
189 OnDeviceListsComplete()); | |
190 } | 191 } |
191 } | 192 } |
192 | 193 |
| 194 NOTIFY_OBSERVERS(NotifyObserversTouchscreenDeviceConfigurationChanged, |
| 195 OnTouchscreenDeviceConfigurationChanged); |
| 196 |
| 197 NOTIFY_OBSERVERS(NotifyObserversKeyboardDeviceConfigurationChanged, |
| 198 OnKeyboardDeviceConfigurationChanged); |
| 199 |
| 200 NOTIFY_OBSERVERS(NotifyObserversMouseDeviceConfigurationChanged, |
| 201 OnMouseDeviceConfigurationChanged); |
| 202 |
| 203 NOTIFY_OBSERVERS(NotifyObserversTouchpadDeviceConfigurationChanged, |
| 204 OnTouchpadDeviceConfigurationChanged); |
| 205 |
| 206 NOTIFY_OBSERVERS(NotifyObserversDeviceListsComplete, OnDeviceListsComplete); |
| 207 |
193 void DeviceDataManager::AddObserver(InputDeviceEventObserver* observer) { | 208 void DeviceDataManager::AddObserver(InputDeviceEventObserver* observer) { |
194 observers_.AddObserver(observer); | 209 observers_.AddObserver(observer); |
195 } | 210 } |
196 | 211 |
197 void DeviceDataManager::RemoveObserver(InputDeviceEventObserver* observer) { | 212 void DeviceDataManager::RemoveObserver(InputDeviceEventObserver* observer) { |
198 observers_.RemoveObserver(observer); | 213 observers_.RemoveObserver(observer); |
199 } | 214 } |
200 | 215 |
201 } // namespace ui | 216 } // namespace ui |
OLD | NEW |