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.adselection; 18 19 import androidx.annotation.NonNull; 20 21 import com.android.adservices.LoggerFactory; 22 import com.android.adservices.data.adselection.AdSelectionEntryDao; 23 import com.android.adservices.data.adselection.AppInstallDao; 24 import com.android.adservices.data.adselection.FrequencyCapDao; 25 import com.android.adservices.service.Flags; 26 import com.android.adservices.service.common.BinderFlagReader; 27 import com.android.adservices.service.common.FrequencyCapAdDataValidator; 28 import com.android.adservices.service.common.FrequencyCapAdDataValidatorImpl; 29 import com.android.adservices.service.common.FrequencyCapAdDataValidatorNoOpImpl; 30 31 import java.time.Clock; 32 import java.util.Objects; 33 34 /** Factory for implementations of the ad filtering feature interfaces. */ 35 public final class AdFilteringFeatureFactory { 36 37 private static final LoggerFactory.Logger sLogger = LoggerFactory.getFledgeLogger(); 38 private final boolean mIsFledgeFrequencyCapFilteringEnabled; 39 private final boolean mIsFledgeAppInstallFilteringEnabled; 40 private final int mHistogramAbsoluteMaxTotalEventCount; 41 private final int mHistogramLowerMaxTotalEventCount; 42 private final int mHistogramAbsoluteMaxPerBuyerEventCount; 43 private final int mHistogramLowerMaxPerBuyerEventCount; 44 private final AppInstallDao mAppInstallDao; 45 private final FrequencyCapDao mFrequencyCapDao; 46 private final boolean mShouldUseUnifiedTables; 47 AdFilteringFeatureFactory( AppInstallDao appInstallDao, FrequencyCapDao frequencyCapDao, Flags flags)48 public AdFilteringFeatureFactory( 49 AppInstallDao appInstallDao, FrequencyCapDao frequencyCapDao, Flags flags) { 50 mIsFledgeFrequencyCapFilteringEnabled = 51 BinderFlagReader.readFlag(flags::getFledgeFrequencyCapFilteringEnabled); 52 mIsFledgeAppInstallFilteringEnabled = 53 BinderFlagReader.readFlag(flags::getFledgeAppInstallFilteringEnabled); 54 mHistogramAbsoluteMaxTotalEventCount = 55 BinderFlagReader.readFlag( 56 flags::getFledgeAdCounterHistogramAbsoluteMaxTotalEventCount); 57 mHistogramLowerMaxTotalEventCount = 58 BinderFlagReader.readFlag( 59 flags::getFledgeAdCounterHistogramLowerMaxTotalEventCount); 60 mHistogramAbsoluteMaxPerBuyerEventCount = 61 BinderFlagReader.readFlag( 62 flags::getFledgeAdCounterHistogramAbsoluteMaxPerBuyerEventCount); 63 mHistogramLowerMaxPerBuyerEventCount = 64 BinderFlagReader.readFlag( 65 flags::getFledgeAdCounterHistogramLowerMaxPerBuyerEventCount); 66 mShouldUseUnifiedTables = 67 BinderFlagReader.readFlag(flags::getFledgeOnDeviceAuctionShouldUseUnifiedTables); 68 69 mAppInstallDao = appInstallDao; 70 mFrequencyCapDao = frequencyCapDao; 71 sLogger.v( 72 "Initializing AdFilteringFeatureFactory with frequency cap filtering %s and app" 73 + " install filtering %s", 74 mIsFledgeFrequencyCapFilteringEnabled ? "enabled" : "disabled", 75 mIsFledgeAppInstallFilteringEnabled ? "enabled" : "disabled"); 76 } 77 78 /** 79 * Returns the correct {@link FrequencyCapAdFilterer} implementation to use based on the given 80 * {@link Flags}. 81 * 82 * @return an instance of {@link FrequencyCapAdFiltererImpl} if frequency cap filtering is 83 * enabled and an instance of {@link FrequencyCapAdFiltererNoOpImpl} otherwise 84 */ getFrequencyCapAdFilterer()85 public FrequencyCapAdFilterer getFrequencyCapAdFilterer() { 86 if (mIsFledgeFrequencyCapFilteringEnabled) { 87 return new FrequencyCapAdFiltererImpl(mFrequencyCapDao, Clock.systemUTC()); 88 } else { 89 return new FrequencyCapAdFiltererNoOpImpl(); 90 } 91 } 92 93 /** 94 * Returns the correct {@link AppInstallAdFilterer} implementation to use based on the given 95 * {@link Flags}. 96 * 97 * @return an instance of {@link AppInstallAdFiltererImpl} if app install filtering is enabled 98 * and an instance of {@link AppInstallAdFiltererNoOpImpl} otherwise 99 */ getAppInstallAdFilterer()100 public AppInstallAdFilterer getAppInstallAdFilterer() { 101 if (mIsFledgeAppInstallFilteringEnabled) { 102 return new AppInstallAdFiltererImpl(mAppInstallDao, Clock.systemUTC()); 103 } else { 104 return new AppInstallAdFiltererNoOpImpl(); 105 } 106 } 107 108 /** 109 * Gets the {@link AdCounterKeyCopier} implementation to use, dependent on whether the ad 110 * filtering features is enabled. 111 * 112 * @return an {@link AdCounterKeyCopierImpl} instance if the ad filtering feature is enabled, or 113 * an {@link AdCounterKeyCopierNoOpImpl} instance otherwise 114 */ getAdCounterKeyCopier()115 public AdCounterKeyCopier getAdCounterKeyCopier() { 116 if (mIsFledgeFrequencyCapFilteringEnabled) { 117 return new AdCounterKeyCopierImpl(); 118 } else { 119 return new AdCounterKeyCopierNoOpImpl(); 120 } 121 } 122 123 /** 124 * Gets the {@link FrequencyCapAdDataValidator} implementation to use, dependent on whether the 125 * ad filtering feature is enabled. 126 * 127 * @return a {@link FrequencyCapAdDataValidatorImpl} instance if the ad filtering feature is 128 * enabled, or a {@link FrequencyCapAdDataValidatorNoOpImpl} instance otherwise 129 */ getFrequencyCapAdDataValidator()130 public FrequencyCapAdDataValidator getFrequencyCapAdDataValidator() { 131 if (mIsFledgeFrequencyCapFilteringEnabled) { 132 return new FrequencyCapAdDataValidatorImpl(); 133 } else { 134 return new FrequencyCapAdDataValidatorNoOpImpl(); 135 } 136 } 137 138 /** 139 * Gets the {@link AdCounterHistogramUpdater} implementation to use, dependent on whether the ad 140 * filtering feature is enabled. 141 * 142 * @return a {@link AdCounterHistogramUpdaterImpl} instance if the ad filtering feature is 143 * enabled, or a {@link AdCounterHistogramUpdaterNoOpImpl} instance otherwise 144 */ getAdCounterHistogramUpdater( @onNull AdSelectionEntryDao adSelectionEntryDao, boolean auctionServerEnabledForUpdateHistogram)145 public AdCounterHistogramUpdater getAdCounterHistogramUpdater( 146 @NonNull AdSelectionEntryDao adSelectionEntryDao, 147 boolean auctionServerEnabledForUpdateHistogram) { 148 Objects.requireNonNull(adSelectionEntryDao); 149 150 if (mIsFledgeFrequencyCapFilteringEnabled) { 151 return new AdCounterHistogramUpdaterImpl( 152 adSelectionEntryDao, 153 mFrequencyCapDao, 154 mHistogramAbsoluteMaxTotalEventCount, 155 mHistogramLowerMaxTotalEventCount, 156 mHistogramAbsoluteMaxPerBuyerEventCount, 157 mHistogramLowerMaxPerBuyerEventCount, 158 auctionServerEnabledForUpdateHistogram, 159 mShouldUseUnifiedTables); 160 } else { 161 return new AdCounterHistogramUpdaterNoOpImpl(); 162 } 163 } 164 } 165