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