1 /*
2  * Copyright (C) 2021 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 package com.android.server.biometrics.sensors;
18 
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.content.Context;
22 import android.os.IBinder;
23 
24 import com.android.server.biometrics.log.BiometricContext;
25 import com.android.server.biometrics.log.BiometricLogger;
26 import com.android.server.biometrics.log.OperationContextExt;
27 
28 import java.util.function.Supplier;
29 
30 /**
31  * Abstract {@link BaseClientMonitor} implementation that supports HAL operations.
32  * @param <T> HAL template
33  */
34 public abstract class HalClientMonitor<T> extends BaseClientMonitor {
35 
36     @NonNull
37     protected final Supplier<T> mLazyDaemon;
38 
39     @NonNull
40     private final OperationContextExt mOperationContext;
41 
42     /**
43      * @param context    system_server context
44      * @param lazyDaemon pointer for lazy retrieval of the HAL
45      * @param token      a unique token for the client
46      * @param listener   recipient of related events (e.g. authentication)
47      * @param userId     target user id for operation
48      * @param owner      name of the client that owns this
49      * @param cookie     BiometricPrompt authentication cookie (to be moved into a subclass soon)
50      * @param sensorId   ID of the sensor that the operation should be requested of
51      * @param biometricLogger framework stats logger
52      * @param biometricContext system context metadata
53      */
HalClientMonitor(@onNull Context context, @NonNull Supplier<T> lazyDaemon, @Nullable IBinder token, @Nullable ClientMonitorCallbackConverter listener, int userId, @NonNull String owner, int cookie, int sensorId, @NonNull BiometricLogger biometricLogger, @NonNull BiometricContext biometricContext)54     public HalClientMonitor(@NonNull Context context, @NonNull Supplier<T> lazyDaemon,
55             @Nullable IBinder token, @Nullable ClientMonitorCallbackConverter listener, int userId,
56             @NonNull String owner, int cookie, int sensorId,
57             @NonNull BiometricLogger biometricLogger, @NonNull BiometricContext biometricContext) {
58         super(context, token, listener, userId, owner, cookie, sensorId,
59                 biometricLogger, biometricContext);
60         mLazyDaemon = lazyDaemon;
61         mOperationContext = new OperationContextExt(isBiometricPrompt());
62     }
63 
64     @Nullable
getFreshDaemon()65     public T getFreshDaemon() {
66         return mLazyDaemon.get();
67     }
68 
69     /**
70      * Starts the HAL operation specific to the ClientMonitor subclass.
71      */
startHalOperation()72     protected abstract void startHalOperation();
73 
74     /**
75      * Invoked if the scheduler is unable to start the ClientMonitor (for example the HAL is null).
76      * If such a problem is detected, the scheduler will not invoke
77      * {@link #start(ClientMonitorCallback)}.
78      */
unableToStart()79     public abstract void unableToStart();
80 
81     @Override
destroy()82     public void destroy() {
83         super.destroy();
84 
85         // subclasses should do this earlier in most cases, but ensure it happens now
86         unsubscribeBiometricContext();
87     }
88 
isBiometricPrompt()89     public boolean isBiometricPrompt() {
90         return getCookie() != 0;
91     }
92 
getOperationContext()93     protected OperationContextExt getOperationContext() {
94         return getBiometricContext().updateContext(mOperationContext, isCryptoOperation());
95     }
96 
getBiometricContextUnsubscriber()97     protected ClientMonitorCallback getBiometricContextUnsubscriber() {
98         return new ClientMonitorCallback() {
99             @Override
100             public void onClientFinished(@NonNull BaseClientMonitor monitor, boolean success) {
101                 unsubscribeBiometricContext();
102             }
103         };
104     }
105 
106     protected void unsubscribeBiometricContext() {
107         getBiometricContext().unsubscribe(mOperationContext);
108     }
109 }
110