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

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 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.
21 // Header of the path of an record. A unique number will be appended when an
22 // record is created.
23 const char kFakeRecordObjectPathHeader[] = "/fake/Record/";
Daniel Erat 2017/04/07 18:27:56 s/Header/Prefix/
sammiequon 2017/04/07 20:45:50 Done.
24 // Path of an auth session. There should only be one auth sesion at a given
25 // time.
26 const char kFakeAuthSessionObjectPath[] = "/fake/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(char 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
Daniel Erat 2017/04/07 18:27:55 DCHECK(current_record_)
sammiequon 2017/04/07 20:45:50 Done.
41 // The fake fingerprint gets appended to the current fake fingerprints.
42 current_record_->fake_fingerprint += fingerprint;
43
44 // If the enroll is complete, save the record and exit enroll mode.
45 if (is_complete) {
46 records_[*current_record_path_] = std::move(current_record_);
47 current_record_path_.reset();
48 current_record_.reset();
49 current_session_ = FingerprintSession::NONE;
50 }
51
21 for (auto& observer : observers_) 52 for (auto& observer : observers_)
22 observer.BiodEnrollScanDoneReceived(type_result, is_complete); 53 observer.BiodEnrollScanDoneReceived(type_result, is_complete);
23 } 54 }
24 55
25 void FakeBiodClient::SendAuthScanDone(biod::ScanResult type_result, 56 void FakeBiodClient::SendAuthScanDone(char fingerprint,
26 const AuthScanMatches& matches) { 57 biod::ScanResult type_result) {
58 // Auth scan signals do nothing if an auth session is not happening.
59 if (current_session_ != FingerprintSession::AUTH)
60 return;
61
62 AuthScanMatches matches;
63 // 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.
64 // populate |matches| accordingly. This searches through all the records and
65 // 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.
66 // more than five entries.
67 for (const auto& record : records_) {
68 if (record.second->fake_fingerprint.find(fingerprint) !=
69 std::string::npos) {
70 std::string user_id = record.second->user_id;
71 if (matches.find(user_id) == matches.end())
72 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.
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(current_session_ == FingerprintSession::NONE);
Daniel Erat 2017/04/07 18:27:56 DCHECK_EQ
sammiequon 2017/04/07 20:45:50 Done.
107
108 // Create the enrollment with |user_id|, |label| and a empty fake fingerprint.
109 current_record_path_ = base::MakeUnique<dbus::ObjectPath>(
110 kFakeRecordObjectPathHeader + 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_record_->fake_fingerprint = "";
115 current_session_ = FingerprintSession::ENROLL;
116
53 base::ThreadTaskRunnerHandle::Get()->PostTask( 117 base::ThreadTaskRunnerHandle::Get()->PostTask(
54 FROM_HERE, base::Bind(callback, dbus::ObjectPath())); 118 FROM_HERE,
119 base::Bind(callback, dbus::ObjectPath(kFakeEnrollSessionObjectPath)));
55 } 120 }
56 121
57 void FakeBiodClient::GetRecordsForUser(const std::string& /* user_id */, 122 void FakeBiodClient::GetRecordsForUser(const std::string& user_id,
58 const UserRecordsCallback& callback) { 123 const UserRecordsCallback& callback) {
124 std::vector<dbus::ObjectPath> records_object_paths;
125 for (const auto& record : records_) {
126 if (record.second->user_id == user_id)
127 records_object_paths.push_back(record.first);
128 }
129
59 base::ThreadTaskRunnerHandle::Get()->PostTask( 130 base::ThreadTaskRunnerHandle::Get()->PostTask(
60 FROM_HERE, base::Bind(callback, std::vector<dbus::ObjectPath>())); 131 FROM_HERE, base::Bind(callback, records_object_paths));
61 } 132 }
62 133
63 void FakeBiodClient::DestroyAllRecords() {} 134 void FakeBiodClient::DestroyAllRecords() {
135 records_.clear();
136 }
64 137
65 void FakeBiodClient::StartAuthSession(const ObjectPathCallback& callback) { 138 void FakeBiodClient::StartAuthSession(const ObjectPathCallback& callback) {
139 DCHECK(current_session_ == FingerprintSession::NONE);
Daniel Erat 2017/04/07 18:27:55 DCHECK_EQ
sammiequon 2017/04/07 20:45:50 Done.
140
141 current_session_ = FingerprintSession::AUTH;
66 base::ThreadTaskRunnerHandle::Get()->PostTask( 142 base::ThreadTaskRunnerHandle::Get()->PostTask(
67 FROM_HERE, base::Bind(callback, dbus::ObjectPath())); 143 FROM_HERE,
144 base::Bind(callback, dbus::ObjectPath(kFakeAuthSessionObjectPath)));
68 } 145 }
69 146
70 void FakeBiodClient::RequestType(const BiometricTypeCallback& callback) { 147 void FakeBiodClient::RequestType(const BiometricTypeCallback& callback) {
71 base::ThreadTaskRunnerHandle::Get()->PostTask( 148 base::ThreadTaskRunnerHandle::Get()->PostTask(
72 FROM_HERE, 149 FROM_HERE,
73 base::Bind(callback, biod::BiometricType::BIOMETRIC_TYPE_UNKNOWN)); 150 base::Bind(callback, biod::BiometricType::BIOMETRIC_TYPE_FINGERPRINT));
74 } 151 }
75 152
76 void FakeBiodClient::CancelEnrollSession( 153 void FakeBiodClient::CancelEnrollSession(
77 const dbus::ObjectPath& /* enroll_session_path */) {} 154 const dbus::ObjectPath& enroll_session_path) {
155 DCHECK(current_session_ == FingerprintSession::ENROLL);
Daniel Erat 2017/04/07 18:27:56 DCHECK_EQ
sammiequon 2017/04/07 20:45:50 Done.
156 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
157 return;
78 158
79 void FakeBiodClient::EndAuthSession( 159 // Clean up the in progress enrollment.
80 const dbus::ObjectPath& /* auth_session_path */) {} 160 current_record_.reset();
161 current_record_path_.reset();
162 current_session_ = FingerprintSession::NONE;
163 }
81 164
82 void FakeBiodClient::SetRecordLabel(const dbus::ObjectPath& /* record_path */, 165 void FakeBiodClient::EndAuthSession(const dbus::ObjectPath& auth_session_path) {
83 const std::string& /* label */) {} 166 DCHECK(current_session_ == FingerprintSession::AUTH);
Daniel Erat 2017/04/07 18:27:56 DCHECK_EQ
sammiequon 2017/04/07 20:45:50 Done.
167 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.
168 return;
84 169
85 void FakeBiodClient::RemoveRecord(const dbus::ObjectPath& /* record_path */) {} 170 current_session_ = FingerprintSession::NONE;
171 }
86 172
87 void FakeBiodClient::RequestRecordLabel( 173 void FakeBiodClient::SetRecordLabel(const dbus::ObjectPath& record_path,
88 const dbus::ObjectPath& /* record_path */, 174 const std::string& label) {
89 const LabelCallback& callback) { 175 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.
90 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, 176 records_[record_path]->label = label;
91 base::Bind(callback, "")); 177 }
178
179 void FakeBiodClient::RemoveRecord(const dbus::ObjectPath& record_path) {
180 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
181 if (it != records_.end())
182 records_.erase(it);
183 }
184
185 void FakeBiodClient::RequestRecordLabel(const dbus::ObjectPath& record_path,
186 const LabelCallback& callback) {
187 std::string record_label;
188 if (records_.find(record_path) != records_.end())
189 record_label = records_[record_path]->label;
190
191 base::ThreadTaskRunnerHandle::Get()->PostTask(
192 FROM_HERE, base::Bind(callback, record_label));
193 }
194
195 void FakeBiodClient::Reset() {
196 records_.clear();
197 current_record_.reset();
198 current_record_path_.reset();
199 current_session_ = FingerprintSession::NONE;
92 } 200 }
93 201
94 } // namespace chromeos 202 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698