• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     @Override
toString()102     public String toString() {
103         return "EnrollmentData{"
104                 + "mEnrollmentId='"
105                 + mEnrollmentId
106                 + "', mEnrolledSite='"
107                 + mEnrolledSite
108                 + "', mEnrolledAPIsString='"
109                 + mEnrolledAPIsString
110                 + "', mEnrolledAPIs="
111                 + mEnrolledAPIs
112                 + ", mSdkNames="
113                 + mSdkNames
114                 + ", mAttributionSourceRegistrationUrl="
115                 + mAttributionSourceRegistrationUrl
116                 + ", mAttributionTriggerRegistrationUrl="
117                 + mAttributionTriggerRegistrationUrl
118                 + ", mAttributionReportingUrl="
119                 + mAttributionReportingUrl
120                 + ", mRemarketingResponseBasedRegistrationUrl="
121                 + mRemarketingResponseBasedRegistrationUrl
122                 + ", mEncryptionKeyUrl='"
123                 + mEncryptionKeyUrl
124                 + "'}";
125     }
126 
127     /** Returns ID provided to the Adtech at the end of the enrollment process. */
getEnrollmentId()128     public String getEnrollmentId() {
129         return mEnrollmentId;
130     }
131 
132     /** Returns enrolled site provided by Adtech during enrollment process. */
getEnrolledSite()133     public String getEnrolledSite() {
134         return mEnrolledSite;
135     }
136 
137     /** Return Enrolled APIs of given enrollment in string format */
138     @Nullable
getEnrolledAPIsString()139     public String getEnrolledAPIsString() {
140         return mEnrolledAPIsString;
141     }
142 
143     /** Return list of Enrolled APIs of given enrollment */
144     @Nullable
getEnrolledAPIs()145     public List<PrivacySandboxApi> getEnrolledAPIs() {
146         return mEnrolledAPIs;
147     }
148 
149     /** List of SDKs belonging to the same enrollment. */
getSdkNames()150     public List<String> getSdkNames() {
151         return mSdkNames;
152     }
153 
154     /** Returns URLs used to register attribution sources for measurement. */
getAttributionSourceRegistrationUrl()155     public List<String> getAttributionSourceRegistrationUrl() {
156         return mAttributionSourceRegistrationUrl;
157     }
158 
159     /** Returns URLs used to register triggers for measurement. */
getAttributionTriggerRegistrationUrl()160     public List<String> getAttributionTriggerRegistrationUrl() {
161         return mAttributionTriggerRegistrationUrl;
162     }
163 
164     /** Returns URLs that the Measurement module will send Attribution reports to. */
getAttributionReportingUrl()165     public List<String> getAttributionReportingUrl() {
166         return mAttributionReportingUrl;
167     }
168 
169     /** Returns URLs used for response-based-registration for joinCustomAudience. */
getRemarketingResponseBasedRegistrationUrl()170     public List<String> getRemarketingResponseBasedRegistrationUrl() {
171         return mRemarketingResponseBasedRegistrationUrl;
172     }
173 
174     /** Returns URL used to fetch public/private keys for encrypting API requests. */
175     @Nullable
getEncryptionKeyUrl()176     public String getEncryptionKeyUrl() {
177         return mEncryptionKeyUrl;
178     }
179 
180     /**
181      * Returns the given {@code input} as a list of values split by the separator value used for all
182      * enrollment data.
183      */
184     @NonNull
splitEnrollmentInputToList(@ullable String input)185     public static List<String> splitEnrollmentInputToList(@Nullable String input) {
186         if (input == null || input.trim().isEmpty()) {
187             return Collections.emptyList();
188         }
189 
190         return Arrays.asList(input.trim().split(SEPARATOR));
191     }
192 
193     /**
194      * Returns the given {@code enrolledAPIs} as a list of {@link PrivacySandboxApi} enum values.
195      */
enrolledApisToEnums(String enrolledAPIs)196     private static List<PrivacySandboxApi> enrolledApisToEnums(String enrolledAPIs) {
197         List<PrivacySandboxApi> enrolledApiEnums = new ArrayList<PrivacySandboxApi>();
198         if (enrolledAPIs == null || enrolledAPIs.trim().isEmpty()) {
199             return enrolledApiEnums;
200         }
201 
202         String[] enrolledAPIsList = enrolledAPIs.trim().split("\\s+");
203         for (String enrolledApi : enrolledAPIsList) {
204             PrivacySandboxApi enrolledApiEnum =
205                     ENROLLMENT_API_ENUM_STRING_MAP
206                             .inverse()
207                             .getOrDefault(
208                                     enrolledApi,
209                                     /* defaultValue= */ PrivacySandboxApi
210                                             .PRIVACY_SANDBOX_API_UNKNOWN);
211             enrolledApiEnums.add(enrolledApiEnum);
212         }
213         return enrolledApiEnums;
214     }
215 
216     // LINT.IfChange(EnrollmentApiEnumStringMap)
217     public static final ImmutableBiMap<PrivacySandboxApi, String> ENROLLMENT_API_ENUM_STRING_MAP =
218             ImmutableBiMap.<PrivacySandboxApi, String>builder()
219                     .put(PrivacySandboxApi.PRIVACY_SANDBOX_API_TOPICS, "PRIVACY_SANDBOX_API_TOPICS")
220                     .put(
221                             PrivacySandboxApi.PRIVACY_SANDBOX_API_PROTECTED_AUDIENCE,
222                             "PRIVACY_SANDBOX_API_PROTECTED_AUDIENCE")
223                     .put(
224                             PrivacySandboxApi.PRIVACY_SANDBOX_API_PRIVATE_AGGREGATION,
225                             "PRIVACY_SANDBOX_API_PRIVATE_AGGREGATION")
226                     .put(
227                             PrivacySandboxApi.PRIVACY_SANDBOX_API_ATTRIBUTION_REPORTING,
228                             "PRIVACY_SANDBOX_API_ATTRIBUTION_REPORTING")
229                     .put(
230                             PrivacySandboxApi.PRIVACY_SANDBOX_API_SHARED_STORAGE,
231                             "PRIVACY_SANDBOX_API_SHARED_STORAGE")
232                     .put(
233                             PrivacySandboxApi.PRIVACY_SANDBOX_API_PROTECTED_APP_SIGNALS,
234                             "PRIVACY_SANDBOX_API_PROTECTED_APP_SIGNALS")
235                     .build();
236 
237     // LINT.ThenChange(/adservices/service-core/proto/rb_enrollment.proto:PrivacySandboxApi)
238 
239     /** Returns the builder for the instance */
240     @NonNull
cloneToBuilder()241     public EnrollmentData.Builder cloneToBuilder() {
242         return new EnrollmentData.Builder()
243                 .setEnrollmentId(this.mEnrollmentId)
244                 .setEnrolledSite(this.mEnrolledSite)
245                 .setEnrolledAPIs(this.mEnrolledAPIsString)
246                 .setSdkNames(this.mSdkNames)
247                 .setAttributionSourceRegistrationUrl(this.mAttributionSourceRegistrationUrl)
248                 .setAttributionTriggerRegistrationUrl(this.mAttributionTriggerRegistrationUrl)
249                 .setAttributionReportingUrl(this.mAttributionReportingUrl)
250                 .setRemarketingResponseBasedRegistrationUrl(
251                         this.mRemarketingResponseBasedRegistrationUrl)
252                 .setEncryptionKeyUrl(this.mEncryptionKeyUrl);
253     }
254 
255     /** Builder for {@link EnrollmentData}. */
256     public static final class Builder {
257         private final EnrollmentData mBuilding;
258 
Builder()259         public Builder() {
260             mBuilding = new EnrollmentData();
261         }
262 
263         /** See {@link EnrollmentData#getEnrollmentId()}. */
setEnrollmentId(String enrollmentId)264         public Builder setEnrollmentId(String enrollmentId) {
265             mBuilding.mEnrollmentId = enrollmentId;
266             return this;
267         }
268 
269         /** See {@link EnrollmentData#getEnrolledSite()}. */
setEnrolledSite(String enrolledSite)270         public Builder setEnrolledSite(String enrolledSite) {
271             mBuilding.mEnrolledSite = enrolledSite;
272             return this;
273         }
274 
275         /** See {@link EnrollmentData#getEnrolledAPIs()}. */
setEnrolledAPIs(String enrolledAPIs)276         public Builder setEnrolledAPIs(String enrolledAPIs) {
277             mBuilding.mEnrolledAPIsString = enrolledAPIs;
278             mBuilding.mEnrolledAPIs = enrolledApisToEnums(enrolledAPIs);
279             return this;
280         }
281 
282         /** See {@link EnrollmentData#getSdkNames()} */
setSdkNames(List<String> sdkNames)283         public Builder setSdkNames(List<String> sdkNames) {
284             mBuilding.mSdkNames = sdkNames;
285             return this;
286         }
287 
288         /** See {@link EnrollmentData#getSdkNames()} */
setSdkNames(String sdkNames)289         public Builder setSdkNames(String sdkNames) {
290             mBuilding.mSdkNames = splitEnrollmentInputToList(sdkNames);
291             return this;
292         }
293 
294         /** See {@link EnrollmentData#getAttributionSourceRegistrationUrl()}. */
setAttributionSourceRegistrationUrl( List<String> attributionSourceRegistrationUrl)295         public Builder setAttributionSourceRegistrationUrl(
296                 List<String> attributionSourceRegistrationUrl) {
297             mBuilding.mAttributionSourceRegistrationUrl = attributionSourceRegistrationUrl;
298             return this;
299         }
300 
301         /** See {@link EnrollmentData#getAttributionSourceRegistrationUrl()}. */
setAttributionSourceRegistrationUrl( String attributionSourceRegistrationUrl)302         public Builder setAttributionSourceRegistrationUrl(
303                 String attributionSourceRegistrationUrl) {
304             mBuilding.mAttributionSourceRegistrationUrl =
305                     splitEnrollmentInputToList(attributionSourceRegistrationUrl);
306             return this;
307         }
308 
309         /** See {@link EnrollmentData#getAttributionTriggerRegistrationUrl()}. */
setAttributionTriggerRegistrationUrl( List<String> attributionTriggerRegistrationUrl)310         public Builder setAttributionTriggerRegistrationUrl(
311                 List<String> attributionTriggerRegistrationUrl) {
312             mBuilding.mAttributionTriggerRegistrationUrl = attributionTriggerRegistrationUrl;
313             return this;
314         }
315 
316         /** See {@link EnrollmentData#getAttributionTriggerRegistrationUrl()}. */
setAttributionTriggerRegistrationUrl( String attributionTriggerRegistrationUrl)317         public Builder setAttributionTriggerRegistrationUrl(
318                 String attributionTriggerRegistrationUrl) {
319             mBuilding.mAttributionTriggerRegistrationUrl =
320                     splitEnrollmentInputToList(attributionTriggerRegistrationUrl);
321             return this;
322         }
323 
324         /** See {@link EnrollmentData#getAttributionReportingUrl()}. */
setAttributionReportingUrl(List<String> attributionReportingUrl)325         public Builder setAttributionReportingUrl(List<String> attributionReportingUrl) {
326             mBuilding.mAttributionReportingUrl = attributionReportingUrl;
327             return this;
328         }
329 
330         /** See {@link EnrollmentData#getAttributionReportingUrl()}. */
setAttributionReportingUrl(String attributionReportingUrl)331         public Builder setAttributionReportingUrl(String attributionReportingUrl) {
332             mBuilding.mAttributionReportingUrl =
333                     splitEnrollmentInputToList(attributionReportingUrl);
334             return this;
335         }
336 
337         /** See {@link EnrollmentData#getRemarketingResponseBasedRegistrationUrl()}. */
setRemarketingResponseBasedRegistrationUrl( List<String> remarketingResponseBasedRegistrationUrl)338         public Builder setRemarketingResponseBasedRegistrationUrl(
339                 List<String> remarketingResponseBasedRegistrationUrl) {
340             mBuilding.mRemarketingResponseBasedRegistrationUrl =
341                     remarketingResponseBasedRegistrationUrl;
342             return this;
343         }
344 
345         /** See {@link EnrollmentData#getRemarketingResponseBasedRegistrationUrl()}. */
setRemarketingResponseBasedRegistrationUrl( String remarketingResponseBasedRegistrationUrl)346         public Builder setRemarketingResponseBasedRegistrationUrl(
347                 String remarketingResponseBasedRegistrationUrl) {
348             mBuilding.mRemarketingResponseBasedRegistrationUrl =
349                     splitEnrollmentInputToList(remarketingResponseBasedRegistrationUrl);
350             return this;
351         }
352 
353         /** See {@link EnrollmentData#getEncryptionKeyUrl()}. */
setEncryptionKeyUrl(String encryptionKeyUrl)354         public Builder setEncryptionKeyUrl(String encryptionKeyUrl) {
355             mBuilding.mEncryptionKeyUrl = encryptionKeyUrl;
356             return this;
357         }
358 
359         /** Builder the {@link EnrollmentData}. */
build()360         public EnrollmentData build() {
361             return mBuilding;
362         }
363     }
364 }
365