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 "device/hid/input_service_linux.h" | 5 #include "device/hid/input_service_linux.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/lazy_instance.h" | 8 #include "base/lazy_instance.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
73 return result; | 73 return result; |
74 } | 74 } |
75 | 75 |
76 class InputServiceLinuxImpl : public InputServiceLinux, | 76 class InputServiceLinuxImpl : public InputServiceLinux, |
77 public DeviceMonitorLinux::Observer { | 77 public DeviceMonitorLinux::Observer { |
78 public: | 78 public: |
79 // Implements DeviceMonitorLinux::Observer: | 79 // Implements DeviceMonitorLinux::Observer: |
80 void OnDeviceAdded(udev_device* device) override; | 80 void OnDeviceAdded(udev_device* device) override; |
81 void OnDeviceRemoved(udev_device* device) override; | 81 void OnDeviceRemoved(udev_device* device) override; |
82 | 82 |
| 83 // InputServiceLinux overrides: |
| 84 void AddObserver(InputServiceLinux::Observer* observer) override; |
| 85 void RemoveObserver(InputServiceLinux::Observer* observer) override; |
| 86 void GetDevices(std::vector<InputDeviceInfo>* devices) override; |
| 87 bool GetDeviceInfo( |
| 88 const std::string& id, InputDeviceInfo* info) const override; |
| 89 void WillDestroyCurrentMessageLoop() override; |
| 90 |
| 91 protected: |
| 92 bool CalledOnValidThread() const; |
| 93 |
83 private: | 94 private: |
84 friend class InputServiceLinux; | 95 friend class InputServiceLinux; |
85 | 96 |
86 InputServiceLinuxImpl(); | 97 InputServiceLinuxImpl(); |
87 ~InputServiceLinuxImpl() override; | 98 ~InputServiceLinuxImpl() override; |
88 | 99 |
| 100 base::ThreadChecker thread_checker_; |
| 101 |
89 DISALLOW_COPY_AND_ASSIGN(InputServiceLinuxImpl); | 102 DISALLOW_COPY_AND_ASSIGN(InputServiceLinuxImpl); |
90 }; | 103 }; |
91 | 104 |
92 InputServiceLinuxImpl::InputServiceLinuxImpl() { | 105 InputServiceLinuxImpl::InputServiceLinuxImpl() { |
| 106 base::ThreadRestrictions::AssertIOAllowed(); |
| 107 base::MessageLoop::current()->AddDestructionObserver(this); |
| 108 |
93 DeviceMonitorLinux::GetInstance()->AddObserver(this); | 109 DeviceMonitorLinux::GetInstance()->AddObserver(this); |
94 DeviceMonitorLinux::GetInstance()->Enumerate(base::Bind( | 110 DeviceMonitorLinux::GetInstance()->Enumerate(base::Bind( |
95 &InputServiceLinuxImpl::OnDeviceAdded, base::Unretained(this))); | 111 &InputServiceLinuxImpl::OnDeviceAdded, base::Unretained(this))); |
96 } | 112 } |
97 | 113 |
98 InputServiceLinuxImpl::~InputServiceLinuxImpl() { | 114 InputServiceLinuxImpl::~InputServiceLinuxImpl() { |
99 if (DeviceMonitorLinux::HasInstance()) | 115 DCHECK(CalledOnValidThread()); |
100 DeviceMonitorLinux::GetInstance()->RemoveObserver(this); | 116 base::MessageLoop::current()->RemoveDestructionObserver(this); |
| 117 } |
| 118 |
| 119 void InputServiceLinuxImpl::AddObserver(InputServiceLinux::Observer* observer) { |
| 120 DCHECK(CalledOnValidThread()); |
| 121 InputServiceLinux::AddObserver(observer); |
| 122 } |
| 123 |
| 124 void InputServiceLinuxImpl::RemoveObserver( |
| 125 InputServiceLinux::Observer* observer) { |
| 126 DCHECK(CalledOnValidThread()); |
| 127 InputServiceLinux::RemoveObserver(observer); |
| 128 } |
| 129 |
| 130 void InputServiceLinuxImpl::GetDevices(std::vector<InputDeviceInfo>* devices) { |
| 131 DCHECK(CalledOnValidThread()); |
| 132 InputServiceLinux::GetDevices(devices); |
| 133 } |
| 134 |
| 135 bool InputServiceLinuxImpl::GetDeviceInfo(const std::string& id, |
| 136 InputDeviceInfo* info) const { |
| 137 DCHECK(CalledOnValidThread()); |
| 138 return InputServiceLinux::GetDeviceInfo(id, info); |
101 } | 139 } |
102 | 140 |
103 void InputServiceLinuxImpl::OnDeviceAdded(udev_device* device) { | 141 void InputServiceLinuxImpl::OnDeviceAdded(udev_device* device) { |
104 DCHECK(CalledOnValidThread()); | 142 DCHECK(CalledOnValidThread()); |
105 if (!device) | 143 if (!device) |
106 return; | 144 return; |
107 const char* devnode = udev_device_get_devnode(device); | 145 const char* devnode = udev_device_get_devnode(device); |
108 if (!devnode) | 146 if (!devnode) |
109 return; | 147 return; |
110 | 148 |
(...skipping 29 matching lines...) Expand all Loading... |
140 | 178 |
141 void InputServiceLinuxImpl::OnDeviceRemoved(udev_device* device) { | 179 void InputServiceLinuxImpl::OnDeviceRemoved(udev_device* device) { |
142 DCHECK(CalledOnValidThread()); | 180 DCHECK(CalledOnValidThread()); |
143 if (!device) | 181 if (!device) |
144 return; | 182 return; |
145 const char* devnode = udev_device_get_devnode(device); | 183 const char* devnode = udev_device_get_devnode(device); |
146 if (devnode) | 184 if (devnode) |
147 RemoveDevice(devnode); | 185 RemoveDevice(devnode); |
148 } | 186 } |
149 | 187 |
| 188 void InputServiceLinuxImpl::WillDestroyCurrentMessageLoop() { |
| 189 DCHECK(CalledOnValidThread()); |
| 190 InputServiceLinux::WillDestroyCurrentMessageLoop(); |
| 191 } |
| 192 |
| 193 bool InputServiceLinuxImpl::CalledOnValidThread() const { |
| 194 return thread_checker_.CalledOnValidThread(); |
| 195 } |
| 196 |
150 } // namespace | 197 } // namespace |
151 | 198 |
152 InputServiceLinux::InputDeviceInfo::InputDeviceInfo() | 199 InputServiceLinux::InputDeviceInfo::InputDeviceInfo() |
153 : subsystem(SUBSYSTEM_UNKNOWN), | 200 : subsystem(SUBSYSTEM_UNKNOWN), |
154 type(TYPE_UNKNOWN), | 201 type(TYPE_UNKNOWN), |
155 is_accelerometer(false), | 202 is_accelerometer(false), |
156 is_joystick(false), | 203 is_joystick(false), |
157 is_key(false), | 204 is_key(false), |
158 is_keyboard(false), | 205 is_keyboard(false), |
159 is_mouse(false), | 206 is_mouse(false), |
160 is_tablet(false), | 207 is_tablet(false), |
161 is_touchpad(false), | 208 is_touchpad(false), |
162 is_touchscreen(false) {} | 209 is_touchscreen(false) {} |
163 | 210 |
164 InputServiceLinux::InputServiceLinux() { | 211 InputServiceLinux::InputServiceLinux() { |
165 base::ThreadRestrictions::AssertIOAllowed(); | |
166 base::MessageLoop::current()->AddDestructionObserver(this); | |
167 } | 212 } |
168 | 213 |
169 InputServiceLinux::~InputServiceLinux() { | 214 InputServiceLinux::~InputServiceLinux() { |
170 DCHECK(CalledOnValidThread()); | |
171 base::MessageLoop::current()->RemoveDestructionObserver(this); | |
172 } | 215 } |
173 | 216 |
174 // static | 217 // static |
175 InputServiceLinux* InputServiceLinux::GetInstance() { | 218 InputServiceLinux* InputServiceLinux::GetInstance() { |
176 if (!HasInstance()) | 219 if (!HasInstance()) |
177 g_input_service_linux_ptr.Get().reset(new InputServiceLinuxImpl()); | 220 g_input_service_linux_ptr.Get().reset(new InputServiceLinuxImpl()); |
178 return g_input_service_linux_ptr.Get().get(); | 221 return g_input_service_linux_ptr.Get().get(); |
179 } | 222 } |
180 | 223 |
181 // static | 224 // static |
182 bool InputServiceLinux::HasInstance() { | 225 bool InputServiceLinux::HasInstance() { |
183 return g_input_service_linux_ptr.Get().get(); | 226 return g_input_service_linux_ptr.Get().get(); |
184 } | 227 } |
185 | 228 |
186 // static | 229 // static |
187 void InputServiceLinux::SetForTesting(InputServiceLinux* service) { | 230 void InputServiceLinux::SetForTesting(InputServiceLinux* service) { |
188 g_input_service_linux_ptr.Get().reset(service); | 231 g_input_service_linux_ptr.Get().reset(service); |
189 } | 232 } |
190 | 233 |
191 void InputServiceLinux::AddObserver(Observer* observer) { | 234 void InputServiceLinux::AddObserver(Observer* observer) { |
192 DCHECK(CalledOnValidThread()); | |
193 if (observer) | 235 if (observer) |
194 observers_.AddObserver(observer); | 236 observers_.AddObserver(observer); |
195 } | 237 } |
196 | 238 |
197 void InputServiceLinux::RemoveObserver(Observer* observer) { | 239 void InputServiceLinux::RemoveObserver(Observer* observer) { |
198 DCHECK(CalledOnValidThread()); | |
199 if (observer) | 240 if (observer) |
200 observers_.RemoveObserver(observer); | 241 observers_.RemoveObserver(observer); |
201 } | 242 } |
202 | 243 |
203 void InputServiceLinux::GetDevices(std::vector<InputDeviceInfo>* devices) { | 244 void InputServiceLinux::GetDevices(std::vector<InputDeviceInfo>* devices) { |
204 DCHECK(CalledOnValidThread()); | |
205 for (DeviceMap::iterator it = devices_.begin(), ie = devices_.end(); it != ie; | 245 for (DeviceMap::iterator it = devices_.begin(), ie = devices_.end(); it != ie; |
206 ++it) { | 246 ++it) { |
207 devices->push_back(it->second); | 247 devices->push_back(it->second); |
208 } | 248 } |
209 } | 249 } |
210 | 250 |
211 bool InputServiceLinux::GetDeviceInfo(const std::string& id, | 251 bool InputServiceLinux::GetDeviceInfo(const std::string& id, |
212 InputDeviceInfo* info) const { | 252 InputDeviceInfo* info) const { |
213 DCHECK(CalledOnValidThread()); | |
214 DeviceMap::const_iterator it = devices_.find(id); | 253 DeviceMap::const_iterator it = devices_.find(id); |
215 if (it == devices_.end()) | 254 if (it == devices_.end()) |
216 return false; | 255 return false; |
217 *info = it->second; | 256 *info = it->second; |
218 return true; | 257 return true; |
219 } | 258 } |
220 | 259 |
221 void InputServiceLinux::WillDestroyCurrentMessageLoop() { | 260 void InputServiceLinux::WillDestroyCurrentMessageLoop() { |
222 DCHECK(CalledOnValidThread()); | |
223 g_input_service_linux_ptr.Get().reset(NULL); | 261 g_input_service_linux_ptr.Get().reset(NULL); |
224 } | 262 } |
225 | 263 |
226 void InputServiceLinux::AddDevice(const InputDeviceInfo& info) { | 264 void InputServiceLinux::AddDevice(const InputDeviceInfo& info) { |
227 devices_[info.id] = info; | 265 devices_[info.id] = info; |
228 FOR_EACH_OBSERVER(Observer, observers_, OnInputDeviceAdded(info)); | 266 FOR_EACH_OBSERVER(Observer, observers_, OnInputDeviceAdded(info)); |
229 } | 267 } |
230 | 268 |
231 void InputServiceLinux::RemoveDevice(const std::string& id) { | 269 void InputServiceLinux::RemoveDevice(const std::string& id) { |
232 devices_.erase(id); | 270 devices_.erase(id); |
233 FOR_EACH_OBSERVER(Observer, observers_, OnInputDeviceRemoved(id)); | 271 FOR_EACH_OBSERVER(Observer, observers_, OnInputDeviceRemoved(id)); |
234 } | 272 } |
235 | 273 |
236 bool InputServiceLinux::CalledOnValidThread() const { | |
237 return thread_checker_.CalledOnValidThread(); | |
238 } | |
239 | |
240 } // namespace device | 274 } // namespace device |
OLD | NEW |