hid_manager_impl.cc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // Copyright 2017 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "services/device/hid/hid_manager_impl.h"
  5. #include <utility>
  6. #include "base/bind.h"
  7. #include "base/callback_helpers.h"
  8. #include "base/lazy_instance.h"
  9. #include "mojo/public/cpp/bindings/associated_remote.h"
  10. #include "mojo/public/cpp/bindings/pending_remote.h"
  11. #include "mojo/public/cpp/bindings/self_owned_receiver.h"
  12. #include "services/device/hid/hid_connection_impl.h"
  13. namespace device {
  14. base::LazyInstance<std::unique_ptr<HidService>>::Leaky g_hid_service =
  15. LAZY_INSTANCE_INITIALIZER;
  16. HidManagerImpl::HidManagerImpl() {
  17. if (g_hid_service.Get())
  18. hid_service_ = std::move(g_hid_service.Get());
  19. else
  20. hid_service_ = HidService::Create();
  21. DCHECK(hid_service_);
  22. hid_service_observation_.Observe(hid_service_.get());
  23. }
  24. HidManagerImpl::~HidManagerImpl() {}
  25. // static
  26. void HidManagerImpl::SetHidServiceForTesting(
  27. std::unique_ptr<HidService> hid_service) {
  28. g_hid_service.Get() = std::move(hid_service);
  29. }
  30. // static
  31. bool HidManagerImpl::IsHidServiceTesting() {
  32. return g_hid_service.IsCreated();
  33. }
  34. void HidManagerImpl::AddReceiver(
  35. mojo::PendingReceiver<mojom::HidManager> receiver) {
  36. receivers_.Add(this, std::move(receiver));
  37. }
  38. void HidManagerImpl::GetDevicesAndSetClient(
  39. mojo::PendingAssociatedRemote<mojom::HidManagerClient> client,
  40. GetDevicesCallback callback) {
  41. hid_service_->GetDevices(base::BindOnce(
  42. &HidManagerImpl::CreateDeviceList, weak_factory_.GetWeakPtr(),
  43. std::move(callback), std::move(client)));
  44. }
  45. void HidManagerImpl::GetDevices(GetDevicesCallback callback) {
  46. hid_service_->GetDevices(base::BindOnce(
  47. &HidManagerImpl::CreateDeviceList, weak_factory_.GetWeakPtr(),
  48. std::move(callback), mojo::NullAssociatedRemote()));
  49. }
  50. void HidManagerImpl::CreateDeviceList(
  51. GetDevicesCallback callback,
  52. mojo::PendingAssociatedRemote<mojom::HidManagerClient> client,
  53. std::vector<mojom::HidDeviceInfoPtr> devices) {
  54. std::move(callback).Run(std::move(devices));
  55. if (!client.is_valid())
  56. return;
  57. clients_.Add(std::move(client));
  58. }
  59. void HidManagerImpl::Connect(
  60. const std::string& device_guid,
  61. mojo::PendingRemote<mojom::HidConnectionClient> connection_client,
  62. mojo::PendingRemote<mojom::HidConnectionWatcher> watcher,
  63. bool allow_protected_reports,
  64. bool allow_fido_reports,
  65. ConnectCallback callback) {
  66. hid_service_->Connect(
  67. device_guid, allow_protected_reports, allow_fido_reports,
  68. base::BindOnce(&HidManagerImpl::CreateConnection,
  69. weak_factory_.GetWeakPtr(), std::move(callback),
  70. std::move(connection_client), std::move(watcher)));
  71. }
  72. void HidManagerImpl::CreateConnection(
  73. ConnectCallback callback,
  74. mojo::PendingRemote<mojom::HidConnectionClient> connection_client,
  75. mojo::PendingRemote<mojom::HidConnectionWatcher> watcher,
  76. scoped_refptr<HidConnection> connection) {
  77. if (!connection) {
  78. std::move(callback).Run(mojo::NullRemote());
  79. return;
  80. }
  81. mojo::PendingRemote<mojom::HidConnection> client;
  82. HidConnectionImpl::Create(connection, client.InitWithNewPipeAndPassReceiver(),
  83. std::move(connection_client), std::move(watcher));
  84. std::move(callback).Run(std::move(client));
  85. }
  86. void HidManagerImpl::OnDeviceAdded(mojom::HidDeviceInfoPtr device) {
  87. for (auto& client : clients_)
  88. client->DeviceAdded(device->Clone());
  89. }
  90. void HidManagerImpl::OnDeviceRemoved(mojom::HidDeviceInfoPtr device) {
  91. for (auto& client : clients_)
  92. client->DeviceRemoved(device->Clone());
  93. }
  94. void HidManagerImpl::OnDeviceChanged(mojom::HidDeviceInfoPtr device) {
  95. for (auto& client : clients_)
  96. client->DeviceChanged(device->Clone());
  97. }
  98. } // namespace device