Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(68)

Side by Side Diff: chromeos/dbus/biod/fake_biod_client.cc

Issue 2644233002: cros: Added a fake fingerprint storage class. (Closed)
Patch Set: Changed fake fingerprint from string to string vec. Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2017 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chromeos/dbus/biod/fake_biod_client.h" 5 #include "chromeos/dbus/biod/fake_biod_client.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/memory/ptr_util.h"
8 #include "base/single_thread_task_runner.h" 9 #include "base/single_thread_task_runner.h"
9 #include "base/threading/thread_task_runner_handle.h" 10 #include "base/threading/thread_task_runner_handle.h"
10 #include "dbus/object_path.h" 11 #include "dbus/object_path.h"
11 #include "third_party/cros_system_api/dbus/service_constants.h" 12 #include "third_party/cros_system_api/dbus/service_constants.h"
12 13
13 namespace chromeos { 14 namespace chromeos {
14 15
16 namespace {
17
18 // Path of an enroll session. There should only be one enroll session at a
19 // given time.
20 const char kEnrollSessionObjectPath[] = "/EnrollSession/";
21 // Header of the path of an record. A unique number will be appended when an
22 // record is created.
23 const char RecordObjectPathPrefix[] = "/Record/";
24 // Path of an auth session. There should only be one auth sesion at a given
25 // time.
26 const char kAuthSessionObjectPath[] = "/AuthSession/";
27
28 } // namespace
29
15 FakeBiodClient::FakeBiodClient() {} 30 FakeBiodClient::FakeBiodClient() {}
16 31
17 FakeBiodClient::~FakeBiodClient() {} 32 FakeBiodClient::~FakeBiodClient() {}
18 33
19 void FakeBiodClient::SendEnrollScanDone(biod::ScanResult type_result, 34 void FakeBiodClient::SendEnrollScanDone(const std::string& fingerprint,
35 biod::ScanResult type_result,
20 bool is_complete) { 36 bool is_complete) {
37 // Enroll scan signals do nothing if an enroll session is not happening.
38 if (current_session_ != FingerprintSession::ENROLL)
39 return;
40
41 DCHECK(current_record_);
42 // The fake fingerprint gets appended to the current fake fingerprints.
43 current_record_->fake_fingerprint.push_back(fingerprint);
44
45 // If the enroll is complete, save the record and exit enroll mode.
46 if (is_complete) {
47 records_[current_record_path_] = std::move(current_record_);
48 current_record_path_ = dbus::ObjectPath();
49 current_record_.reset();
50 current_session_ = FingerprintSession::NONE;
51 }
52
21 for (auto& observer : observers_) 53 for (auto& observer : observers_)
22 observer.BiodEnrollScanDoneReceived(type_result, is_complete); 54 observer.BiodEnrollScanDoneReceived(type_result, is_complete);
23 } 55 }
24 56
25 void FakeBiodClient::SendAuthScanDone(biod::ScanResult type_result, 57 void FakeBiodClient::SendAuthScanDone(const std::string& fingerprint,
26 const AuthScanMatches& matches) { 58 biod::ScanResult type_result) {
59 // Auth scan signals do nothing if an auth session is not happening.
60 if (current_session_ != FingerprintSession::AUTH)
61 return;
62
63 AuthScanMatches matches;
64 // Iterate through all the records to check if fingerprint is a match and
65 // populate |matches| accordingly. This searches through all the records and
66 // then each record's fake fingerprint, but neither of these should ever have
67 // more than five entries.
68 for (const auto& record : records_) {
69 if (std::find(record.second->fake_fingerprint.begin(),
70 record.second->fake_fingerprint.end(),
71 fingerprint) != record.second->fake_fingerprint.end()) {
72 std::string user_id = record.second->user_id;
Daniel Erat 2017/04/07 23:40:56 const std::string& ?
sammiequon 2017/04/08 00:43:32 Done.
73 matches[user_id].push_back(record.second->label);
74 }
75 }
76
27 for (auto& observer : observers_) 77 for (auto& observer : observers_)
28 observer.BiodAuthScanDoneReceived(type_result, matches); 78 observer.BiodAuthScanDoneReceived(type_result, matches);
29 } 79 }
30 80
31 void FakeBiodClient::SendSessionFailed() { 81 void FakeBiodClient::SendSessionFailed() {
82 if (current_session_ == FingerprintSession::NONE)
83 return;
84
32 for (auto& observer : observers_) 85 for (auto& observer : observers_)
33 observer.BiodSessionFailedReceived(); 86 observer.BiodSessionFailedReceived();
34 } 87 }
35 88
36 void FakeBiodClient::Init(dbus::Bus* bus) {} 89 void FakeBiodClient::Init(dbus::Bus* bus) {}
37 90
38 void FakeBiodClient::AddObserver(Observer* observer) { 91 void FakeBiodClient::AddObserver(Observer* observer) {
39 observers_.AddObserver(observer); 92 observers_.AddObserver(observer);
40 } 93 }
41 94
42 void FakeBiodClient::RemoveObserver(Observer* observer) { 95 void FakeBiodClient::RemoveObserver(Observer* observer) {
43 observers_.RemoveObserver(observer); 96 observers_.RemoveObserver(observer);
44 } 97 }
45 98
46 bool FakeBiodClient::HasObserver(const Observer* observer) const { 99 bool FakeBiodClient::HasObserver(const Observer* observer) const {
47 return observers_.HasObserver(observer); 100 return observers_.HasObserver(observer);
48 } 101 }
49 102
50 void FakeBiodClient::StartEnrollSession(const std::string& /* user_id */, 103 void FakeBiodClient::StartEnrollSession(const std::string& user_id,
51 const std::string& /* label */, 104 const std::string& label,
52 const ObjectPathCallback& callback) { 105 const ObjectPathCallback& callback) {
106 DCHECK_EQ(current_session_, FingerprintSession::NONE);
107
108 // Create the enrollment with |user_id|, |label| and a empty fake fingerprint.
109 current_record_path_ = dbus::ObjectPath(
110 RecordObjectPathPrefix + std::to_string(next_record_unique_id_++));
111 current_record_ = base::MakeUnique<FakeRecord>();
112 current_record_->user_id = user_id;
113 current_record_->label = label;
114 current_session_ = FingerprintSession::ENROLL;
115
53 base::ThreadTaskRunnerHandle::Get()->PostTask( 116 base::ThreadTaskRunnerHandle::Get()->PostTask(
54 FROM_HERE, base::Bind(callback, dbus::ObjectPath())); 117 FROM_HERE,
118 base::Bind(callback, dbus::ObjectPath(kEnrollSessionObjectPath)));
55 } 119 }
56 120
57 void FakeBiodClient::GetRecordsForUser(const std::string& /* user_id */, 121 void FakeBiodClient::GetRecordsForUser(const std::string& user_id,
58 const UserRecordsCallback& callback) { 122 const UserRecordsCallback& callback) {
123 std::vector<dbus::ObjectPath> records_object_paths;
124 for (const auto& record : records_) {
125 if (record.second->user_id == user_id)
126 records_object_paths.push_back(record.first);
127 }
128
59 base::ThreadTaskRunnerHandle::Get()->PostTask( 129 base::ThreadTaskRunnerHandle::Get()->PostTask(
60 FROM_HERE, base::Bind(callback, std::vector<dbus::ObjectPath>())); 130 FROM_HERE, base::Bind(callback, records_object_paths));
61 } 131 }
62 132
63 void FakeBiodClient::DestroyAllRecords() {} 133 void FakeBiodClient::DestroyAllRecords() {
134 records_.clear();
135 }
64 136
65 void FakeBiodClient::StartAuthSession(const ObjectPathCallback& callback) { 137 void FakeBiodClient::StartAuthSession(const ObjectPathCallback& callback) {
138 DCHECK_EQ(current_session_, FingerprintSession::NONE);
139
140 current_session_ = FingerprintSession::AUTH;
66 base::ThreadTaskRunnerHandle::Get()->PostTask( 141 base::ThreadTaskRunnerHandle::Get()->PostTask(
67 FROM_HERE, base::Bind(callback, dbus::ObjectPath())); 142 FROM_HERE,
143 base::Bind(callback, dbus::ObjectPath(kAuthSessionObjectPath)));
68 } 144 }
69 145
70 void FakeBiodClient::RequestType(const BiometricTypeCallback& callback) { 146 void FakeBiodClient::RequestType(const BiometricTypeCallback& callback) {
71 base::ThreadTaskRunnerHandle::Get()->PostTask( 147 base::ThreadTaskRunnerHandle::Get()->PostTask(
72 FROM_HERE, 148 FROM_HERE,
73 base::Bind(callback, biod::BiometricType::BIOMETRIC_TYPE_UNKNOWN)); 149 base::Bind(callback, biod::BiometricType::BIOMETRIC_TYPE_FINGERPRINT));
74 } 150 }
75 151
76 void FakeBiodClient::CancelEnrollSession( 152 void FakeBiodClient::CancelEnrollSession(
77 const dbus::ObjectPath& /* enroll_session_path */) {} 153 const dbus::ObjectPath& enroll_session_path) {
154 DCHECK_EQ(current_session_, FingerprintSession::ENROLL);
155 DCHECK_EQ(enroll_session_path.value(), kEnrollSessionObjectPath);
78 156
79 void FakeBiodClient::EndAuthSession( 157 // Clean up the in progress enrollment.
80 const dbus::ObjectPath& /* auth_session_path */) {} 158 current_record_.reset();
81 159 current_record_path_ = dbus::ObjectPath();
82 void FakeBiodClient::SetRecordLabel(const dbus::ObjectPath& /* record_path */, 160 current_session_ = FingerprintSession::NONE;
83 const std::string& /* label */) {}
84
85 void FakeBiodClient::RemoveRecord(const dbus::ObjectPath& /* record_path */) {}
86
87 void FakeBiodClient::RequestRecordLabel(
88 const dbus::ObjectPath& /* record_path */,
89 const LabelCallback& callback) {
90 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
91 base::Bind(callback, ""));
92 } 161 }
93 162
163 void FakeBiodClient::EndAuthSession(const dbus::ObjectPath& auth_session_path) {
164 DCHECK_EQ(current_session_, FingerprintSession::AUTH);
165 DCHECK_EQ(auth_session_path.value(), kAuthSessionObjectPath);
166
167 current_session_ = FingerprintSession::NONE;
168 }
169
170 void FakeBiodClient::SetRecordLabel(const dbus::ObjectPath& record_path,
171 const std::string& label) {
172 if (records_.find(record_path) != records_.end())
173 records_[record_path]->label = label;
174 }
175
176 void FakeBiodClient::RemoveRecord(const dbus::ObjectPath& record_path) {
177 records_.erase(record_path);
178 }
179
180 void FakeBiodClient::RequestRecordLabel(const dbus::ObjectPath& record_path,
181 const LabelCallback& callback) {
182 std::string record_label;
183 if (records_.find(record_path) != records_.end())
184 record_label = records_[record_path]->label;
185
186 base::ThreadTaskRunnerHandle::Get()->PostTask(
187 FROM_HERE, base::Bind(callback, record_label));
188 }
189
190 void FakeBiodClient::Reset() {
191 records_.clear();
192 current_record_.reset();
193 current_record_path_ = dbus::ObjectPath();
194 current_session_ = FingerprintSession::NONE;
195 }
196
197 FakeBiodClient::FakeRecord::FakeRecord() {}
198
199 FakeBiodClient::FakeRecord::~FakeRecord() {}
200
94 } // namespace chromeos 201 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698