1 /*
2  * Copyright (C) 2023 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.DEBUG_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_DEBUG_REPORTING_FALLBACK_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.common.util.concurrent.ListeningExecutorService;
45 
46 import java.time.Clock;
47 import java.time.Instant;
48 import java.util.concurrent.Future;
49 
50 /**
51  * Fallback service for scheduling debug reporting jobs. This runs periodically to handle any
52  * reports that the {@link DebugReportingJobService } failed/missed. The actual job execution logic
53  * is part of {@link EventReportingJobHandler } and {@link AggregateReportingJobHandler}.
54  */
55 public class DebugReportingFallbackJobService extends JobService {
56 
57     private static final int MEASUREMENT_DEBUG_REPORTING_FALLBACK_JOB_ID =
58             MEASUREMENT_DEBUG_REPORTING_FALLBACK_JOB.getJobId();
59 
60     private static final ListeningExecutorService sBlockingExecutor =
61             AdServicesExecutors.getBlockingExecutor();
62 
63     private Future mExecutorFuture;
64 
65     @Override
onStartJob(JobParameters params)66     public boolean onStartJob(JobParameters params) {
67         // Always ensure that the first thing this job does is check if it should be running, and
68         // cancel itself if it's not supposed to be.
69         if (ServiceCompatUtils.shouldDisableExtServicesJobOnTPlus(this)) {
70             LogUtil.d(
71                     "Disabling DebugReportingFallbackJobService job because it's running in"
72                             + " ExtServices on T+");
73             return skipAndCancelBackgroundJob(params, /* skipReason=*/ 0, /* doRecord=*/ false);
74         }
75 
76         AdServicesJobServiceLogger.getInstance()
77                 .recordOnStartJob(MEASUREMENT_DEBUG_REPORTING_FALLBACK_JOB_ID);
78 
79         if (FlagsFactory.getFlags().getMeasurementDebugReportingFallbackJobKillSwitch()) {
80             LoggerFactory.getMeasurementLogger().e("DebugReportingFallbackJobService is disabled.");
81             return skipAndCancelBackgroundJob(
82                     params,
83                     AD_SERVICES_BACKGROUND_JOBS_EXECUTION_REPORTED__EXECUTION_RESULT_CODE__SKIP_FOR_KILL_SWITCH_ON,
84                     /* doRecord */ true);
85         }
86 
87         Instant jobStartTime = Clock.systemUTC().instant();
88         LoggerFactory.getMeasurementLogger()
89                 .d(
90                         "DebugReportingFallbackJobService.onStartJob " + "at %s",
91                         jobStartTime.toString());
92         mExecutorFuture =
93                 sBlockingExecutor.submit(
94                         () -> {
95                             sendReports();
96                             boolean shouldRetry = false;
97                             AdServicesJobServiceLogger.getInstance()
98                                     .recordJobFinished(
99                                             MEASUREMENT_DEBUG_REPORTING_FALLBACK_JOB_ID,
100                                             /* isSuccessful */ true,
101                                             shouldRetry);
102                             jobFinished(params, false);
103                         });
104         return true;
105     }
106 
107     @Override
onStopJob(JobParameters params)108     public boolean onStopJob(JobParameters params) {
109         LoggerFactory.getMeasurementLogger().d("DebugReportingJobService.onStopJob");
110         boolean shouldRetry = true;
111         if (mExecutorFuture != null) {
112             shouldRetry = mExecutorFuture.cancel(/* mayInterruptIfRunning */ true);
113         }
114         AdServicesJobServiceLogger.getInstance()
115                 .recordOnStopJob(params, MEASUREMENT_DEBUG_REPORTING_FALLBACK_JOB_ID, shouldRetry);
116         return shouldRetry;
117     }
118 
119     @VisibleForTesting
schedule(JobScheduler jobScheduler, JobInfo job)120     protected static void schedule(JobScheduler jobScheduler, JobInfo job) {
121         jobScheduler.schedule(job);
122     }
123 
124     /**
125      * Schedule Debug Reporting Fallback Job Service if it is not already scheduled.
126      *
127      * @param context the context
128      * @param forceSchedule flag to indicate whether to force rescheduling the job.
129      */
scheduleIfNeeded(Context context, boolean forceSchedule)130     public static void scheduleIfNeeded(Context context, boolean forceSchedule) {
131         Flags flags = FlagsFactory.getFlags();
132         if (flags.getMeasurementDebugReportingFallbackJobKillSwitch()) {
133             LoggerFactory.getMeasurementLogger()
134                     .e("DebugReportingFallbackJobService is disabled, skip scheduling");
135             return;
136         }
137 
138         final JobScheduler jobScheduler = context.getSystemService(JobScheduler.class);
139         if (jobScheduler == null) {
140             LoggerFactory.getMeasurementLogger().e("JobScheduler not found");
141             return;
142         }
143 
144         final JobInfo scheduledJob =
145                 jobScheduler.getPendingJob(MEASUREMENT_DEBUG_REPORTING_FALLBACK_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 DebugReportingFallbackJobService");
151         } else {
152             LoggerFactory.getMeasurementLogger()
153                     .d("DebugReportingFallbackJobService already scheduled, skipping reschedule");
154         }
155     }
156 
skipAndCancelBackgroundJob( final JobParameters params, int skipReason, boolean doRecord)157     private boolean skipAndCancelBackgroundJob(
158             final JobParameters params, int skipReason, boolean doRecord) {
159         final JobScheduler jobScheduler = this.getSystemService(JobScheduler.class);
160         if (jobScheduler != null) {
161             jobScheduler.cancel(MEASUREMENT_DEBUG_REPORTING_FALLBACK_JOB_ID);
162         }
163 
164         if (doRecord) {
165             AdServicesJobServiceLogger.getInstance()
166                     .recordJobSkipped(MEASUREMENT_DEBUG_REPORTING_FALLBACK_JOB_ID, skipReason);
167         }
168 
169         // Tell the JobScheduler that the job is done and does not need to be rescheduled
170         jobFinished(params, false);
171 
172         // Returning false to reschedule this job.
173         return false;
174     }
175 
buildJobInfo(Context context, Flags flags)176     private static JobInfo buildJobInfo(Context context, Flags flags) {
177         return new JobInfo.Builder(
178                         MEASUREMENT_DEBUG_REPORTING_FALLBACK_JOB_ID,
179                         new ComponentName(context, DebugReportingFallbackJobService.class))
180                 .setRequiredNetworkType(
181                         flags.getMeasurementDebugReportingFallbackJobRequiredNetworkType())
182                 .setPeriodic(flags.getMeasurementDebugReportingFallbackJobPeriodMs())
183                 .setPersisted(flags.getMeasurementDebugReportingFallbackJobPersisted())
184                 .build();
185     }
186 
187     @VisibleForTesting
sendReports()188     void sendReports() {
189         final JobLockHolder lock = JobLockHolder.getInstance(DEBUG_REPORTING);
190         if (lock.tryLock()) {
191             try {
192                 DatastoreManager datastoreManager =
193                         DatastoreManagerFactory.getDatastoreManager(getApplicationContext());
194                 new EventReportingJobHandler(
195                                 datastoreManager,
196                                 FlagsFactory.getFlags(),
197                                 AdServicesLoggerImpl.getInstance(),
198                                 ReportingStatus.ReportType.DEBUG_EVENT,
199                                 ReportingStatus.UploadMethod.FALLBACK,
200                                 getApplicationContext())
201                         .setIsDebugInstance(true)
202                         .performScheduledPendingReportsInWindow(0, 0);
203                 new AggregateReportingJobHandler(
204                                 datastoreManager,
205                                 new AggregateEncryptionKeyManager(
206                                         datastoreManager, getApplicationContext()),
207                                 FlagsFactory.getFlags(),
208                                 AdServicesLoggerImpl.getInstance(),
209                                 ReportingStatus.ReportType.DEBUG_AGGREGATE,
210                                 ReportingStatus.UploadMethod.FALLBACK,
211                                 getApplicationContext())
212                         .setIsDebugInstance(true)
213                         .performScheduledPendingReportsInWindow(0, 0);
214                 return;
215             } finally {
216                 lock.unlock();
217             }
218         }
219         LoggerFactory.getMeasurementLogger()
220                 .d("DebugReportingFallbackJobService did not acquire the lock");
221     }
222 
223     @VisibleForTesting
getFutureForTesting()224     Future getFutureForTesting() {
225         return mExecutorFuture;
226     }
227 }
228