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.DEBUG_REPORTING;
20 import static com.android.adservices.spe.AdServicesJobInfo.MEASUREMENT_DEBUG_REPORT_JOB;
21 
22 import android.app.job.JobInfo;
23 import android.app.job.JobParameters;
24 import android.app.job.JobScheduler;
25 import android.app.job.JobService;
26 import android.content.ComponentName;
27 import android.content.Context;
28 
29 import com.android.adservices.LogUtil;
30 import com.android.adservices.LoggerFactory;
31 import com.android.adservices.concurrency.AdServicesExecutors;
32 import com.android.adservices.data.measurement.DatastoreManager;
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.aggregation.AggregateEncryptionKeyManager;
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.util.concurrent.Future;
46 
47 /**
48  * Main service for scheduling debug reporting jobs. The actual job execution logic is part of
49  * {@link EventReportingJobHandler } and {@link AggregateReportingJobHandler}
50  */
51 public final class DebugReportingJobService extends JobService {
52     private static final ListeningExecutorService sBlockingExecutor =
53             AdServicesExecutors.getBlockingExecutor();
54     static final int DEBUG_REPORT_JOB_ID = MEASUREMENT_DEBUG_REPORT_JOB.getJobId();
55     private Future mExecutorFuture;
56 
57     @Override
onStartJob(JobParameters params)58     public boolean onStartJob(JobParameters params) {
59         // Always ensure that the first thing this job does is check if it should be running, and
60         // cancel itself if it's not supposed to be.
61         if (ServiceCompatUtils.shouldDisableExtServicesJobOnTPlus(this)) {
62             LogUtil.d(
63                     "Disabling DebugReportingJobService job because it's running in ExtServices on"
64                             + " T+");
65             return skipAndCancelBackgroundJob(params);
66         }
67 
68         AdServicesJobServiceLogger.getInstance().recordOnStartJob(DEBUG_REPORT_JOB_ID);
69 
70         if (FlagsFactory.getFlags().getMeasurementJobDebugReportingKillSwitch()) {
71             LoggerFactory.getMeasurementLogger().e("DebugReportingJobService is disabled");
72             return skipAndCancelBackgroundJob(params);
73         }
74 
75         LoggerFactory.getMeasurementLogger().d("DebugReportingJobService.onStartJob");
76         mExecutorFuture =
77                 sBlockingExecutor.submit(
78                         () -> {
79                             sendReports();
80                             AdServicesJobServiceLogger.getInstance()
81                                     .recordJobFinished(
82                                             DEBUG_REPORT_JOB_ID,
83                                             /* isSuccessful */ true,
84                                             /* shouldRetry*/ false);
85                             jobFinished(params, /* wantsReschedule= */ false);
86                         });
87         return true;
88     }
89 
90     @Override
onStopJob(JobParameters params)91     public boolean onStopJob(JobParameters params) {
92         LoggerFactory.getMeasurementLogger().d("DebugReportingJobService.onStopJob");
93         boolean shouldRetry = true;
94         if (mExecutorFuture != null) {
95             shouldRetry = mExecutorFuture.cancel(/* mayInterruptIfRunning */ true);
96         }
97         AdServicesJobServiceLogger.getInstance()
98                 .recordOnStopJob(params, DEBUG_REPORT_JOB_ID, shouldRetry);
99         return shouldRetry;
100     }
101 
102     /** Schedules {@link DebugReportingJobService} */
103     @VisibleForTesting
schedule(JobScheduler jobScheduler, JobInfo job)104     static void schedule(JobScheduler jobScheduler, JobInfo job) {
105         jobScheduler.schedule(job);
106     }
107 
108     /**
109      * Schedule Debug Reporting Job if it is not already scheduled
110      *
111      * @param context the context
112      * @param forceSchedule flag to indicate whether to force rescheduling the job.
113      */
scheduleIfNeeded(Context context, boolean forceSchedule)114     public static void scheduleIfNeeded(Context context, boolean forceSchedule) {
115         Flags flags = FlagsFactory.getFlags();
116         if (flags.getMeasurementJobDebugReportingKillSwitch()) {
117             LoggerFactory.getMeasurementLogger()
118                     .d("DebugReportingJobService is disabled, skip scheduling");
119             return;
120         }
121 
122         final JobScheduler jobScheduler = context.getSystemService(JobScheduler.class);
123         if (jobScheduler == null) {
124             LoggerFactory.getMeasurementLogger().e("JobScheduler not found");
125             return;
126         }
127 
128         final JobInfo scheduledJobInfo = jobScheduler.getPendingJob(DEBUG_REPORT_JOB_ID);
129         // Schedule if it hasn't been scheduled already or force rescheduling
130         JobInfo job = buildJobInfo(context, flags);
131         if (forceSchedule || !job.equals(scheduledJobInfo)) {
132             schedule(jobScheduler, job);
133             LoggerFactory.getMeasurementLogger().d("Scheduled DebugReportingJobService");
134         } else {
135             LoggerFactory.getMeasurementLogger()
136                     .d("DebugReportingJobService already scheduled, skipping reschedule");
137         }
138     }
139 
buildJobInfo(Context context, Flags flags)140     private static JobInfo buildJobInfo(Context context, Flags flags) {
141         return new JobInfo.Builder(
142                         DEBUG_REPORT_JOB_ID,
143                         new ComponentName(context, DebugReportingJobService.class))
144                 .setRequiredNetworkType(flags.getMeasurementDebugReportingJobRequiredNetworkType())
145                 .build();
146     }
147 
skipAndCancelBackgroundJob(final JobParameters params)148     private boolean skipAndCancelBackgroundJob(final JobParameters params) {
149         final JobScheduler jobScheduler = this.getSystemService(JobScheduler.class);
150         if (jobScheduler != null) {
151             jobScheduler.cancel(DEBUG_REPORT_JOB_ID);
152         }
153 
154         // Tell the JobScheduler that the job has completed and does not need to be rescheduled.
155         jobFinished(params, /* wantsReschedule= */ false);
156 
157         // Returning false means that this job has completed its work.
158         return false;
159     }
160 
161     @VisibleForTesting
sendReports()162     void sendReports() {
163         final JobLockHolder lock = JobLockHolder.getInstance(DEBUG_REPORTING);
164         if (lock.tryLock()) {
165             try {
166                 DatastoreManager datastoreManager =
167                         DatastoreManagerFactory.getDatastoreManager(getApplicationContext());
168                 new EventReportingJobHandler(
169                                 datastoreManager,
170                                 FlagsFactory.getFlags(),
171                                 AdServicesLoggerImpl.getInstance(),
172                                 ReportingStatus.ReportType.DEBUG_EVENT,
173                                 ReportingStatus.UploadMethod.REGULAR,
174                                 getApplicationContext())
175                         .setIsDebugInstance(true)
176                         .performScheduledPendingReportsInWindow(0, 0);
177                 new AggregateReportingJobHandler(
178                                 datastoreManager,
179                                 new AggregateEncryptionKeyManager(
180                                         datastoreManager, getApplicationContext()),
181                                 FlagsFactory.getFlags(),
182                                 AdServicesLoggerImpl.getInstance(),
183                                 ReportingStatus.ReportType.DEBUG_AGGREGATE,
184                                 ReportingStatus.UploadMethod.REGULAR,
185                                 getApplicationContext())
186                         .setIsDebugInstance(true)
187                         .performScheduledPendingReportsInWindow(0, 0);
188                 return;
189             } finally {
190                 lock.unlock();
191             }
192         }
193         LoggerFactory.getMeasurementLogger().d("DebugReportingJobService did not acquire the lock");
194     }
195 
196     @VisibleForTesting
getFutureForTesting()197     Future getFutureForTesting() {
198         return mExecutorFuture;
199     }
200 }
201