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.common; 18 19 import android.annotation.NonNull; 20 import android.content.Context; 21 22 import com.android.adservices.LoggerFactory; 23 import com.android.adservices.data.adselection.AdSelectionDatabase; 24 import com.android.adservices.data.adselection.AdSelectionEntryDao; 25 import com.android.adservices.service.FlagsFactory; 26 import com.android.internal.annotations.VisibleForTesting; 27 28 import java.time.Clock; 29 import java.time.Instant; 30 31 /** Utility class to perform Fledge maintenance tasks */ 32 public class FledgeMaintenanceTasksWorker { 33 private static final LoggerFactory.Logger sLogger = LoggerFactory.getFledgeLogger(); 34 @NonNull private AdSelectionEntryDao mAdSelectionEntryDao; 35 36 @VisibleForTesting FledgeMaintenanceTasksWorker(AdSelectionEntryDao adSelectionEntryDao)37 public FledgeMaintenanceTasksWorker(AdSelectionEntryDao adSelectionEntryDao) { 38 mAdSelectionEntryDao = adSelectionEntryDao; 39 } 40 FledgeMaintenanceTasksWorker(Context context)41 private FledgeMaintenanceTasksWorker(Context context) { 42 mAdSelectionEntryDao = AdSelectionDatabase.getInstance(context).adSelectionEntryDao(); 43 } 44 45 /** Creates a new instance of {@link FledgeMaintenanceTasksWorker}. */ create(@onNull Context context)46 public static FledgeMaintenanceTasksWorker create(@NonNull Context context) { 47 return new FledgeMaintenanceTasksWorker(context); 48 } 49 50 /** 51 * Clears all entries in the {@code ad_selection} table that are older than {@code 52 * expirationTime}. Then, clears all expired entries in the {@code buyer_decision_logic} as well 53 * as the {@code registered_ad_interactions} table. 54 */ clearExpiredAdSelectionData()55 public void clearExpiredAdSelectionData() { 56 Instant expirationTime = 57 Clock.systemUTC() 58 .instant() 59 .minusSeconds(FlagsFactory.getFlags().getAdSelectionExpirationWindowS()); 60 sLogger.v("Clearing expired Ad Selection data"); 61 mAdSelectionEntryDao.removeExpiredAdSelection(expirationTime); 62 63 sLogger.v("Clearing expired Buyer Decision Logic data "); 64 mAdSelectionEntryDao.removeExpiredBuyerDecisionLogic(); 65 66 sLogger.v("Clearing expired Registered Ad Interaction data "); 67 mAdSelectionEntryDao.removeExpiredRegisteredAdInteractions(); 68 } 69 } 70