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.customaudience;
18 
19 import static com.android.adservices.service.customaudience.CustomAudienceUpdatableDataReader.AD_RENDER_ID_KEY;
20 
21 import android.annotation.NonNull;
22 
23 import com.android.adservices.data.common.DBAdData;
24 import com.android.adservices.service.common.AdRenderIdValidator;
25 import com.android.adservices.service.common.JsonUtils;
26 
27 import org.json.JSONException;
28 import org.json.JSONObject;
29 
30 /** Factory for {@link ReadAdRenderIdFromJsonStrategy} */
31 public class ReadAdRenderIdFromJsonStrategyFactory {
32 
33     private static class AdRenderIdEnabledStrategy implements ReadAdRenderIdFromJsonStrategy {
34 
35         @NonNull private final AdRenderIdValidator mAdRenderIdValidator;
36 
AdRenderIdEnabledStrategy(long maxLength)37         AdRenderIdEnabledStrategy(long maxLength) {
38             mAdRenderIdValidator = AdRenderIdValidator.createEnabledInstance(maxLength);
39         }
40 
41         /**
42          * Adds ad render id field to the provided AdData builder.
43          *
44          * @param adDataBuilder the AdData builder to modify.
45          * @param adDataJsonObj the AdData JSON to extract from
46          * @throws JSONException if the key is found but the schema is incorrect
47          * @throws NullPointerException if the key found by the field is null
48          */
49         @Override
readId(DBAdData.Builder adDataBuilder, JSONObject adDataJsonObj)50         public void readId(DBAdData.Builder adDataBuilder, JSONObject adDataJsonObj)
51                 throws JSONException, NullPointerException, IllegalArgumentException {
52             String adRenderId = null;
53             if (adDataJsonObj.has(AD_RENDER_ID_KEY)) {
54                 adRenderId = JsonUtils.getStringFromJson(adDataJsonObj, AD_RENDER_ID_KEY);
55                 mAdRenderIdValidator.validate(adRenderId);
56             }
57             adDataBuilder.setAdRenderId(adRenderId);
58         }
59     }
60 
61     private static class AdRenderIdDisabledStrategy implements ReadAdRenderIdFromJsonStrategy {
62         /**
63          * Does nothing.
64          *
65          * @param adDataBuilder unused
66          * @param adDataJsonObj unused
67          */
68         @Override
readId(DBAdData.Builder adDataBuilder, JSONObject adDataJsonObj)69         public void readId(DBAdData.Builder adDataBuilder, JSONObject adDataJsonObj) {}
70     }
71 
72     /**
73      * Returns the appropriate {@link ReadAdRenderIdFromJsonStrategy} based whether ad render id is
74      * enabled
75      *
76      * @param adRenderIdEnabled Should be true if ad render id is enabled.
77      * @return An implementation of {@link ReadAdRenderIdFromJsonStrategy}
78      */
getStrategy( boolean adRenderIdEnabled, long adRenderIdMaxLength)79     public static ReadAdRenderIdFromJsonStrategy getStrategy(
80             boolean adRenderIdEnabled, long adRenderIdMaxLength) {
81         if (adRenderIdEnabled) {
82             return new ReadAdRenderIdFromJsonStrategyFactory.AdRenderIdEnabledStrategy(
83                     adRenderIdMaxLength);
84         }
85         return new ReadAdRenderIdFromJsonStrategyFactory.AdRenderIdDisabledStrategy();
86     }
87 }
88