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 android.adservices.adselection;
18 
19 import static android.adservices.adselection.SetAdCounterHistogramOverrideRequest.NULL_BUYER_MESSAGE;
20 import static android.adservices.adselection.UpdateAdCounterHistogramRequest.UNSET_AD_EVENT_TYPE_MESSAGE;
21 import static android.adservices.common.FrequencyCapFilters.AD_EVENT_TYPE_INVALID;
22 
23 import android.adservices.common.AdTechIdentifier;
24 import android.adservices.common.FrequencyCapFilters;
25 import android.annotation.NonNull;
26 import android.annotation.Nullable;
27 import android.os.Parcel;
28 import android.os.Parcelable;
29 
30 import com.android.internal.util.Preconditions;
31 
32 import java.util.Objects;
33 
34 /**
35  * Input object for removing ad counter histogram overrides.
36  *
37  * <p>Histogram overrides replace actual ad counter histograms used in ad selection. Overrides may
38  * only be set in debuggable apps on phones running a debuggable OS build with developer options
39  * enabled. Overrides are only available from the calling app.
40  *
41  * @hide
42  */
43 public final class RemoveAdCounterHistogramOverrideInput implements Parcelable {
44     @FrequencyCapFilters.AdEventType private final int mAdEventType;
45     private final int mAdCounterKey;
46     @NonNull private final AdTechIdentifier mBuyer;
47 
48     @NonNull
49     public static final Creator<RemoveAdCounterHistogramOverrideInput> CREATOR =
50             new Creator<RemoveAdCounterHistogramOverrideInput>() {
51                 @Override
52                 public RemoveAdCounterHistogramOverrideInput createFromParcel(@NonNull Parcel in) {
53                     Objects.requireNonNull(in);
54 
55                     return new RemoveAdCounterHistogramOverrideInput(in);
56                 }
57 
58                 @Override
59                 public RemoveAdCounterHistogramOverrideInput[] newArray(int size) {
60                     return new RemoveAdCounterHistogramOverrideInput[size];
61                 }
62             };
63 
RemoveAdCounterHistogramOverrideInput(@onNull Builder builder)64     private RemoveAdCounterHistogramOverrideInput(@NonNull Builder builder) {
65         Objects.requireNonNull(builder);
66 
67         mAdEventType = builder.mAdEventType;
68         mAdCounterKey = builder.mAdCounterKey;
69         mBuyer = builder.mBuyer;
70     }
71 
RemoveAdCounterHistogramOverrideInput(@onNull Parcel in)72     private RemoveAdCounterHistogramOverrideInput(@NonNull Parcel in) {
73         Objects.requireNonNull(in);
74 
75         mAdEventType = in.readInt();
76         mAdCounterKey = in.readInt();
77         mBuyer = AdTechIdentifier.fromString(in.readString());
78     }
79 
80     /**
81      * Gets the {@link FrequencyCapFilters.AdEventType} for the ad counter histogram override.
82      *
83      * <p>The ad event type is used with the ad counter key from {@link #getAdCounterKey()} and the
84      * buyer adtech from {@link #getBuyer()} to specify which histogram to use in ad selection
85      * filtering. The ad event type would normally be specified by an app/SDK after a
86      * FLEDGE-selected ad is rendered.
87      */
88     @FrequencyCapFilters.AdEventType
getAdEventType()89     public int getAdEventType() {
90         return mAdEventType;
91     }
92 
93     /**
94      * Gets the ad counter key for the ad counter histogram override.
95      *
96      * <p>The ad counter key is used with the ad event type from {@link #getAdEventType()} and the
97      * buyer adtech from {@link #getBuyer()} to specify which histogram to use in ad selection
98      * filtering. The ad counter key would normally be specified by a custom audience ad to
99      * represent a grouping to filter on.
100      */
101     @NonNull
getAdCounterKey()102     public int getAdCounterKey() {
103         return mAdCounterKey;
104     }
105 
106     /**
107      * Gets the {@link AdTechIdentifier} for the buyer which owns the ad counter histogram.
108      *
109      * <p>During filtering in FLEDGE ad selection, ads can only use ad counter histogram data
110      * generated by the same buyer. For {@link FrequencyCapFilters#AD_EVENT_TYPE_WIN}, ad counter
111      * histogram data is further restricted to ads from the same custom audience, which is
112      * identified by the buyer, the custom audience's owner app package name, and the custom
113      * audience name.
114      */
115     @NonNull
getBuyer()116     public AdTechIdentifier getBuyer() {
117         return mBuyer;
118     }
119 
120     /** @hide */
121     @Override
describeContents()122     public int describeContents() {
123         return 0;
124     }
125 
126     @Override
toString()127     public String toString() {
128         return "RemoveAdCounterHistogramOverrideInput{"
129                 + "mAdEventType="
130                 + mAdEventType
131                 + ", mAdCounterKey="
132                 + mAdCounterKey
133                 + ", mBuyer="
134                 + mBuyer
135                 + '}';
136     }
137 
138     @Override
writeToParcel(@onNull Parcel dest, int flags)139     public void writeToParcel(@NonNull Parcel dest, int flags) {
140         Objects.requireNonNull(dest);
141 
142         dest.writeInt(mAdEventType);
143         dest.writeInt(mAdCounterKey);
144         dest.writeString(mBuyer.toString());
145     }
146 
147     /** Builder for {@link RemoveAdCounterHistogramOverrideInput} objects. */
148     public static final class Builder {
149         @FrequencyCapFilters.AdEventType private int mAdEventType = AD_EVENT_TYPE_INVALID;
150         private int mAdCounterKey;
151         @Nullable private AdTechIdentifier mBuyer;
152 
Builder()153         public Builder() {}
154 
155         /**
156          * Sets the {@link FrequencyCapFilters.AdEventType} for the ad counter histogram override.
157          *
158          * <p>See {@link #getAdEventType()} for more information.
159          */
160         @NonNull
setAdEventType(@requencyCapFilters.AdEventType int adEventType)161         public Builder setAdEventType(@FrequencyCapFilters.AdEventType int adEventType) {
162             mAdEventType = adEventType;
163             return this;
164         }
165 
166         /**
167          * Sets the ad counter key for the ad counter histogram override.
168          *
169          * <p>See {@link #getAdCounterKey()} for more information.
170          */
171         @NonNull
setAdCounterKey(int adCounterKey)172         public Builder setAdCounterKey(int adCounterKey) {
173             mAdCounterKey = adCounterKey;
174             return this;
175         }
176 
177         /**
178          * Sets the {@link AdTechIdentifier} for the buyer which owns the ad counter histogram.
179          *
180          * <p>See {@link #getBuyer()} for more information.
181          */
182         @NonNull
setBuyer(@onNull AdTechIdentifier buyer)183         public Builder setBuyer(@NonNull AdTechIdentifier buyer) {
184             Objects.requireNonNull(buyer, NULL_BUYER_MESSAGE);
185             mBuyer = buyer;
186             return this;
187         }
188 
189         /**
190          * Builds the {@link RemoveAdCounterHistogramOverrideInput} object.
191          *
192          * @throws NullPointerException if any parameters are not set
193          * @throws IllegalArgumentException if the ad event type is invalid
194          */
195         @NonNull
build()196         public RemoveAdCounterHistogramOverrideInput build()
197                 throws NullPointerException, IllegalArgumentException {
198             Preconditions.checkArgument(
199                     mAdEventType != AD_EVENT_TYPE_INVALID, UNSET_AD_EVENT_TYPE_MESSAGE);
200             Objects.requireNonNull(mBuyer, NULL_BUYER_MESSAGE);
201 
202             return new RemoveAdCounterHistogramOverrideInput(this);
203         }
204     }
205 }
206