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.net.Uri;
21 import android.os.Parcel;
22 import android.os.Parcelable;
23 
24 import java.util.Objects;
25 
26 /** Class holding trigger registration parameters. */
27 public final class WebTriggerParams implements Parcelable {
28     /** Creator for Paracelable (via reflection). */
29     @NonNull
30     public static final Creator<WebTriggerParams> CREATOR =
31             new Creator<WebTriggerParams>() {
32                 @Override
33                 public WebTriggerParams createFromParcel(Parcel in) {
34                     return new WebTriggerParams(in);
35                 }
36 
37                 @Override
38                 public WebTriggerParams[] newArray(int size) {
39                     return new WebTriggerParams[size];
40                 }
41             };
42     /**
43      * URI that the Attribution Reporting API sends a request to in order to obtain trigger
44      * registration parameters.
45      */
46     @NonNull private final Uri mRegistrationUri;
47     /**
48      * Used by the browser to indicate whether the debug key obtained from the registration URI is
49      * allowed to be used.
50      */
51     private final boolean mDebugKeyAllowed;
52 
WebTriggerParams(@onNull Builder builder)53     private WebTriggerParams(@NonNull Builder builder) {
54         mRegistrationUri = builder.mRegistrationUri;
55         mDebugKeyAllowed = builder.mDebugKeyAllowed;
56     }
57 
58     /** Unpack a TriggerRegistration from a Parcel. */
WebTriggerParams(@onNull Parcel in)59     private WebTriggerParams(@NonNull Parcel in) {
60         mRegistrationUri = Uri.CREATOR.createFromParcel(in);
61         mDebugKeyAllowed = in.readBoolean();
62     }
63 
64     @Override
equals(Object o)65     public boolean equals(Object o) {
66         if (this == o) return true;
67         if (!(o instanceof WebTriggerParams)) return false;
68         WebTriggerParams that = (WebTriggerParams) o;
69         return mDebugKeyAllowed == that.mDebugKeyAllowed
70                 && Objects.equals(mRegistrationUri, that.mRegistrationUri);
71     }
72 
73     @Override
hashCode()74     public int hashCode() {
75         return Objects.hash(mRegistrationUri, mDebugKeyAllowed);
76     }
77 
78     /** Getter for registration Uri. */
79     @NonNull
getRegistrationUri()80     public Uri getRegistrationUri() {
81         return mRegistrationUri;
82     }
83 
84     /**
85      * Getter for debug allowed/disallowed flag. Its value as {@code true} means to allow parsing
86      * debug keys from registration responses and their addition in the generated reports.
87      */
isDebugKeyAllowed()88     public boolean isDebugKeyAllowed() {
89         return mDebugKeyAllowed;
90     }
91 
92     @Override
describeContents()93     public int describeContents() {
94         return 0;
95     }
96 
97     @Override
writeToParcel(@onNull Parcel out, int flags)98     public void writeToParcel(@NonNull Parcel out, int flags) {
99         Objects.requireNonNull(out);
100         mRegistrationUri.writeToParcel(out, flags);
101         out.writeBoolean(mDebugKeyAllowed);
102     }
103 
104     /** A builder for {@link WebTriggerParams}. */
105     public static final class Builder {
106         /**
107          * URI that the Attribution Reporting API sends a request to in order to obtain trigger
108          * registration parameters.
109          */
110         @NonNull private final Uri mRegistrationUri;
111         /**
112          * Used by the browser to indicate whether the debug key obtained from the registration URI
113          * is allowed to be used.
114          */
115         private boolean mDebugKeyAllowed;
116 
117         /**
118          * Builder constructor for {@link WebTriggerParams}. {@code mIsDebugKeyAllowed} is assigned
119          * false by default.
120          *
121          * @param registrationUri URI that the Attribution Reporting API sends a request to in order
122          *     to obtain trigger registration parameters
123          * @throws IllegalArgumentException if the scheme for {@code registrationUri} is not HTTPS
124          */
Builder(@onNull Uri registrationUri)125         public Builder(@NonNull Uri registrationUri) {
126             Objects.requireNonNull(registrationUri);
127             if (registrationUri.getScheme() == null
128                     || !registrationUri.getScheme().equalsIgnoreCase("https")) {
129                 throw new IllegalArgumentException("registrationUri must have an HTTPS scheme");
130             }
131             mRegistrationUri = registrationUri;
132             mDebugKeyAllowed = false;
133         }
134 
135         /**
136          * Setter for debug allow/disallow flag. Setting it to true will allow parsing debug keys
137          * from registration responses and their addition in the generated reports.
138          *
139          * @param debugKeyAllowed used by the browser to indicate whether the debug key obtained
140          *     from the registration URI is allowed to be used
141          * @return builder
142          */
143         @NonNull
setDebugKeyAllowed(boolean debugKeyAllowed)144         public Builder setDebugKeyAllowed(boolean debugKeyAllowed) {
145             mDebugKeyAllowed = debugKeyAllowed;
146             return this;
147         }
148 
149         /**
150          * Builds immutable {@link WebTriggerParams}.
151          *
152          * @return immutable {@link WebTriggerParams}
153          */
154         @NonNull
build()155         public WebTriggerParams build() {
156             return new WebTriggerParams(this);
157         }
158     }
159 }
160