1 /* 2 * Copyright (C) 2022 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.adservices.service.measurement.attribution; 18 19 import static com.android.adservices.service.measurement.util.JobLockHolder.Type.ATTRIBUTION_PROCESSING; 20 import static com.android.adservices.service.profiling.RbATraceProvider.FeatureNames.MEASUREMENT_API; 21 import static com.android.adservices.service.profiling.TracingNames.CLASS_NAME_ATTRIBUTION_FALLBACK_JOB_SERVICE; 22 import static com.android.adservices.service.profiling.TracingNames.METHOD_NAME_ON_START_JOB; 23 import static com.android.adservices.service.stats.AdServicesStatsLog.AD_SERVICES_BACKGROUND_JOBS_EXECUTION_REPORTED__EXECUTION_RESULT_CODE__SKIP_FOR_KILL_SWITCH_ON; 24 import static com.android.adservices.spe.AdServicesJobInfo.MEASUREMENT_ATTRIBUTION_FALLBACK_JOB; 25 26 import android.app.job.JobInfo; 27 import android.app.job.JobParameters; 28 import android.app.job.JobScheduler; 29 import android.app.job.JobService; 30 import android.content.ComponentName; 31 import android.content.Context; 32 33 import com.android.adservices.LogUtil; 34 import com.android.adservices.LoggerFactory; 35 import com.android.adservices.concurrency.AdServicesExecutors; 36 import com.android.adservices.data.measurement.DatastoreManagerFactory; 37 import com.android.adservices.service.Flags; 38 import com.android.adservices.service.FlagsFactory; 39 import com.android.adservices.service.common.compat.ServiceCompatUtils; 40 import com.android.adservices.service.measurement.Trigger; 41 import com.android.adservices.service.measurement.reporting.AggregateDebugReportApi; 42 import com.android.adservices.service.measurement.reporting.DebugReportApi; 43 import com.android.adservices.service.measurement.reporting.DebugReportingJobService; 44 import com.android.adservices.service.measurement.reporting.ImmediateAggregateReportingJobService; 45 import com.android.adservices.service.measurement.reporting.ReportingJobService; 46 import com.android.adservices.service.measurement.util.JobLockHolder; 47 import com.android.adservices.service.profiling.RbATraceProvider; 48 import com.android.adservices.spe.AdServicesJobServiceLogger; 49 import com.android.internal.annotations.VisibleForTesting; 50 51 import com.google.common.util.concurrent.ListeningExecutorService; 52 53 import java.util.concurrent.Future; 54 55 /** 56 * Fallback attribution job. The actual job execution logic is part of {@link 57 * AttributionJobHandler}. 58 */ 59 // TODO(b/311183933): Remove passed in Context from static method. 60 @SuppressWarnings("AvoidStaticContext") 61 public final class AttributionFallbackJobService extends JobService { 62 private static final int MEASUREMENT_ATTRIBUTION_FALLBACK_JOB_ID = 63 MEASUREMENT_ATTRIBUTION_FALLBACK_JOB.getJobId(); 64 private static final ListeningExecutorService sBackgroundExecutor = 65 AdServicesExecutors.getBackgroundExecutor(); 66 private Future mExecutorFuture; 67 68 @Override onCreate()69 public void onCreate() { 70 LogUtil.d("AttributionFallbackJobService.onCreate"); 71 super.onCreate(); 72 } 73 74 @Override onStartJob(JobParameters params)75 public boolean onStartJob(JobParameters params) { 76 // Always ensure that the first thing this job does is check if it should be running, and 77 // cancel itself if it's not supposed to be. 78 if (ServiceCompatUtils.shouldDisableExtServicesJobOnTPlus(this)) { 79 LogUtil.d( 80 "Disabling AttributionFallbackJobService job because it's running in" 81 + " ExtServices on T+"); 82 return skipAndCancelBackgroundJob(params, /* skipReason=*/ 0, /* doRecord=*/ false); 83 } 84 85 int traceCookie = 86 RbATraceProvider.beginAsyncSection( 87 MEASUREMENT_API, 88 CLASS_NAME_ATTRIBUTION_FALLBACK_JOB_SERVICE, 89 METHOD_NAME_ON_START_JOB, 90 FlagsFactory.getFlags()); 91 92 AdServicesJobServiceLogger.getInstance() 93 .recordOnStartJob(MEASUREMENT_ATTRIBUTION_FALLBACK_JOB_ID); 94 95 if (!FlagsFactory.getFlags().getMeasurementAttributionFallbackJobEnabled()) { 96 LoggerFactory.getMeasurementLogger().e("AttributionFallbackJobService is disabled"); 97 return skipAndCancelBackgroundJob( 98 params, 99 AD_SERVICES_BACKGROUND_JOBS_EXECUTION_REPORTED__EXECUTION_RESULT_CODE__SKIP_FOR_KILL_SWITCH_ON, 100 /* doRecord=*/ true); 101 } 102 103 LoggerFactory.getMeasurementLogger().d("AttributionFallbackJobService.onStartJob"); 104 mExecutorFuture = 105 sBackgroundExecutor.submit( 106 () -> { 107 processPendingAttributions(); 108 109 DebugReportingJobService.scheduleIfNeeded( 110 getApplicationContext(), /* forceSchedule */ false); 111 112 // TODO(b/342687685): fold this service into ReportingJobService 113 ImmediateAggregateReportingJobService.scheduleIfNeeded( 114 getApplicationContext(), /* forceSchedule */ false); 115 116 ReportingJobService.scheduleIfNeeded( 117 getApplicationContext(), /* forceSchedule */ false); 118 119 AdServicesJobServiceLogger.getInstance() 120 .recordJobFinished( 121 MEASUREMENT_ATTRIBUTION_FALLBACK_JOB_ID, 122 /* isSuccessful */ true, 123 /* shouldRetry */ false); 124 125 jobFinished(params, /* wantsReschedule= */ false); 126 RbATraceProvider.endAsyncSection( 127 MEASUREMENT_API, 128 CLASS_NAME_ATTRIBUTION_FALLBACK_JOB_SERVICE, 129 METHOD_NAME_ON_START_JOB, 130 traceCookie, 131 FlagsFactory.getFlags()); 132 }); 133 return true; 134 } 135 136 @VisibleForTesting processPendingAttributions()137 void processPendingAttributions() { 138 JobLockHolder.getInstance(ATTRIBUTION_PROCESSING) 139 .runWithLock( 140 "AttributionFallbackJobService", 141 () -> { 142 new AttributionJobHandler( 143 DatastoreManagerFactory.getDatastoreManager(), 144 new DebugReportApi( 145 getApplicationContext(), 146 FlagsFactory.getFlags()), 147 new AggregateDebugReportApi(FlagsFactory.getFlags())) 148 .performPendingAttributions(); 149 }); 150 } 151 152 @Override onStopJob(JobParameters params)153 public boolean onStopJob(JobParameters params) { 154 LoggerFactory.getMeasurementLogger().d("AttributionFallbackJobService.onStopJob"); 155 boolean shouldRetry = true; 156 if (mExecutorFuture != null) { 157 shouldRetry = mExecutorFuture.cancel(/* mayInterruptIfRunning */ true); 158 } 159 AdServicesJobServiceLogger.getInstance() 160 .recordOnStopJob(params, MEASUREMENT_ATTRIBUTION_FALLBACK_JOB_ID, shouldRetry); 161 return shouldRetry; 162 } 163 164 /** 165 * Schedules {@link AttributionFallbackJobService} to observer {@link Trigger} content URI 166 * change. 167 */ 168 @VisibleForTesting schedule(JobScheduler jobScheduler, JobInfo job)169 static void schedule(JobScheduler jobScheduler, JobInfo job) { 170 jobScheduler.schedule(job); 171 } 172 buildJobInfo(Context context, Flags flags)173 private static JobInfo buildJobInfo(Context context, Flags flags) { 174 return new JobInfo.Builder( 175 MEASUREMENT_ATTRIBUTION_FALLBACK_JOB_ID, 176 new ComponentName(context, AttributionFallbackJobService.class)) 177 .setPeriodic(flags.getMeasurementAttributionFallbackJobPeriodMs()) 178 .setPersisted(flags.getMeasurementAttributionFallbackJobPersisted()) 179 .build(); 180 } 181 182 /** 183 * Schedule Attribution Fallback Job if it is not already scheduled 184 * 185 * @param context the context 186 * @param forceSchedule flag to indicate whether to force rescheduling the job. 187 */ 188 // TODO(b/311183933): Remove passed in Context from static method. 189 @SuppressWarnings("AvoidStaticContext") scheduleIfNeeded(Context context, boolean forceSchedule)190 public static void scheduleIfNeeded(Context context, boolean forceSchedule) { 191 Flags flags = FlagsFactory.getFlags(); 192 if (!flags.getMeasurementAttributionFallbackJobEnabled()) { 193 LoggerFactory.getMeasurementLogger() 194 .e("AttributionFallbackJobService is disabled, skip scheduling"); 195 return; 196 } 197 198 final JobScheduler jobScheduler = context.getSystemService(JobScheduler.class); 199 if (jobScheduler == null) { 200 LoggerFactory.getMeasurementLogger().e("JobScheduler not found"); 201 return; 202 } 203 204 final JobInfo scheduledJob = 205 jobScheduler.getPendingJob(MEASUREMENT_ATTRIBUTION_FALLBACK_JOB_ID); 206 // Schedule if it hasn't been scheduled already or force rescheduling 207 JobInfo jobInfo = buildJobInfo(context, flags); 208 if (forceSchedule || !jobInfo.equals(scheduledJob)) { 209 schedule(jobScheduler, jobInfo); 210 LoggerFactory.getMeasurementLogger().d("Scheduled AttributionFallbackJobService"); 211 } else { 212 LoggerFactory.getMeasurementLogger() 213 .d("AttributionFallbackJobService already scheduled, skipping reschedule"); 214 } 215 } 216 skipAndCancelBackgroundJob( final JobParameters params, int skipReason, boolean doRecord)217 private boolean skipAndCancelBackgroundJob( 218 final JobParameters params, int skipReason, boolean doRecord) { 219 final JobScheduler jobScheduler = this.getSystemService(JobScheduler.class); 220 if (jobScheduler != null) { 221 jobScheduler.cancel(MEASUREMENT_ATTRIBUTION_FALLBACK_JOB_ID); 222 } 223 224 if (doRecord) { 225 AdServicesJobServiceLogger.getInstance() 226 .recordJobSkipped(MEASUREMENT_ATTRIBUTION_FALLBACK_JOB_ID, skipReason); 227 } 228 229 // Tell the JobScheduler that the job has completed and does not need to be rescheduled. 230 jobFinished(params, false); 231 232 // Returning false means that this job has completed its work. 233 return false; 234 } 235 236 @VisibleForTesting getFutureForTesting()237 Future getFutureForTesting() { 238 return mExecutorFuture; 239 } 240 } 241