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 android.adservices.measurement;
18 
19 import android.annotation.NonNull;
20 import android.os.Parcel;
21 import android.os.Parcelable;
22 
23 import java.util.Objects;
24 
25 /**
26  * Internal trigger registration request object to communicate from {@link MeasurementManager} to
27  * {@link IMeasurementService}.
28  *
29  * @hide
30  */
31 public class WebTriggerRegistrationRequestInternal implements Parcelable {
32     /** Creator for Parcelable (via reflection). */
33     @NonNull
34     public static final Creator<WebTriggerRegistrationRequestInternal> CREATOR =
35             new Creator<WebTriggerRegistrationRequestInternal>() {
36                 @Override
37                 public WebTriggerRegistrationRequestInternal createFromParcel(Parcel in) {
38                     return new WebTriggerRegistrationRequestInternal(in);
39                 }
40 
41                 @Override
42                 public WebTriggerRegistrationRequestInternal[] newArray(int size) {
43                     return new WebTriggerRegistrationRequestInternal[size];
44                 }
45             };
46     /** Holds input to measurement trigger registration calls from web context. */
47     @NonNull private final WebTriggerRegistrationRequest mTriggerRegistrationRequest;
48     /** Holds app package info of where the request is coming from. */
49     @NonNull private final String mAppPackageName;
50     /** Holds sdk package info of where the request is coming from. */
51     @NonNull private final String mSdkPackageName;
52     /** AD ID Permission Granted. */
53     private final boolean mIsAdIdPermissionGranted;
54 
WebTriggerRegistrationRequestInternal(@onNull Builder builder)55     private WebTriggerRegistrationRequestInternal(@NonNull Builder builder) {
56         mTriggerRegistrationRequest = builder.mTriggerRegistrationRequest;
57         mAppPackageName = builder.mAppPackageName;
58         mSdkPackageName = builder.mSdkPackageName;
59         mIsAdIdPermissionGranted = builder.mIsAdIdPermissionGranted;
60     }
61 
WebTriggerRegistrationRequestInternal(Parcel in)62     private WebTriggerRegistrationRequestInternal(Parcel in) {
63         Objects.requireNonNull(in);
64         mTriggerRegistrationRequest = WebTriggerRegistrationRequest.CREATOR.createFromParcel(in);
65         mAppPackageName = in.readString();
66         mSdkPackageName = in.readString();
67         mIsAdIdPermissionGranted = in.readBoolean();
68     }
69 
70     /** Getter for {@link #mTriggerRegistrationRequest}. */
getTriggerRegistrationRequest()71     public WebTriggerRegistrationRequest getTriggerRegistrationRequest() {
72         return mTriggerRegistrationRequest;
73     }
74 
75     /** Getter for {@link #mAppPackageName}. */
getAppPackageName()76     public String getAppPackageName() {
77         return mAppPackageName;
78     }
79 
80     /** Getter for {@link #mSdkPackageName}. */
getSdkPackageName()81     public String getSdkPackageName() {
82         return mSdkPackageName;
83     }
84 
85     /** Getter for {@link #mIsAdIdPermissionGranted}. */
isAdIdPermissionGranted()86     public boolean isAdIdPermissionGranted() {
87         return mIsAdIdPermissionGranted;
88     }
89 
90     @Override
equals(Object o)91     public boolean equals(Object o) {
92         if (this == o) return true;
93         if (!(o instanceof WebTriggerRegistrationRequestInternal)) return false;
94         WebTriggerRegistrationRequestInternal that = (WebTriggerRegistrationRequestInternal) o;
95         return Objects.equals(mTriggerRegistrationRequest, that.mTriggerRegistrationRequest)
96                 && Objects.equals(mAppPackageName, that.mAppPackageName)
97                 && Objects.equals(mSdkPackageName, that.mSdkPackageName)
98                 && Objects.equals(mIsAdIdPermissionGranted, that.mIsAdIdPermissionGranted);
99     }
100 
101     @Override
hashCode()102     public int hashCode() {
103         return Objects.hash(
104                 mTriggerRegistrationRequest,
105                 mAppPackageName,
106                 mSdkPackageName,
107                 mIsAdIdPermissionGranted);
108     }
109 
110     @Override
describeContents()111     public int describeContents() {
112         return 0;
113     }
114 
115     @Override
writeToParcel(@onNull Parcel out, int flags)116     public void writeToParcel(@NonNull Parcel out, int flags) {
117         Objects.requireNonNull(out);
118         mTriggerRegistrationRequest.writeToParcel(out, flags);
119         out.writeString(mAppPackageName);
120         out.writeString(mSdkPackageName);
121         out.writeBoolean(mIsAdIdPermissionGranted);
122     }
123 
124     /** Builder for {@link WebTriggerRegistrationRequestInternal}. */
125     public static final class Builder {
126         /** External trigger registration request from client app SDK. */
127         @NonNull private final WebTriggerRegistrationRequest mTriggerRegistrationRequest;
128         /** Package name of the app used for the registration. Used to determine the registrant. */
129         @NonNull private final String mAppPackageName;
130         /** Package name of the sdk used for the registration. */
131         @NonNull private final String mSdkPackageName;
132         /** AD ID Permission Granted. */
133         private boolean mIsAdIdPermissionGranted;
134 
135         /**
136          * Builder constructor for {@link WebTriggerRegistrationRequestInternal}.
137          *
138          * @param triggerRegistrationRequest external trigger registration request
139          * @param appPackageName app package name that is calling PP API
140          * @param sdkPackageName sdk package name that is calling PP API
141          */
Builder( @onNull WebTriggerRegistrationRequest triggerRegistrationRequest, @NonNull String appPackageName, @NonNull String sdkPackageName)142         public Builder(
143                 @NonNull WebTriggerRegistrationRequest triggerRegistrationRequest,
144                 @NonNull String appPackageName,
145                 @NonNull String sdkPackageName) {
146             Objects.requireNonNull(triggerRegistrationRequest);
147             Objects.requireNonNull(appPackageName);
148             Objects.requireNonNull(sdkPackageName);
149             mTriggerRegistrationRequest = triggerRegistrationRequest;
150             mAppPackageName = appPackageName;
151             mSdkPackageName = sdkPackageName;
152         }
153 
154         /** Pre-validates parameters and builds {@link WebTriggerRegistrationRequestInternal}. */
155         @NonNull
build()156         public WebTriggerRegistrationRequestInternal build() {
157             return new WebTriggerRegistrationRequestInternal(this);
158         }
159 
160         /** See {@link WebTriggerRegistrationRequestInternal#isAdIdPermissionGranted()}. */
setAdIdPermissionGranted( boolean isAdIdPermissionGranted)161         public WebTriggerRegistrationRequestInternal.Builder setAdIdPermissionGranted(
162                 boolean isAdIdPermissionGranted) {
163             mIsAdIdPermissionGranted = isAdIdPermissionGranted;
164             return this;
165         }
166     }
167 }
168