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 android.adservices.common.AdTechIdentifier;
20 import android.adservices.customaudience.CustomAudience;
21 import android.annotation.FlaggedApi;
22 import android.annotation.NonNull;
23 import android.os.Parcel;
24 import android.os.Parcelable;
25 
26 import com.android.adservices.AdServicesParcelableUtil;
27 import com.android.adservices.flags.Flags;
28 
29 import java.util.Collections;
30 import java.util.Map;
31 import java.util.Objects;
32 
33 /**
34  * The override object for decision logic JS per buyer for {@link SignedContextualAds}.
35  *
36  * <p>This decision logic is used for reporting when an ad wins from a buyer's bundle of {@link
37  * SignedContextualAds}.
38  *
39  * <p>This JS code may be extended to updating bid values for contextual ads in the future.
40  *
41  * <p>See {@link CustomAudience#getBiddingLogicUri()}.
42  */
43 @FlaggedApi(Flags.FLAG_FLEDGE_AD_SELECTION_FILTERING_ENABLED)
44 public final class PerBuyerDecisionLogic implements Parcelable {
45 
46     @NonNull
47     public static final PerBuyerDecisionLogic EMPTY =
48             new PerBuyerDecisionLogic(Collections.emptyMap());
49 
50     @NonNull private final Map<AdTechIdentifier, DecisionLogic> mPerBuyerLogicMap;
51 
52     /**
53      * Builds a {@link PerBuyerDecisionLogic} instance.
54      *
55      * @param perBuyerLogicMap map of buyers and their decision logic to be fetched during ad
56      *     selection
57      */
PerBuyerDecisionLogic(@onNull Map<AdTechIdentifier, DecisionLogic> perBuyerLogicMap)58     public PerBuyerDecisionLogic(@NonNull Map<AdTechIdentifier, DecisionLogic> perBuyerLogicMap) {
59         Objects.requireNonNull(perBuyerLogicMap);
60         mPerBuyerLogicMap = perBuyerLogicMap;
61     }
62 
PerBuyerDecisionLogic(@onNull Parcel in)63     private PerBuyerDecisionLogic(@NonNull Parcel in) {
64         mPerBuyerLogicMap =
65                 AdServicesParcelableUtil.readMapFromParcel(
66                         in, AdTechIdentifier::fromString, DecisionLogic.class);
67     }
68 
69     @NonNull
70     public static final Creator<PerBuyerDecisionLogic> CREATOR =
71             new Creator<PerBuyerDecisionLogic>() {
72                 @Override
73                 public PerBuyerDecisionLogic createFromParcel(Parcel in) {
74                     Objects.requireNonNull(in);
75                     return new PerBuyerDecisionLogic(in);
76                 }
77 
78                 @Override
79                 public PerBuyerDecisionLogic[] newArray(int size) {
80                     return new PerBuyerDecisionLogic[size];
81                 }
82             };
83 
84     @Override
describeContents()85     public int describeContents() {
86         return 0;
87     }
88 
89     @Override
writeToParcel(@onNull Parcel dest, int flags)90     public void writeToParcel(@NonNull Parcel dest, int flags) {
91         Objects.requireNonNull(dest);
92         AdServicesParcelableUtil.writeMapToParcel(dest, mPerBuyerLogicMap);
93     }
94 
95     @Override
hashCode()96     public int hashCode() {
97         return Objects.hash(mPerBuyerLogicMap);
98     }
99 
100     @Override
equals(Object o)101     public boolean equals(Object o) {
102         if (this == o) return true;
103         if (!(o instanceof PerBuyerDecisionLogic)) return false;
104         PerBuyerDecisionLogic logicMap = (PerBuyerDecisionLogic) o;
105         return mPerBuyerLogicMap.equals(logicMap.getPerBuyerLogicMap());
106     }
107 
108     @NonNull
getPerBuyerLogicMap()109     public Map<AdTechIdentifier, DecisionLogic> getPerBuyerLogicMap() {
110         return mPerBuyerLogicMap;
111     }
112 }
113