Chromium Code Reviews| Index: components/input_device_service/input_device_server.cc |
| diff --git a/components/input_device_service/input_device_server.cc b/components/input_device_service/input_device_server.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..fd1dca34df5cee585bb342d5d32dfda7baf25038 |
| --- /dev/null |
| +++ b/components/input_device_service/input_device_server.cc |
| @@ -0,0 +1,133 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "components/input_device_service/input_device_server.h" |
| + |
| +#include <utility> |
| +#include <vector> |
| + |
| +#include "mojo/public/cpp/bindings/array.h" |
| +#include "ui/events/devices/device_data_manager.h" |
| + |
| +namespace input_device { |
| + |
| +using ui::DeviceDataManager; |
| + |
| +namespace { |
| + |
| +template <class T> |
| +std::vector<ui::InputDevice> ToInputDevices(const std::vector<T>& input) { |
| + std::vector<ui::InputDevice> devices; |
| + for (const T& device : input) { |
| + devices.push_back(ui::InputDevice(device)); |
| + } |
| + return devices; |
| +} |
| + |
| +std::vector<ui::InputDevice> GetKeyboardList() { |
| + // TODO(kylechar): Change DDM to hold ui::InputDevice instead. |
| + return ToInputDevices(DeviceDataManager::GetInstance()->keyboard_devices()); |
| +} |
| + |
| +std::vector<ui::InputDevice> GetTouchscreenList() { |
| + // TODO(kylechar): Change DDM to hold ui::InputDevice instead. |
| + return ToInputDevices( |
| + DeviceDataManager::GetInstance()->touchscreen_devices()); |
| +} |
| + |
| +const std::vector<ui::InputDevice>& GetMouseList() { |
| + return DeviceDataManager::GetInstance()->mouse_devices(); |
| +} |
| + |
| +const std::vector<ui::InputDevice>& GetTouchpadList() { |
| + return DeviceDataManager::GetInstance()->touchpad_devices(); |
| +} |
| + |
| +} // namespace |
| + |
| +InputDeviceServer::InputDeviceServer() {} |
| + |
| +InputDeviceServer::~InputDeviceServer() { |
| + if (is_observing_ && DeviceDataManager::HasInstance()) { |
| + DeviceDataManager::GetInstance()->RemoveObserver(this); |
| + } |
| + is_observing_ = false; |
| +} |
| + |
| +void InputDeviceServer::RegisterAsObserver() { |
| + if (!is_observing_) { |
| + DeviceDataManager::GetInstance()->AddObserver(this); |
|
sadrul
2016/05/25 20:40:08
What creates this DDM instance?
kylechar
2016/05/27 19:52:22
It's created as part of Ozone initialization or as
|
| + is_observing_ = true; |
| + } |
| +} |
| + |
| +void InputDeviceServer::RegisterInterface(shell::Connection* connection) { |
| + DCHECK(is_observing_); |
| + connection->AddInterface<mojom::InputDeviceService>(this); |
| +} |
| + |
| +void InputDeviceServer::Create(shell::Connection* connection, |
| + mojom::InputDeviceServiceRequest request) { |
| + bindings_.AddBinding(this, std::move(request)); |
| +} |
| + |
| +void InputDeviceServer::AddObserver(mojom::InputDeviceObserverPtr observer) { |
| + observers_.AddPtr(std::move(observer)); |
| +} |
| + |
| +void InputDeviceServer::OnKeyboardDeviceConfigurationChanged() { |
| + auto devices = GetKeyboardList(); |
| + observers_.ForAllPtrs([&devices](mojom::InputDeviceObserver* observer) { |
| + observer->OnKeyboardDeviceConfigurationChanged( |
| + mojo::Array<ui::InputDevice>::From(devices)); |
| + }); |
| +} |
| + |
| +void InputDeviceServer::OnTouchscreenDeviceConfigurationChanged() { |
| + auto devices = GetTouchscreenList(); |
| + observers_.ForAllPtrs([&devices](mojom::InputDeviceObserver* observer) { |
| + observer->OnTouchscreenDeviceConfigurationChanged( |
| + mojo::Array<ui::InputDevice>::From(devices)); |
| + }); |
| +} |
| + |
| +void InputDeviceServer::OnMouseDeviceConfigurationChanged() { |
| + auto& devices = GetMouseList(); |
| + LOG(ERROR) << "InputDeviceServer::OnMouseDeviceConfigurationChanged " |
| + << devices.size(); |
| + observers_.ForAllPtrs([&devices](mojom::InputDeviceObserver* observer) { |
| + observer->OnMouseDeviceConfigurationChanged( |
| + mojo::Array<ui::InputDevice>::From(devices)); |
| + }); |
| +} |
| + |
| +void InputDeviceServer::OnTouchpadDeviceConfigurationChanged() { |
| + auto& devices = GetTouchpadList(); |
| + observers_.ForAllPtrs([&devices](mojom::InputDeviceObserver* observer) { |
| + observer->OnTouchpadDeviceConfigurationChanged( |
| + mojo::Array<ui::InputDevice>::From(devices)); |
| + }); |
| +} |
| + |
| +void InputDeviceServer::GetKeyboardDevices( |
| + const GetKeyboardDevicesCallback& callback) { |
| + callback.Run(mojo::Array<ui::InputDevice>::From(GetKeyboardList())); |
| +} |
| + |
| +void InputDeviceServer::GetTouchscreenDevices( |
| + const GetTouchscreenDevicesCallback& callback) { |
| + callback.Run(mojo::Array<ui::InputDevice>::From(GetTouchscreenList())); |
| +} |
| + |
| +void InputDeviceServer::GetMouseDevices( |
| + const GetMouseDevicesCallback& callback) { |
| + callback.Run(mojo::Array<ui::InputDevice>::From(GetMouseList())); |
| +} |
| + |
| +void InputDeviceServer::GetTouchpadDevices( |
| + const GetTouchpadDevicesCallback& callback) { |
| + callback.Run(mojo::Array<ui::InputDevice>::From(GetTouchpadList())); |
| +} |
| + |
| +} // namespace input_device |