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;
18 
19 import static com.android.adservices.service.stats.AdServicesStatsLog.AD_SERVICES_BACKGROUND_JOBS_EXECUTION_REPORTED__EXECUTION_RESULT_CODE__SKIP_FOR_KILL_SWITCH_ON;
20 import static com.android.adservices.spe.AdServicesJobInfo.MEASUREMENT_DELETE_EXPIRED_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 androidx.annotation.Nullable;
30 
31 import com.android.adservices.LogUtil;
32 import com.android.adservices.LoggerFactory;
33 import com.android.adservices.concurrency.AdServicesExecutors;
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.spe.AdServicesJobServiceLogger;
39 import com.android.internal.annotations.VisibleForTesting;
40 
41 import java.util.concurrent.Executor;
42 
43 /** Service for scheduling delete-expired-records job. */
44 public final class DeleteExpiredJobService extends JobService {
45     private static final int MEASUREMENT_DELETE_EXPIRED_JOB_ID =
46             MEASUREMENT_DELETE_EXPIRED_JOB.getJobId();
47 
48     private static final Executor sBackgroundExecutor = AdServicesExecutors.getBackgroundExecutor();
49 
50     @Override
onStartJob(JobParameters params)51     public boolean onStartJob(JobParameters params) {
52         // Always ensure that the first thing this job does is check if it should be running, and
53         // cancel itself if it's not supposed to be.
54         if (ServiceCompatUtils.shouldDisableExtServicesJobOnTPlus(this)) {
55             LogUtil.d(
56                     "Disabling DeleteExpiredJobService job because it's running in ExtServices on"
57                             + " T+");
58             return skipAndCancelBackgroundJob(params, /* skipReason=*/ 0, /* doRecord=*/ false);
59         }
60 
61         AdServicesJobServiceLogger.getInstance()
62                 .recordOnStartJob(MEASUREMENT_DELETE_EXPIRED_JOB_ID);
63 
64         if (FlagsFactory.getFlags().getMeasurementJobDeleteExpiredKillSwitch()) {
65             LoggerFactory.getMeasurementLogger().e("DeleteExpiredJobService is disabled");
66             return skipAndCancelBackgroundJob(
67                     params,
68                     AD_SERVICES_BACKGROUND_JOBS_EXECUTION_REPORTED__EXECUTION_RESULT_CODE__SKIP_FOR_KILL_SWITCH_ON,
69                     /* doRecord=*/ true);
70         }
71 
72         LoggerFactory.getMeasurementLogger().d("DeleteExpiredJobService.onStartJob");
73         sBackgroundExecutor.execute(
74                 () -> {
75                     Flags flags = FlagsFactory.getFlags();
76                     long earliestValidInsertion =
77                             System.currentTimeMillis()
78                                     - FlagsFactory.getFlags().getMeasurementDataExpiryWindowMs();
79                     int retryLimit =
80                             FlagsFactory.getFlags()
81                                     .getMeasurementMaxRetriesPerRegistrationRequest();
82                     DatastoreManagerFactory.getDatastoreManager(this)
83                             .runInTransaction(
84                                     dao ->
85                                             dao.deleteExpiredRecords(
86                                                     earliestValidInsertion,
87                                                     retryLimit,
88                                                     getEarliestValidAppReportInsertion(flags)));
89 
90                     boolean shouldRetry = false;
91                     AdServicesJobServiceLogger.getInstance()
92                             .recordJobFinished(
93                                     MEASUREMENT_DELETE_EXPIRED_JOB_ID,
94                                     /* isSuccessful */ true,
95                                     shouldRetry);
96 
97                     jobFinished(params, shouldRetry);
98                 });
99         return true;
100     }
101 
getEarliestValidAppReportInsertion(Flags flags)102     private @Nullable Long getEarliestValidAppReportInsertion(Flags flags) {
103         return flags.getMeasurementEnableReinstallReattribution()
104                 ? System.currentTimeMillis()
105                         - flags.getMeasurementMaxReinstallReattributionWindowSeconds()
106                 : null;
107     }
108 
109     @Override
onStopJob(JobParameters params)110     public boolean onStopJob(JobParameters params) {
111         LoggerFactory.getMeasurementLogger().d("DeleteExpiredJobService.onStopJob");
112         boolean shouldRetry = false;
113 
114         AdServicesJobServiceLogger.getInstance()
115                 .recordOnStopJob(params, MEASUREMENT_DELETE_EXPIRED_JOB_ID, shouldRetry);
116         return shouldRetry;
117     }
118 
119     /** Schedule the job. */
120     @VisibleForTesting
schedule(JobScheduler jobScheduler, JobInfo jobInfo)121     static void schedule(JobScheduler jobScheduler, JobInfo jobInfo) {
122         jobScheduler.schedule(jobInfo);
123     }
124 
125     /**
126      * Schedule Delete Expired Job Service if it is not already scheduled
127      *
128      * @param context the context
129      * @param forceSchedule flag to indicate whether to force rescheduling the job.
130      */
scheduleIfNeeded(Context context, boolean forceSchedule)131     public static void scheduleIfNeeded(Context context, boolean forceSchedule) {
132         Flags flags = FlagsFactory.getFlags();
133         if (flags.getMeasurementJobDeleteExpiredKillSwitch()) {
134             LoggerFactory.getMeasurementLogger()
135                     .e("DeleteExpiredJobService is disabled, skip scheduling");
136             return;
137         }
138 
139         final JobScheduler jobScheduler = context.getSystemService(JobScheduler.class);
140         if (jobScheduler == null) {
141             LoggerFactory.getMeasurementLogger().e("JobScheduler not found");
142             return;
143         }
144 
145         final JobInfo scheduledJob = jobScheduler.getPendingJob(MEASUREMENT_DELETE_EXPIRED_JOB_ID);
146         // Schedule if it hasn't been scheduled already or force rescheduling
147         JobInfo jobInfo = buildJobInfo(context, flags);
148         if (forceSchedule || !jobInfo.equals(scheduledJob)) {
149             schedule(jobScheduler, jobInfo);
150             LoggerFactory.getMeasurementLogger().d("Scheduled DeleteExpiredJobService");
151         } else {
152             LoggerFactory.getMeasurementLogger()
153                     .d("DeleteExpiredJobService already scheduled, skipping reschedule");
154         }
155     }
156 
buildJobInfo(Context context, Flags flags)157     private static JobInfo buildJobInfo(Context context, Flags flags) {
158         return new JobInfo.Builder(
159                         MEASUREMENT_DELETE_EXPIRED_JOB_ID,
160                         new ComponentName(context, DeleteExpiredJobService.class))
161                 .setRequiresDeviceIdle(flags.getMeasurementDeleteExpiredJobRequiresDeviceIdle())
162                 .setPeriodic(flags.getMeasurementDeleteExpiredJobPeriodMs())
163                 .setPersisted(flags.getMeasurementDeleteExpiredJobPersisted())
164                 .build();
165     }
166 
skipAndCancelBackgroundJob( final JobParameters params, int skipReason, boolean doRecord)167     private boolean skipAndCancelBackgroundJob(
168             final JobParameters params, int skipReason, boolean doRecord) {
169         final JobScheduler jobScheduler = this.getSystemService(JobScheduler.class);
170         if (jobScheduler != null) {
171             jobScheduler.cancel(MEASUREMENT_DELETE_EXPIRED_JOB_ID);
172         }
173 
174         if (doRecord) {
175             AdServicesJobServiceLogger.getInstance()
176                     .recordJobSkipped(MEASUREMENT_DELETE_EXPIRED_JOB_ID, skipReason);
177         }
178 
179         // Tell the JobScheduler that the job has completed and does not need to be rescheduled.
180         jobFinished(params, false);
181 
182         // Returning false means that this job has completed its work.
183         return false;
184     }
185 }
186