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