Chromium Code Reviews| Index: components/mus/input_devices/input_device_server.cc |
| diff --git a/components/mus/input_devices/input_device_server.cc b/components/mus/input_devices/input_device_server.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..3d201f50543294fd1059e5af7f4775563281d469 |
| --- /dev/null |
| +++ b/components/mus/input_devices/input_device_server.cc |
| @@ -0,0 +1,112 @@ |
| +// 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/mus/input_devices/input_device_server.h" |
| + |
| +#include <utility> |
| +#include <vector> |
| + |
| +#include "mojo/public/cpp/bindings/array.h" |
| +#include "ui/events/devices/input_device.h" |
| +#include "ui/events/devices/touchscreen_device.h" |
| + |
| +namespace input_device { |
| + |
| +InputDeviceServer::InputDeviceServer() {} |
| + |
| +InputDeviceServer::~InputDeviceServer() { |
| + if (manager_ && ui::DeviceDataManager::HasInstance()) { |
| + manager_->RemoveObserver(this); |
| + manager_ = nullptr; |
| + } |
| +} |
| + |
| +void InputDeviceServer::RegisterAsObserver() { |
| + if (!manager_) { |
| + manager_ = ui::DeviceDataManager::GetInstance(); |
| + manager_->AddObserver(this); |
| + } |
| +} |
| + |
| +void InputDeviceServer::RegisterInterface(shell::Connection* connection) { |
| + DCHECK(manager_); |
| + connection->AddInterface<mojom::InputDeviceServer>(this); |
| +} |
| + |
| +void InputDeviceServer::Create(shell::Connection* connection, |
|
sadrul
2016/06/10 18:01:49
This should be at the end (order in .h should matc
kylechar
2016/06/10 19:29:31
Done.
|
| + mojom::InputDeviceServerRequest request) { |
| + bindings_.AddBinding(this, std::move(request)); |
| +} |
| + |
| +void InputDeviceServer::AddObserver( |
| + mojom::InputDeviceObserverMojoPtr observer) { |
| + // We only want to send this message once, so we need to check to make sure |
| + // device lists are actually complete before sending it to a new observer. |
| + if (manager_->AreDeviceListsComplete()) |
| + SendDeviceListsComplete(observer.get()); |
| + observers_.AddPtr(std::move(observer)); |
| +} |
| + |
| +void InputDeviceServer::OnKeyboardDeviceConfigurationChanged() { |
| + if (manager_->AreDeviceListsComplete()) { |
|
sadrul
2016/06/10 18:01:49
Early return if not-complete instead in these func
kylechar
2016/06/10 19:29:31
Done.
|
| + auto& devices = manager_->GetKeyboardDevices(); |
| + observers_.ForAllPtrs([&devices](mojom::InputDeviceObserverMojo* observer) { |
| + observer->OnKeyboardDeviceConfigurationChanged( |
| + mojo::Array<ui::InputDevice>::From(devices)); |
| + }); |
| + } |
| +} |
| + |
| +void InputDeviceServer::OnTouchscreenDeviceConfigurationChanged() { |
| + if (manager_->AreDeviceListsComplete()) { |
| + auto& devices = manager_->GetTouchscreenDevices(); |
| + observers_.ForAllPtrs([&devices](mojom::InputDeviceObserverMojo* observer) { |
| + observer->OnTouchscreenDeviceConfigurationChanged( |
| + mojo::Array<ui::TouchscreenDevice>::From(devices)); |
| + }); |
| + } |
| +} |
| + |
| +void InputDeviceServer::OnMouseDeviceConfigurationChanged() { |
| + if (manager_->AreDeviceListsComplete()) { |
| + auto& devices = manager_->GetMouseDevices(); |
| + observers_.ForAllPtrs([&devices](mojom::InputDeviceObserverMojo* observer) { |
| + observer->OnMouseDeviceConfigurationChanged( |
| + mojo::Array<ui::InputDevice>::From(devices)); |
| + }); |
| + } |
| +} |
| + |
| +void InputDeviceServer::OnTouchpadDeviceConfigurationChanged() { |
| + if (manager_->AreDeviceListsComplete()) { |
| + auto& devices = manager_->GetTouchpadDevices(); |
| + observers_.ForAllPtrs([&devices](mojom::InputDeviceObserverMojo* observer) { |
| + observer->OnTouchpadDeviceConfigurationChanged( |
| + mojo::Array<ui::InputDevice>::From(devices)); |
| + }); |
| + } |
| +} |
| + |
| +void InputDeviceServer::OnDeviceListsComplete() { |
| + observers_.ForAllPtrs([this](mojom::InputDeviceObserverMojo* observer) { |
| + SendDeviceListsComplete(observer); |
| + }); |
| +} |
| + |
| +void InputDeviceServer::SendDeviceListsComplete( |
| + mojom::InputDeviceObserverMojo* observer) { |
| + DCHECK(manager_->AreDeviceListsComplete()); |
| + |
| + auto& keyboard_devices = manager_->GetKeyboardDevices(); |
| + auto& touchscreen_devices = manager_->GetTouchscreenDevices(); |
| + auto& mouse_devices = manager_->GetMouseDevices(); |
| + auto& touchpad_devices = manager_->GetTouchpadDevices(); |
| + observer->OnDeviceListsComplete( |
| + mojo::Array<ui::InputDevice>::From(keyboard_devices), |
|
sadrul
2016/06/10 18:01:49
Do you need to explicitly do this? i.e. are struct
kylechar
2016/06/10 19:29:31
Ah they are smart enough. Fixed here and above.
|
| + mojo::Array<ui::TouchscreenDevice>::From(touchscreen_devices), |
| + mojo::Array<ui::InputDevice>::From(mouse_devices), |
| + mojo::Array<ui::InputDevice>::From(touchpad_devices)); |
| +} |
| + |
| +} // namespace input_device |