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