summaryrefslogtreecommitdiffstats
path: root/device/hid
diff options
context:
space:
mode:
authorygorshenin@chromium.org <ygorshenin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2014-04-17 13:03:52 +0000
committerygorshenin@chromium.org <ygorshenin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2014-04-17 13:03:52 +0000
commit2ec4cd9da32b9cd341d31c7dfab81fa0d694cf25 (patch)
tree062d0e395d218faa412c5d3ce36e4f146d1f7c21 /device/hid
parentcaad7bd9397b8eb75e8eea9ecc58874d86d9a874 (diff)
downloadchromium_src-2ec4cd9da32b9cd341d31c7dfab81fa0d694cf25.zip
chromium_src-2ec4cd9da32b9cd341d31c7dfab81fa0d694cf25.tar.gz
chromium_src-2ec4cd9da32b9cd341d31c7dfab81fa0d694cf25.tar.bz2
Added InputServiceProxy, which lives on the UI thread and forwards all calls to the InputServiceLinux.
BUG=357050 TEST=browser_tests:InputServiceProxy* Review URL: https://codereview.chromium.org/232773015 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@264495 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'device/hid')
-rw-r--r--device/hid/input_service_linux.cc154
-rw-r--r--device/hid/input_service_linux.h15
2 files changed, 106 insertions, 63 deletions
diff --git a/device/hid/input_service_linux.cc b/device/hid/input_service_linux.cc
index 7552824..09ecaaf 100644
--- a/device/hid/input_service_linux.cc
+++ b/device/hid/input_service_linux.cc
@@ -44,6 +44,80 @@ bool GetBoolProperty(udev_device* device, const char* key) {
return (value != 0);
}
+class InputServiceLinuxImpl : public InputServiceLinux,
+ public DeviceMonitorLinux::Observer {
+ public:
+ // Implements DeviceMonitorLinux::Observer:
+ virtual void OnDeviceAdded(udev_device* device) OVERRIDE;
+ virtual void OnDeviceRemoved(udev_device* device) OVERRIDE;
+
+ private:
+ friend class InputServiceLinux;
+
+ InputServiceLinuxImpl();
+ virtual ~InputServiceLinuxImpl();
+
+ DISALLOW_COPY_AND_ASSIGN(InputServiceLinuxImpl);
+};
+
+InputServiceLinuxImpl::InputServiceLinuxImpl() {
+ DeviceMonitorLinux::GetInstance()->AddObserver(this);
+ DeviceMonitorLinux::GetInstance()->Enumerate(base::Bind(
+ &InputServiceLinuxImpl::OnDeviceAdded, base::Unretained(this)));
+}
+
+InputServiceLinuxImpl::~InputServiceLinuxImpl() {
+ if (DeviceMonitorLinux::HasInstance())
+ DeviceMonitorLinux::GetInstance()->RemoveObserver(this);
+}
+
+void InputServiceLinuxImpl::OnDeviceAdded(udev_device* device) {
+ DCHECK(CalledOnValidThread());
+ if (!device)
+ return;
+ const char* path = udev_device_get_syspath(device);
+ if (!path)
+ return;
+
+ InputDeviceInfo info;
+ info.id = path;
+
+ const char* name = udev_device_get_property_value(device, "NAME");
+ if (name)
+ info.name = name;
+
+ const char* subsystem = udev_device_get_subsystem(device);
+ if (!subsystem)
+ return;
+ else if (strcmp(subsystem, kHidSubsystem) == 0)
+ info.subsystem = InputServiceLinux::InputDeviceInfo::SUBSYSTEM_HID;
+ else if (strcmp(subsystem, kInputSubsystem) == 0)
+ info.subsystem = InputServiceLinux::InputDeviceInfo::SUBSYSTEM_INPUT;
+ else
+ return;
+
+ info.is_accelerometer = GetBoolProperty(device, kIdInputAccelerometer);
+ info.is_joystick = GetBoolProperty(device, kIdInputJoystick);
+ info.is_key = GetBoolProperty(device, kIdInputKey);
+ info.is_keyboard = GetBoolProperty(device, kIdInputKeyboard);
+ info.is_mouse = GetBoolProperty(device, kIdInputMouse);
+ info.is_tablet = GetBoolProperty(device, kIdInputTablet);
+ info.is_touchpad = GetBoolProperty(device, kIdInputTouchpad);
+ info.is_touchscreen = GetBoolProperty(device, kIdInputTouchscreen);
+
+ AddDevice(info);
+}
+
+void InputServiceLinuxImpl::OnDeviceRemoved(udev_device* device) {
+ DCHECK(CalledOnValidThread());
+ if (!device)
+ return;
+ const char* path = udev_device_get_syspath(device);
+ if (!path)
+ return;
+ RemoveDevice(path);
+}
+
} // namespace
InputServiceLinux::InputDeviceInfo::InputDeviceInfo()
@@ -60,9 +134,11 @@ InputServiceLinux::InputDeviceInfo::InputDeviceInfo()
InputServiceLinux::InputServiceLinux() {
base::ThreadRestrictions::AssertIOAllowed();
base::MessageLoop::current()->AddDestructionObserver(this);
- DeviceMonitorLinux::GetInstance()->AddObserver(this);
- DeviceMonitorLinux::GetInstance()->Enumerate(
- base::Bind(&InputServiceLinux::OnDeviceAdded, base::Unretained(this)));
+}
+
+InputServiceLinux::~InputServiceLinux() {
+ DCHECK(CalledOnValidThread());
+ base::MessageLoop::current()->RemoveDestructionObserver(this);
}
// static
@@ -77,28 +153,34 @@ bool InputServiceLinux::HasInstance() {
return g_input_service_linux_ptr.Get().get();
}
+// static
+void InputServiceLinux::SetForTesting(InputServiceLinux* service) {
+ g_input_service_linux_ptr.Get().reset(service);
+}
+
void InputServiceLinux::AddObserver(Observer* observer) {
- DCHECK(thread_checker_.CalledOnValidThread());
+ DCHECK(CalledOnValidThread());
if (observer)
observers_.AddObserver(observer);
}
void InputServiceLinux::RemoveObserver(Observer* observer) {
- DCHECK(thread_checker_.CalledOnValidThread());
+ DCHECK(CalledOnValidThread());
if (observer)
observers_.RemoveObserver(observer);
}
void InputServiceLinux::GetDevices(std::vector<InputDeviceInfo>* devices) {
- DCHECK(thread_checker_.CalledOnValidThread());
+ DCHECK(CalledOnValidThread());
for (DeviceMap::iterator it = devices_.begin(), ie = devices_.end(); it != ie;
- ++it)
+ ++it) {
devices->push_back(it->second);
+ }
}
bool InputServiceLinux::GetDeviceInfo(const std::string& id,
InputDeviceInfo* info) const {
- DCHECK(thread_checker_.CalledOnValidThread());
+ DCHECK(CalledOnValidThread());
DeviceMap::const_iterator it = devices_.find(id);
if (it == devices_.end())
return false;
@@ -107,64 +189,22 @@ bool InputServiceLinux::GetDeviceInfo(const std::string& id,
}
void InputServiceLinux::WillDestroyCurrentMessageLoop() {
- DCHECK(thread_checker_.CalledOnValidThread());
+ DCHECK(CalledOnValidThread());
g_input_service_linux_ptr.Get().reset(NULL);
}
-void InputServiceLinux::OnDeviceAdded(udev_device* device) {
- DCHECK(thread_checker_.CalledOnValidThread());
- if (!device)
- return;
- const char* path = udev_device_get_syspath(device);
- if (!path)
- return;
-
- InputDeviceInfo info;
- info.id = path;
-
- const char* name = udev_device_get_property_value(device, "NAME");
- if (name)
- info.name = name;
-
- const char* subsystem = udev_device_get_subsystem(device);
- if (!subsystem)
- return;
- else if (strcmp(subsystem, kHidSubsystem) == 0)
- info.subsystem = InputServiceLinux::InputDeviceInfo::SUBSYSTEM_HID;
- else if (strcmp(subsystem, kInputSubsystem) == 0)
- info.subsystem = InputServiceLinux::InputDeviceInfo::SUBSYSTEM_INPUT;
- else
- return;
-
- info.is_accelerometer = GetBoolProperty(device, kIdInputAccelerometer);
- info.is_joystick = GetBoolProperty(device, kIdInputJoystick);
- info.is_key = GetBoolProperty(device, kIdInputKey);
- info.is_keyboard = GetBoolProperty(device, kIdInputKeyboard);
- info.is_mouse = GetBoolProperty(device, kIdInputMouse);
- info.is_tablet = GetBoolProperty(device, kIdInputTablet);
- info.is_touchpad = GetBoolProperty(device, kIdInputTouchpad);
- info.is_touchscreen = GetBoolProperty(device, kIdInputTouchscreen);
-
+void InputServiceLinux::AddDevice(const InputDeviceInfo& info) {
devices_[info.id] = info;
FOR_EACH_OBSERVER(Observer, observers_, OnInputDeviceAdded(info));
}
-void InputServiceLinux::OnDeviceRemoved(udev_device* device) {
- DCHECK(thread_checker_.CalledOnValidThread());
- if (!device)
- return;
- const char* path = udev_device_get_syspath(device);
- if (!path)
- return;
- devices_.erase(path);
- FOR_EACH_OBSERVER(Observer, observers_, OnInputDeviceRemoved(path));
+void InputServiceLinux::RemoveDevice(const std::string& id) {
+ devices_.erase(id);
+ FOR_EACH_OBSERVER(Observer, observers_, OnInputDeviceRemoved(id));
}
-InputServiceLinux::~InputServiceLinux() {
- DCHECK(thread_checker_.CalledOnValidThread());
- base::MessageLoop::current()->RemoveDestructionObserver(this);
- if (DeviceMonitorLinux::HasInstance())
- DeviceMonitorLinux::GetInstance()->RemoveObserver(this);
+bool InputServiceLinux::CalledOnValidThread() const {
+ return thread_checker_.CalledOnValidThread();
}
} // namespace device
diff --git a/device/hid/input_service_linux.h b/device/hid/input_service_linux.h
index 48584f4..edd957b 100644
--- a/device/hid/input_service_linux.h
+++ b/device/hid/input_service_linux.h
@@ -22,8 +22,7 @@ namespace device {
// This class provides information and notifications about
// connected/disconnected input/HID devices. This class is *NOT*
// thread-safe and all methods must be called from the FILE thread.
-class InputServiceLinux : public base::MessageLoop::DestructionObserver,
- public DeviceMonitorLinux::Observer {
+class InputServiceLinux : public base::MessageLoop::DestructionObserver {
public:
struct InputDeviceInfo {
enum Subsystem { SUBSYSTEM_HID, SUBSYSTEM_INPUT, SUBSYSTEM_UNKNOWN };
@@ -55,6 +54,7 @@ class InputServiceLinux : public base::MessageLoop::DestructionObserver,
static InputServiceLinux* GetInstance();
static bool HasInstance();
+ static void SetForTesting(InputServiceLinux* service);
void AddObserver(Observer* observer);
void RemoveObserver(Observer* observer);
@@ -70,15 +70,18 @@ class InputServiceLinux : public base::MessageLoop::DestructionObserver,
// Implements base::MessageLoop::DestructionObserver
virtual void WillDestroyCurrentMessageLoop() OVERRIDE;
- // Implements DeviceMonitorLinux::Observer:
- virtual void OnDeviceAdded(udev_device* device) OVERRIDE;
- virtual void OnDeviceRemoved(udev_device* device) OVERRIDE;
+ protected:
+ virtual ~InputServiceLinux();
+
+ void AddDevice(const InputDeviceInfo& info);
+ void RemoveDevice(const std::string& id);
+
+ bool CalledOnValidThread() const;
private:
friend struct base::DefaultDeleter<InputServiceLinux>;
typedef base::hash_map<std::string, InputDeviceInfo> DeviceMap;
- virtual ~InputServiceLinux();
DeviceMap devices_;
ObserverList<Observer> observers_;