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.common.util.concurrent.ListeningExecutorService;
46 
47 import java.util.concurrent.Future;
48 
49 /**
50  * Main service for scheduling aggregate reporting jobs. The actual job execution logic is part of
51  * {@link AggregateReportingJobHandler}
52  */
53 public final class AggregateFallbackReportingJobService extends JobService {
54     private static final int MEASUREMENT_AGGREGATE_FALLBACK_REPORTING_JOB_ID =
55             MEASUREMENT_AGGREGATE_FALLBACK_REPORTING_JOB.getJobId();
56 
57     private static final ListeningExecutorService sBlockingExecutor =
58             AdServicesExecutors.getBlockingExecutor();
59     private Future mExecutorFuture;
60 
61     @Override
onStartJob(JobParameters params)62     public boolean onStartJob(JobParameters params) {
63         // Always ensure that the first thing this job does is check if it should be running, and
64         // cancel itself if it's not supposed to be.
65         if (ServiceCompatUtils.shouldDisableExtServicesJobOnTPlus(this)) {
66             LogUtil.d(
67                     "Disabling AggregateFallbackReportingJobService job because it's running in"
68                             + " ExtServices on T+");
69             return skipAndCancelBackgroundJob(params, /* skipReason=*/ 0, /* doRecord=*/ false);
70         }
71 
72         AdServicesJobServiceLogger.getInstance()
73                 .recordOnStartJob(MEASUREMENT_AGGREGATE_FALLBACK_REPORTING_JOB_ID);
74 
75         if (FlagsFactory.getFlags().getMeasurementJobAggregateFallbackReportingKillSwitch()) {
76             LoggerFactory.getMeasurementLogger()
77                     .e("AggregateFallbackReportingJobService is disabled");
78             return skipAndCancelBackgroundJob(
79                     params,
80                     AD_SERVICES_BACKGROUND_JOBS_EXECUTION_REPORTED__EXECUTION_RESULT_CODE__SKIP_FOR_KILL_SWITCH_ON,
81                     /* doRecord=*/ true);
82         }
83 
84         LoggerFactory.getMeasurementLogger().d("AggregateFallbackReportingJobService.onStartJob");
85         mExecutorFuture =
86                 sBlockingExecutor.submit(
87                         () -> {
88                             processPendingReports();
89 
90                             AdServicesJobServiceLogger.getInstance()
91                                     .recordJobFinished(
92                                             MEASUREMENT_AGGREGATE_FALLBACK_REPORTING_JOB_ID,
93                                             /* isSuccessful= */ true,
94                                             /* shouldRetry= */ false);
95 
96                             jobFinished(params, /* wantsReschedule= */ false);
97                         });
98         return true;
99     }
100 
101     @VisibleForTesting
processPendingReports()102     void processPendingReports() {
103         final JobLockHolder lock = JobLockHolder.getInstance(AGGREGATE_REPORTING);
104         if (lock.tryLock()) {
105             try {
106                 final long windowStartTime =
107                         System.currentTimeMillis()
108                                 - FlagsFactory.getFlags()
109                                         .getMeasurementMaxAggregateReportUploadRetryWindowMs();
110                 final long windowEndTime =
111                         System.currentTimeMillis()
112                                 - AdServicesConfig
113                                         .getMeasurementAggregateMainReportingJobPeriodMs();
114                 DatastoreManager datastoreManager =
115                         DatastoreManagerFactory.getDatastoreManager(getApplicationContext());
116                 new AggregateReportingJobHandler(
117                                 datastoreManager,
118                                 new AggregateEncryptionKeyManager(
119                                         datastoreManager, getApplicationContext()),
120                                 FlagsFactory.getFlags(),
121                                 AdServicesLoggerImpl.getInstance(),
122                                 ReportingStatus.ReportType.AGGREGATE,
123                                 ReportingStatus.UploadMethod.FALLBACK,
124                                 getApplicationContext())
125                         .performScheduledPendingReportsInWindow(windowStartTime, windowEndTime);
126                 return;
127             } finally {
128                 lock.unlock();
129             }
130         }
131         LoggerFactory.getMeasurementLogger()
132                 .d("AggregateFallbackReportingJobService did not acquire the lock");
133     }
134 
135     @Override
onStopJob(JobParameters params)136     public boolean onStopJob(JobParameters params) {
137         LoggerFactory.getMeasurementLogger().d("AggregateFallbackReportingJobService.onStopJob");
138         boolean shouldRetry = true;
139         if (mExecutorFuture != null) {
140             shouldRetry = mExecutorFuture.cancel(/* mayInterruptIfRunning */ true);
141         }
142         AdServicesJobServiceLogger.getInstance()
143                 .recordOnStopJob(
144                         params, MEASUREMENT_AGGREGATE_FALLBACK_REPORTING_JOB_ID, shouldRetry);
145         return shouldRetry;
146     }
147 
148     /** Schedules {@link AggregateFallbackReportingJobService} */
149     @VisibleForTesting
schedule(JobScheduler jobScheduler, JobInfo jobInfo)150     static void schedule(JobScheduler jobScheduler, JobInfo jobInfo) {
151         jobScheduler.schedule(jobInfo);
152     }
153 
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      */
scheduleIfNeeded(Context context, boolean forceSchedule)173     public static void scheduleIfNeeded(Context context, boolean forceSchedule) {
174         Flags flags = FlagsFactory.getFlags();
175         if (flags.getMeasurementJobAggregateFallbackReportingKillSwitch()) {
176             LoggerFactory.getMeasurementLogger()
177                     .d("AggregateFallbackReportingJobService is disabled, skip scheduling");
178             return;
179         }
180 
181         final JobScheduler jobScheduler = context.getSystemService(JobScheduler.class);
182         if (jobScheduler == null) {
183             LoggerFactory.getMeasurementLogger().e("JobScheduler not found");
184             return;
185         }
186 
187         final JobInfo scheduledJob =
188                 jobScheduler.getPendingJob(MEASUREMENT_AGGREGATE_FALLBACK_REPORTING_JOB_ID);
189         // Schedule if it hasn't been scheduled already or force rescheduling
190         JobInfo jobInfo = buildJobInfo(context, flags);
191         if (forceSchedule || !jobInfo.equals(scheduledJob)) {
192             schedule(jobScheduler, jobInfo);
193             LoggerFactory.getMeasurementLogger()
194                     .d("Scheduled AggregateFallbackReportingJobService");
195         } else {
196             LoggerFactory.getMeasurementLogger()
197                     .d(
198                             "AggregateFallbackReportingJobService already scheduled, skipping"
199                                     + " reschedule");
200         }
201     }
202 
skipAndCancelBackgroundJob( final JobParameters params, int skipReason, boolean doRecord)203     private boolean skipAndCancelBackgroundJob(
204             final JobParameters params, int skipReason, boolean doRecord) {
205         final JobScheduler jobScheduler = this.getSystemService(JobScheduler.class);
206         if (jobScheduler != null) {
207             jobScheduler.cancel(MEASUREMENT_AGGREGATE_FALLBACK_REPORTING_JOB_ID);
208         }
209 
210         if (doRecord) {
211             AdServicesJobServiceLogger.getInstance()
212                     .recordJobSkipped(MEASUREMENT_AGGREGATE_FALLBACK_REPORTING_JOB_ID, skipReason);
213         }
214 
215         // Tell the JobScheduler that the job has completed and does not need to be rescheduled.
216         jobFinished(params, false);
217 
218         // Returning false means that this job has completed its work.
219         return false;
220     }
221 
222     @VisibleForTesting
getFutureForTesting()223     Future getFutureForTesting() {
224         return mExecutorFuture;
225     }
226 }
227