forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfake_hid_impl_for_testing.cc
153 lines (118 loc) · 4.79 KB
/
fake_hid_impl_for_testing.cc
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Copyright 2017 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 "device/fido/fake_hid_impl_for_testing.h"
#include <utility>
#include "base/optional.h"
namespace device {
MockHidConnection::MockHidConnection(
device::mojom::HidDeviceInfoPtr device,
device::mojom::HidConnectionRequest request,
std::vector<uint8_t> connection_channel_id)
: binding_(this, std::move(request)),
device_(std::move(device)),
connection_channel_id_(connection_channel_id) {}
MockHidConnection::~MockHidConnection() {}
void MockHidConnection::Read(ReadCallback callback) {
return ReadPtr(&callback);
}
void MockHidConnection::Write(uint8_t report_id,
const std::vector<uint8_t>& buffer,
WriteCallback callback) {
return WritePtr(report_id, buffer, &callback);
}
void MockHidConnection::GetFeatureReport(uint8_t report_id,
GetFeatureReportCallback callback) {
NOTREACHED();
}
void MockHidConnection::SendFeatureReport(uint8_t report_id,
const std::vector<uint8_t>& buffer,
SendFeatureReportCallback callback) {
NOTREACHED();
}
void MockHidConnection::SetNonce(base::span<uint8_t const> nonce) {
nonce_ = std::vector<uint8_t>(nonce.begin(), nonce.end());
}
bool FakeHidConnection::mock_connection_error_ = false;
FakeHidConnection::FakeHidConnection(device::mojom::HidDeviceInfoPtr device)
: device_(std::move(device)) {}
FakeHidConnection::~FakeHidConnection() = default;
void FakeHidConnection::Read(ReadCallback callback) {
std::vector<uint8_t> buffer = {'F', 'a', 'k', 'e', ' ', 'H', 'i', 'd'};
std::move(callback).Run(true, 0, buffer);
}
void FakeHidConnection::Write(uint8_t report_id,
const std::vector<uint8_t>& buffer,
WriteCallback callback) {
if (mock_connection_error_) {
std::move(callback).Run(false);
return;
}
std::move(callback).Run(true);
}
void FakeHidConnection::GetFeatureReport(uint8_t report_id,
GetFeatureReportCallback callback) {
NOTREACHED();
}
void FakeHidConnection::SendFeatureReport(uint8_t report_id,
const std::vector<uint8_t>& buffer,
SendFeatureReportCallback callback) {
NOTREACHED();
}
FakeHidManager::FakeHidManager() = default;
FakeHidManager::~FakeHidManager() = default;
void FakeHidManager::AddBinding(mojo::ScopedMessagePipeHandle handle) {
bindings_.AddBinding(this,
device::mojom::HidManagerRequest(std::move(handle)));
}
void FakeHidManager::AddBinding2(device::mojom::HidManagerRequest request) {
bindings_.AddBinding(this, std::move(request));
}
void FakeHidManager::GetDevicesAndSetClient(
device::mojom::HidManagerClientAssociatedPtrInfo client,
GetDevicesCallback callback) {
GetDevices(std::move(callback));
device::mojom::HidManagerClientAssociatedPtr client_ptr;
client_ptr.Bind(std::move(client));
clients_.AddPtr(std::move(client_ptr));
}
void FakeHidManager::GetDevices(GetDevicesCallback callback) {
std::vector<device::mojom::HidDeviceInfoPtr> device_list;
for (auto& map_entry : devices_)
device_list.push_back(map_entry.second->Clone());
std::move(callback).Run(std::move(device_list));
}
void FakeHidManager::Connect(const std::string& device_guid,
ConnectCallback callback) {
auto device_it = devices_.find(device_guid);
auto connection_it = connections_.find(device_guid);
if (device_it == devices_.end() || connection_it == connections_.end()) {
std::move(callback).Run(nullptr);
return;
}
std::move(callback).Run(std::move(connection_it->second));
}
void FakeHidManager::AddDevice(device::mojom::HidDeviceInfoPtr device) {
device::mojom::HidDeviceInfo* device_info = device.get();
clients_.ForAllPtrs([device_info](device::mojom::HidManagerClient* client) {
client->DeviceAdded(device_info->Clone());
});
devices_[device->guid] = std::move(device);
}
void FakeHidManager::AddDeviceAndSetConnection(
device::mojom::HidDeviceInfoPtr device,
device::mojom::HidConnectionPtr connection) {
connections_[device->guid] = std::move(connection);
AddDevice(std::move(device));
}
void FakeHidManager::RemoveDevice(const std::string device_guid) {
auto it = devices_.find(device_guid);
if (it == devices_.end())
return;
device::mojom::HidDeviceInfo* device_info = it->second.get();
clients_.ForAllPtrs([device_info](device::mojom::HidManagerClient* client) {
client->DeviceRemoved(device_info->Clone());
});
devices_.erase(it);
}
} // namespace device