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 android.adservices.common.AdSelectionSignals;
20 
21 import androidx.annotation.Nullable;
22 
23 import com.android.adservices.LoggerFactory;
24 
25 import com.google.auto.value.AutoValue;
26 
27 import org.json.JSONException;
28 import org.json.JSONObject;
29 
30 /** Represents buyer contextual signals that will be passed through buyer JS functions. */
31 @AutoValue
32 public abstract class BuyerContextualSignals {
33     private static final LoggerFactory.Logger sLogger = LoggerFactory.getFledgeLogger();
34 
35     @Nullable
getAdCost()36     abstract AdCost getAdCost();
37 
38     @Nullable
getDataVersion()39     abstract Integer getDataVersion();
40 
41     /** Creates a builder for a {@link BuyerContextualSignals} object. */
builder()42     public static BuyerContextualSignals.Builder builder() {
43         return new AutoValue_BuyerContextualSignals.Builder();
44     }
45 
46     /** Defines a builder for a {@link BuyerContextualSignals} object. */
47     @AutoValue.Builder
48     public abstract static class Builder {
49         /** Sets the adCost. */
setAdCost(@ullable AdCost adCost)50         public abstract Builder setAdCost(@Nullable AdCost adCost);
51 
setDataVersion(@ullable Integer dataVersion)52         public abstract Builder setDataVersion(@Nullable Integer dataVersion);
53 
54         /** Builds a {@link BuyerContextualSignals} object. */
build()55         public abstract BuyerContextualSignals build();
56     }
57 
58     /** Returns {@link BuyerContextualSignals} in a JSON format */
59     @Override
toString()60     public final String toString() {
61         try {
62             JSONObject json = new JSONObject();
63             json.put("adCost", getAdCost());
64             json.put("dataVersion", getDataVersion());
65             return json.toString();
66         } catch (JSONException e) {
67             sLogger.e(
68                     e,
69                     "BuyerContextualSignals's String representation is invalid. "
70                             + "Returning empty AdSelectionSignals");
71             return AdSelectionSignals.EMPTY.toString();
72         }
73     }
74 
toAdSelectionSignals()75     public AdSelectionSignals toAdSelectionSignals() {
76         return AdSelectionSignals.fromString(toString());
77     }
78 }
79