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

Commit 011540ad authored by Arpan Kaphle's avatar Arpan Kaphle
Browse files

Adds a Quickly Designed Logging Setup

A new object is generated that mimics the proto object, but allows us to
hold state better (CandidateProviderMetrics and ChosenProviderMetrics).

Then within our calls, at designated locations in the query and final
phase, we set these objects to have certain values. This occurs in base
classes, such as ProviderSession, or in specific parts of RequestSession
where it makes most sense.

Finally, in all existing logging methods, a way to retrieve the data is
used. To avoid issues with slow or expired requests coming back, the
iteration is protected so that the log occurs for what is obtained at
the moment (synchronized...() methods don't prevent iteration issues).

Bug: 267669240
Bug: 253157163
Test: Builds for now
Change-Id: Ia51c0fbf6bd4954721c15776bb3a2cfee508c06e
parent 97dec9ac
Loading
Loading
Loading
Loading
+8 −1
Original line number Original line Diff line number Diff line
@@ -83,7 +83,10 @@ public final class ClearRequestSession extends RequestSession<ClearCredentialSta
    }
    }


    @Override
    @Override
    public void onFinalResponseReceived(ComponentName componentName, Void response) {
    public void onFinalResponseReceived(
            ComponentName componentName,
            Void response) {
        setChosenMetric(componentName);
        respondToClientWithResponseAndFinish();
        respondToClientWithResponseAndFinish();
    }
    }


@@ -114,6 +117,8 @@ public final class ClearRequestSession extends RequestSession<ClearCredentialSta
        Log.i(TAG, "respondToClientWithResponseAndFinish");
        Log.i(TAG, "respondToClientWithResponseAndFinish");
        if (isSessionCancelled()) {
        if (isSessionCancelled()) {
            // TODO: Differentiate btw cancelled and false
            // TODO: Differentiate btw cancelled and false
            mChosenProviderMetric.setChosenProviderStatus(
                    MetricUtilities.METRICS_PROVIDER_STATUS_FINAL_SUCCESS);
            logApiCalled(RequestType.CLEAR_CREDENTIALS, /* isSuccessful */ true);
            logApiCalled(RequestType.CLEAR_CREDENTIALS, /* isSuccessful */ true);
            finishSession(/*propagateCancellation=*/true);
            finishSession(/*propagateCancellation=*/true);
            return;
            return;
@@ -122,6 +127,8 @@ public final class ClearRequestSession extends RequestSession<ClearCredentialSta
            mClientCallback.onSuccess();
            mClientCallback.onSuccess();
            logApiCalled(RequestType.CLEAR_CREDENTIALS, /* isSuccessful */ true);
            logApiCalled(RequestType.CLEAR_CREDENTIALS, /* isSuccessful */ true);
        } catch (RemoteException e) {
        } catch (RemoteException e) {
            mChosenProviderMetric.setChosenProviderStatus(
                    MetricUtilities.METRICS_PROVIDER_STATUS_FINAL_FAILURE);
            Log.i(TAG, "Issue while propagating the response to the client");
            Log.i(TAG, "Issue while propagating the response to the client");
            logApiCalled(RequestType.CLEAR_CREDENTIALS, /* isSuccessful */ false);
            logApiCalled(RequestType.CLEAR_CREDENTIALS, /* isSuccessful */ false);
        }
        }
+8 −0
Original line number Original line Diff line number Diff line
@@ -16,6 +16,9 @@


package com.android.server.credentials;
package com.android.server.credentials;


import static com.android.server.credentials.MetricUtilities.METRICS_PROVIDER_STATUS_FINAL_FAILURE;
import static com.android.server.credentials.MetricUtilities.METRICS_PROVIDER_STATUS_FINAL_SUCCESS;

import android.annotation.NonNull;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.Nullable;
import android.content.ComponentName;
import android.content.ComponentName;
@@ -94,9 +97,14 @@ public final class CreateRequestSession extends RequestSession<CreateCredentialR
    public void onFinalResponseReceived(ComponentName componentName,
    public void onFinalResponseReceived(ComponentName componentName,
            @Nullable CreateCredentialResponse response) {
            @Nullable CreateCredentialResponse response) {
        Log.i(TAG, "onFinalCredentialReceived from: " + componentName.flattenToString());
        Log.i(TAG, "onFinalCredentialReceived from: " + componentName.flattenToString());
        setChosenMetric(componentName);
        if (response != null) {
        if (response != null) {
            mChosenProviderMetric.setChosenProviderStatus(
                    METRICS_PROVIDER_STATUS_FINAL_SUCCESS);
            respondToClientWithResponseAndFinish(response);
            respondToClientWithResponseAndFinish(response);
        } else {
        } else {
            mChosenProviderMetric.setChosenProviderStatus(
                    METRICS_PROVIDER_STATUS_FINAL_FAILURE);
            respondToClientWithErrorAndFinish(CreateCredentialException.TYPE_NO_CREATE_OPTIONS,
            respondToClientWithErrorAndFinish(CreateCredentialException.TYPE_NO_CREATE_OPTIONS,
                    "Invalid response");
                    "Invalid response");
        }
        }
+1 −1
Original line number Original line Diff line number Diff line
@@ -357,7 +357,6 @@ public final class CredentialManagerService
            final int userId = UserHandle.getCallingUserId();
            final int userId = UserHandle.getCallingUserId();
            final int callingUid = Binder.getCallingUid();
            final int callingUid = Binder.getCallingUid();
            enforceCallingPackage(callingPackage, callingUid);
            enforceCallingPackage(callingPackage, callingUid);

            // New request session, scoped for this request only.
            // New request session, scoped for this request only.
            final GetRequestSession session =
            final GetRequestSession session =
                    new GetRequestSession(
                    new GetRequestSession(
@@ -497,6 +496,7 @@ public final class CredentialManagerService
            Log.i(TAG, "starting executeCreateCredential with callingPackage: "
            Log.i(TAG, "starting executeCreateCredential with callingPackage: "
                    + callingPackage);
                    + callingPackage);
            ICancellationSignal cancelTransport = CancellationSignal.createTransport();
            ICancellationSignal cancelTransport = CancellationSignal.createTransport();

            final int userId = UserHandle.getCallingUserId();
            final int userId = UserHandle.getCallingUserId();
            final int callingUid = Binder.getCallingUid();
            final int callingUid = Binder.getCallingUid();
            enforceCallingPackage(callingPackage, callingUid);
            enforceCallingPackage(callingPackage, callingUid);
+8 −0
Original line number Original line Diff line number Diff line
@@ -16,6 +16,9 @@


package com.android.server.credentials;
package com.android.server.credentials;


import static com.android.server.credentials.MetricUtilities.METRICS_PROVIDER_STATUS_FINAL_FAILURE;
import static com.android.server.credentials.MetricUtilities.METRICS_PROVIDER_STATUS_FINAL_SUCCESS;

import android.annotation.Nullable;
import android.annotation.Nullable;
import android.content.ComponentName;
import android.content.ComponentName;
import android.content.Context;
import android.content.Context;
@@ -87,9 +90,14 @@ public final class GetRequestSession extends RequestSession<GetCredentialRequest
    public void onFinalResponseReceived(ComponentName componentName,
    public void onFinalResponseReceived(ComponentName componentName,
            @Nullable GetCredentialResponse response) {
            @Nullable GetCredentialResponse response) {
        Log.i(TAG, "onFinalCredentialReceived from: " + componentName.flattenToString());
        Log.i(TAG, "onFinalCredentialReceived from: " + componentName.flattenToString());
        setChosenMetric(componentName);
        if (response != null) {
        if (response != null) {
            mChosenProviderMetric.setChosenProviderStatus(
                    METRICS_PROVIDER_STATUS_FINAL_SUCCESS);
            respondToClientWithResponseAndFinish(response);
            respondToClientWithResponseAndFinish(response);
        } else {
        } else {
            mChosenProviderMetric.setChosenProviderStatus(
                    METRICS_PROVIDER_STATUS_FINAL_FAILURE);
            respondToClientWithErrorAndFinish(GetCredentialException.TYPE_NO_CREDENTIAL,
            respondToClientWithErrorAndFinish(GetCredentialException.TYPE_NO_CREDENTIAL,
                    "Invalid response from provider");
                    "Invalid response from provider");
        }
        }
+95 −0
Original line number Original line Diff line number Diff line
/*
 * Copyright (C) 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

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_CLIENT_CANCELED;
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 static com.android.internal.util.FrameworkStatsLog.CREDENTIAL_MANAGER_API_CALLED__API_STATUS__API_STATUS_USER_CANCELED;
import static com.android.internal.util.FrameworkStatsLog.CREDENTIAL_MANAGER_API_CALLED__CANDIDATE_PROVIDER_STATUS__PROVIDER_FINAL_FAILURE;
import static com.android.internal.util.FrameworkStatsLog.CREDENTIAL_MANAGER_API_CALLED__CANDIDATE_PROVIDER_STATUS__PROVIDER_FINAL_SUCCESS;
import static com.android.internal.util.FrameworkStatsLog.CREDENTIAL_MANAGER_API_CALLED__CANDIDATE_PROVIDER_STATUS__PROVIDER_QUERY_FAILURE;
import static com.android.internal.util.FrameworkStatsLog.CREDENTIAL_MANAGER_API_CALLED__CANDIDATE_PROVIDER_STATUS__PROVIDER_QUERY_SUCCESS;
import static com.android.internal.util.FrameworkStatsLog.CREDENTIAL_MANAGER_API_CALLED__CANDIDATE_PROVIDER_STATUS__PROVIDER_UNKNOWN;

import android.content.ComponentName;
import android.content.Context;
import android.content.pm.PackageManager;
import android.util.Log;

/**
 * For all future metric additions, this will contain their names for local usage after importing
 * from {@link com.android.internal.util.FrameworkStatsLog}.
 */
public class MetricUtilities {

    private static final String TAG = "MetricUtilities";

    // Metrics constants
    protected static final int METRICS_API_NAME_UNKNOWN =
            CREDENTIAL_MANAGER_API_CALLED__API_NAME__API_NAME_UNKNOWN;
    protected static final int METRICS_API_NAME_GET_CREDENTIAL =
            CREDENTIAL_MANAGER_API_CALLED__API_NAME__API_NAME_GET_CREDENTIAL;
    protected static final int METRICS_API_NAME_CREATE_CREDENTIAL =
            CREDENTIAL_MANAGER_API_CALLED__API_NAME__API_NAME_CREATE_CREDENTIAL;
    protected static final int METRICS_API_NAME_CLEAR_CREDENTIAL =
            CREDENTIAL_MANAGER_API_CALLED__API_NAME__API_NAME_CLEAR_CREDENTIAL;
    // TODO add isEnabled
    protected static final int METRICS_API_STATUS_SUCCESS =
            CREDENTIAL_MANAGER_API_CALLED__API_STATUS__API_STATUS_SUCCESS;
    protected static final int METRICS_API_STATUS_FAILURE =
            CREDENTIAL_MANAGER_API_CALLED__API_STATUS__API_STATUS_FAILURE;
    protected static final int METRICS_API_STATUS_CLIENT_CANCEL =
            CREDENTIAL_MANAGER_API_CALLED__API_STATUS__API_STATUS_CLIENT_CANCELED;
    protected static final int METRICS_API_STATUS_USER_CANCEL =
            CREDENTIAL_MANAGER_API_CALLED__API_STATUS__API_STATUS_USER_CANCELED;
    protected static final int METRICS_PROVIDER_STATUS_FINAL_FAILURE =
            CREDENTIAL_MANAGER_API_CALLED__CANDIDATE_PROVIDER_STATUS__PROVIDER_FINAL_FAILURE;
    protected static final int METRICS_PROVIDER_STATUS_QUERY_FAILURE =
            CREDENTIAL_MANAGER_API_CALLED__CANDIDATE_PROVIDER_STATUS__PROVIDER_QUERY_FAILURE;
    protected static final int METRICS_PROVIDER_STATUS_FINAL_SUCCESS =
            CREDENTIAL_MANAGER_API_CALLED__CANDIDATE_PROVIDER_STATUS__PROVIDER_FINAL_SUCCESS;
    protected static final int METRICS_PROVIDER_STATUS_QUERY_SUCCESS =
            CREDENTIAL_MANAGER_API_CALLED__CANDIDATE_PROVIDER_STATUS__PROVIDER_QUERY_SUCCESS;
    protected static final int METRICS_PROVIDER_STATUS_UNKNOWN =
            CREDENTIAL_MANAGER_API_CALLED__CANDIDATE_PROVIDER_STATUS__PROVIDER_UNKNOWN;


    /**
     * This retrieves the uid of any package name, given a context and a component name for the
     * package. By default, if the desired package uid cannot be found, it will fall back to a
     * bogus uid.
     * @return the uid of a given package
     */
    protected static int getPackageUid(Context context, ComponentName componentName) {
        int sessUid = -1;
        try {
            // Only for T and above, which is fine for our use case
            sessUid = context.getPackageManager().getApplicationInfo(
                    componentName.getPackageName(),
                    PackageManager.ApplicationInfoFlags.of(0)).uid;
        } catch (Throwable t) {
            Log.i(TAG, "Couldn't find required uid");
        }
        return sessUid;
    }

}
Loading