Chromium Code Reviews| Index: chromeos/dbus/biod/fake_biod_client.cc |
| diff --git a/chromeos/dbus/biod/fake_biod_client.cc b/chromeos/dbus/biod/fake_biod_client.cc |
| index 0ff6ab5d9cf9732bdd43d4da22f608e819f405e0..dfdfd0a17c570d86f7aeaa83c1d792b40b3dcd98 100644 |
| --- a/chromeos/dbus/biod/fake_biod_client.cc |
| +++ b/chromeos/dbus/biod/fake_biod_client.cc |
| @@ -5,6 +5,7 @@ |
| #include "chromeos/dbus/biod/fake_biod_client.h" |
| #include "base/bind.h" |
| +#include "base/memory/ptr_util.h" |
| #include "base/single_thread_task_runner.h" |
| #include "base/threading/thread_task_runner_handle.h" |
| #include "dbus/object_path.h" |
| @@ -12,23 +13,75 @@ |
| namespace chromeos { |
| +namespace { |
| + |
| +// Path of an enroll session. There should only be one enroll session at a |
| +// given time. |
| +const char kFakeEnrollSessionObjectPath[] = "/fake/EnrollSession/"; |
|
Daniel Erat
2017/04/07 18:27:55
i would drop "Fake" from all of these names to mak
sammiequon
2017/04/07 20:45:50
Done.
|
| +// Header of the path of an record. A unique number will be appended when an |
| +// record is created. |
| +const char kFakeRecordObjectPathHeader[] = "/fake/Record/"; |
|
Daniel Erat
2017/04/07 18:27:56
s/Header/Prefix/
sammiequon
2017/04/07 20:45:50
Done.
|
| +// Path of an auth session. There should only be one auth sesion at a given |
| +// time. |
| +const char kFakeAuthSessionObjectPath[] = "/fake/AuthSession/"; |
| + |
| +} // namespace |
| + |
| FakeBiodClient::FakeBiodClient() {} |
| FakeBiodClient::~FakeBiodClient() {} |
| -void FakeBiodClient::SendEnrollScanDone(biod::ScanResult type_result, |
| +void FakeBiodClient::SendEnrollScanDone(char fingerprint, |
| + biod::ScanResult type_result, |
| bool is_complete) { |
| + // Enroll scan signals do nothing if an enroll session is not happening. |
| + if (current_session_ != FingerprintSession::ENROLL) |
| + return; |
| + |
|
Daniel Erat
2017/04/07 18:27:55
DCHECK(current_record_)
sammiequon
2017/04/07 20:45:50
Done.
|
| + // The fake fingerprint gets appended to the current fake fingerprints. |
| + current_record_->fake_fingerprint += fingerprint; |
| + |
| + // If the enroll is complete, save the record and exit enroll mode. |
| + if (is_complete) { |
| + records_[*current_record_path_] = std::move(current_record_); |
| + current_record_path_.reset(); |
| + current_record_.reset(); |
| + current_session_ = FingerprintSession::NONE; |
| + } |
| + |
| for (auto& observer : observers_) |
| observer.BiodEnrollScanDoneReceived(type_result, is_complete); |
| } |
| -void FakeBiodClient::SendAuthScanDone(biod::ScanResult type_result, |
| - const AuthScanMatches& matches) { |
| +void FakeBiodClient::SendAuthScanDone(char fingerprint, |
| + biod::ScanResult type_result) { |
| + // Auth scan signals do nothing if an auth session is not happening. |
| + if (current_session_ != FingerprintSession::AUTH) |
| + return; |
| + |
| + AuthScanMatches matches; |
| + // Iterate throught all the records to check if fingerprint is a match and |
|
Daniel Erat
2017/04/07 18:27:56
s/throught/through/
sammiequon
2017/04/07 20:45:50
Done.
|
| + // populate |matches| accordingly. This searches through all the records and |
| + // then each records' fake fingerprint, but neither of these should ever have |
|
Daniel Erat
2017/04/07 18:27:55
s/records'/record's/
sammiequon
2017/04/07 20:45:50
Done.
|
| + // more than five entries. |
| + for (const auto& record : records_) { |
| + if (record.second->fake_fingerprint.find(fingerprint) != |
| + std::string::npos) { |
| + std::string user_id = record.second->user_id; |
| + if (matches.find(user_id) == matches.end()) |
| + matches[user_id] = std::vector<std::string>(); |
|
Daniel Erat
2017/04/07 18:27:56
you shouldn't need to create this if it's missing;
sammiequon
2017/04/07 20:45:50
Done.
|
| + matches[user_id].push_back(record.second->label); |
| + } |
| + } |
| + |
| for (auto& observer : observers_) |
| observer.BiodAuthScanDoneReceived(type_result, matches); |
| } |
| void FakeBiodClient::SendSessionFailed() { |
| + if (current_session_ == FingerprintSession::NONE) |
| + return; |
| + |
| for (auto& observer : observers_) |
| observer.BiodSessionFailedReceived(); |
| } |
| @@ -47,48 +100,103 @@ bool FakeBiodClient::HasObserver(const Observer* observer) const { |
| return observers_.HasObserver(observer); |
| } |
| -void FakeBiodClient::StartEnrollSession(const std::string& /* user_id */, |
| - const std::string& /* label */, |
| +void FakeBiodClient::StartEnrollSession(const std::string& user_id, |
| + const std::string& label, |
| const ObjectPathCallback& callback) { |
| + DCHECK(current_session_ == FingerprintSession::NONE); |
|
Daniel Erat
2017/04/07 18:27:56
DCHECK_EQ
sammiequon
2017/04/07 20:45:50
Done.
|
| + |
| + // Create the enrollment with |user_id|, |label| and a empty fake fingerprint. |
| + current_record_path_ = base::MakeUnique<dbus::ObjectPath>( |
| + kFakeRecordObjectPathHeader + std::to_string(next_record_unique_id_++)); |
| + current_record_ = base::MakeUnique<FakeRecord>(); |
| + current_record_->user_id = user_id; |
| + current_record_->label = label; |
| + current_record_->fake_fingerprint = ""; |
| + current_session_ = FingerprintSession::ENROLL; |
| + |
| base::ThreadTaskRunnerHandle::Get()->PostTask( |
| - FROM_HERE, base::Bind(callback, dbus::ObjectPath())); |
| + FROM_HERE, |
| + base::Bind(callback, dbus::ObjectPath(kFakeEnrollSessionObjectPath))); |
| } |
| -void FakeBiodClient::GetRecordsForUser(const std::string& /* user_id */, |
| +void FakeBiodClient::GetRecordsForUser(const std::string& user_id, |
| const UserRecordsCallback& callback) { |
| + std::vector<dbus::ObjectPath> records_object_paths; |
| + for (const auto& record : records_) { |
| + if (record.second->user_id == user_id) |
| + records_object_paths.push_back(record.first); |
| + } |
| + |
| base::ThreadTaskRunnerHandle::Get()->PostTask( |
| - FROM_HERE, base::Bind(callback, std::vector<dbus::ObjectPath>())); |
| + FROM_HERE, base::Bind(callback, records_object_paths)); |
| } |
| -void FakeBiodClient::DestroyAllRecords() {} |
| +void FakeBiodClient::DestroyAllRecords() { |
| + records_.clear(); |
| +} |
| void FakeBiodClient::StartAuthSession(const ObjectPathCallback& callback) { |
| + DCHECK(current_session_ == FingerprintSession::NONE); |
|
Daniel Erat
2017/04/07 18:27:55
DCHECK_EQ
sammiequon
2017/04/07 20:45:50
Done.
|
| + |
| + current_session_ = FingerprintSession::AUTH; |
| base::ThreadTaskRunnerHandle::Get()->PostTask( |
| - FROM_HERE, base::Bind(callback, dbus::ObjectPath())); |
| + FROM_HERE, |
| + base::Bind(callback, dbus::ObjectPath(kFakeAuthSessionObjectPath))); |
| } |
| void FakeBiodClient::RequestType(const BiometricTypeCallback& callback) { |
| base::ThreadTaskRunnerHandle::Get()->PostTask( |
| FROM_HERE, |
| - base::Bind(callback, biod::BiometricType::BIOMETRIC_TYPE_UNKNOWN)); |
| + base::Bind(callback, biod::BiometricType::BIOMETRIC_TYPE_FINGERPRINT)); |
| } |
| void FakeBiodClient::CancelEnrollSession( |
| - const dbus::ObjectPath& /* enroll_session_path */) {} |
| + const dbus::ObjectPath& enroll_session_path) { |
| + DCHECK(current_session_ == FingerprintSession::ENROLL); |
|
Daniel Erat
2017/04/07 18:27:56
DCHECK_EQ
sammiequon
2017/04/07 20:45:50
Done.
|
| + if (enroll_session_path.value() != kFakeEnrollSessionObjectPath) |
|
Daniel Erat
2017/04/07 18:27:56
what does it mean if this happens? isn't the calli
sammiequon
2017/04/07 20:45:50
It means user used the path that was handed out by
|
| + return; |
| + |
| + // Clean up the in progress enrollment. |
| + current_record_.reset(); |
| + current_record_path_.reset(); |
| + current_session_ = FingerprintSession::NONE; |
| +} |
| + |
| +void FakeBiodClient::EndAuthSession(const dbus::ObjectPath& auth_session_path) { |
| + DCHECK(current_session_ == FingerprintSession::AUTH); |
|
Daniel Erat
2017/04/07 18:27:56
DCHECK_EQ
sammiequon
2017/04/07 20:45:50
Done.
|
| + if (auth_session_path.value() != kFakeEnrollSessionObjectPath) |
|
Daniel Erat
2017/04/07 18:27:56
same question about what this means? DCHECK? log a
sammiequon
2017/04/07 20:45:50
Done.
|
| + return; |
| + |
| + current_session_ = FingerprintSession::NONE; |
| +} |
| + |
| +void FakeBiodClient::SetRecordLabel(const dbus::ObjectPath& record_path, |
| + const std::string& label) { |
| + if (!label.empty() && records_.find(record_path) != records_.end()) |
|
Daniel Erat
2017/04/07 18:27:56
do you expect the caller to pass an empty label?
sammiequon
2017/04/07 20:45:50
No, but I thought it wouldn't hurt to check.
|
| + records_[record_path]->label = label; |
| +} |
| -void FakeBiodClient::EndAuthSession( |
| - const dbus::ObjectPath& /* auth_session_path */) {} |
| +void FakeBiodClient::RemoveRecord(const dbus::ObjectPath& record_path) { |
| + auto it = records_.find(record_path); |
|
Daniel Erat
2017/04/07 18:27:55
can you just do records_.erase(record_path) if you
sammiequon
2017/04/07 20:45:50
What does it mean bogus paths? If it does the same
|
| + if (it != records_.end()) |
| + records_.erase(it); |
| +} |
| -void FakeBiodClient::SetRecordLabel(const dbus::ObjectPath& /* record_path */, |
| - const std::string& /* label */) {} |
| +void FakeBiodClient::RequestRecordLabel(const dbus::ObjectPath& record_path, |
| + const LabelCallback& callback) { |
| + std::string record_label; |
| + if (records_.find(record_path) != records_.end()) |
| + record_label = records_[record_path]->label; |
| -void FakeBiodClient::RemoveRecord(const dbus::ObjectPath& /* record_path */) {} |
| + base::ThreadTaskRunnerHandle::Get()->PostTask( |
| + FROM_HERE, base::Bind(callback, record_label)); |
| +} |
| -void FakeBiodClient::RequestRecordLabel( |
| - const dbus::ObjectPath& /* record_path */, |
| - const LabelCallback& callback) { |
| - base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| - base::Bind(callback, "")); |
| +void FakeBiodClient::Reset() { |
| + records_.clear(); |
| + current_record_.reset(); |
| + current_record_path_.reset(); |
| + current_session_ = FingerprintSession::NONE; |
| } |
| } // namespace chromeos |