• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.ADS_KEY;
20 import static com.android.adservices.service.customaudience.CustomAudienceUpdatableDataReader.AD_COUNTERS_KEY;
21 import static com.android.adservices.service.customaudience.CustomAudienceUpdatableDataReader.AD_FILTERS_KEY;
22 import static com.android.adservices.service.customaudience.CustomAudienceUpdatableDataReader.STRING_ERROR_FORMAT;
23 
24 import android.adservices.common.AdFilters;
25 
26 import com.android.adservices.data.common.DBAdData;
27 import com.android.adservices.service.common.JsonUtils;
28 
29 import org.json.JSONArray;
30 import org.json.JSONException;
31 import org.json.JSONObject;
32 
33 import java.util.HashSet;
34 import java.util.Set;
35 /** Factory for ReadFiltersFromJsonStrategys */
36 public class ReadFiltersFromJsonStrategyFactory {
37 
38     private static class FilteringEnabledStrategy implements ReadFiltersFromJsonStrategy {
39 
40         /**
41          * Adds filtering fields to the provided AdData builder.
42          *
43          * @param adDataBuilder the AdData builder to modify.
44          * @param adDataJsonObj the AdData JSON to extract from
45          * @throws JSONException if the key is found but the schema is incorrect
46          * @throws NullPointerException if the key found by the field is null
47          */
48         @Override
readFilters(DBAdData.Builder adDataBuilder, JSONObject adDataJsonObj)49         public void readFilters(DBAdData.Builder adDataBuilder, JSONObject adDataJsonObj)
50                 throws JSONException, NullPointerException, IllegalArgumentException {
51             Set<String> adCounterKeys = new HashSet<>();
52             if (adDataJsonObj.has(AD_COUNTERS_KEY)) {
53                 JSONArray adCounterKeysJson = adDataJsonObj.getJSONArray(AD_COUNTERS_KEY);
54                 for (int j = 0; j < adCounterKeysJson.length(); j++) {
55                     adCounterKeys.add(
56                             JsonUtils.getStringFromJsonArrayAtIndex(
57                                     adCounterKeysJson,
58                                     j,
59                                     String.format(STRING_ERROR_FORMAT, AD_COUNTERS_KEY, ADS_KEY)));
60                 }
61             }
62             AdFilters adFilters = null;
63             if (adDataJsonObj.has(AD_FILTERS_KEY)) {
64                 adFilters = AdFilters.fromJson(adDataJsonObj.getJSONObject(AD_FILTERS_KEY));
65             }
66             adDataBuilder.setAdCounterKeys(adCounterKeys).setAdFilters(adFilters);
67         }
68     }
69 
70     private static class FilteringDisabledStrategy implements ReadFiltersFromJsonStrategy {
71         /**
72          * Does nothing.
73          *
74          * @param adDataBuilder unused
75          * @param adDataJsonObj unused
76          */
77         @Override
readFilters(DBAdData.Builder adDataBuilder, JSONObject adDataJsonObj)78         public void readFilters(DBAdData.Builder adDataBuilder, JSONObject adDataJsonObj) {}
79     }
80 
81     /**
82      * Returns the appropriate ReadFiltersFromJsonStrategy based whether filtering is enabled
83      *
84      * @param filteringEnabled Should be true if filtering is enabled.
85      * @return An implementation of ReadFiltersFromJsonStrategy
86      */
getStrategy(boolean filteringEnabled)87     public static ReadFiltersFromJsonStrategy getStrategy(boolean filteringEnabled) {
88         if (filteringEnabled) {
89             return new FilteringEnabledStrategy();
90         }
91         return new FilteringDisabledStrategy();
92     }
93 }
94