diff options
Diffstat (limited to 'adservices/service-core/java/com/android/adservices/service/adselection/AdSelectionServiceImpl.java')
-rw-r--r-- | adservices/service-core/java/com/android/adservices/service/adselection/AdSelectionServiceImpl.java | 106 |
1 files changed, 90 insertions, 16 deletions
diff --git a/adservices/service-core/java/com/android/adservices/service/adselection/AdSelectionServiceImpl.java b/adservices/service-core/java/com/android/adservices/service/adselection/AdSelectionServiceImpl.java index 85070de404..dadd927f8e 100644 --- a/adservices/service-core/java/com/android/adservices/service/adselection/AdSelectionServiceImpl.java +++ b/adservices/service-core/java/com/android/adservices/service/adselection/AdSelectionServiceImpl.java @@ -30,6 +30,7 @@ import android.adservices.adselection.ReportImpressionCallback; import android.adservices.adselection.ReportImpressionInput; import android.adservices.common.AdSelectionSignals; import android.adservices.common.AdServicesStatusUtils; +import android.adservices.common.CallerMetadata; import android.annotation.NonNull; import android.content.Context; @@ -56,10 +57,13 @@ import com.android.adservices.service.js.JSScriptEngine; import com.android.adservices.service.stats.AdServicesLogger; import com.android.adservices.service.stats.AdServicesLoggerImpl; import com.android.adservices.service.stats.AdServicesStatsLog; +import com.android.adservices.service.stats.ApiServiceLatencyCalculator; +import com.android.adservices.service.stats.Clock; import com.android.internal.annotations.VisibleForTesting; import java.util.Objects; import java.util.concurrent.ExecutorService; +import java.util.concurrent.ScheduledThreadPoolExecutor; /** * Implementation of {@link AdSelectionService}. @@ -73,6 +77,7 @@ public class AdSelectionServiceImpl extends AdSelectionService.Stub { @NonNull private final AdServicesHttpsClient mAdServicesHttpsClient; @NonNull private final ExecutorService mLightweightExecutor; @NonNull private final ExecutorService mBackgroundExecutor; + @NonNull private final ScheduledThreadPoolExecutor mScheduledExecutor; @NonNull private final Context mContext; @NonNull private final ConsentManager mConsentManager; @NonNull private final DevContextFilter mDevContextFilter; @@ -96,6 +101,7 @@ public class AdSelectionServiceImpl extends AdSelectionService.Stub { @NonNull AppImportanceFilter appImportanceFilter, @NonNull ExecutorService lightweightExecutorService, @NonNull ExecutorService backgroundExecutorService, + @NonNull ScheduledThreadPoolExecutor scheduledExecutor, @NonNull Context context, ConsentManager consentManager, @NonNull AdServicesLogger adServicesLogger, @@ -111,6 +117,7 @@ public class AdSelectionServiceImpl extends AdSelectionService.Stub { Objects.requireNonNull(appImportanceFilter); Objects.requireNonNull(lightweightExecutorService); Objects.requireNonNull(backgroundExecutorService); + Objects.requireNonNull(scheduledExecutor); Objects.requireNonNull(consentManager); Objects.requireNonNull(adServicesLogger); Objects.requireNonNull(flags); @@ -124,6 +131,7 @@ public class AdSelectionServiceImpl extends AdSelectionService.Stub { mAppImportanceFilter = appImportanceFilter; mLightweightExecutor = lightweightExecutorService; mBackgroundExecutor = backgroundExecutorService; + mScheduledExecutor = scheduledExecutor; mContext = context; mConsentManager = consentManager; mAdServicesLogger = adServicesLogger; @@ -151,6 +159,7 @@ public class AdSelectionServiceImpl extends AdSelectionService.Stub { () -> FlagsFactory.getFlags().getForegroundStatuslLevelForValidation()), AdServicesExecutors.getLightWeightExecutor(), AdServicesExecutors.getBackgroundExecutor(), + AdServicesExecutors.getScheduler(), context, ConsentManager.getInstance(context), AdServicesLoggerImpl.getInstance(), @@ -164,43 +173,107 @@ public class AdSelectionServiceImpl extends AdSelectionService.Stub { // TODO(b/233116758): Validate all the fields inside the adSelectionConfig. @Override public void runAdSelection( - @NonNull AdSelectionInput inputParams, @NonNull AdSelectionCallback callback) { + @NonNull AdSelectionInput inputParams, + @NonNull CallerMetadata callerMetadata, + @NonNull AdSelectionCallback callback) { + final ApiServiceLatencyCalculator apiServiceLatencyCalculator = + new ApiServiceLatencyCalculator(callerMetadata, Clock.SYSTEM_CLOCK); int apiName = AdServicesStatsLog.AD_SERVICES_API_CALLED__API_NAME__SELECT_ADS; // Caller permissions must be checked in the binder thread, before anything else mFledgeAuthorizationFilter.assertAppDeclaredPermission(mContext, apiName); - try { Objects.requireNonNull(inputParams); Objects.requireNonNull(callback); } catch (NullPointerException exception) { + int overallLatencyMs = apiServiceLatencyCalculator.getApiServiceOverallLatencyMs(); + LogUtil.v( + "The runAdSelection() arguments should not be null, failed with overall" + + "latency %d in ms.", + overallLatencyMs); mAdServicesLogger.logFledgeApiCallStats( - apiName, AdServicesStatusUtils.STATUS_INVALID_ARGUMENT); + apiName, AdServicesStatusUtils.STATUS_INVALID_ARGUMENT, overallLatencyMs); // Rethrow because we want to fail fast throw exception; } DevContext devContext = mDevContextFilter.createDevContext(); + int callerUid = getCallingUid(apiName); + mLightweightExecutor.execute( + () -> { + // TODO(b/249298855): Evolve off device ad selection logic. + if (mFlags.getAdSelectionOffDeviceEnabled()) { + runOffDeviceAdSelection( + devContext, + callerUid, + inputParams, + callback, + apiServiceLatencyCalculator); + } else { + runOnDeviceAdSelection( + devContext, + callerUid, + inputParams, + callback, + apiServiceLatencyCalculator); + } + }); + } - AdSelectionRunner adSelectionRunner = - new AdSelectionRunner( + private void runOnDeviceAdSelection( + DevContext devContext, + int callerUid, + @NonNull AdSelectionInput inputParams, + @NonNull AdSelectionCallback callback, + @NonNull ApiServiceLatencyCalculator apiServiceLatencyCalculator) { + OnDeviceAdSelectionRunner runner = + new OnDeviceAdSelectionRunner( mContext, mCustomAudienceDao, mAdSelectionEntryDao, mAdServicesHttpsClient, mLightweightExecutor, mBackgroundExecutor, + mScheduledExecutor, mConsentManager, mAdServicesLogger, devContext, mAppImportanceFilter, mFlags, () -> Throttler.getInstance(mFlags.getSdkRequestPermitsPerSecond()), - getCallingUid(apiName), + callerUid, mFledgeAuthorizationFilter, - mFledgeAllowListsFilter); + mFledgeAllowListsFilter, + apiServiceLatencyCalculator); + runner.runAdSelection(inputParams, callback); + } - adSelectionRunner.runAdSelection(inputParams, callback); + private void runOffDeviceAdSelection( + DevContext devContext, + int callerUid, + @NonNull AdSelectionInput inputParams, + @NonNull AdSelectionCallback callback, + @NonNull ApiServiceLatencyCalculator apiServiceLatencyCalculator) { + TrustedServerAdSelectionRunner runner = + new TrustedServerAdSelectionRunner( + mContext, + mCustomAudienceDao, + mAdSelectionEntryDao, + mAdServicesHttpsClient, + mLightweightExecutor, + mBackgroundExecutor, + mScheduledExecutor, + mConsentManager, + mAdServicesLogger, + devContext, + mAppImportanceFilter, + mFlags, + () -> Throttler.getInstance(mFlags.getSdkRequestPermitsPerSecond()), + callerUid, + mFledgeAuthorizationFilter, + mFledgeAllowListsFilter, + apiServiceLatencyCalculator); + runner.runAdSelection(inputParams, callback); } @Override @@ -217,7 +290,7 @@ public class AdSelectionServiceImpl extends AdSelectionService.Stub { Objects.requireNonNull(callback); } catch (NullPointerException exception) { mAdServicesLogger.logFledgeApiCallStats( - apiName, AdServicesStatusUtils.STATUS_INVALID_ARGUMENT); + apiName, AdServicesStatusUtils.STATUS_INVALID_ARGUMENT, 0); // Rethrow because we want to fail fast throw exception; } @@ -229,6 +302,7 @@ public class AdSelectionServiceImpl extends AdSelectionService.Stub { mContext, mLightweightExecutor, mBackgroundExecutor, + mScheduledExecutor, mAdSelectionEntryDao, mAdServicesHttpsClient, mConsentManager, @@ -260,7 +334,7 @@ public class AdSelectionServiceImpl extends AdSelectionService.Stub { Objects.requireNonNull(callback); } catch (NullPointerException exception) { mAdServicesLogger.logFledgeApiCallStats( - apiName, AdServicesStatusUtils.STATUS_INVALID_ARGUMENT); + apiName, AdServicesStatusUtils.STATUS_INVALID_ARGUMENT, 0); // Rethrow because we want to fail fast throw exception; } @@ -269,7 +343,7 @@ public class AdSelectionServiceImpl extends AdSelectionService.Stub { if (!devContext.getDevOptionsEnabled()) { mAdServicesLogger.logFledgeApiCallStats( - apiName, AdServicesStatusUtils.STATUS_INTERNAL_ERROR); + apiName, AdServicesStatusUtils.STATUS_INTERNAL_ERROR, 0); throw new SecurityException(API_NOT_AUTHORIZED_MSG); } @@ -294,7 +368,7 @@ public class AdSelectionServiceImpl extends AdSelectionService.Stub { return mCallingAppUidSupplier.getCallingAppUid(); } catch (IllegalStateException illegalStateException) { mAdServicesLogger.logFledgeApiCallStats( - apiNameLoggingId, AdServicesStatusUtils.STATUS_INTERNAL_ERROR); + apiNameLoggingId, AdServicesStatusUtils.STATUS_INTERNAL_ERROR, 0); throw illegalStateException; } } @@ -315,7 +389,7 @@ public class AdSelectionServiceImpl extends AdSelectionService.Stub { Objects.requireNonNull(callback); } catch (NullPointerException exception) { mAdServicesLogger.logFledgeApiCallStats( - apiName, AdServicesStatusUtils.STATUS_INVALID_ARGUMENT); + apiName, AdServicesStatusUtils.STATUS_INVALID_ARGUMENT, 0); // Rethrow because we want to fail fast throw exception; } @@ -324,7 +398,7 @@ public class AdSelectionServiceImpl extends AdSelectionService.Stub { if (!devContext.getDevOptionsEnabled()) { mAdServicesLogger.logFledgeApiCallStats( - apiName, AdServicesStatusUtils.STATUS_INTERNAL_ERROR); + apiName, AdServicesStatusUtils.STATUS_INTERNAL_ERROR, 0); throw new SecurityException(API_NOT_AUTHORIZED_MSG); } @@ -358,7 +432,7 @@ public class AdSelectionServiceImpl extends AdSelectionService.Stub { Objects.requireNonNull(callback); } catch (NullPointerException exception) { mAdServicesLogger.logFledgeApiCallStats( - apiName, AdServicesStatusUtils.STATUS_INVALID_ARGUMENT); + apiName, AdServicesStatusUtils.STATUS_INVALID_ARGUMENT, 0); // Rethrow because we want to fail fast throw exception; } @@ -367,7 +441,7 @@ public class AdSelectionServiceImpl extends AdSelectionService.Stub { if (!devContext.getDevOptionsEnabled()) { mAdServicesLogger.logFledgeApiCallStats( - apiName, AdServicesStatusUtils.STATUS_INTERNAL_ERROR); + apiName, AdServicesStatusUtils.STATUS_INTERNAL_ERROR, 0); throw new SecurityException(API_NOT_AUTHORIZED_MSG); } |