diff options
author | 2024-11-29 17:04:00 +0000 | |
---|---|---|
committer | 2024-12-09 13:17:54 +0000 | |
commit | 1670d281b339817d114f71cc927850504a973bf9 (patch) | |
tree | 7506004c4f8301ffc402162abbfa0d0e122487a2 /services/appfunctions/java | |
parent | e6378dbf40a5973771d3444f0235ca1d2fb71664 (diff) |
Log AppFunctionsRequestReported
Flag: android.app.appfunctions.flags.enable_app_function_manager
Bug: 376688078
Test: manual test
Change-Id: I9e4cdb9d517c6f953f08668d0c89373ffa6b6261
Diffstat (limited to 'services/appfunctions/java')
4 files changed, 107 insertions, 7 deletions
diff --git a/services/appfunctions/java/com/android/server/appfunctions/AppFunctionExecutors.java b/services/appfunctions/java/com/android/server/appfunctions/AppFunctionExecutors.java index 81e83b563945..eaea4435099c 100644 --- a/services/appfunctions/java/com/android/server/appfunctions/AppFunctionExecutors.java +++ b/services/appfunctions/java/com/android/server/appfunctions/AppFunctionExecutors.java @@ -16,6 +16,8 @@ package com.android.server.appfunctions; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; @@ -35,6 +37,11 @@ public final class AppFunctionExecutors { /* workQueue= */ new LinkedBlockingQueue<>(), new NamedThreadFactory("AppFunctionExecutors")); + /** Executor for stats logging. */ + public static final ExecutorService LOGGING_THREAD_EXECUTOR = + Executors.newSingleThreadExecutor( + new NamedThreadFactory("AppFunctionsLoggingExecutors")); + static { THREAD_POOL_EXECUTOR.allowCoreThreadTimeOut(true); } diff --git a/services/appfunctions/java/com/android/server/appfunctions/AppFunctionManagerServiceImpl.java b/services/appfunctions/java/com/android/server/appfunctions/AppFunctionManagerServiceImpl.java index c17c34061d1b..9cc5a8c97258 100644 --- a/services/appfunctions/java/com/android/server/appfunctions/AppFunctionManagerServiceImpl.java +++ b/services/appfunctions/java/com/android/server/appfunctions/AppFunctionManagerServiceImpl.java @@ -30,6 +30,7 @@ import android.app.appfunctions.AppFunctionManagerHelper; import android.app.appfunctions.AppFunctionRuntimeMetadata; import android.app.appfunctions.AppFunctionStaticMetadataHelper; import android.app.appfunctions.ExecuteAppFunctionAidlRequest; +import android.app.appfunctions.ExecuteAppFunctionResponse; import android.app.appfunctions.IAppFunctionEnabledCallback; import android.app.appfunctions.IAppFunctionManager; import android.app.appfunctions.IAppFunctionService; @@ -85,6 +86,7 @@ public class AppFunctionManagerServiceImpl extends IAppFunctionManager.Stub { private final ServiceConfig mServiceConfig; private final Context mContext; private final Map<String, Object> mLocks = new WeakHashMap<>(); + private final AppFunctionsLoggerWrapper mLoggerWrapper; public AppFunctionManagerServiceImpl(@NonNull Context context) { this( @@ -93,7 +95,8 @@ public class AppFunctionManagerServiceImpl extends IAppFunctionManager.Stub { context, IAppFunctionService.Stub::asInterface, THREAD_POOL_EXECUTOR), new CallerValidatorImpl(context), new ServiceHelperImpl(context), - new ServiceConfigImpl()); + new ServiceConfigImpl(), + new AppFunctionsLoggerWrapper(context)); } @VisibleForTesting @@ -102,12 +105,14 @@ public class AppFunctionManagerServiceImpl extends IAppFunctionManager.Stub { RemoteServiceCaller<IAppFunctionService> remoteServiceCaller, CallerValidator callerValidator, ServiceHelper appFunctionInternalServiceHelper, - ServiceConfig serviceConfig) { + ServiceConfig serviceConfig, + AppFunctionsLoggerWrapper loggerWrapper) { mContext = Objects.requireNonNull(context); mRemoteServiceCaller = Objects.requireNonNull(remoteServiceCaller); mCallerValidator = Objects.requireNonNull(callerValidator); mInternalServiceHelper = Objects.requireNonNull(appFunctionInternalServiceHelper); mServiceConfig = serviceConfig; + mLoggerWrapper = loggerWrapper; } /** Called when the user is unlocked. */ @@ -146,8 +151,25 @@ public class AppFunctionManagerServiceImpl extends IAppFunctionManager.Stub { Objects.requireNonNull(requestInternal); Objects.requireNonNull(executeAppFunctionCallback); + int callingUid = Binder.getCallingUid(); + int callingPid = Binder.getCallingPid(); + final SafeOneTimeExecuteAppFunctionCallback safeExecuteAppFunctionCallback = - new SafeOneTimeExecuteAppFunctionCallback(executeAppFunctionCallback); + new SafeOneTimeExecuteAppFunctionCallback(executeAppFunctionCallback, + new SafeOneTimeExecuteAppFunctionCallback.CompletionCallback() { + @Override + public void finalizeOnSuccess( + @NonNull ExecuteAppFunctionResponse result) { + mLoggerWrapper.logAppFunctionSuccess(requestInternal, result, + callingUid); + } + + @Override + public void finalizeOnError(@NonNull AppFunctionException error) { + mLoggerWrapper.logAppFunctionError(requestInternal, + error.getErrorCode(), callingUid); + } + }); String validatedCallingPackage; try { @@ -162,9 +184,6 @@ public class AppFunctionManagerServiceImpl extends IAppFunctionManager.Stub { return null; } - int callingUid = Binder.getCallingUid(); - int callingPid = Binder.getCallingPid(); - ICancellationSignal localCancelTransport = CancellationSignal.createTransport(); THREAD_POOL_EXECUTOR.execute( diff --git a/services/appfunctions/java/com/android/server/appfunctions/AppFunctionsLoggerWrapper.java b/services/appfunctions/java/com/android/server/appfunctions/AppFunctionsLoggerWrapper.java new file mode 100644 index 000000000000..b59915aa6343 --- /dev/null +++ b/services/appfunctions/java/com/android/server/appfunctions/AppFunctionsLoggerWrapper.java @@ -0,0 +1,74 @@ +/* + * Copyright (C) 2024 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.appfunctions; + +import static com.android.server.appfunctions.AppFunctionExecutors.LOGGING_THREAD_EXECUTOR; + +import android.annotation.NonNull; +import android.app.appfunctions.ExecuteAppFunctionAidlRequest; +import android.app.appfunctions.ExecuteAppFunctionResponse; +import android.content.Context; +import android.content.pm.PackageManager; +import android.os.SystemClock; +import android.util.Slog; + +import java.util.Objects; + +/** Wraps AppFunctionsStatsLog. */ +public class AppFunctionsLoggerWrapper { + private static final String TAG = AppFunctionsLoggerWrapper.class.getSimpleName(); + + private static final int SUCCESS_RESPONSE_CODE = -1; + + private final Context mContext; + + public AppFunctionsLoggerWrapper(@NonNull Context context) { + mContext = Objects.requireNonNull(context); + } + + void logAppFunctionSuccess(ExecuteAppFunctionAidlRequest request, + ExecuteAppFunctionResponse response, int callingUid) { + logAppFunctionsRequestReported(request, SUCCESS_RESPONSE_CODE, + response.getResponseDataSize(), callingUid); + } + + void logAppFunctionError(ExecuteAppFunctionAidlRequest request, int errorCode, int callingUid) { + logAppFunctionsRequestReported(request, errorCode, /* responseSizeBytes = */ 0, callingUid); + } + + private void logAppFunctionsRequestReported(ExecuteAppFunctionAidlRequest request, + int errorCode, int responseSizeBytes, int callingUid) { + final long latency = SystemClock.elapsedRealtime() - request.getRequestTime(); + LOGGING_THREAD_EXECUTOR.execute(() -> AppFunctionsStatsLog.write( + AppFunctionsStatsLog.APP_FUNCTIONS_REQUEST_REPORTED, + callingUid, + getPackageUid(request.getClientRequest().getTargetPackageName()), + errorCode, + request.getClientRequest().getRequestDataSize(), responseSizeBytes, + latency) + ); + } + + private int getPackageUid(String packageName) { + try { + return mContext.getPackageManager().getPackageUid(packageName, 0); + } catch (PackageManager.NameNotFoundException e) { + Slog.e(TAG, "Package uid not found for " + packageName); + } + return 0; + } +} diff --git a/services/appfunctions/java/com/android/server/appfunctions/RunAppFunctionServiceCallback.java b/services/appfunctions/java/com/android/server/appfunctions/RunAppFunctionServiceCallback.java index c689bb92f8f7..896c0fc51683 100644 --- a/services/appfunctions/java/com/android/server/appfunctions/RunAppFunctionServiceCallback.java +++ b/services/appfunctions/java/com/android/server/appfunctions/RunAppFunctionServiceCallback.java @@ -16,8 +16,8 @@ package com.android.server.appfunctions; import android.annotation.NonNull; -import android.app.appfunctions.ExecuteAppFunctionAidlRequest; import android.app.appfunctions.AppFunctionException; +import android.app.appfunctions.ExecuteAppFunctionAidlRequest; import android.app.appfunctions.ExecuteAppFunctionResponse; import android.app.appfunctions.IAppFunctionService; import android.app.appfunctions.ICancellationCallback; |