From 51367f892d14b21a1625d62003b213b4d2e85481 Mon Sep 17 00:00:00 2001 From: Helen Qin Date: Thu, 12 Jan 2023 22:45:55 +0000 Subject: [PATCH] Log the Credential Manager api invocations. Test: presubmit Bug: 265333011 Change-Id: I86b00009688b3fe9d3bc3383f25373a650802461 --- .../credentials/ClearRequestSession.java | 12 ++- .../credentials/CreateRequestSession.java | 10 ++- .../credentials/CredentialManagerService.java | 6 ++ .../server/credentials/GetRequestSession.java | 7 +- .../server/credentials/RequestSession.java | 83 ++++++++++++++++--- 5 files changed, 99 insertions(+), 19 deletions(-) diff --git a/services/credentials/java/com/android/server/credentials/ClearRequestSession.java b/services/credentials/java/com/android/server/credentials/ClearRequestSession.java index 595d03d9845dc..be60946dc6550 100644 --- a/services/credentials/java/com/android/server/credentials/ClearRequestSession.java +++ b/services/credentials/java/com/android/server/credentials/ClearRequestSession.java @@ -39,15 +39,17 @@ public final class ClearRequestSession extends RequestSession { 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 { 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); } /** @@ -63,7 +64,7 @@ public final class CreateRequestSession extends RequestSession { 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 implements CredentialManagerUi.CredentialManagerUiCallback{ +abstract class RequestSession 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 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 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.