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

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

Issue 2644233002: cros: Added a fake fingerprint storage class. (Closed)
Patch Set: Rebased. 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/";
Daniel Erat 2017/04/11 17:50:13 nit: consider adding a blank line after this line
sammiequon 2017/04/11 18:37:49 Done.
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/";
Daniel Erat 2017/04/11 17:50:13 missing the 'k' on the beginning of this constant
sammiequon 2017/04/11 18:37:49 Oops. Done.
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_) {
Daniel Erat 2017/04/11 17:50:13 nit: maybe call this |it| or |pair| or |entry| or
sammiequon 2017/04/11 18:37:49 Done.
69 if (std::find(record.second->fake_fingerprint.begin(),
70 record.second->fake_fingerprint.end(),
71 fingerprint) != record.second->fake_fingerprint.end()) {
72 const std::string& user_id = record.second->user_id;
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
89 void FakeBiodClient::Reset() {
90 records_.clear();
91 current_record_.reset();
92 current_record_path_ = dbus::ObjectPath();
93 current_session_ = FingerprintSession::NONE;
94 }
95
36 void FakeBiodClient::Init(dbus::Bus* bus) {} 96 void FakeBiodClient::Init(dbus::Bus* bus) {}
37 97
38 void FakeBiodClient::AddObserver(Observer* observer) { 98 void FakeBiodClient::AddObserver(Observer* observer) {
39 observers_.AddObserver(observer); 99 observers_.AddObserver(observer);
40 } 100 }
41 101
42 void FakeBiodClient::RemoveObserver(Observer* observer) { 102 void FakeBiodClient::RemoveObserver(Observer* observer) {
43 observers_.RemoveObserver(observer); 103 observers_.RemoveObserver(observer);
44 } 104 }
45 105
46 bool FakeBiodClient::HasObserver(const Observer* observer) const { 106 bool FakeBiodClient::HasObserver(const Observer* observer) const {
47 return observers_.HasObserver(observer); 107 return observers_.HasObserver(observer);
48 } 108 }
49 109
50 void FakeBiodClient::StartEnrollSession(const std::string& /* user_id */, 110 void FakeBiodClient::StartEnrollSession(const std::string& user_id,
51 const std::string& /* label */, 111 const std::string& label,
52 const ObjectPathCallback& callback) { 112 const ObjectPathCallback& callback) {
113 DCHECK_EQ(current_session_, FingerprintSession::NONE);
114
115 // Create the enrollment with |user_id|, |label| and a empty fake fingerprint.
116 current_record_path_ = dbus::ObjectPath(
117 RecordObjectPathPrefix + std::to_string(next_record_unique_id_++));
118 current_record_ = base::MakeUnique<FakeRecord>();
119 current_record_->user_id = user_id;
120 current_record_->label = label;
121 current_session_ = FingerprintSession::ENROLL;
122
53 base::ThreadTaskRunnerHandle::Get()->PostTask( 123 base::ThreadTaskRunnerHandle::Get()->PostTask(
54 FROM_HERE, base::Bind(callback, dbus::ObjectPath())); 124 FROM_HERE,
125 base::Bind(callback, dbus::ObjectPath(kEnrollSessionObjectPath)));
55 } 126 }
56 127
57 void FakeBiodClient::GetRecordsForUser(const std::string& /* user_id */, 128 void FakeBiodClient::GetRecordsForUser(const std::string& user_id,
58 const UserRecordsCallback& callback) { 129 const UserRecordsCallback& callback) {
130 std::vector<dbus::ObjectPath> records_object_paths;
131 for (const auto& record : records_) {
132 if (record.second->user_id == user_id)
133 records_object_paths.push_back(record.first);
134 }
135
59 base::ThreadTaskRunnerHandle::Get()->PostTask( 136 base::ThreadTaskRunnerHandle::Get()->PostTask(
60 FROM_HERE, base::Bind(callback, std::vector<dbus::ObjectPath>())); 137 FROM_HERE, base::Bind(callback, records_object_paths));
61 } 138 }
62 139
63 void FakeBiodClient::DestroyAllRecords(const VoidDBusMethodCallback& callback) { 140 void FakeBiodClient::DestroyAllRecords(const VoidDBusMethodCallback& callback) {
141 records_.clear();
142
64 base::ThreadTaskRunnerHandle::Get()->PostTask( 143 base::ThreadTaskRunnerHandle::Get()->PostTask(
65 FROM_HERE, base::Bind(callback, DBUS_METHOD_CALL_SUCCESS)); 144 FROM_HERE, base::Bind(callback, DBUS_METHOD_CALL_SUCCESS));
66 } 145 }
67 146
68 void FakeBiodClient::StartAuthSession(const ObjectPathCallback& callback) { 147 void FakeBiodClient::StartAuthSession(const ObjectPathCallback& callback) {
148 DCHECK_EQ(current_session_, FingerprintSession::NONE);
149
150 current_session_ = FingerprintSession::AUTH;
69 base::ThreadTaskRunnerHandle::Get()->PostTask( 151 base::ThreadTaskRunnerHandle::Get()->PostTask(
70 FROM_HERE, base::Bind(callback, dbus::ObjectPath())); 152 FROM_HERE,
153 base::Bind(callback, dbus::ObjectPath(kAuthSessionObjectPath)));
71 } 154 }
72 155
73 void FakeBiodClient::RequestType(const BiometricTypeCallback& callback) { 156 void FakeBiodClient::RequestType(const BiometricTypeCallback& callback) {
74 base::ThreadTaskRunnerHandle::Get()->PostTask( 157 base::ThreadTaskRunnerHandle::Get()->PostTask(
75 FROM_HERE, 158 FROM_HERE,
76 base::Bind(callback, biod::BiometricType::BIOMETRIC_TYPE_UNKNOWN)); 159 base::Bind(callback, biod::BiometricType::BIOMETRIC_TYPE_FINGERPRINT));
77 } 160 }
78 161
79 void FakeBiodClient::CancelEnrollSession( 162 void FakeBiodClient::CancelEnrollSession(
80 const dbus::ObjectPath& /* enroll_session_path */, 163 const dbus::ObjectPath& enroll_session_path,
81 const VoidDBusMethodCallback& callback) { 164 const VoidDBusMethodCallback& callback) {
165 DCHECK_EQ(current_session_, FingerprintSession::ENROLL);
166 DCHECK_EQ(enroll_session_path.value(), kEnrollSessionObjectPath);
167
168 // Clean up the in progress enrollment.
169 current_record_.reset();
170 current_record_path_ = dbus::ObjectPath();
171 current_session_ = FingerprintSession::NONE;
172
82 base::ThreadTaskRunnerHandle::Get()->PostTask( 173 base::ThreadTaskRunnerHandle::Get()->PostTask(
83 FROM_HERE, base::Bind(callback, DBUS_METHOD_CALL_SUCCESS)); 174 FROM_HERE, base::Bind(callback, DBUS_METHOD_CALL_SUCCESS));
84 } 175 }
85 176
86 void FakeBiodClient::EndAuthSession( 177 void FakeBiodClient::EndAuthSession(const dbus::ObjectPath& auth_session_path,
87 const dbus::ObjectPath& /* auth_session_path */, 178 const VoidDBusMethodCallback& callback) {
88 const VoidDBusMethodCallback& callback) { 179 DCHECK_EQ(current_session_, FingerprintSession::AUTH);
180 DCHECK_EQ(auth_session_path.value(), kAuthSessionObjectPath);
181
182 current_session_ = FingerprintSession::NONE;
89 base::ThreadTaskRunnerHandle::Get()->PostTask( 183 base::ThreadTaskRunnerHandle::Get()->PostTask(
90 FROM_HERE, base::Bind(callback, DBUS_METHOD_CALL_SUCCESS)); 184 FROM_HERE, base::Bind(callback, DBUS_METHOD_CALL_SUCCESS));
91 } 185 }
92 186
93 void FakeBiodClient::SetRecordLabel(const dbus::ObjectPath& /* record_path */, 187 void FakeBiodClient::SetRecordLabel(const dbus::ObjectPath& record_path,
94 const std::string& /* label */, 188 const std::string& label,
95 const VoidDBusMethodCallback& callback) { 189 const VoidDBusMethodCallback& callback) {
190 if (records_.find(record_path) != records_.end())
191 records_[record_path]->label = label;
192
96 base::ThreadTaskRunnerHandle::Get()->PostTask( 193 base::ThreadTaskRunnerHandle::Get()->PostTask(
97 FROM_HERE, base::Bind(callback, DBUS_METHOD_CALL_SUCCESS)); 194 FROM_HERE, base::Bind(callback, DBUS_METHOD_CALL_SUCCESS));
98 } 195 }
99 196
100 void FakeBiodClient::RemoveRecord(const dbus::ObjectPath& /* record_path */, 197 void FakeBiodClient::RemoveRecord(const dbus::ObjectPath& record_path,
101 const VoidDBusMethodCallback& callback) { 198 const VoidDBusMethodCallback& callback) {
199 records_.erase(record_path);
200
102 base::ThreadTaskRunnerHandle::Get()->PostTask( 201 base::ThreadTaskRunnerHandle::Get()->PostTask(
103 FROM_HERE, base::Bind(callback, DBUS_METHOD_CALL_SUCCESS)); 202 FROM_HERE, base::Bind(callback, DBUS_METHOD_CALL_SUCCESS));
104 } 203 }
105 204
106 void FakeBiodClient::RequestRecordLabel( 205 void FakeBiodClient::RequestRecordLabel(const dbus::ObjectPath& record_path,
107 const dbus::ObjectPath& /* record_path */, 206 const LabelCallback& callback) {
108 const LabelCallback& callback) { 207 std::string record_label;
109 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, 208 if (records_.find(record_path) != records_.end())
110 base::Bind(callback, "")); 209 record_label = records_[record_path]->label;
210
211 base::ThreadTaskRunnerHandle::Get()->PostTask(
212 FROM_HERE, base::Bind(callback, record_label));
111 } 213 }
112 214
215 FakeBiodClient::FakeRecord::FakeRecord() {}
216
217 FakeBiodClient::FakeRecord::~FakeRecord() {}
218
113 } // namespace chromeos 219 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698