1 /* 2 * Copyright (C) 2024 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.reporting; 18 19 import static com.android.adservices.service.measurement.util.JobLockHolder.Type.AGGREGATE_REPORTING; 20 import static com.android.adservices.service.stats.AdServicesStatsLog.AD_SERVICES_BACKGROUND_JOBS_EXECUTION_REPORTED__EXECUTION_RESULT_CODE__SKIP_FOR_KILL_SWITCH_ON; 21 import static com.android.adservices.spe.AdServicesJobInfo.MEASUREMENT_IMMEDIATE_AGGREGATE_REPORTING_JOB; 22 23 import android.app.job.JobInfo; 24 import android.app.job.JobParameters; 25 import android.app.job.JobScheduler; 26 import android.app.job.JobService; 27 import android.content.ComponentName; 28 import android.content.Context; 29 30 import com.android.adservices.LogUtil; 31 import com.android.adservices.LoggerFactory; 32 import com.android.adservices.concurrency.AdServicesExecutors; 33 import com.android.adservices.data.measurement.DatastoreManager; 34 import com.android.adservices.data.measurement.DatastoreManagerFactory; 35 import com.android.adservices.service.Flags; 36 import com.android.adservices.service.FlagsFactory; 37 import com.android.adservices.service.common.compat.ServiceCompatUtils; 38 import com.android.adservices.service.measurement.aggregation.AggregateEncryptionKeyManager; 39 import com.android.adservices.service.measurement.util.JobLockHolder; 40 import com.android.adservices.service.stats.AdServicesLoggerImpl; 41 import com.android.adservices.spe.AdServicesJobServiceLogger; 42 import com.android.internal.annotations.VisibleForTesting; 43 44 import com.google.android.libraries.mobiledatadownload.internal.AndroidTimeSource; 45 import com.google.common.util.concurrent.ListeningExecutorService; 46 47 import java.util.concurrent.Future; 48 49 /** 50 * Service for immediately scheduling aggregate reporting jobs. Aggregate reports that have trigger 51 * context ids are meant for immediate delivery. The actual job execution logic is part of {@link 52 * AggregateReportingJobHandler} 53 */ 54 public final class ImmediateAggregateReportingJobService extends JobService { 55 private static final int MEASUREMENT_IMMEDIATE_AGGREGATE_REPORTING_JOB_ID = 56 MEASUREMENT_IMMEDIATE_AGGREGATE_REPORTING_JOB.getJobId(); 57 private static final ListeningExecutorService sBlockingExecutor = 58 AdServicesExecutors.getBlockingExecutor(); 59 60 private Future mExecutorFuture; 61 62 @Override onStartJob(JobParameters params)63 public boolean onStartJob(JobParameters params) { 64 // Always ensure that the first thing this job does is check if it should be running, and 65 // cancel itself if it's not supposed to be. 66 if (ServiceCompatUtils.shouldDisableExtServicesJobOnTPlus(this)) { 67 LogUtil.d( 68 String.format( 69 "Disabling %s job because it's running in" + " ExtServices on T+", 70 this.getClass().getSimpleName())); 71 return skipAndCancelBackgroundJob(params, /* skipReason=*/ 0, /* doRecord=*/ false); 72 } 73 74 AdServicesJobServiceLogger.getInstance() 75 .recordOnStartJob(MEASUREMENT_IMMEDIATE_AGGREGATE_REPORTING_JOB_ID); 76 77 if (FlagsFactory.getFlags().getMeasurementJobImmediateAggregateReportingKillSwitch()) { 78 LoggerFactory.getMeasurementLogger() 79 .e(this.getClass().getSimpleName() + " is disabled"); 80 return skipAndCancelBackgroundJob( 81 params, 82 AD_SERVICES_BACKGROUND_JOBS_EXECUTION_REPORTED__EXECUTION_RESULT_CODE__SKIP_FOR_KILL_SWITCH_ON, 83 /* doRecord=*/ true); 84 } 85 86 LoggerFactory.getMeasurementLogger().d(this.getClass().getSimpleName() + ".onStartJob"); 87 mExecutorFuture = 88 sBlockingExecutor.submit( 89 () -> { 90 processPendingReports(); 91 92 AdServicesJobServiceLogger.getInstance() 93 .recordJobFinished( 94 MEASUREMENT_IMMEDIATE_AGGREGATE_REPORTING_JOB_ID, 95 /* isSuccessful= */ true, 96 /* shouldRetry= */ false); 97 98 jobFinished(params, /* wantsReschedule= */ false); 99 }); 100 return true; 101 } 102 103 @VisibleForTesting processPendingReports()104 void processPendingReports() { 105 JobLockHolder.getInstance(AGGREGATE_REPORTING) 106 .runWithLock( 107 this.getClass().getSimpleName(), 108 () -> { 109 long maxAggregateReportUploadRetryWindowMs = 110 FlagsFactory.getFlags() 111 .getMeasurementMaxAggregateReportUploadRetryWindowMs(); 112 DatastoreManager datastoreManager = 113 DatastoreManagerFactory.getDatastoreManager(); 114 new AggregateReportingJobHandler( 115 datastoreManager, 116 new AggregateEncryptionKeyManager( 117 datastoreManager, getApplicationContext()), 118 FlagsFactory.getFlags(), 119 AdServicesLoggerImpl.getInstance(), 120 ReportingStatus.ReportType.AGGREGATE, 121 ReportingStatus.UploadMethod.REGULAR, 122 getApplicationContext(), 123 new AndroidTimeSource()) 124 .performScheduledPendingReportsInWindow( 125 System.currentTimeMillis() 126 - maxAggregateReportUploadRetryWindowMs, 127 System.currentTimeMillis()); 128 }); 129 } 130 131 @Override onStopJob(JobParameters params)132 public boolean onStopJob(JobParameters params) { 133 LoggerFactory.getMeasurementLogger().d(this.getClass().getSimpleName() + ".onStopJob"); 134 boolean shouldRetry = true; 135 if (mExecutorFuture != null) { 136 shouldRetry = mExecutorFuture.cancel(/* mayInterruptIfRunning */ true); 137 } 138 AdServicesJobServiceLogger.getInstance() 139 .recordOnStopJob( 140 params, MEASUREMENT_IMMEDIATE_AGGREGATE_REPORTING_JOB_ID, shouldRetry); 141 return shouldRetry; 142 } 143 144 /** Schedules {@link ImmediateAggregateReportingJobService} */ 145 @VisibleForTesting schedule(JobScheduler jobScheduler, JobInfo job)146 static void schedule(JobScheduler jobScheduler, JobInfo job) { 147 jobScheduler.schedule(job); 148 } 149 150 /** 151 * Schedule Immediate Aggregate Reporting Job if it is not already scheduled 152 * 153 * @param context the context 154 * @param forceSchedule flag to indicate whether to force rescheduling the job. 155 */ 156 // TODO(b/311183933): Remove passed in Context from static method. 157 @SuppressWarnings("AvoidStaticContext") scheduleIfNeeded(Context context, boolean forceSchedule)158 public static void scheduleIfNeeded(Context context, boolean forceSchedule) { 159 Flags flags = FlagsFactory.getFlags(); 160 if (flags.getMeasurementJobImmediateAggregateReportingKillSwitch()) { 161 LoggerFactory.getMeasurementLogger() 162 .d("ImmediateAggregateReportingJobService is disabled, skip scheduling"); 163 return; 164 } 165 166 final JobScheduler jobScheduler = context.getSystemService(JobScheduler.class); 167 if (jobScheduler == null) { 168 LoggerFactory.getMeasurementLogger().e("JobScheduler not found"); 169 return; 170 } 171 172 final JobInfo scheduledJobInfo = 173 jobScheduler.getPendingJob(MEASUREMENT_IMMEDIATE_AGGREGATE_REPORTING_JOB_ID); 174 JobInfo jobInfo = buildJobInfo(context, flags); 175 // Schedule if it hasn't been scheduled already or force rescheduling 176 if (forceSchedule || !jobInfo.equals(scheduledJobInfo)) { 177 schedule(jobScheduler, jobInfo); 178 LoggerFactory.getMeasurementLogger() 179 .d("Scheduled ImmediateAggregateReportingJobService"); 180 } else { 181 LoggerFactory.getMeasurementLogger() 182 .d( 183 "ImmediateAggregateReportingJobService already scheduled, skipping" 184 + " reschedule"); 185 } 186 } 187 188 // TODO(b/311183933): Remove passed in Context from static method. 189 @SuppressWarnings("AvoidStaticContext") buildJobInfo(Context context, Flags flags)190 private static JobInfo buildJobInfo(Context context, Flags flags) { 191 return new JobInfo.Builder( 192 MEASUREMENT_IMMEDIATE_AGGREGATE_REPORTING_JOB_ID, 193 new ComponentName(context, ImmediateAggregateReportingJobService.class)) 194 .setRequiresBatteryNotLow( 195 flags.getMeasurementImmediateAggregateReportingJobRequiredBatteryNotLow()) 196 .setRequiredNetworkType( 197 flags.getMeasurementImmediateAggregateReportingJobRequiredNetworkType()) 198 .setPersisted(flags.getMeasurementImmediateAggregateReportingJobPersisted()) 199 .build(); 200 } 201 skipAndCancelBackgroundJob( final JobParameters params, int skipReason, boolean doRecord)202 private boolean skipAndCancelBackgroundJob( 203 final JobParameters params, int skipReason, boolean doRecord) { 204 final JobScheduler jobScheduler = this.getSystemService(JobScheduler.class); 205 if (jobScheduler != null) { 206 jobScheduler.cancel(MEASUREMENT_IMMEDIATE_AGGREGATE_REPORTING_JOB_ID); 207 } 208 209 if (doRecord) { 210 AdServicesJobServiceLogger.getInstance() 211 .recordJobSkipped(MEASUREMENT_IMMEDIATE_AGGREGATE_REPORTING_JOB_ID, skipReason); 212 } 213 214 // Tell the JobScheduler that the job has completed and does not need to be rescheduled. 215 jobFinished(params, false); 216 217 // Returning false means that this job has completed its work. 218 return false; 219 } 220 221 @VisibleForTesting getFutureForTesting()222 Future getFutureForTesting() { 223 return mExecutorFuture; 224 } 225 } 226