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.measurement.registration;
18 
19 import android.annotation.NonNull;
20 import android.net.Uri;
21 
22 import androidx.annotation.Nullable;
23 
24 import com.android.adservices.service.measurement.Source;
25 import com.android.adservices.service.measurement.util.Validation;
26 
27 import java.util.Objects;
28 
29 /** POJO for AsyncRegistration. */
30 public class AsyncRegistration {
31 
32     public enum RegistrationType {
33         APP_SOURCE,
34         APP_SOURCES,
35         APP_TRIGGER,
36         WEB_SOURCE,
37         WEB_TRIGGER
38     }
39 
40     private final String mId;
41     private final Uri mOsDestination;
42     private final Uri mWebDestination;
43     private final Uri mRegistrationUri;
44     private final Uri mVerifiedDestination;
45     private final Uri mTopOrigin;
46     private final Uri mRegistrant;
47     private final Source.SourceType mSourceType;
48     private long mRequestTime;
49     private long mRetryCount;
50     private final RegistrationType mType;
51     private final boolean mDebugKeyAllowed;
52     private final boolean mAdIdPermission;
53     @Nullable private String mRegistrationId;
54     @Nullable private final String mPlatformAdId;
55     @Nullable private String mPostBody;
56     private final AsyncRedirect.RedirectBehavior mRedirectBehavior;
57 
58     public enum RedirectType {
59         LOCATION,
60         LIST
61     }
62 
AsyncRegistration(@onNull AsyncRegistration.Builder builder)63     public AsyncRegistration(@NonNull AsyncRegistration.Builder builder) {
64         mId = builder.mId;
65         mOsDestination = builder.mOsDestination;
66         mWebDestination = builder.mWebDestination;
67         mRegistrationUri = builder.mRegistrationUri;
68         mVerifiedDestination = builder.mVerifiedDestination;
69         mTopOrigin = builder.mTopOrigin;
70         mRegistrant = builder.mRegistrant;
71         mSourceType = builder.mSourceType;
72         mRequestTime = builder.mRequestTime;
73         mRetryCount = builder.mRetryCount;
74         mType = builder.mType;
75         mDebugKeyAllowed = builder.mDebugKeyAllowed;
76         mAdIdPermission = builder.mAdIdPermission;
77         mRegistrationId = builder.mRegistrationId;
78         mPlatformAdId = builder.mPlatformAdId;
79         mPostBody = builder.mPostBody;
80         mRedirectBehavior = builder.mRedirectBehavior;
81     }
82 
83     @Override
equals(Object o)84     public boolean equals(Object o) {
85         if (this == o) return true;
86         if (!(o instanceof AsyncRegistration)) return false;
87         AsyncRegistration that = (AsyncRegistration) o;
88         return mRequestTime == that.mRequestTime
89                 && mRetryCount == that.mRetryCount
90                 && mDebugKeyAllowed == that.mDebugKeyAllowed
91                 && mAdIdPermission == that.mAdIdPermission
92                 && Objects.equals(mId, that.mId)
93                 && Objects.equals(mOsDestination, that.mOsDestination)
94                 && Objects.equals(mWebDestination, that.mWebDestination)
95                 && Objects.equals(mRegistrationUri, that.mRegistrationUri)
96                 && Objects.equals(mVerifiedDestination, that.mVerifiedDestination)
97                 && Objects.equals(mTopOrigin, that.mTopOrigin)
98                 && Objects.equals(mRegistrant, that.mRegistrant)
99                 && mSourceType == that.mSourceType
100                 && mType == that.mType
101                 && Objects.equals(mRegistrationId, that.mRegistrationId)
102                 && mPlatformAdId.equals(that.mPlatformAdId)
103                 && Objects.equals(mPostBody, that.mPostBody)
104                 && Objects.equals(mRedirectBehavior, that.mRedirectBehavior);
105     }
106 
107     @Override
hashCode()108     public int hashCode() {
109         return Objects.hash(
110                 mId,
111                 mOsDestination,
112                 mWebDestination,
113                 mRegistrationUri,
114                 mVerifiedDestination,
115                 mTopOrigin,
116                 mRegistrant,
117                 mSourceType,
118                 mRequestTime,
119                 mRetryCount,
120                 mType,
121                 mDebugKeyAllowed,
122                 mAdIdPermission,
123                 mRegistrationId,
124                 mPlatformAdId,
125                 mPostBody,
126                 mRedirectBehavior);
127     }
128 
129     /** Unique identifier for the {@link AsyncRegistration}. */
getId()130     public String getId() {
131         return mId;
132     }
133 
134     /** App destination of the {@link Source}. */
135     @Nullable
getOsDestination()136     public Uri getOsDestination() {
137         return mOsDestination;
138     }
139 
140     /** Web destination of the {@link Source}. */
141     @Nullable
getWebDestination()142     public Uri getWebDestination() {
143         return mWebDestination;
144     }
145 
146     /** Represents the location of registration payload. */
147     @NonNull
getRegistrationUri()148     public Uri getRegistrationUri() {
149         return mRegistrationUri;
150     }
151 
152     /** Uri used to identify and locate a {@link Source} originating from the web. */
153     @Nullable
getVerifiedDestination()154     public Uri getVerifiedDestination() {
155         return mVerifiedDestination;
156     }
157 
158     /** Package name of caller app. */
159     @NonNull
getTopOrigin()160     public Uri getTopOrigin() {
161         return mTopOrigin;
162     }
163 
164     /** Package name of caller app, name comes from context. */
165     @NonNull
getRegistrant()166     public Uri getRegistrant() {
167         return mRegistrant;
168     }
169 
170     /** Determines whether the input event was a click or view. */
getSourceType()171     public Source.SourceType getSourceType() {
172         return mSourceType;
173     }
174 
175     /** Time in ms that record arrived at Registration Queue. */
getRequestTime()176     public long getRequestTime() {
177         return mRequestTime;
178     }
179 
180     /** Retry attempt counter. */
getRetryCount()181     public long getRetryCount() {
182         return mRetryCount;
183     }
184 
185     /** Indicates how the record will be processed . */
getType()186     public RegistrationType getType() {
187         return mType;
188     }
189 
190     /** Indicates whether the debug key provided by Ad-Tech is allowed to be used or not. */
getDebugKeyAllowed()191     public boolean getDebugKeyAllowed() {
192         return mDebugKeyAllowed;
193     }
194 
195     /** Indicates whether Ad Id permission is enabled. */
hasAdIdPermission()196     public boolean hasAdIdPermission() {
197         return mAdIdPermission;
198     }
199 
200     /** Returns the registration id. */
201     @NonNull
getRegistrationId()202     public String getRegistrationId() {
203         return mRegistrationId;
204     }
205 
206     /**
207      * Returns the AdID from an app registration, to be matched with a value from a web registration
208      * response for supplying debug keys.
209      */
getPlatformAdId()210     public String getPlatformAdId() {
211         return mPlatformAdId;
212     }
213 
214     /** Returns the post body. */
215     @Nullable
getPostBody()216     public String getPostBody() {
217         return mPostBody;
218     }
219 
220     /** Return the configuration for redirect behavior. */
getRedirectBehavior()221     public AsyncRedirect.RedirectBehavior getRedirectBehavior() {
222         return mRedirectBehavior;
223     }
224 
225     /** Increments the retry count of the current record. */
incrementRetryCount()226     public void incrementRetryCount() {
227         ++mRetryCount;
228     }
229 
230     /** Indicates whether the registration runner should process redirects for this registration. */
shouldProcessRedirects()231     public boolean shouldProcessRedirects() {
232         return mType == RegistrationType.APP_SOURCE || mType == RegistrationType.APP_TRIGGER;
233     }
234 
isWebRequest()235     public boolean isWebRequest() {
236         return mType == RegistrationType.WEB_SOURCE || mType == RegistrationType.WEB_TRIGGER;
237     }
238 
isAppRequest()239     public boolean isAppRequest() {
240         return mType == RegistrationType.APP_SOURCE
241                 || mType == RegistrationType.APP_TRIGGER
242                 || mType == RegistrationType.APP_SOURCES;
243     }
244 
isSourceRequest()245     public boolean isSourceRequest() {
246         return mType == RegistrationType.APP_SOURCE
247                 || mType == RegistrationType.WEB_SOURCE
248                 || mType == RegistrationType.APP_SOURCES;
249     }
250 
isTriggerRequest()251     public boolean isTriggerRequest() {
252         return mType == RegistrationType.APP_TRIGGER || mType == RegistrationType.WEB_TRIGGER;
253     }
254 
255     /** Builder for {@link AsyncRegistration}. */
256     public static class Builder {
257         private String mId;
258         private Uri mOsDestination;
259         private Uri mWebDestination;
260         private Uri mRegistrationUri;
261         private Uri mVerifiedDestination;
262         private Uri mTopOrigin;
263         private Uri mRegistrant;
264         private Source.SourceType mSourceType;
265         private long mRequestTime;
266         private long mRetryCount = 0;
267         private AsyncRegistration.RegistrationType mType;
268         private boolean mDebugKeyAllowed;
269         private boolean mAdIdPermission;
270         @Nullable private String mRegistrationId;
271         @Nullable private String mPlatformAdId;
272         @Nullable private String mPostBody;
273         private AsyncRedirect.RedirectBehavior mRedirectBehavior;
274 
275         /** See {@link AsyncRegistration#getId()}. */
276         @NonNull
setId(@onNull String id)277         public Builder setId(@NonNull String id) {
278             Validation.validateNonNull(id);
279             mId = id;
280             return this;
281         }
282 
283         /** See {@link AsyncRegistration#getOsDestination()}. */
284         @NonNull
setOsDestination(@ullable Uri osDestination)285         public Builder setOsDestination(@Nullable Uri osDestination) {
286             mOsDestination = osDestination;
287             return this;
288         }
289 
290         /** See {@link AsyncRegistration#getRegistrationUri()}. */
291         @NonNull
setRegistrationUri(@onNull Uri registrationUri)292         public Builder setRegistrationUri(@NonNull Uri registrationUri) {
293             Validation.validateNonNull(registrationUri);
294             mRegistrationUri = registrationUri;
295             return this;
296         }
297 
298         /** See {@link AsyncRegistration#getVerifiedDestination()}. */
299         @NonNull
setVerifiedDestination(@ullable Uri verifiedDestination)300         public Builder setVerifiedDestination(@Nullable Uri verifiedDestination) {
301             mVerifiedDestination = verifiedDestination;
302             return this;
303         }
304 
305         /** See {@link AsyncRegistration#getWebDestination()}. */
306         @NonNull
setWebDestination(@ullable Uri webDestination)307         public Builder setWebDestination(@Nullable Uri webDestination) {
308             mWebDestination = webDestination;
309             return this;
310         }
311 
312         /** See {@link AsyncRegistration#getTopOrigin()}. */
313         @NonNull
setTopOrigin(@ullable Uri topOrigin)314         public Builder setTopOrigin(@Nullable Uri topOrigin) {
315             mTopOrigin = topOrigin;
316             return this;
317         }
318 
319         /** See {@link AsyncRegistration#getRegistrant()}. */
320         @NonNull
setRegistrant(@onNull Uri registrant)321         public Builder setRegistrant(@NonNull Uri registrant) {
322             Validation.validateNonNull(registrant);
323             mRegistrant = registrant;
324             return this;
325         }
326 
327         /**
328          * See {@link AsyncRegistration#getSourceType()}. Valid inputs are ordinals of {@link
329          * Source.SourceType} enum values.
330          */
331         @NonNull
setSourceType(Source.SourceType sourceType)332         public Builder setSourceType(Source.SourceType sourceType) {
333             mSourceType = sourceType;
334             return this;
335         }
336 
337         /** See {@link AsyncRegistration#getRequestTime()}. */
338         @NonNull
setRequestTime(long requestTime)339         public Builder setRequestTime(long requestTime) {
340             mRequestTime = requestTime;
341             return this;
342         }
343 
344         /** See {@link AsyncRegistration#getRetryCount()}. */
345         @NonNull
setRetryCount(long retryCount)346         public Builder setRetryCount(long retryCount) {
347             mRetryCount = retryCount;
348             return this;
349         }
350 
351         /**
352          * See {@link AsyncRegistration#getType()}. Valid inputs are ordinals of {@link
353          * AsyncRegistration.RegistrationType} enum values.
354          */
355         @NonNull
setType(RegistrationType type)356         public Builder setType(RegistrationType type) {
357             mType = type;
358             return this;
359         }
360 
361         /** See {@link AsyncRegistration#getDebugKeyAllowed()}. */
362         @NonNull
setDebugKeyAllowed(boolean debugKeyAllowed)363         public Builder setDebugKeyAllowed(boolean debugKeyAllowed) {
364             mDebugKeyAllowed = debugKeyAllowed;
365             return this;
366         }
367 
368         /** See {@link AsyncRegistration#hasAdIdPermission()}. */
369         @NonNull
setAdIdPermission(boolean adIdPermission)370         public Builder setAdIdPermission(boolean adIdPermission) {
371             mAdIdPermission = adIdPermission;
372             return this;
373         }
374 
375         /** See {@link AsyncRegistration#getRegistrationId()} */
376         @NonNull
setRegistrationId(@onNull String registrationId)377         public Builder setRegistrationId(@NonNull String registrationId) {
378             mRegistrationId = registrationId;
379             return this;
380         }
381 
382         /** See {@link AsyncRegistration#getPlatformAdId()} */
383         @NonNull
setPlatformAdId(@ullable String platformAdId)384         public Builder setPlatformAdId(@Nullable String platformAdId) {
385             mPlatformAdId = platformAdId;
386             return this;
387         }
388 
389         /** See {@link AsyncRegistration#getPlatformAdId()} */
390         @NonNull
setPostBody(@ullable String postBody)391         public Builder setPostBody(@Nullable String postBody) {
392             mPostBody = postBody;
393             return this;
394         }
395 
396         /** See {@link AsyncRegistration#getRedirectBehavior()}. */
setRedirectBehavior(AsyncRedirect.RedirectBehavior redirectBehavior)397         public Builder setRedirectBehavior(AsyncRedirect.RedirectBehavior redirectBehavior) {
398             mRedirectBehavior = redirectBehavior;
399             return this;
400         }
401 
402         /** Build the {@link AsyncRegistration}. */
403         @NonNull
build()404         public AsyncRegistration build() {
405             Objects.requireNonNull(mRegistrationId);
406             return new AsyncRegistration(this);
407         }
408     }
409 }
410