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.data.adselection; 18 19 import android.adservices.common.AdTechIdentifier; 20 import android.adservices.common.FrequencyCapFilters; 21 22 import androidx.annotation.NonNull; 23 import androidx.annotation.Nullable; 24 import androidx.room.ColumnInfo; 25 import androidx.room.Entity; 26 import androidx.room.PrimaryKey; 27 28 import com.android.adservices.service.adselection.HistogramEvent; 29 30 import com.google.auto.value.AutoValue; 31 32 import java.util.Objects; 33 34 /** 35 * POJO for the identifying fields associated with a histogram of ad events registered by an adtech. 36 * 37 * <p>These events are used to compute frequency histograms to be used during ad selection 38 * filtering. 39 */ 40 @AutoValue 41 @AutoValue.CopyAnnotations 42 @Entity(tableName = DBHistogramIdentifier.TABLE_NAME, inheritSuperIndices = true) 43 public abstract class DBHistogramIdentifier { 44 public static final String TABLE_NAME = "fcap_histogram_ids"; 45 46 /** 47 * Returns the numerical ID linking {@link DBHistogramEventData} to their associated 48 * identifiers. 49 * 50 * <p>This ID is only used internally in the frequency cap histogram tables and does not need to 51 * be stable or reproducible. It is auto-generated by Room if set to {@code null} on insertion. 52 */ 53 @AutoValue.CopyAnnotations 54 @ColumnInfo(name = "foreign_key_id") 55 @PrimaryKey(autoGenerate = true) 56 @Nullable getHistogramIdentifierForeignKey()57 public abstract Long getHistogramIdentifierForeignKey(); 58 59 /** 60 * Returns the arbitrary String representing a grouping that a buyer adtech has assigned to an 61 * ad or histogram. 62 */ 63 @AutoValue.CopyAnnotations 64 @ColumnInfo(name = "ad_counter_key", index = true) 65 @NonNull getAdCounterKey()66 public abstract String getAdCounterKey(); 67 68 /** Returns the histogram's buyer adtech's {@link AdTechIdentifier}. */ 69 @AutoValue.CopyAnnotations 70 @ColumnInfo(name = "buyer", index = true) 71 @NonNull getBuyer()72 public abstract AdTechIdentifier getBuyer(); 73 74 /** Returns the owner package name of the custom audience the histogram is associated with. */ 75 @AutoValue.CopyAnnotations 76 @ColumnInfo(name = "custom_audience_owner", index = true) 77 @Nullable getCustomAudienceOwner()78 public abstract String getCustomAudienceOwner(); 79 80 /** Returns the name of the custom audience the histogram is associated with. */ 81 @AutoValue.CopyAnnotations 82 @ColumnInfo(name = "custom_audience_name", index = true) 83 @Nullable getCustomAudienceName()84 public abstract String getCustomAudienceName(); 85 86 /** Returns an AutoValue builder for a {@link DBHistogramIdentifier} object. */ 87 @NonNull builder()88 public static Builder builder() { 89 return new AutoValue_DBHistogramIdentifier.Builder() 90 .setHistogramIdentifierForeignKey( 91 SharedStorageDatabase.FOREIGN_KEY_AUTOGENERATE_SUBSTITUTE); 92 } 93 94 /** 95 * Creates a {@link DBHistogramIdentifier} object using the builder. 96 * 97 * <p>Required for Room SQLite integration. 98 */ 99 @NonNull create( @ullable Long histogramIdentifierForeignKey, @NonNull String adCounterKey, @NonNull AdTechIdentifier buyer, @Nullable String customAudienceOwner, @Nullable String customAudienceName)100 public static DBHistogramIdentifier create( 101 @Nullable Long histogramIdentifierForeignKey, 102 @NonNull String adCounterKey, 103 @NonNull AdTechIdentifier buyer, 104 @Nullable String customAudienceOwner, 105 @Nullable String customAudienceName) { 106 return builder() 107 .setHistogramIdentifierForeignKey(histogramIdentifierForeignKey) 108 .setAdCounterKey(adCounterKey) 109 .setBuyer(buyer) 110 .setCustomAudienceOwner(customAudienceOwner) 111 .setCustomAudienceName(customAudienceName) 112 .build(); 113 } 114 115 /** 116 * Creates and returns a new {@link DBHistogramIdentifier} object from the given {@link 117 * HistogramEvent}. 118 * 119 * <p>The resulting {@link DBHistogramIdentifier} object is built to autogenerate a foreign key 120 * ID on insertion into the database. 121 */ 122 @NonNull fromHistogramEvent(@onNull HistogramEvent event)123 public static DBHistogramIdentifier fromHistogramEvent(@NonNull HistogramEvent event) { 124 Objects.requireNonNull(event); 125 126 Builder tempBuilder = 127 builder().setAdCounterKey(event.getAdCounterKey()).setBuyer(event.getBuyer()); 128 129 // Only win-typed events must be scoped to a custom audience, so leave them null otherwise 130 if (event.getAdEventType() == FrequencyCapFilters.AD_EVENT_TYPE_WIN) { 131 tempBuilder 132 .setCustomAudienceOwner(event.getCustomAudienceOwner()) 133 .setCustomAudienceName(event.getCustomAudienceName()); 134 } 135 136 return tempBuilder.build(); 137 } 138 139 /** Builder class for a {@link DBHistogramIdentifier} object. */ 140 @AutoValue.Builder 141 public abstract static class Builder { 142 /** 143 * Sets the numerical ID linking {@link DBHistogramEventData} to their associated 144 * identifiers. 145 * 146 * <p>This ID is only used internally in the frequency cap histogram tables and does not 147 * need to be stable or reproducible. It is auto-generated by Room if set to {@code null} on 148 * insertion. 149 */ 150 @NonNull setHistogramIdentifierForeignKey(@ullable Long value)151 public abstract Builder setHistogramIdentifierForeignKey(@Nullable Long value); 152 153 /** 154 * Sets the arbitrary String representing a grouping that a buyer adtech has assigned to an 155 * ad or histogram. 156 */ 157 @NonNull setAdCounterKey(@onNull String value)158 public abstract Builder setAdCounterKey(@NonNull String value); 159 160 /** Sets the histogram's buyer adtech's {@link AdTechIdentifier}. */ 161 @NonNull setBuyer(@onNull AdTechIdentifier value)162 public abstract Builder setBuyer(@NonNull AdTechIdentifier value); 163 164 /** Sets the owner package name of the custom audience the histogram is associated with. */ 165 @NonNull setCustomAudienceOwner(@ullable String value)166 public abstract Builder setCustomAudienceOwner(@Nullable String value); 167 168 /** Sets the name of the custom audience the histogram is associated with. */ 169 @NonNull setCustomAudienceName(@ullable String value)170 public abstract Builder setCustomAudienceName(@Nullable String value); 171 172 /** 173 * Builds and returns the {@link DBHistogramIdentifier} object. 174 * 175 * @throws IllegalStateException if any required field is unset when the object is built 176 */ 177 @NonNull build()178 public abstract DBHistogramIdentifier build(); 179 } 180 } 181