1 /*
2  * Copyright (C) 2022 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.enrollment;
18 
19 import androidx.annotation.NonNull;
20 import androidx.annotation.Nullable;
21 
22 import com.android.adservices.service.proto.PrivacySandboxApi;
23 import com.android.internal.annotations.VisibleForTesting;
24 
25 import com.google.common.collect.ImmutableBiMap;
26 
27 import java.util.ArrayList;
28 import java.util.Arrays;
29 import java.util.Collections;
30 import java.util.List;
31 import java.util.Objects;
32 
33 /** POJO for Adtech EnrollmentData, store the data download using MDD. */
34 public class EnrollmentData {
35     @VisibleForTesting public static String SEPARATOR = " ";
36 
37     private String mEnrollmentId;
38     private String mEnrolledSite;
39     private String mEnrolledAPIsString;
40     // mEnrolled APIs are derived from mEnrolledAPIsString, they represent the same enrolledAPIs
41     private List<PrivacySandboxApi> mEnrolledAPIs;
42     private List<String> mSdkNames;
43     private List<String> mAttributionSourceRegistrationUrl;
44     private List<String> mAttributionTriggerRegistrationUrl;
45     private List<String> mAttributionReportingUrl;
46     private List<String> mRemarketingResponseBasedRegistrationUrl;
47     private String mEncryptionKeyUrl;
48 
EnrollmentData()49     private EnrollmentData() {
50         mEnrollmentId = null;
51         mEnrolledSite = null;
52         mEnrolledAPIsString = null;
53         mEnrolledAPIs = new ArrayList<PrivacySandboxApi>();
54         mSdkNames = new ArrayList<>();
55         mAttributionSourceRegistrationUrl = new ArrayList<>();
56         mAttributionTriggerRegistrationUrl = new ArrayList<>();
57         mAttributionReportingUrl = new ArrayList<>();
58         mRemarketingResponseBasedRegistrationUrl = new ArrayList<>();
59         mEncryptionKeyUrl = null;
60     }
61 
62     @Override
equals(Object obj)63     public boolean equals(Object obj) {
64         if (!(obj instanceof EnrollmentData)) {
65             return false;
66         }
67         EnrollmentData enrollmentData = (EnrollmentData) obj;
68         return Objects.equals(mEnrollmentId, enrollmentData.mEnrollmentId)
69                 && Objects.equals(mEnrolledSite, enrollmentData.mEnrolledSite)
70                 && Objects.equals(mEnrolledAPIsString, enrollmentData.mEnrolledAPIsString)
71                 && Objects.equals(mEnrolledAPIs, enrollmentData.mEnrolledAPIs)
72                 && Objects.equals(mSdkNames, enrollmentData.mSdkNames)
73                 && Objects.equals(
74                         mAttributionSourceRegistrationUrl,
75                         enrollmentData.mAttributionSourceRegistrationUrl)
76                 && Objects.equals(
77                         mAttributionTriggerRegistrationUrl,
78                         enrollmentData.mAttributionTriggerRegistrationUrl)
79                 && Objects.equals(mAttributionReportingUrl, enrollmentData.mAttributionReportingUrl)
80                 && Objects.equals(
81                         mRemarketingResponseBasedRegistrationUrl,
82                         enrollmentData.mRemarketingResponseBasedRegistrationUrl)
83                 && Objects.equals(mEncryptionKeyUrl, enrollmentData.mEncryptionKeyUrl);
84     }
85 
86     @Override
hashCode()87     public int hashCode() {
88         return Objects.hash(
89                 mEnrollmentId,
90                 mEnrolledSite,
91                 mEnrolledAPIsString,
92                 mEnrolledAPIs,
93                 mSdkNames,
94                 mAttributionSourceRegistrationUrl,
95                 mAttributionTriggerRegistrationUrl,
96                 mAttributionReportingUrl,
97                 mRemarketingResponseBasedRegistrationUrl,
98                 mEncryptionKeyUrl);
99     }
100 
101     /** Returns ID provided to the Adtech at the end of the enrollment process. */
getEnrollmentId()102     public String getEnrollmentId() {
103         return mEnrollmentId;
104     }
105 
106     /** Returns enrolled site provided by Adtech during enrollment process. */
getEnrolledSite()107     public String getEnrolledSite() {
108         return mEnrolledSite;
109     }
110 
111     /** Return Enrolled APIs of given enrollment in string format */
112     @Nullable
getEnrolledAPIsString()113     public String getEnrolledAPIsString() {
114         return mEnrolledAPIsString;
115     }
116 
117     /** Return list of Enrolled APIs of given enrollment */
118     @Nullable
getEnrolledAPIs()119     public List<PrivacySandboxApi> getEnrolledAPIs() {
120         return mEnrolledAPIs;
121     }
122 
123     /** List of SDKs belonging to the same enrollment. */
getSdkNames()124     public List<String> getSdkNames() {
125         return mSdkNames;
126     }
127 
128     /** Returns URLs used to register attribution sources for measurement. */
getAttributionSourceRegistrationUrl()129     public List<String> getAttributionSourceRegistrationUrl() {
130         return mAttributionSourceRegistrationUrl;
131     }
132 
133     /** Returns URLs used to register triggers for measurement. */
getAttributionTriggerRegistrationUrl()134     public List<String> getAttributionTriggerRegistrationUrl() {
135         return mAttributionTriggerRegistrationUrl;
136     }
137 
138     /** Returns URLs that the Measurement module will send Attribution reports to. */
getAttributionReportingUrl()139     public List<String> getAttributionReportingUrl() {
140         return mAttributionReportingUrl;
141     }
142 
143     /** Returns URLs used for response-based-registration for joinCustomAudience. */
getRemarketingResponseBasedRegistrationUrl()144     public List<String> getRemarketingResponseBasedRegistrationUrl() {
145         return mRemarketingResponseBasedRegistrationUrl;
146     }
147 
148     /** Returns URL used to fetch public/private keys for encrypting API requests. */
149     @Nullable
getEncryptionKeyUrl()150     public String getEncryptionKeyUrl() {
151         return mEncryptionKeyUrl;
152     }
153 
154     /**
155      * Returns the given {@code input} as a list of values split by the separator value used for all
156      * enrollment data.
157      */
158     @NonNull
splitEnrollmentInputToList(@ullable String input)159     public static List<String> splitEnrollmentInputToList(@Nullable String input) {
160         if (input == null || input.trim().isEmpty()) {
161             return Collections.emptyList();
162         }
163 
164         return Arrays.asList(input.trim().split(SEPARATOR));
165     }
166 
167     /**
168      * Returns the given {@code enrolledAPIs} as a list of {@link PrivacySandboxApi} enum values.
169      */
enrolledApisToEnums(String enrolledAPIs)170     private static List<PrivacySandboxApi> enrolledApisToEnums(String enrolledAPIs) {
171         List<PrivacySandboxApi> enrolledApiEnums = new ArrayList<PrivacySandboxApi>();
172         if (enrolledAPIs == null || enrolledAPIs.trim().isEmpty()) {
173             return enrolledApiEnums;
174         }
175 
176         String[] enrolledAPIsList = enrolledAPIs.trim().split("\\s+");
177         for (String enrolledApi : enrolledAPIsList) {
178             PrivacySandboxApi enrolledApiEnum =
179                     ENROLLMENT_API_ENUM_STRING_MAP
180                             .inverse()
181                             .getOrDefault(
182                                     enrolledApi,
183                                     /* defaultValue= */ PrivacySandboxApi
184                                             .PRIVACY_SANDBOX_API_UNKNOWN);
185             enrolledApiEnums.add(enrolledApiEnum);
186         }
187         return enrolledApiEnums;
188     }
189 
190     // LINT.IfChange(EnrollmentApiEnumStringMap)
191     public static final ImmutableBiMap<PrivacySandboxApi, String> ENROLLMENT_API_ENUM_STRING_MAP =
192             ImmutableBiMap.<PrivacySandboxApi, String>builder()
193                     .put(PrivacySandboxApi.PRIVACY_SANDBOX_API_TOPICS, "PRIVACY_SANDBOX_API_TOPICS")
194                     .put(
195                             PrivacySandboxApi.PRIVACY_SANDBOX_API_PROTECTED_AUDIENCE,
196                             "PRIVACY_SANDBOX_API_PROTECTED_AUDIENCE")
197                     .put(
198                             PrivacySandboxApi.PRIVACY_SANDBOX_API_PRIVATE_AGGREGATION,
199                             "PRIVACY_SANDBOX_API_PRIVATE_AGGREGATION")
200                     .put(
201                             PrivacySandboxApi.PRIVACY_SANDBOX_API_ATTRIBUTION_REPORTING,
202                             "PRIVACY_SANDBOX_API_ATTRIBUTION_REPORTING")
203                     .put(
204                             PrivacySandboxApi.PRIVACY_SANDBOX_API_SHARED_STORAGE,
205                             "PRIVACY_SANDBOX_API_SHARED_STORAGE")
206                     .put(
207                             PrivacySandboxApi.PRIVACY_SANDBOX_API_PROTECTED_APP_SIGNALS,
208                             "PRIVACY_SANDBOX_API_PROTECTED_APP_SIGNALS")
209                     .build();
210 
211     // LINT.ThenChange(/adservices/service-core/proto/rb_enrollment.proto:PrivacySandboxApi)
212 
213     /** Returns the builder for the instance */
214     @NonNull
cloneToBuilder()215     public EnrollmentData.Builder cloneToBuilder() {
216         return new EnrollmentData.Builder()
217                 .setEnrollmentId(this.mEnrollmentId)
218                 .setEnrolledSite(this.mEnrolledSite)
219                 .setEnrolledAPIs(this.mEnrolledAPIsString)
220                 .setSdkNames(this.mSdkNames)
221                 .setAttributionSourceRegistrationUrl(this.mAttributionSourceRegistrationUrl)
222                 .setAttributionTriggerRegistrationUrl(this.mAttributionTriggerRegistrationUrl)
223                 .setAttributionReportingUrl(this.mAttributionReportingUrl)
224                 .setRemarketingResponseBasedRegistrationUrl(
225                         this.mRemarketingResponseBasedRegistrationUrl)
226                 .setEncryptionKeyUrl(this.mEncryptionKeyUrl);
227     }
228 
229     /** Builder for {@link EnrollmentData}. */
230     public static final class Builder {
231         private final EnrollmentData mBuilding;
232 
Builder()233         public Builder() {
234             mBuilding = new EnrollmentData();
235         }
236 
237         /** See {@link EnrollmentData#getEnrollmentId()}. */
setEnrollmentId(String enrollmentId)238         public Builder setEnrollmentId(String enrollmentId) {
239             mBuilding.mEnrollmentId = enrollmentId;
240             return this;
241         }
242 
243         /** See {@link EnrollmentData#getEnrolledSite()}. */
setEnrolledSite(String enrolledSite)244         public Builder setEnrolledSite(String enrolledSite) {
245             mBuilding.mEnrolledSite = enrolledSite;
246             return this;
247         }
248 
249         /** See {@link EnrollmentData#getEnrolledAPIs()}. */
setEnrolledAPIs(String enrolledAPIs)250         public Builder setEnrolledAPIs(String enrolledAPIs) {
251             mBuilding.mEnrolledAPIsString = enrolledAPIs;
252             mBuilding.mEnrolledAPIs = enrolledApisToEnums(enrolledAPIs);
253             return this;
254         }
255 
256         /** See {@link EnrollmentData#getSdkNames()} */
setSdkNames(List<String> sdkNames)257         public Builder setSdkNames(List<String> sdkNames) {
258             mBuilding.mSdkNames = sdkNames;
259             return this;
260         }
261 
262         /** See {@link EnrollmentData#getSdkNames()} */
setSdkNames(String sdkNames)263         public Builder setSdkNames(String sdkNames) {
264             mBuilding.mSdkNames = splitEnrollmentInputToList(sdkNames);
265             return this;
266         }
267 
268         /** See {@link EnrollmentData#getAttributionSourceRegistrationUrl()}. */
setAttributionSourceRegistrationUrl( List<String> attributionSourceRegistrationUrl)269         public Builder setAttributionSourceRegistrationUrl(
270                 List<String> attributionSourceRegistrationUrl) {
271             mBuilding.mAttributionSourceRegistrationUrl = attributionSourceRegistrationUrl;
272             return this;
273         }
274 
275         /** See {@link EnrollmentData#getAttributionSourceRegistrationUrl()}. */
setAttributionSourceRegistrationUrl( String attributionSourceRegistrationUrl)276         public Builder setAttributionSourceRegistrationUrl(
277                 String attributionSourceRegistrationUrl) {
278             mBuilding.mAttributionSourceRegistrationUrl =
279                     splitEnrollmentInputToList(attributionSourceRegistrationUrl);
280             return this;
281         }
282 
283         /** See {@link EnrollmentData#getAttributionTriggerRegistrationUrl()}. */
setAttributionTriggerRegistrationUrl( List<String> attributionTriggerRegistrationUrl)284         public Builder setAttributionTriggerRegistrationUrl(
285                 List<String> attributionTriggerRegistrationUrl) {
286             mBuilding.mAttributionTriggerRegistrationUrl = attributionTriggerRegistrationUrl;
287             return this;
288         }
289 
290         /** See {@link EnrollmentData#getAttributionTriggerRegistrationUrl()}. */
setAttributionTriggerRegistrationUrl( String attributionTriggerRegistrationUrl)291         public Builder setAttributionTriggerRegistrationUrl(
292                 String attributionTriggerRegistrationUrl) {
293             mBuilding.mAttributionTriggerRegistrationUrl =
294                     splitEnrollmentInputToList(attributionTriggerRegistrationUrl);
295             return this;
296         }
297 
298         /** See {@link EnrollmentData#getAttributionReportingUrl()}. */
setAttributionReportingUrl(List<String> attributionReportingUrl)299         public Builder setAttributionReportingUrl(List<String> attributionReportingUrl) {
300             mBuilding.mAttributionReportingUrl = attributionReportingUrl;
301             return this;
302         }
303 
304         /** See {@link EnrollmentData#getAttributionReportingUrl()}. */
setAttributionReportingUrl(String attributionReportingUrl)305         public Builder setAttributionReportingUrl(String attributionReportingUrl) {
306             mBuilding.mAttributionReportingUrl =
307                     splitEnrollmentInputToList(attributionReportingUrl);
308             return this;
309         }
310 
311         /** See {@link EnrollmentData#getRemarketingResponseBasedRegistrationUrl()}. */
setRemarketingResponseBasedRegistrationUrl( List<String> remarketingResponseBasedRegistrationUrl)312         public Builder setRemarketingResponseBasedRegistrationUrl(
313                 List<String> remarketingResponseBasedRegistrationUrl) {
314             mBuilding.mRemarketingResponseBasedRegistrationUrl =
315                     remarketingResponseBasedRegistrationUrl;
316             return this;
317         }
318 
319         /** See {@link EnrollmentData#getRemarketingResponseBasedRegistrationUrl()}. */
setRemarketingResponseBasedRegistrationUrl( String remarketingResponseBasedRegistrationUrl)320         public Builder setRemarketingResponseBasedRegistrationUrl(
321                 String remarketingResponseBasedRegistrationUrl) {
322             mBuilding.mRemarketingResponseBasedRegistrationUrl =
323                     splitEnrollmentInputToList(remarketingResponseBasedRegistrationUrl);
324             return this;
325         }
326 
327         /** See {@link EnrollmentData#getEncryptionKeyUrl()}. */
setEncryptionKeyUrl(String encryptionKeyUrl)328         public Builder setEncryptionKeyUrl(String encryptionKeyUrl) {
329             mBuilding.mEncryptionKeyUrl = encryptionKeyUrl;
330             return this;
331         }
332 
333         /** Builder the {@link EnrollmentData}. */
build()334         public EnrollmentData build() {
335             return mBuilding;
336         }
337     }
338 }
339