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