OLD | NEW |
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 |
| 22 // Header of the path of an record. A unique number will be appended when an |
| 23 // record is created. |
| 24 const char kRecordObjectPathPrefix[] = "/Record/"; |
| 25 |
| 26 // Path of an auth session. There should only be one auth sesion at a given |
| 27 // time. |
| 28 const char kAuthSessionObjectPath[] = "/AuthSession/"; |
| 29 |
| 30 } // namespace |
| 31 |
| 32 // FakeRecord is the definition of a fake stored fingerprint template. |
| 33 struct FakeBiodClient::FakeRecord { |
| 34 std::string user_id; |
| 35 std::string label; |
| 36 // A fake fingerprint is a vector which consists of all the strings which |
| 37 // were "pressed" during the enroll session. |
| 38 std::vector<std::string> fake_fingerprint; |
| 39 }; |
| 40 |
15 FakeBiodClient::FakeBiodClient() {} | 41 FakeBiodClient::FakeBiodClient() {} |
16 | 42 |
17 FakeBiodClient::~FakeBiodClient() {} | 43 FakeBiodClient::~FakeBiodClient() {} |
18 | 44 |
19 void FakeBiodClient::SendEnrollScanDone(biod::ScanResult type_result, | 45 void FakeBiodClient::SendEnrollScanDone(const std::string& fingerprint, |
| 46 biod::ScanResult type_result, |
20 bool is_complete) { | 47 bool is_complete) { |
| 48 // Enroll scan signals do nothing if an enroll session is not happening. |
| 49 if (current_session_ != FingerprintSession::ENROLL) |
| 50 return; |
| 51 |
| 52 DCHECK(current_record_); |
| 53 // The fake fingerprint gets appended to the current fake fingerprints. |
| 54 current_record_->fake_fingerprint.push_back(fingerprint); |
| 55 |
| 56 // If the enroll is complete, save the record and exit enroll mode. |
| 57 if (is_complete) { |
| 58 records_[current_record_path_] = std::move(current_record_); |
| 59 current_record_path_ = dbus::ObjectPath(); |
| 60 current_record_.reset(); |
| 61 current_session_ = FingerprintSession::NONE; |
| 62 } |
| 63 |
21 for (auto& observer : observers_) | 64 for (auto& observer : observers_) |
22 observer.BiodEnrollScanDoneReceived(type_result, is_complete); | 65 observer.BiodEnrollScanDoneReceived(type_result, is_complete); |
23 } | 66 } |
24 | 67 |
25 void FakeBiodClient::SendAuthScanDone(biod::ScanResult type_result, | 68 void FakeBiodClient::SendAuthScanDone(const std::string& fingerprint, |
26 const AuthScanMatches& matches) { | 69 biod::ScanResult type_result) { |
| 70 // Auth scan signals do nothing if an auth session is not happening. |
| 71 if (current_session_ != FingerprintSession::AUTH) |
| 72 return; |
| 73 |
| 74 AuthScanMatches matches; |
| 75 // Iterate through all the records to check if fingerprint is a match and |
| 76 // populate |matches| accordingly. This searches through all the records and |
| 77 // then each record's fake fingerprint, but neither of these should ever have |
| 78 // more than five entries. |
| 79 for (const auto& entry : records_) { |
| 80 const std::unique_ptr<FakeRecord>& record = entry.second; |
| 81 if (std::find(record->fake_fingerprint.begin(), |
| 82 record->fake_fingerprint.end(), |
| 83 fingerprint) != record->fake_fingerprint.end()) { |
| 84 const std::string& user_id = record->user_id; |
| 85 matches[user_id].push_back(record->label); |
| 86 } |
| 87 } |
| 88 |
27 for (auto& observer : observers_) | 89 for (auto& observer : observers_) |
28 observer.BiodAuthScanDoneReceived(type_result, matches); | 90 observer.BiodAuthScanDoneReceived(type_result, matches); |
29 } | 91 } |
30 | 92 |
31 void FakeBiodClient::SendSessionFailed() { | 93 void FakeBiodClient::SendSessionFailed() { |
| 94 if (current_session_ == FingerprintSession::NONE) |
| 95 return; |
| 96 |
32 for (auto& observer : observers_) | 97 for (auto& observer : observers_) |
33 observer.BiodSessionFailedReceived(); | 98 observer.BiodSessionFailedReceived(); |
34 } | 99 } |
35 | 100 |
| 101 void FakeBiodClient::Reset() { |
| 102 records_.clear(); |
| 103 current_record_.reset(); |
| 104 current_record_path_ = dbus::ObjectPath(); |
| 105 current_session_ = FingerprintSession::NONE; |
| 106 } |
| 107 |
36 void FakeBiodClient::Init(dbus::Bus* bus) {} | 108 void FakeBiodClient::Init(dbus::Bus* bus) {} |
37 | 109 |
38 void FakeBiodClient::AddObserver(Observer* observer) { | 110 void FakeBiodClient::AddObserver(Observer* observer) { |
39 observers_.AddObserver(observer); | 111 observers_.AddObserver(observer); |
40 } | 112 } |
41 | 113 |
42 void FakeBiodClient::RemoveObserver(Observer* observer) { | 114 void FakeBiodClient::RemoveObserver(Observer* observer) { |
43 observers_.RemoveObserver(observer); | 115 observers_.RemoveObserver(observer); |
44 } | 116 } |
45 | 117 |
46 bool FakeBiodClient::HasObserver(const Observer* observer) const { | 118 bool FakeBiodClient::HasObserver(const Observer* observer) const { |
47 return observers_.HasObserver(observer); | 119 return observers_.HasObserver(observer); |
48 } | 120 } |
49 | 121 |
50 void FakeBiodClient::StartEnrollSession(const std::string& /* user_id */, | 122 void FakeBiodClient::StartEnrollSession(const std::string& user_id, |
51 const std::string& /* label */, | 123 const std::string& label, |
52 const ObjectPathCallback& callback) { | 124 const ObjectPathCallback& callback) { |
| 125 DCHECK_EQ(current_session_, FingerprintSession::NONE); |
| 126 |
| 127 // Create the enrollment with |user_id|, |label| and a empty fake fingerprint. |
| 128 current_record_path_ = dbus::ObjectPath( |
| 129 kRecordObjectPathPrefix + std::to_string(next_record_unique_id_++)); |
| 130 current_record_ = base::MakeUnique<FakeRecord>(); |
| 131 current_record_->user_id = user_id; |
| 132 current_record_->label = label; |
| 133 current_session_ = FingerprintSession::ENROLL; |
| 134 |
53 base::ThreadTaskRunnerHandle::Get()->PostTask( | 135 base::ThreadTaskRunnerHandle::Get()->PostTask( |
54 FROM_HERE, base::Bind(callback, dbus::ObjectPath())); | 136 FROM_HERE, |
| 137 base::Bind(callback, dbus::ObjectPath(kEnrollSessionObjectPath))); |
55 } | 138 } |
56 | 139 |
57 void FakeBiodClient::GetRecordsForUser(const std::string& /* user_id */, | 140 void FakeBiodClient::GetRecordsForUser(const std::string& user_id, |
58 const UserRecordsCallback& callback) { | 141 const UserRecordsCallback& callback) { |
| 142 std::vector<dbus::ObjectPath> records_object_paths; |
| 143 for (const auto& record : records_) { |
| 144 if (record.second->user_id == user_id) |
| 145 records_object_paths.push_back(record.first); |
| 146 } |
| 147 |
59 base::ThreadTaskRunnerHandle::Get()->PostTask( | 148 base::ThreadTaskRunnerHandle::Get()->PostTask( |
60 FROM_HERE, base::Bind(callback, std::vector<dbus::ObjectPath>())); | 149 FROM_HERE, base::Bind(callback, records_object_paths)); |
61 } | 150 } |
62 | 151 |
63 void FakeBiodClient::DestroyAllRecords(const VoidDBusMethodCallback& callback) { | 152 void FakeBiodClient::DestroyAllRecords(const VoidDBusMethodCallback& callback) { |
| 153 records_.clear(); |
| 154 |
64 base::ThreadTaskRunnerHandle::Get()->PostTask( | 155 base::ThreadTaskRunnerHandle::Get()->PostTask( |
65 FROM_HERE, base::Bind(callback, DBUS_METHOD_CALL_SUCCESS)); | 156 FROM_HERE, base::Bind(callback, DBUS_METHOD_CALL_SUCCESS)); |
66 } | 157 } |
67 | 158 |
68 void FakeBiodClient::StartAuthSession(const ObjectPathCallback& callback) { | 159 void FakeBiodClient::StartAuthSession(const ObjectPathCallback& callback) { |
| 160 DCHECK_EQ(current_session_, FingerprintSession::NONE); |
| 161 |
| 162 current_session_ = FingerprintSession::AUTH; |
69 base::ThreadTaskRunnerHandle::Get()->PostTask( | 163 base::ThreadTaskRunnerHandle::Get()->PostTask( |
70 FROM_HERE, base::Bind(callback, dbus::ObjectPath())); | 164 FROM_HERE, |
| 165 base::Bind(callback, dbus::ObjectPath(kAuthSessionObjectPath))); |
71 } | 166 } |
72 | 167 |
73 void FakeBiodClient::RequestType(const BiometricTypeCallback& callback) { | 168 void FakeBiodClient::RequestType(const BiometricTypeCallback& callback) { |
74 base::ThreadTaskRunnerHandle::Get()->PostTask( | 169 base::ThreadTaskRunnerHandle::Get()->PostTask( |
75 FROM_HERE, | 170 FROM_HERE, |
76 base::Bind(callback, biod::BiometricType::BIOMETRIC_TYPE_UNKNOWN)); | 171 base::Bind(callback, biod::BiometricType::BIOMETRIC_TYPE_FINGERPRINT)); |
77 } | 172 } |
78 | 173 |
79 void FakeBiodClient::CancelEnrollSession( | 174 void FakeBiodClient::CancelEnrollSession( |
80 const dbus::ObjectPath& /* enroll_session_path */, | 175 const dbus::ObjectPath& enroll_session_path, |
81 const VoidDBusMethodCallback& callback) { | 176 const VoidDBusMethodCallback& callback) { |
| 177 DCHECK_EQ(current_session_, FingerprintSession::ENROLL); |
| 178 DCHECK_EQ(enroll_session_path.value(), kEnrollSessionObjectPath); |
| 179 |
| 180 // Clean up the in progress enrollment. |
| 181 current_record_.reset(); |
| 182 current_record_path_ = dbus::ObjectPath(); |
| 183 current_session_ = FingerprintSession::NONE; |
| 184 |
82 base::ThreadTaskRunnerHandle::Get()->PostTask( | 185 base::ThreadTaskRunnerHandle::Get()->PostTask( |
83 FROM_HERE, base::Bind(callback, DBUS_METHOD_CALL_SUCCESS)); | 186 FROM_HERE, base::Bind(callback, DBUS_METHOD_CALL_SUCCESS)); |
84 } | 187 } |
85 | 188 |
86 void FakeBiodClient::EndAuthSession( | 189 void FakeBiodClient::EndAuthSession(const dbus::ObjectPath& auth_session_path, |
87 const dbus::ObjectPath& /* auth_session_path */, | 190 const VoidDBusMethodCallback& callback) { |
88 const VoidDBusMethodCallback& callback) { | 191 DCHECK_EQ(current_session_, FingerprintSession::AUTH); |
| 192 DCHECK_EQ(auth_session_path.value(), kAuthSessionObjectPath); |
| 193 |
| 194 current_session_ = FingerprintSession::NONE; |
89 base::ThreadTaskRunnerHandle::Get()->PostTask( | 195 base::ThreadTaskRunnerHandle::Get()->PostTask( |
90 FROM_HERE, base::Bind(callback, DBUS_METHOD_CALL_SUCCESS)); | 196 FROM_HERE, base::Bind(callback, DBUS_METHOD_CALL_SUCCESS)); |
91 } | 197 } |
92 | 198 |
93 void FakeBiodClient::SetRecordLabel(const dbus::ObjectPath& /* record_path */, | 199 void FakeBiodClient::SetRecordLabel(const dbus::ObjectPath& record_path, |
94 const std::string& /* label */, | 200 const std::string& label, |
95 const VoidDBusMethodCallback& callback) { | 201 const VoidDBusMethodCallback& callback) { |
| 202 if (records_.find(record_path) != records_.end()) |
| 203 records_[record_path]->label = label; |
| 204 |
96 base::ThreadTaskRunnerHandle::Get()->PostTask( | 205 base::ThreadTaskRunnerHandle::Get()->PostTask( |
97 FROM_HERE, base::Bind(callback, DBUS_METHOD_CALL_SUCCESS)); | 206 FROM_HERE, base::Bind(callback, DBUS_METHOD_CALL_SUCCESS)); |
98 } | 207 } |
99 | 208 |
100 void FakeBiodClient::RemoveRecord(const dbus::ObjectPath& /* record_path */, | 209 void FakeBiodClient::RemoveRecord(const dbus::ObjectPath& record_path, |
101 const VoidDBusMethodCallback& callback) { | 210 const VoidDBusMethodCallback& callback) { |
| 211 records_.erase(record_path); |
| 212 |
102 base::ThreadTaskRunnerHandle::Get()->PostTask( | 213 base::ThreadTaskRunnerHandle::Get()->PostTask( |
103 FROM_HERE, base::Bind(callback, DBUS_METHOD_CALL_SUCCESS)); | 214 FROM_HERE, base::Bind(callback, DBUS_METHOD_CALL_SUCCESS)); |
104 } | 215 } |
105 | 216 |
106 void FakeBiodClient::RequestRecordLabel( | 217 void FakeBiodClient::RequestRecordLabel(const dbus::ObjectPath& record_path, |
107 const dbus::ObjectPath& /* record_path */, | 218 const LabelCallback& callback) { |
108 const LabelCallback& callback) { | 219 std::string record_label; |
109 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 220 if (records_.find(record_path) != records_.end()) |
110 base::Bind(callback, "")); | 221 record_label = records_[record_path]->label; |
| 222 |
| 223 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 224 FROM_HERE, base::Bind(callback, record_label)); |
111 } | 225 } |
112 | 226 |
113 } // namespace chromeos | 227 } // namespace chromeos |
OLD | NEW |