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.VERBOSE_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_VERBOSE_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.util.JobLockHolder;
39 import com.android.adservices.service.stats.AdServicesLoggerImpl;
40 import com.android.adservices.spe.AdServicesJobServiceLogger;
41 import com.android.internal.annotations.VisibleForTesting;
42 
43 import com.google.common.util.concurrent.ListeningExecutorService;
44 
45 import java.time.Clock;
46 import java.time.Instant;
47 import java.util.concurrent.Future;
48 
49 /**
50  * Fallback service for scheduling debug reporting jobs. This runs periodically to handle any
51  * reports that the {@link DebugReportingJobService } failed/missed. The actual job execution logic
52  * is part of {@link DebugReportingJobHandler }.
53  */
54 public class VerboseDebugReportingFallbackJobService extends JobService {
55 
56     private static final int MEASUREMENT_VERBOSE_DEBUG_REPORTING_FALLBACK_JOB_ID =
57             MEASUREMENT_VERBOSE_DEBUG_REPORTING_FALLBACK_JOB.getJobId();
58 
59     private static final ListeningExecutorService sBlockingExecutor =
60             AdServicesExecutors.getBlockingExecutor();
61 
62     private Future mExecutorFuture;
63 
64     @Override
onStartJob(JobParameters params)65     public boolean onStartJob(JobParameters params) {
66         // Always ensure that the first thing this job does is check if it should be running, and
67         // cancel itself if it's not supposed to be.
68         if (ServiceCompatUtils.shouldDisableExtServicesJobOnTPlus(this)) {
69             LogUtil.d(
70                     "Disabling VerboseDebugReportingFallbackJobService job because it's running in"
71                             + " ExtServices on T+");
72             return skipAndCancelBackgroundJob(params, /* skipReason=*/ 0, /* doRecord=*/ false);
73         }
74 
75         AdServicesJobServiceLogger.getInstance()
76                 .recordOnStartJob(MEASUREMENT_VERBOSE_DEBUG_REPORTING_FALLBACK_JOB_ID);
77 
78         if (FlagsFactory.getFlags().getMeasurementVerboseDebugReportingFallbackJobKillSwitch()) {
79             LoggerFactory.getMeasurementLogger()
80                     .e("VerboseDebugReportingFallbackJobService 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                         "VerboseDebugReportingFallbackJobService.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_VERBOSE_DEBUG_REPORTING_FALLBACK_JOB_ID,
100                                             /* isSuccessful */ true,
101                                             shouldRetry);
102 
103                             jobFinished(params, false);
104                         });
105         return true;
106     }
107 
108     @Override
onStopJob(JobParameters params)109     public boolean onStopJob(JobParameters params) {
110         LoggerFactory.getMeasurementLogger().d("VerboseDebugReportingJobService.onStopJob");
111         boolean shouldRetry = true;
112         if (mExecutorFuture != null) {
113             shouldRetry = mExecutorFuture.cancel(/* mayInterruptIfRunning */ true);
114         }
115         AdServicesJobServiceLogger.getInstance()
116                 .recordOnStopJob(
117                         params, MEASUREMENT_VERBOSE_DEBUG_REPORTING_FALLBACK_JOB_ID, shouldRetry);
118         return shouldRetry;
119     }
120 
121     @VisibleForTesting
schedule(JobScheduler jobScheduler, JobInfo job)122     protected static void schedule(JobScheduler jobScheduler, JobInfo job) {
123         jobScheduler.schedule(job);
124     }
125 
126     /**
127      * Schedule Verbose Debug Reporting Fallback Job Service if it is not already scheduled.
128      *
129      * @param context the context
130      * @param forceSchedule flag to indicate whether to force rescheduling the job.
131      */
scheduleIfNeeded(Context context, boolean forceSchedule)132     public static void scheduleIfNeeded(Context context, boolean forceSchedule) {
133         Flags flags = FlagsFactory.getFlags();
134         if (flags.getMeasurementVerboseDebugReportingFallbackJobKillSwitch()) {
135             LoggerFactory.getMeasurementLogger()
136                     .e("VerboseDebugReportingFallbackJobService is disabled, skip scheduling");
137             return;
138         }
139 
140         final JobScheduler jobScheduler = context.getSystemService(JobScheduler.class);
141         if (jobScheduler == null) {
142             LoggerFactory.getMeasurementLogger().e("JobScheduler not found");
143             return;
144         }
145 
146         final JobInfo scheduledJob =
147                 jobScheduler.getPendingJob(MEASUREMENT_VERBOSE_DEBUG_REPORTING_FALLBACK_JOB_ID);
148         JobInfo jobInfo = buildJobInfo(context, flags);
149         // Schedule if it hasn't been scheduled already or force rescheduling
150         if (forceSchedule || !jobInfo.equals(scheduledJob)) {
151             schedule(jobScheduler, jobInfo);
152             LoggerFactory.getMeasurementLogger()
153                     .d("Scheduled VerboseDebugReportingFallbackJobService");
154         } else {
155             LoggerFactory.getMeasurementLogger()
156                     .d(
157                             "VerboseDebugReportingFallbackJobService already scheduled, skipping"
158                                     + " reschedule");
159         }
160     }
161 
buildJobInfo(Context context, Flags flags)162     private static JobInfo buildJobInfo(Context context, Flags flags) {
163         return new JobInfo.Builder(
164                         MEASUREMENT_VERBOSE_DEBUG_REPORTING_FALLBACK_JOB_ID,
165                         new ComponentName(context, VerboseDebugReportingFallbackJobService.class))
166                 .setRequiredNetworkType(
167                         flags.getMeasurementVerboseDebugReportingJobRequiredNetworkType())
168                 .setPeriodic(flags.getMeasurementVerboseDebugReportingFallbackJobPeriodMs())
169                 .setPersisted(flags.getMeasurementVerboseDebugReportingFallbackJobPersisted())
170                 .build();
171     }
172 
skipAndCancelBackgroundJob( final JobParameters params, int skipReason, boolean doRecord)173     private boolean skipAndCancelBackgroundJob(
174             final JobParameters params, int skipReason, boolean doRecord) {
175         final JobScheduler jobScheduler = this.getSystemService(JobScheduler.class);
176         if (jobScheduler != null) {
177             jobScheduler.cancel(MEASUREMENT_VERBOSE_DEBUG_REPORTING_FALLBACK_JOB_ID);
178         }
179 
180         if (doRecord) {
181             AdServicesJobServiceLogger.getInstance()
182                     .recordJobSkipped(
183                             MEASUREMENT_VERBOSE_DEBUG_REPORTING_FALLBACK_JOB_ID, skipReason);
184         }
185 
186         // Tell the JobScheduler that the job is done and does not need to be rescheduled
187         jobFinished(params, false);
188 
189         // Returning false to reschedule this job.
190         return false;
191     }
192 
193     @VisibleForTesting
sendReports()194     void sendReports() {
195         final JobLockHolder lock = JobLockHolder.getInstance(VERBOSE_DEBUG_REPORTING);
196         if (lock.tryLock()) {
197             try {
198                 DatastoreManager datastoreManager =
199                         DatastoreManagerFactory.getDatastoreManager(getApplicationContext());
200                 new DebugReportingJobHandler(
201                                 datastoreManager,
202                                 FlagsFactory.getFlags(),
203                                 AdServicesLoggerImpl.getInstance(),
204                                 ReportingStatus.UploadMethod.FALLBACK,
205                                 getApplicationContext())
206                         .performScheduledPendingReports();
207                 return;
208             } finally {
209                 lock.unlock();
210             }
211         }
212         LoggerFactory.getMeasurementLogger()
213                 .d("VerboseDebugReportingFallbackJobService did not acquire the lock");
214     }
215 
216     @VisibleForTesting
getFutureForTesting()217     Future getFutureForTesting() {
218         return mExecutorFuture;
219     }
220 }
221