Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 51367f89 authored by Helen Qin's avatar Helen Qin
Browse files

Log the Credential Manager api invocations.

Test: presubmit
Bug: 265333011
Change-Id: I86b00009688b3fe9d3bc3383f25373a650802461
parent 51d602dc
Loading
Loading
Loading
Loading
+9 −3
Original line number Diff line number Diff line
@@ -39,15 +39,17 @@ public final class ClearRequestSession extends RequestSession<ClearCredentialSta
        implements ProviderSession.ProviderInternalCallback<Void> {
    private static final String TAG = "GetRequestSession";

    public ClearRequestSession(Context context, int userId,
    public ClearRequestSession(Context context, int userId, int callingUid,
            IClearCredentialStateCallback callback, ClearCredentialStateRequest request,
            CallingAppInfo callingAppInfo) {
        super(context, userId, request, callback, RequestInfo.TYPE_UNDEFINED, callingAppInfo);
        super(context, userId, callingUid, request, callback, RequestInfo.TYPE_UNDEFINED,
                callingAppInfo);
    }

    /**
     * Creates a new provider session, and adds it list of providers that are contributing to
     * this session.
     *
     * @return the provider session created within this request session, for the given provider
     * info.
     */
@@ -111,8 +113,10 @@ public final class ClearRequestSession extends RequestSession<ClearCredentialSta
        Log.i(TAG, "respondToClientWithResponseAndFinish");
        try {
            mClientCallback.onSuccess();
            logApiCalled(RequestType.CLEAR_CREDENTIALS, /* isSuccessful */ true);
        } catch (RemoteException e) {
            Log.i(TAG, "Issue while propagating the response to the client");
            logApiCalled(RequestType.CLEAR_CREDENTIALS, /* isSuccessful */ false);
        }
        finishSession();
    }
@@ -124,8 +128,10 @@ public final class ClearRequestSession extends RequestSession<ClearCredentialSta
        } catch (RemoteException e) {
            e.printStackTrace();
        }
        logApiCalled(RequestType.CLEAR_CREDENTIALS, /* isSuccessful */ false);
        finishSession();
    }

    private void processResponses() {
        for (ProviderSession session : mProviders.values()) {
            if (session.isProviderResponseSet()) {
+7 −3
Original line number Diff line number Diff line
@@ -44,11 +44,12 @@ public final class CreateRequestSession extends RequestSession<CreateCredentialR
        implements ProviderSession.ProviderInternalCallback<CreateCredentialResponse> {
    private static final String TAG = "CreateRequestSession";

    CreateRequestSession(@NonNull Context context, int userId,
    CreateRequestSession(@NonNull Context context, int userId, int callingUid,
            CreateCredentialRequest request,
            ICreateCredentialCallback callback,
            CallingAppInfo callingAppInfo) {
        super(context, userId, request, callback, RequestInfo.TYPE_CREATE, callingAppInfo);
        super(context, userId, callingUid, request, callback, RequestInfo.TYPE_CREATE,
                callingAppInfo);
    }

    /**
@@ -115,8 +116,10 @@ public final class CreateRequestSession extends RequestSession<CreateCredentialR
        Log.i(TAG, "respondToClientWithResponseAndFinish");
        try {
            mClientCallback.onResponse(response);
            logApiCalled(RequestType.CREATE_CREDENTIALS, /* isSuccessful */ true);
        } catch (RemoteException e) {
            Log.i(TAG, "Issue while responding to client: " + e.getMessage());
            logApiCalled(RequestType.CREATE_CREDENTIALS, /* isSuccessful */ false);
        }
        finishSession();
    }
@@ -128,6 +131,7 @@ public final class CreateRequestSession extends RequestSession<CreateCredentialR
        } catch (RemoteException e) {
            Log.i(TAG, "Issue while responding to client: " + e.getMessage());
        }
        logApiCalled(RequestType.CREATE_CREDENTIALS, /* isSuccessful */ false);
        finishSession();
    }

+6 −0
Original line number Diff line number Diff line
@@ -269,11 +269,13 @@ public final class CredentialManagerService
            ICancellationSignal cancelTransport = CancellationSignal.createTransport();

            int userId = UserHandle.getCallingUserId();
            int callingUid = Binder.getCallingUid();
            // New request session, scoped for this request only.
            final GetRequestSession session =
                    new GetRequestSession(
                            getContext(),
                            userId,
                            callingUid,
                            callback,
                            request,
                            constructCallingAppInfo(callingPackage, userId));
@@ -319,10 +321,12 @@ public final class CredentialManagerService

            // New request session, scoped for this request only.
            int userId = UserHandle.getCallingUserId();
            int callingUid = Binder.getCallingUid();
            final CreateRequestSession session =
                    new CreateRequestSession(
                            getContext(),
                            userId,
                            callingUid,
                            request,
                            callback,
                            constructCallingAppInfo(callingPackage, userId));
@@ -434,10 +438,12 @@ public final class CredentialManagerService

            // New request session, scoped for this request only.
            int userId = UserHandle.getCallingUserId();
            int callingUid = Binder.getCallingUid();
            final ClearRequestSession session =
                    new ClearRequestSession(
                            getContext(),
                            userId,
                            callingUid,
                            callback,
                            request,
                            constructCallingAppInfo(callingPackage, userId));
+5 −2
Original line number Diff line number Diff line
@@ -41,10 +41,10 @@ public final class GetRequestSession extends RequestSession<GetCredentialRequest
        implements ProviderSession.ProviderInternalCallback<GetCredentialResponse> {
    private static final String TAG = "GetRequestSession";

    public GetRequestSession(Context context, int userId,
    public GetRequestSession(Context context, int userId, int callingUid,
            IGetCredentialCallback callback, GetCredentialRequest request,
            CallingAppInfo callingAppInfo) {
        super(context, userId, request, callback, RequestInfo.TYPE_GET, callingAppInfo);
        super(context, userId, callingUid, request, callback, RequestInfo.TYPE_GET, callingAppInfo);
    }

    /**
@@ -104,8 +104,10 @@ public final class GetRequestSession extends RequestSession<GetCredentialRequest
    private void respondToClientWithResponseAndFinish(GetCredentialResponse response) {
        try {
            mClientCallback.onResponse(response);
            logApiCalled(RequestType.GET_CREDENTIALS, /* isSuccessful */ true);
        } catch (RemoteException e) {
            Log.i(TAG, "Issue while responding to client with a response : " + e.getMessage());
            logApiCalled(RequestType.GET_CREDENTIALS, /* isSuccessful */ false);
        }
        finishSession();
    }
@@ -117,6 +119,7 @@ public final class GetRequestSession extends RequestSession<GetCredentialRequest
            Log.i(TAG, "Issue while responding to client with error : " + e.getMessage());

        }
        logApiCalled(RequestType.GET_CREDENTIALS, /* isSuccessful */ false);
        finishSession();
    }

+72 −11
Original line number Diff line number Diff line
@@ -16,6 +16,13 @@

package com.android.server.credentials;

import static com.android.internal.util.FrameworkStatsLog.CREDENTIAL_MANAGER_API_CALLED__API_NAME__API_NAME_CLEAR_CREDENTIAL;
import static com.android.internal.util.FrameworkStatsLog.CREDENTIAL_MANAGER_API_CALLED__API_NAME__API_NAME_CREATE_CREDENTIAL;
import static com.android.internal.util.FrameworkStatsLog.CREDENTIAL_MANAGER_API_CALLED__API_NAME__API_NAME_GET_CREDENTIAL;
import static com.android.internal.util.FrameworkStatsLog.CREDENTIAL_MANAGER_API_CALLED__API_NAME__API_NAME_UNKNOWN;
import static com.android.internal.util.FrameworkStatsLog.CREDENTIAL_MANAGER_API_CALLED__API_STATUS__API_STATUS_FAILURE;
import static com.android.internal.util.FrameworkStatsLog.CREDENTIAL_MANAGER_API_CALLED__API_STATUS__API_STATUS_SUCCESS;

import android.annotation.NonNull;
import android.annotation.UserIdInt;
import android.content.Context;
@@ -29,6 +36,8 @@ import android.service.credentials.CallingAppInfo;
import android.service.credentials.CredentialProviderInfo;
import android.util.Log;

import com.android.internal.util.FrameworkStatsLog;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
@@ -40,25 +49,50 @@ import java.util.Map;
abstract class RequestSession<T, U> implements CredentialManagerUi.CredentialManagerUiCallback {
    private static final String TAG = "RequestSession";

    // Metrics constants
    private static final int METRICS_API_NAME_UNKNOWN =
            CREDENTIAL_MANAGER_API_CALLED__API_NAME__API_NAME_UNKNOWN;
    private static final int METRICS_API_NAME_GET_CREDENTIAL =
            CREDENTIAL_MANAGER_API_CALLED__API_NAME__API_NAME_GET_CREDENTIAL;
    private static final int METRICS_API_NAME_CREATE_CREDENTIAL =
            CREDENTIAL_MANAGER_API_CALLED__API_NAME__API_NAME_CREATE_CREDENTIAL;
    private static final int METRICS_API_NAME_CLEAR_CREDENTIAL =
            CREDENTIAL_MANAGER_API_CALLED__API_NAME__API_NAME_CLEAR_CREDENTIAL;
    private static final int METRICS_API_STATUS_SUCCESS =
            CREDENTIAL_MANAGER_API_CALLED__API_STATUS__API_STATUS_SUCCESS;
    private static final int METRICS_API_STATUS_FAILURE =
            CREDENTIAL_MANAGER_API_CALLED__API_STATUS__API_STATUS_FAILURE;

    // TODO: Revise access levels of attributes
    @NonNull protected final T mClientRequest;
    @NonNull protected final U mClientCallback;
    @NonNull protected final IBinder mRequestId;
    @NonNull protected final Context mContext;
    @NonNull protected final CredentialManagerUi mCredentialManagerUi;
    @NonNull protected final String mRequestType;
    @NonNull protected final Handler mHandler;
    @UserIdInt protected final int mUserId;
    @NonNull protected final CallingAppInfo mClientAppInfo;
    @NonNull
    protected final T mClientRequest;
    @NonNull
    protected final U mClientCallback;
    @NonNull
    protected final IBinder mRequestId;
    @NonNull
    protected final Context mContext;
    @NonNull
    protected final CredentialManagerUi mCredentialManagerUi;
    @NonNull
    protected final String mRequestType;
    @NonNull
    protected final Handler mHandler;
    @UserIdInt
    protected final int mUserId;
    private final int mCallingUid;
    @NonNull
    protected final CallingAppInfo mClientAppInfo;

    protected final Map<String, ProviderSession> mProviders = new HashMap<>();

    protected RequestSession(@NonNull Context context,
            @UserIdInt int userId, @NonNull T clientRequest, U clientCallback,
            @UserIdInt int userId, int callingUid, @NonNull T clientRequest, U clientCallback,
            @NonNull String requestType,
            CallingAppInfo callingAppInfo) {
        mContext = context;
        mUserId = userId;
        mCallingUid = callingUid;
        mClientRequest = clientRequest;
        mClientCallback = clientCallback;
        mRequestType = requestType;
@@ -117,6 +151,33 @@ abstract class RequestSession<T, U> implements CredentialManagerUi.CredentialMan
        return false;
    }

    // TODO: move these definitions to a separate logging focused class.
    enum RequestType {
        GET_CREDENTIALS,
        CREATE_CREDENTIALS,
        CLEAR_CREDENTIALS,
    }

    private static int getApiNameFromRequestType(RequestType requestType) {
        switch (requestType) {
            case GET_CREDENTIALS:
                return METRICS_API_NAME_GET_CREDENTIAL;
            case CREATE_CREDENTIALS:
                return METRICS_API_NAME_CREATE_CREDENTIAL;
            case CLEAR_CREDENTIALS:
                return METRICS_API_NAME_CLEAR_CREDENTIAL;
            default:
                return METRICS_API_NAME_UNKNOWN;
        }
    }

    protected void logApiCalled(RequestType requestType, boolean isSuccessful) {
        FrameworkStatsLog.write(FrameworkStatsLog.CREDENTIAL_MANAGER_API_CALLED,
                /* api_name */getApiNameFromRequestType(requestType), /* caller_uid */
                mCallingUid, /* api_status */
                isSuccessful ? METRICS_API_STATUS_SUCCESS : METRICS_API_STATUS_FAILURE);
    }

    /**
     * Returns true if at least one provider is ready for UI invocation, and no
     * provider is pending a response.