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.stats.AdServicesStatsLog.AD_SERVICES_BACKGROUND_JOBS_EXECUTION_REPORTED__EXECUTION_RESULT_CODE__SKIP_FOR_EXTSERVICES_JOB_ON_TPLUS; 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_ATTRIBUTION_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.concurrency.AdServicesExecutors; 32 import com.android.adservices.data.measurement.DatastoreManagerFactory; 33 import com.android.adservices.service.FlagsFactory; 34 import com.android.adservices.service.common.compat.ServiceCompatUtils; 35 import com.android.adservices.service.measurement.SystemHealthParams; 36 import com.android.adservices.service.measurement.Trigger; 37 import com.android.adservices.service.measurement.reporting.DebugReportApi; 38 import com.android.adservices.service.measurement.reporting.DebugReportingJobService; 39 import com.android.adservices.spe.AdservicesJobServiceLogger; 40 import com.android.internal.annotations.VisibleForTesting; 41 42 import java.util.concurrent.Executor; 43 44 /** 45 * Service for scheduling attribution jobs. The actual job execution logic is part of {@link 46 * AttributionJobHandler}. 47 */ 48 public class AttributionJobService extends JobService { 49 private static final int MEASUREMENT_ATTRIBUTION_JOB_ID = 50 MEASUREMENT_ATTRIBUTION_JOB.getJobId(); 51 private static final Executor sBackgroundExecutor = AdServicesExecutors.getBackgroundExecutor(); 52 53 @Override onCreate()54 public void onCreate() { 55 LogUtil.d("AttributionJobService.onCreate"); 56 super.onCreate(); 57 } 58 59 @Override onStartJob(JobParameters params)60 public boolean onStartJob(JobParameters params) { 61 // Always ensure that the first thing this job does is check if it should be running, and 62 // cancel itself if it's not supposed to be. 63 if (ServiceCompatUtils.shouldDisableExtServicesJobOnTPlus(this)) { 64 LogUtil.d( 65 "Disabling AttributionJobService job because it's running in ExtServices on" 66 + " T+"); 67 return skipAndCancelBackgroundJob( 68 params, 69 AD_SERVICES_BACKGROUND_JOBS_EXECUTION_REPORTED__EXECUTION_RESULT_CODE__SKIP_FOR_EXTSERVICES_JOB_ON_TPLUS); 70 } 71 72 AdservicesJobServiceLogger.getInstance(this) 73 .recordOnStartJob(MEASUREMENT_ATTRIBUTION_JOB_ID); 74 75 if (FlagsFactory.getFlags().getMeasurementJobAttributionKillSwitch()) { 76 LogUtil.e("AttributionJobService is disabled"); 77 return skipAndCancelBackgroundJob( 78 params, 79 AD_SERVICES_BACKGROUND_JOBS_EXECUTION_REPORTED__EXECUTION_RESULT_CODE__SKIP_FOR_KILL_SWITCH_ON); 80 } 81 82 LogUtil.d("AttributionJobService.onStartJob"); 83 sBackgroundExecutor.execute( 84 () -> { 85 boolean success = 86 new AttributionJobHandler( 87 DatastoreManagerFactory.getDatastoreManager( 88 getApplicationContext()), 89 new DebugReportApi( 90 getApplicationContext(), 91 FlagsFactory.getFlags())) 92 .performPendingAttributions(); 93 94 AdservicesJobServiceLogger.getInstance(AttributionJobService.this) 95 .recordJobFinished(MEASUREMENT_ATTRIBUTION_JOB_ID, success, !success); 96 97 jobFinished(params, !success); 98 // jobFinished is asynchronous, so forcing scheduling avoiding concurrency issue 99 scheduleIfNeeded(this, /* forceSchedule */ true); 100 DebugReportingJobService.scheduleIfNeeded( 101 getApplicationContext(), 102 /* forceSchedule */ true, 103 /* isDebugReportApi */ false); 104 }); 105 return true; 106 } 107 108 @Override onStopJob(JobParameters params)109 public boolean onStopJob(JobParameters params) { 110 LogUtil.d("AttributionJobService.onStopJob"); 111 boolean shouldRetry = true; 112 113 AdservicesJobServiceLogger.getInstance(this) 114 .recordOnStopJob(params, MEASUREMENT_ATTRIBUTION_JOB_ID, shouldRetry); 115 return shouldRetry; 116 } 117 118 /** Schedules {@link AttributionJobService} to observer {@link Trigger} content URI change. */ 119 @VisibleForTesting schedule(Context context, JobScheduler jobScheduler)120 static void schedule(Context context, JobScheduler jobScheduler) { 121 final JobInfo job = 122 new JobInfo.Builder( 123 MEASUREMENT_ATTRIBUTION_JOB_ID, 124 new ComponentName(context, AttributionJobService.class)) 125 .addTriggerContentUri( 126 new JobInfo.TriggerContentUri( 127 TriggerContentProvider.TRIGGER_URI, 128 JobInfo.TriggerContentUri.FLAG_NOTIFY_FOR_DESCENDANTS)) 129 .setTriggerContentUpdateDelay( 130 SystemHealthParams.ATTRIBUTION_JOB_TRIGGERING_DELAY_MS) 131 .setPersisted(false) // Can't call addTriggerContentUri() on a persisted job 132 .build(); 133 jobScheduler.schedule(job); 134 } 135 136 /** 137 * Schedule Attribution Job if it is not already scheduled 138 * 139 * @param context the context 140 * @param forceSchedule flag to indicate whether to force rescheduling the job. 141 */ scheduleIfNeeded(Context context, boolean forceSchedule)142 public static void scheduleIfNeeded(Context context, boolean forceSchedule) { 143 if (FlagsFactory.getFlags().getMeasurementJobAttributionKillSwitch()) { 144 LogUtil.e("AttributionJobService is disabled, skip scheduling"); 145 return; 146 } 147 148 final JobScheduler jobScheduler = context.getSystemService(JobScheduler.class); 149 if (jobScheduler == null) { 150 LogUtil.e("JobScheduler not found"); 151 return; 152 } 153 154 final JobInfo job = jobScheduler.getPendingJob(MEASUREMENT_ATTRIBUTION_JOB_ID); 155 // Schedule if it hasn't been scheduled already or force rescheduling 156 if (job == null || forceSchedule) { 157 schedule(context, jobScheduler); 158 LogUtil.d("Scheduled AttributionJobService"); 159 } else { 160 LogUtil.d("AttributionJobService already scheduled, skipping reschedule"); 161 } 162 } 163 skipAndCancelBackgroundJob(final JobParameters params, int skipReason)164 private boolean skipAndCancelBackgroundJob(final JobParameters params, int skipReason) { 165 final JobScheduler jobScheduler = this.getSystemService(JobScheduler.class); 166 if (jobScheduler != null) { 167 jobScheduler.cancel(MEASUREMENT_ATTRIBUTION_JOB_ID); 168 } 169 170 AdservicesJobServiceLogger.getInstance(this) 171 .recordJobSkipped(MEASUREMENT_ATTRIBUTION_JOB_ID, skipReason); 172 173 // Tell the JobScheduler that the job has completed and does not need to be rescheduled. 174 jobFinished(params, false); 175 176 // Returning false means that this job has completed its work. 177 return false; 178 } 179 } 180