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