1 /*
2 * Copyright (C) 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <android-base/logging.h>
18
19 #include "Session.h"
20
21 namespace aidl::android::hardware::biometrics::face {
22
23 constexpr size_t MAX_WORKER_QUEUE_SIZE = 5;
24
Session(std::unique_ptr<FakeFaceEngine> engine,std::shared_ptr<ISessionCallback> cb)25 Session::Session(std::unique_ptr<FakeFaceEngine> engine, std::shared_ptr<ISessionCallback> cb)
26 : mEngine(std::move(engine)), mCb(std::move(cb)), mRandom(std::mt19937::default_seed) {
27 mThread = std::make_unique<WorkerThread>(MAX_WORKER_QUEUE_SIZE);
28 }
29
generateChallenge()30 ndk::ScopedAStatus Session::generateChallenge() {
31 LOG(INFO) << "generateChallenge";
32 mThread->schedule(Callable::from([this] { mEngine->generateChallengeImpl(mCb.get()); }));
33 return ndk::ScopedAStatus::ok();
34 }
35
revokeChallenge(int64_t challenge)36 ndk::ScopedAStatus Session::revokeChallenge(int64_t challenge) {
37 LOG(INFO) << "revokeChallenge";
38 mThread->schedule(Callable::from(
39 [this, challenge] { mEngine->revokeChallengeImpl(mCb.get(), challenge); }));
40 return ndk::ScopedAStatus::ok();
41 }
42
getEnrollmentConfig(EnrollmentType,std::vector<EnrollmentStageConfig> * cancellationSignal)43 ndk::ScopedAStatus Session::getEnrollmentConfig(
44 EnrollmentType /*enrollmentType*/, std::vector<EnrollmentStageConfig>* cancellationSignal) {
45 *cancellationSignal = {};
46 return ndk::ScopedAStatus::ok();
47 }
48
enroll(const keymaster::HardwareAuthToken & hat,EnrollmentType enrollmentType,const std::vector<Feature> & features,const std::optional<NativeHandle> &,std::shared_ptr<biometrics::common::ICancellationSignal> * cancellationSignal)49 ndk::ScopedAStatus Session::enroll(
50 const keymaster::HardwareAuthToken& hat, EnrollmentType enrollmentType,
51 const std::vector<Feature>& features, const std::optional<NativeHandle>& /*previewSurface*/,
52 std::shared_ptr<biometrics::common::ICancellationSignal>* cancellationSignal) {
53 LOG(INFO) << "enroll";
54 std::promise<void> cancellationPromise;
55 auto cancFuture = cancellationPromise.get_future();
56
57 mThread->schedule(Callable::from(
58 [this, hat, enrollmentType, features, cancFuture = std::move(cancFuture)] {
59 mEngine->enrollImpl(mCb.get(), hat, enrollmentType, features, cancFuture);
60 }));
61
62 *cancellationSignal = SharedRefBase::make<CancellationSignal>(std::move(cancellationPromise));
63 return ndk::ScopedAStatus::ok();
64 }
65
authenticate(int64_t keystoreOperationId,std::shared_ptr<common::ICancellationSignal> * cancellationSignal)66 ndk::ScopedAStatus Session::authenticate(
67 int64_t keystoreOperationId,
68 std::shared_ptr<common::ICancellationSignal>* cancellationSignal) {
69 LOG(INFO) << "authenticate";
70 std::promise<void> cancellationPromise;
71 auto cancFuture = cancellationPromise.get_future();
72
73 mThread->schedule(
74 Callable::from([this, keystoreOperationId, cancFuture = std::move(cancFuture)] {
75 mEngine->authenticateImpl(mCb.get(), keystoreOperationId, cancFuture);
76 }));
77
78 *cancellationSignal = SharedRefBase::make<CancellationSignal>(std::move(cancellationPromise));
79 return ndk::ScopedAStatus::ok();
80 }
81
detectInteraction(std::shared_ptr<common::ICancellationSignal> * cancellationSignal)82 ndk::ScopedAStatus Session::detectInteraction(
83 std::shared_ptr<common::ICancellationSignal>* cancellationSignal) {
84 LOG(INFO) << "detectInteraction";
85 std::promise<void> cancellationPromise;
86 auto cancFuture = cancellationPromise.get_future();
87
88 mThread->schedule(Callable::from([this, cancFuture = std::move(cancFuture)] {
89 mEngine->detectInteractionImpl(mCb.get(), cancFuture);
90 }));
91
92 *cancellationSignal = SharedRefBase::make<CancellationSignal>(std::move(cancellationPromise));
93 return ndk::ScopedAStatus::ok();
94 }
95
enumerateEnrollments()96 ndk::ScopedAStatus Session::enumerateEnrollments() {
97 LOG(INFO) << "enumerateEnrollments";
98 mThread->schedule(Callable::from([this] { mEngine->enumerateEnrollmentsImpl(mCb.get()); }));
99 return ndk::ScopedAStatus::ok();
100 }
101
removeEnrollments(const std::vector<int32_t> & enrollmentIds)102 ndk::ScopedAStatus Session::removeEnrollments(const std::vector<int32_t>& enrollmentIds) {
103 LOG(INFO) << "removeEnrollments";
104 mThread->schedule(Callable::from(
105 [this, enrollmentIds] { mEngine->removeEnrollmentsImpl(mCb.get(), enrollmentIds); }));
106 return ndk::ScopedAStatus::ok();
107 }
108
getFeatures()109 ndk::ScopedAStatus Session::getFeatures() {
110 LOG(INFO) << "getFeatures";
111 mThread->schedule(Callable::from([this] { mEngine->getFeaturesImpl(mCb.get()); }));
112 return ndk::ScopedAStatus::ok();
113 }
114
setFeature(const keymaster::HardwareAuthToken & hat,Feature feature,bool enabled)115 ndk::ScopedAStatus Session::setFeature(const keymaster::HardwareAuthToken& hat, Feature feature,
116 bool enabled) {
117 LOG(INFO) << "setFeature";
118 mThread->schedule(Callable::from([this, hat, feature, enabled] {
119 mEngine->setFeatureImpl(mCb.get(), hat, feature, enabled);
120 }));
121 return ndk::ScopedAStatus::ok();
122 }
123
getAuthenticatorId()124 ndk::ScopedAStatus Session::getAuthenticatorId() {
125 LOG(INFO) << "getAuthenticatorId";
126 mThread->schedule(Callable::from([this] { mEngine->getAuthenticatorIdImpl(mCb.get()); }));
127 return ndk::ScopedAStatus::ok();
128 }
129
invalidateAuthenticatorId()130 ndk::ScopedAStatus Session::invalidateAuthenticatorId() {
131 LOG(INFO) << "invalidateAuthenticatorId";
132 mThread->schedule(
133 Callable::from([this] { mEngine->invalidateAuthenticatorIdImpl(mCb.get()); }));
134 return ndk::ScopedAStatus::ok();
135 }
136
resetLockout(const keymaster::HardwareAuthToken & hat)137 ndk::ScopedAStatus Session::resetLockout(const keymaster::HardwareAuthToken& hat) {
138 LOG(INFO) << "resetLockout";
139 mThread->schedule(Callable::from([this, hat] { mEngine->resetLockoutImpl(mCb.get(), hat); }));
140 return ndk::ScopedAStatus::ok();
141 }
142
close()143 ndk::ScopedAStatus Session::close() {
144 if (mCb) {
145 mCb->onSessionClosed();
146 }
147 return ndk::ScopedAStatus::ok();
148 }
149
authenticateWithContext(int64_t operationId,const common::OperationContext &,std::shared_ptr<common::ICancellationSignal> * out)150 ndk::ScopedAStatus Session::authenticateWithContext(
151 int64_t operationId, const common::OperationContext& /*context*/,
152 std::shared_ptr<common::ICancellationSignal>* out) {
153 return authenticate(operationId, out);
154 }
155
enrollWithContext(const keymaster::HardwareAuthToken & hat,EnrollmentType enrollmentType,const std::vector<Feature> & features,const std::optional<NativeHandle> & previewSurface,const common::OperationContext &,std::shared_ptr<common::ICancellationSignal> * out)156 ndk::ScopedAStatus Session::enrollWithContext(const keymaster::HardwareAuthToken& hat,
157 EnrollmentType enrollmentType,
158 const std::vector<Feature>& features,
159 const std::optional<NativeHandle>& previewSurface,
160 const common::OperationContext& /*context*/,
161 std::shared_ptr<common::ICancellationSignal>* out) {
162 return enroll(hat, enrollmentType, features, previewSurface, out);
163 }
164
detectInteractionWithContext(const common::OperationContext &,std::shared_ptr<common::ICancellationSignal> * out)165 ndk::ScopedAStatus Session::detectInteractionWithContext(
166 const common::OperationContext& /*context*/,
167 std::shared_ptr<common::ICancellationSignal>* out) {
168 return detectInteraction(out);
169 }
170
onContextChanged(const common::OperationContext &)171 ndk::ScopedAStatus Session::onContextChanged(const common::OperationContext& /*context*/) {
172 return ndk::ScopedAStatus::ok();
173 }
174
175 } // namespace aidl::android::hardware::biometrics::face
176