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