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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
|
// 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 "chromeos/ash/components/dbus/biod/biod_client.h"
#include <stdint.h>
#include <memory>
#include <utility>
#include "base/command_line.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/logging.h"
#include "base/memory/raw_ptr.h"
#include "base/strings/stringprintf.h"
#include "chromeos/ash/components/dbus/biod/constants.pb.h"
#include "chromeos/ash/components/dbus/biod/fake_biod_client.h"
#include "chromeos/ash/components/dbus/biod/messages.pb.h"
#include "chromeos/dbus/constants/dbus_switches.h"
#include "dbus/bus.h"
#include "dbus/message.h"
#include "dbus/object_path.h"
#include "dbus/object_proxy.h"
namespace ash {
namespace {
BiodClient* g_instance = nullptr;
// D-Bus response handler for methods that use void callbacks.
void OnVoidResponse(chromeos::VoidDBusMethodCallback callback,
dbus::Response* response) {
std::move(callback).Run(response != nullptr);
}
} // namespace
// The BiodClient implementation used in production.
class BiodClientImpl : public BiodClient {
public:
BiodClientImpl() = default;
BiodClientImpl(const BiodClientImpl&) = delete;
BiodClientImpl& operator=(const BiodClientImpl&) = delete;
~BiodClientImpl() override = default;
// BiodClient overrides:
void AddObserver(Observer* observer) override {
observers_.AddObserver(observer);
}
void RemoveObserver(Observer* observer) override {
observers_.RemoveObserver(observer);
}
bool HasObserver(const Observer* observer) const override {
return observers_.HasObserver(observer);
}
void StartEnrollSession(const std::string& user_id,
const std::string& label,
chromeos::ObjectPathCallback callback) override {
// If we are already in enroll session, just return an invalid ObjectPath.
// The one who initially start the enroll session will have control
// over the life cycle of the session.
if (current_enroll_session_path_) {
std::move(callback).Run(dbus::ObjectPath());
return;
}
dbus::MethodCall method_call(
biod::kBiometricsManagerInterface,
biod::kBiometricsManagerStartEnrollSessionMethod);
dbus::MessageWriter writer(&method_call);
writer.AppendString(user_id);
writer.AppendString(label);
biod_proxy_->CallMethod(
&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
base::BindOnce(&BiodClientImpl::OnStartEnrollSession,
weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
}
void GetRecordsForUser(const std::string& user_id,
UserRecordsCallback callback) override {
dbus::MethodCall method_call(
biod::kBiometricsManagerInterface,
biod::kBiometricsManagerGetRecordsForUserMethod);
dbus::MessageWriter writer(&method_call);
writer.AppendString(user_id);
biod_proxy_->CallMethod(
&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
base::BindOnce(&BiodClientImpl::OnGetRecordsForUser,
weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
}
void DestroyAllRecords(chromeos::VoidDBusMethodCallback callback) override {
dbus::MethodCall method_call(
biod::kBiometricsManagerInterface,
biod::kBiometricsManagerDestroyAllRecordsMethod);
biod_proxy_->CallMethod(
&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
base::BindOnce(&OnVoidResponse, std::move(callback)));
}
void StartAuthSession(chromeos::ObjectPathCallback callback) override {
// If we are already in auth session, just return an invalid ObjectPath.
// The one who initially start the auth session will have control
// over the life cycle of the session.
if (current_auth_session_path_) {
std::move(callback).Run(dbus::ObjectPath(std::string()));
return;
}
dbus::MethodCall method_call(
biod::kBiometricsManagerInterface,
biod::kBiometricsManagerStartAuthSessionMethod);
biod_proxy_->CallMethod(
&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
base::BindOnce(&BiodClientImpl::OnStartAuthSession,
weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
}
void RequestType(BiometricTypeCallback callback) override {
dbus::MethodCall method_call(dbus::kDBusPropertiesInterface,
dbus::kDBusPropertiesGet);
dbus::MessageWriter writer(&method_call);
writer.AppendString(biod::kBiometricsManagerInterface);
writer.AppendString(biod::kBiometricsManagerBiometricTypeProperty);
biod_proxy_->CallMethod(
&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
base::BindOnce(&BiodClientImpl::OnRequestType,
weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
}
void CancelEnrollSession(chromeos::VoidDBusMethodCallback callback) override {
if (!current_enroll_session_path_) {
std::move(callback).Run(true);
return;
}
dbus::MethodCall method_call(biod::kEnrollSessionInterface,
biod::kEnrollSessionCancelMethod);
dbus::ObjectProxy* enroll_session_proxy = bus_->GetObjectProxy(
biod::kBiodServiceName, *current_enroll_session_path_);
enroll_session_proxy->CallMethod(
&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
base::BindOnce(&OnVoidResponse, std::move(callback)));
current_enroll_session_path_.reset();
}
void EndAuthSession(chromeos::VoidDBusMethodCallback callback) override {
if (!current_auth_session_path_) {
std::move(callback).Run(true);
return;
}
dbus::MethodCall method_call(biod::kAuthSessionInterface,
biod::kAuthSessionEndMethod);
dbus::ObjectProxy* auth_session_proxy = bus_->GetObjectProxy(
biod::kBiodServiceName, *current_auth_session_path_);
auth_session_proxy->CallMethod(
&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
base::BindOnce(&OnVoidResponse, std::move(callback)));
current_auth_session_path_.reset();
}
void SetRecordLabel(const dbus::ObjectPath& record_path,
const std::string& label,
chromeos::VoidDBusMethodCallback callback) override {
dbus::MethodCall method_call(biod::kRecordInterface,
biod::kRecordSetLabelMethod);
dbus::MessageWriter writer(&method_call);
writer.AppendString(label);
dbus::ObjectProxy* record_proxy =
bus_->GetObjectProxy(biod::kBiodServiceName, record_path);
record_proxy->CallMethod(
&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
base::BindOnce(&OnVoidResponse, std::move(callback)));
}
void RemoveRecord(const dbus::ObjectPath& record_path,
chromeos::VoidDBusMethodCallback callback) override {
dbus::MethodCall method_call(biod::kRecordInterface,
biod::kRecordRemoveMethod);
dbus::ObjectProxy* record_proxy =
bus_->GetObjectProxy(biod::kBiodServiceName, record_path);
record_proxy->CallMethod(
&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
base::BindOnce(&OnVoidResponse, std::move(callback)));
}
void RequestRecordLabel(const dbus::ObjectPath& record_path,
LabelCallback callback) override {
dbus::MethodCall method_call(dbus::kDBusPropertiesInterface,
dbus::kDBusPropertiesGet);
dbus::MessageWriter writer(&method_call);
writer.AppendString(biod::kRecordInterface);
writer.AppendString(biod::kRecordLabelProperty);
dbus::ObjectProxy* record_proxy =
bus_->GetObjectProxy(biod::kBiodServiceName, record_path);
record_proxy->CallMethod(
&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
base::BindOnce(&BiodClientImpl::OnRequestRecordLabel,
weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
}
void Init(dbus::Bus* bus) {
bus_ = bus;
dbus::ObjectPath fpc_bio_path = dbus::ObjectPath(base::StringPrintf(
"%s/%s", biod::kBiodServicePath, biod::kCrosFpBiometricsManagerName));
biod_proxy_ = bus_->GetObjectProxy(biod::kBiodServiceName, fpc_bio_path);
biod_proxy_->SetNameOwnerChangedCallback(
base::BindRepeating(&BiodClientImpl::NameOwnerChangedReceived,
weak_ptr_factory_.GetWeakPtr()));
biod_proxy_->ConnectToSignal(
biod::kBiometricsManagerInterface,
biod::kBiometricsManagerStatusChangedSignal,
base::BindRepeating(&BiodClientImpl::OnStatusChanged,
weak_ptr_factory_.GetWeakPtr()),
base::BindOnce(&BiodClientImpl::OnSignalConnected,
weak_ptr_factory_.GetWeakPtr()));
biod_proxy_->ConnectToSignal(
biod::kBiometricsManagerInterface,
biod::kBiometricsManagerEnrollScanDoneSignal,
base::BindRepeating(&BiodClientImpl::EnrollScanDoneReceived,
weak_ptr_factory_.GetWeakPtr()),
base::BindOnce(&BiodClientImpl::OnSignalConnected,
weak_ptr_factory_.GetWeakPtr()));
biod_proxy_->ConnectToSignal(
biod::kBiometricsManagerInterface,
biod::kBiometricsManagerAuthScanDoneSignal,
base::BindRepeating(&BiodClientImpl::AuthScanDoneReceived,
weak_ptr_factory_.GetWeakPtr()),
base::BindOnce(&BiodClientImpl::OnSignalConnected,
weak_ptr_factory_.GetWeakPtr()));
biod_proxy_->ConnectToSignal(
biod::kBiometricsManagerInterface,
biod::kBiometricsManagerSessionFailedSignal,
base::BindRepeating(&BiodClientImpl::SessionFailedReceived,
weak_ptr_factory_.GetWeakPtr()),
base::BindOnce(&BiodClientImpl::OnSignalConnected,
weak_ptr_factory_.GetWeakPtr()));
}
private:
void OnStartEnrollSession(chromeos::ObjectPathCallback callback,
dbus::Response* response) {
dbus::ObjectPath result;
if (response) {
dbus::MessageReader reader(response);
if (!reader.PopObjectPath(&result)) {
LOG(ERROR) << biod::kBiometricsManagerStartEnrollSessionMethod
<< " had incorrect response.";
}
}
if (result.IsValid())
current_enroll_session_path_ = std::make_unique<dbus::ObjectPath>(result);
std::move(callback).Run(result);
}
void OnGetRecordsForUser(UserRecordsCallback callback,
dbus::Response* response) {
std::vector<dbus::ObjectPath> result;
bool success = false;
if (response) {
success = response->GetMessageType() ==
dbus::Message::MessageType::MESSAGE_METHOD_RETURN;
if (!success) {
LOG(ERROR) << biod::kBiometricsManagerGetRecordsForUserMethod
<< " had error response.";
} else if (response) {
dbus::MessageReader reader(response);
reader.PopArrayOfObjectPaths(&result);
}
}
std::move(callback).Run(result, success);
}
void OnStartAuthSession(chromeos::ObjectPathCallback callback,
dbus::Response* response) {
dbus::ObjectPath result;
if (response) {
dbus::MessageReader reader(response);
if (!reader.PopObjectPath(&result)) {
LOG(ERROR) << biod::kBiometricsManagerStartAuthSessionMethod
<< " had incorrect response.";
}
}
if (result.IsValid())
current_auth_session_path_ = std::make_unique<dbus::ObjectPath>(result);
std::move(callback).Run(result);
}
void OnRequestType(BiometricTypeCallback callback, dbus::Response* response) {
biod::BiometricType result = biod::BIOMETRIC_TYPE_UNKNOWN;
if (response) {
dbus::MessageReader reader(response);
uint32_t value;
if (reader.PopVariantOfUint32(&value)) {
result = static_cast<biod::BiometricType>(value);
CHECK(result >= 0 && result < biod::BIOMETRIC_TYPE_MAX);
} else {
LOG(ERROR) << biod::kBiometricsManagerBiometricTypeProperty
<< " had incorrect response.";
}
}
std::move(callback).Run(result);
}
void OnRequestRecordLabel(LabelCallback callback, dbus::Response* response) {
std::string result;
if (response) {
dbus::MessageReader reader(response);
if (!reader.PopVariantOfString(&result))
LOG(ERROR) << biod::kRecordLabelProperty << " had incorrect response.";
}
std::move(callback).Run(result);
}
// Called when the biometrics signal is initially connected.
void OnSignalConnected(const std::string& interface_name,
const std::string& signal_name,
bool success) {
LOG_IF(ERROR, !success)
<< "Failed to connect to biometrics signal: " << signal_name;
}
void NameOwnerChangedReceived(const std::string& /* old_owner */,
const std::string& new_owner) {
current_enroll_session_path_.reset();
current_auth_session_path_.reset();
if (!new_owner.empty()) {
for (auto& observer : observers_)
observer.BiodServiceRestarted();
}
}
void OnStatusChanged(dbus::Signal* signal) {
current_enroll_session_path_.reset();
current_auth_session_path_.reset();
biod::BiometricsManagerStatusChanged proto;
dbus::MessageReader reader(signal);
CHECK(reader.PopArrayOfBytesAsProto(&proto));
biod::BiometricsManagerStatus status = proto.status();
for (auto& observer : observers_) {
observer.BiodServiceStatusChanged(status);
}
}
void EnrollScanDoneReceived(dbus::Signal* signal) {
dbus::MessageReader reader(signal);
biod::EnrollScanDone protobuf;
if (!reader.PopArrayOfBytesAsProto(&protobuf)) {
LOG(ERROR) << "Unable to decode protocol buffer from "
<< biod::kBiometricsManagerEnrollScanDoneSignal << " signal.";
return;
}
int percent_complete =
protobuf.has_percent_complete() ? protobuf.percent_complete() : -1;
// Enroll session is ended automatically when enrollment is done.
if (protobuf.done())
current_enroll_session_path_.reset();
for (auto& observer : observers_) {
observer.BiodEnrollScanDoneReceived(protobuf.scan_result(),
protobuf.done(), percent_complete);
}
}
void AuthScanDoneReceived(dbus::Signal* signal) {
dbus::MessageReader signal_reader(signal);
dbus::MessageReader array_reader(nullptr);
AuthScanMatches matches;
biod::FingerprintMessage msg;
if (!signal_reader.PopArrayOfBytesAsProto(&msg)) {
LOG(ERROR) << "Signal doesn't contain protobuf with authentication "
<< "result.";
return;
}
if (!signal_reader.PopArray(&array_reader)) {
LOG(ERROR) << "Can't extract matches array from AuthScanDone signal";
return;
}
while (array_reader.HasMoreData()) {
dbus::MessageReader entry_reader(nullptr);
std::string user_id;
std::vector<dbus::ObjectPath> paths;
if (!array_reader.PopDictEntry(&entry_reader) ||
!entry_reader.PopString(&user_id) ||
!entry_reader.PopArrayOfObjectPaths(&paths)) {
LOG(ERROR) << "Can't read match data from AuthScanDone signal";
return;
}
matches[user_id] = std::move(paths);
}
for (auto& observer : observers_) {
observer.BiodAuthScanDoneReceived(msg, matches);
}
}
void SessionFailedReceived(dbus::Signal* signal) {
for (auto& observer : observers_)
observer.BiodSessionFailedReceived();
}
raw_ptr<dbus::Bus> bus_ = nullptr;
raw_ptr<dbus::ObjectProxy> biod_proxy_ = nullptr;
base::ObserverList<Observer>::Unchecked observers_;
std::unique_ptr<dbus::ObjectPath> current_enroll_session_path_;
std::unique_ptr<dbus::ObjectPath> current_auth_session_path_;
// Note: This should remain the last member so it'll be destroyed and
// invalidate its weak pointers before any other members are destroyed.
base::WeakPtrFactory<BiodClientImpl> weak_ptr_factory_{this};
};
BiodClient::BiodClient() {
DCHECK(!g_instance);
g_instance = this;
}
BiodClient::~BiodClient() {
DCHECK_EQ(this, g_instance);
g_instance = nullptr;
}
// static
void BiodClient::Initialize(dbus::Bus* bus) {
if (base::CommandLine::ForCurrentProcess()->HasSwitch(
chromeos::switches::kBiodFake)) {
BiodClient::InitializeFake();
} else {
DCHECK(bus);
(new BiodClientImpl())->Init(bus);
}
}
// static
void BiodClient::InitializeFake() {
new FakeBiodClient();
}
// static
void BiodClient::Shutdown() {
DCHECK(g_instance);
delete g_instance;
}
// static
BiodClient* BiodClient::Get() {
return g_instance;
}
} // namespace ash
|