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 <memory> | 7 #include <memory> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/scoped_observer.h" |
13 #include "base/strings/string_number_conversions.h" | 14 #include "base/strings/string_number_conversions.h" |
14 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
15 #include "base/threading/thread_restrictions.h" | 16 #include "base/threading/thread_restrictions.h" |
| 17 #include "device/core/device_monitor_linux.h" |
16 #include "device/udev_linux/udev.h" | 18 #include "device/udev_linux/udev.h" |
17 | 19 |
18 namespace device { | 20 namespace device { |
19 | 21 |
20 namespace { | 22 namespace { |
21 | 23 |
22 const char kSubsystemHid[] = "hid"; | 24 const char kSubsystemHid[] = "hid"; |
23 const char kSubsystemInput[] = "input"; | 25 const char kSubsystemInput[] = "input"; |
24 const char kSubsystemMisc[] = "misc"; | 26 const char kSubsystemMisc[] = "misc"; |
25 const char kTypeBluetooth[] = "bluetooth"; | 27 const char kTypeBluetooth[] = "bluetooth"; |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
82 base::TrimString(name, "\"", &result); | 84 base::TrimString(name, "\"", &result); |
83 return result; | 85 return result; |
84 } | 86 } |
85 | 87 |
86 class InputServiceLinuxImpl : public InputServiceLinux, | 88 class InputServiceLinuxImpl : public InputServiceLinux, |
87 public DeviceMonitorLinux::Observer { | 89 public DeviceMonitorLinux::Observer { |
88 public: | 90 public: |
89 // Implements DeviceMonitorLinux::Observer: | 91 // Implements DeviceMonitorLinux::Observer: |
90 void OnDeviceAdded(udev_device* device) override; | 92 void OnDeviceAdded(udev_device* device) override; |
91 void OnDeviceRemoved(udev_device* device) override; | 93 void OnDeviceRemoved(udev_device* device) override; |
| 94 void WillDestroyMonitorMessageLoop() override; |
92 | 95 |
93 private: | 96 private: |
94 friend class InputServiceLinux; | 97 friend class InputServiceLinux; |
95 | 98 |
96 InputServiceLinuxImpl(); | 99 InputServiceLinuxImpl(); |
97 ~InputServiceLinuxImpl() override; | 100 ~InputServiceLinuxImpl() override; |
98 | 101 |
| 102 ScopedObserver<DeviceMonitorLinux, DeviceMonitorLinux::Observer> observer_; |
| 103 |
99 DISALLOW_COPY_AND_ASSIGN(InputServiceLinuxImpl); | 104 DISALLOW_COPY_AND_ASSIGN(InputServiceLinuxImpl); |
100 }; | 105 }; |
101 | 106 |
102 InputServiceLinuxImpl::InputServiceLinuxImpl() { | 107 InputServiceLinuxImpl::InputServiceLinuxImpl() : observer_(this) { |
103 base::ThreadRestrictions::AssertIOAllowed(); | 108 base::ThreadRestrictions::AssertIOAllowed(); |
104 base::MessageLoop::current()->AddDestructionObserver(this); | |
105 | 109 |
106 DeviceMonitorLinux::GetInstance()->AddObserver(this); | 110 DeviceMonitorLinux* monitor = DeviceMonitorLinux::GetInstance(); |
107 DeviceMonitorLinux::GetInstance()->Enumerate(base::Bind( | 111 observer_.Add(monitor); |
108 &InputServiceLinuxImpl::OnDeviceAdded, base::Unretained(this))); | 112 monitor->Enumerate(base::Bind(&InputServiceLinuxImpl::OnDeviceAdded, |
| 113 base::Unretained(this))); |
109 } | 114 } |
110 | 115 |
111 InputServiceLinuxImpl::~InputServiceLinuxImpl() { | 116 InputServiceLinuxImpl::~InputServiceLinuxImpl() { |
112 if (DeviceMonitorLinux::HasInstance()) | |
113 DeviceMonitorLinux::GetInstance()->RemoveObserver(this); | |
114 base::MessageLoop::current()->RemoveDestructionObserver(this); | |
115 } | 117 } |
116 | 118 |
117 void InputServiceLinuxImpl::OnDeviceAdded(udev_device* device) { | 119 void InputServiceLinuxImpl::OnDeviceAdded(udev_device* device) { |
118 DCHECK(CalledOnValidThread()); | 120 DCHECK(CalledOnValidThread()); |
119 if (!device) | 121 if (!device) |
120 return; | 122 return; |
121 const char* devnode = udev_device_get_devnode(device); | 123 const char* devnode = udev_device_get_devnode(device); |
122 if (!devnode) | 124 if (!devnode) |
123 return; | 125 return; |
124 | 126 |
(...skipping 29 matching lines...) Expand all Loading... |
154 | 156 |
155 void InputServiceLinuxImpl::OnDeviceRemoved(udev_device* device) { | 157 void InputServiceLinuxImpl::OnDeviceRemoved(udev_device* device) { |
156 DCHECK(CalledOnValidThread()); | 158 DCHECK(CalledOnValidThread()); |
157 if (!device) | 159 if (!device) |
158 return; | 160 return; |
159 const char* devnode = udev_device_get_devnode(device); | 161 const char* devnode = udev_device_get_devnode(device); |
160 if (devnode) | 162 if (devnode) |
161 RemoveDevice(devnode); | 163 RemoveDevice(devnode); |
162 } | 164 } |
163 | 165 |
| 166 void InputServiceLinuxImpl::WillDestroyMonitorMessageLoop() { |
| 167 DCHECK(CalledOnValidThread()); |
| 168 g_input_service_linux_ptr.Get().reset(nullptr); |
| 169 } |
| 170 |
164 } // namespace | 171 } // namespace |
165 | 172 |
166 InputServiceLinux::InputDeviceInfo::InputDeviceInfo() | 173 InputServiceLinux::InputDeviceInfo::InputDeviceInfo() |
167 : subsystem(SUBSYSTEM_UNKNOWN), | 174 : subsystem(SUBSYSTEM_UNKNOWN), |
168 type(TYPE_UNKNOWN), | 175 type(TYPE_UNKNOWN), |
169 is_accelerometer(false), | 176 is_accelerometer(false), |
170 is_joystick(false), | 177 is_joystick(false), |
171 is_key(false), | 178 is_key(false), |
172 is_keyboard(false), | 179 is_keyboard(false), |
173 is_mouse(false), | 180 is_mouse(false), |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
225 bool InputServiceLinux::GetDeviceInfo(const std::string& id, | 232 bool InputServiceLinux::GetDeviceInfo(const std::string& id, |
226 InputDeviceInfo* info) const { | 233 InputDeviceInfo* info) const { |
227 DCHECK(CalledOnValidThread()); | 234 DCHECK(CalledOnValidThread()); |
228 DeviceMap::const_iterator it = devices_.find(id); | 235 DeviceMap::const_iterator it = devices_.find(id); |
229 if (it == devices_.end()) | 236 if (it == devices_.end()) |
230 return false; | 237 return false; |
231 *info = it->second; | 238 *info = it->second; |
232 return true; | 239 return true; |
233 } | 240 } |
234 | 241 |
235 void InputServiceLinux::WillDestroyCurrentMessageLoop() { | |
236 DCHECK(CalledOnValidThread()); | |
237 g_input_service_linux_ptr.Get().reset(NULL); | |
238 } | |
239 | |
240 void InputServiceLinux::AddDevice(const InputDeviceInfo& info) { | 242 void InputServiceLinux::AddDevice(const InputDeviceInfo& info) { |
241 devices_[info.id] = info; | 243 devices_[info.id] = info; |
242 FOR_EACH_OBSERVER(Observer, observers_, OnInputDeviceAdded(info)); | 244 FOR_EACH_OBSERVER(Observer, observers_, OnInputDeviceAdded(info)); |
243 } | 245 } |
244 | 246 |
245 void InputServiceLinux::RemoveDevice(const std::string& id) { | 247 void InputServiceLinux::RemoveDevice(const std::string& id) { |
246 devices_.erase(id); | 248 devices_.erase(id); |
247 FOR_EACH_OBSERVER(Observer, observers_, OnInputDeviceRemoved(id)); | 249 FOR_EACH_OBSERVER(Observer, observers_, OnInputDeviceRemoved(id)); |
248 } | 250 } |
249 | 251 |
250 bool InputServiceLinux::CalledOnValidThread() const { | 252 bool InputServiceLinux::CalledOnValidThread() const { |
251 return thread_checker_.CalledOnValidThread(); | 253 return thread_checker_.CalledOnValidThread(); |
252 } | 254 } |
253 | 255 |
254 } // namespace device | 256 } // namespace device |
OLD | NEW |