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.EVENT_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_EVENT_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.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.util.JobLockHolder;
38 import com.android.adservices.service.stats.AdServicesLoggerImpl;
39 import com.android.adservices.spe.AdServicesJobServiceLogger;
40 import com.android.internal.annotations.VisibleForTesting;
41 
42 import com.google.common.util.concurrent.ListeningExecutorService;
43 
44 import java.util.concurrent.Future;
45 
46 /**
47  * Main service for scheduling event reporting jobs. The actual job execution logic is part of
48  * {@link EventReportingJobHandler}
49  */
50 public final class EventReportingJobService extends JobService {
51     private static final int MEASUREMENT_EVENT_MAIN_REPORTING_JOB_ID =
52             MEASUREMENT_EVENT_MAIN_REPORTING_JOB.getJobId();
53 
54     private static final ListeningExecutorService sBlockingExecutor =
55             AdServicesExecutors.getBlockingExecutor();
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 EventReportingJobService job because it's running in ExtServices on"
65                             + " T+");
66             return skipAndCancelBackgroundJob(params, /* skipReason=*/ 0, /* doRecord=*/ false);
67         }
68 
69         AdServicesJobServiceLogger.getInstance()
70                 .recordOnStartJob(MEASUREMENT_EVENT_MAIN_REPORTING_JOB_ID);
71 
72         if (FlagsFactory.getFlags().getMeasurementJobEventReportingKillSwitch()) {
73             LoggerFactory.getMeasurementLogger().e("EventReportingJobService is disabled");
74             return skipAndCancelBackgroundJob(
75                     params,
76                     AD_SERVICES_BACKGROUND_JOBS_EXECUTION_REPORTED__EXECUTION_RESULT_CODE__SKIP_FOR_KILL_SWITCH_ON,
77                     /* doRecord=*/ true);
78         }
79 
80         LoggerFactory.getMeasurementLogger().d("EventReportingJobService.onStartJob: ");
81         mExecutorFuture =
82                 sBlockingExecutor.submit(
83                         () -> {
84                             processPendingReports();
85 
86                             AdServicesJobServiceLogger.getInstance()
87                                     .recordJobFinished(
88                                             MEASUREMENT_EVENT_MAIN_REPORTING_JOB_ID,
89                                             /* isSuccessful= */ true,
90                                             /* shouldRetry= */ false);
91 
92                             jobFinished(params, /* wantsReschedule= */ false);
93                         });
94         return true;
95     }
96 
97     @VisibleForTesting
processPendingReports()98     void processPendingReports() {
99         final JobLockHolder lock = JobLockHolder.getInstance(EVENT_REPORTING);
100         if (lock.tryLock()) {
101             try {
102                 long maxEventReportUploadRetryWindowMs =
103                         FlagsFactory.getFlags().getMeasurementMaxEventReportUploadRetryWindowMs();
104                 new EventReportingJobHandler(
105                                 DatastoreManagerFactory.getDatastoreManager(
106                                         getApplicationContext()),
107                                 FlagsFactory.getFlags(),
108                                 AdServicesLoggerImpl.getInstance(),
109                                 ReportingStatus.ReportType.EVENT,
110                                 ReportingStatus.UploadMethod.REGULAR,
111                                 getApplicationContext())
112                         .performScheduledPendingReportsInWindow(
113                                 System.currentTimeMillis() - maxEventReportUploadRetryWindowMs,
114                                 System.currentTimeMillis());
115                 return;
116             } finally {
117                 lock.unlock();
118             }
119         }
120         LoggerFactory.getMeasurementLogger().d("EventReportingJobService did not acquire the lock");
121     }
122 
123     @Override
onStopJob(JobParameters params)124     public boolean onStopJob(JobParameters params) {
125         LoggerFactory.getMeasurementLogger().d("EventReportingJobService.onStopJob");
126         boolean shouldRetry = true;
127         if (mExecutorFuture != null) {
128             shouldRetry = mExecutorFuture.cancel(/* mayInterruptIfRunning */ true);
129         }
130         AdServicesJobServiceLogger.getInstance()
131                 .recordOnStopJob(params, MEASUREMENT_EVENT_MAIN_REPORTING_JOB_ID, shouldRetry);
132         return shouldRetry;
133     }
134 
135     /** Schedules {@link EventReportingJobService} */
136     @VisibleForTesting
schedule(JobScheduler jobScheduler, JobInfo job)137     static void schedule(JobScheduler jobScheduler, JobInfo job) {
138         jobScheduler.schedule(job);
139     }
140 
141     /**
142      * Schedule Event Reporting Job if it is not already scheduled
143      *
144      * @param context the context
145      * @param forceSchedule flag to indicate whether to force rescheduling the job.
146      */
scheduleIfNeeded(Context context, boolean forceSchedule)147     public static void scheduleIfNeeded(Context context, boolean forceSchedule) {
148         Flags flags = FlagsFactory.getFlags();
149         if (flags.getMeasurementJobEventReportingKillSwitch()) {
150             LoggerFactory.getMeasurementLogger()
151                     .d("EventReportingJobService is disabled, skip scheduling");
152             return;
153         }
154 
155         final JobScheduler jobScheduler = context.getSystemService(JobScheduler.class);
156         if (jobScheduler == null) {
157             LoggerFactory.getMeasurementLogger().e("JobScheduler not found");
158             return;
159         }
160 
161         final JobInfo scheduledJob =
162                 jobScheduler.getPendingJob(MEASUREMENT_EVENT_MAIN_REPORTING_JOB_ID);
163         // Schedule if it hasn't been scheduled already or force rescheduling
164         JobInfo jobInfo = buildJobInfo(context, flags);
165         if (forceSchedule || !jobInfo.equals(scheduledJob)) {
166             schedule(jobScheduler, jobInfo);
167             LoggerFactory.getMeasurementLogger().d("Scheduled EventReportingJobService");
168         } else {
169             LoggerFactory.getMeasurementLogger()
170                     .d("EventReportingJobService already scheduled, skipping reschedule");
171         }
172     }
173 
buildJobInfo(Context context, Flags flags)174     private static JobInfo buildJobInfo(Context context, Flags flags) {
175         return new JobInfo.Builder(
176                         MEASUREMENT_EVENT_MAIN_REPORTING_JOB_ID,
177                         new ComponentName(context, EventReportingJobService.class))
178                 .setRequiresBatteryNotLow(
179                         flags.getMeasurementEventReportingJobRequiredBatteryNotLow())
180                 .setRequiredNetworkType(flags.getMeasurementEventReportingJobRequiredNetworkType())
181                 .setPeriodic(flags.getMeasurementEventMainReportingJobPeriodMs())
182                 .setPersisted(flags.getMeasurementEventReportingJobPersisted())
183                 .build();
184     }
185 
skipAndCancelBackgroundJob( final JobParameters params, int skipReason, boolean doRecord)186     private boolean skipAndCancelBackgroundJob(
187             final JobParameters params, int skipReason, boolean doRecord) {
188         final JobScheduler jobScheduler = this.getSystemService(JobScheduler.class);
189         if (jobScheduler != null) {
190             jobScheduler.cancel(MEASUREMENT_EVENT_MAIN_REPORTING_JOB_ID);
191         }
192 
193         if (doRecord) {
194             AdServicesJobServiceLogger.getInstance()
195                     .recordJobSkipped(MEASUREMENT_EVENT_MAIN_REPORTING_JOB_ID, skipReason);
196         }
197 
198         // Tell the JobScheduler that the job has completed and does not need to be rescheduled.
199         jobFinished(params, false);
200 
201         // Returning false means that this job has completed its work.
202         return false;
203     }
204 
205     @VisibleForTesting
getFutureForTesting()206     Future getFutureForTesting() {
207         return mExecutorFuture;
208     }
209 }
210