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.Build; 22 import android.os.Parcel; 23 import android.os.Parcelable; 24 25 import java.util.ArrayList; 26 import java.util.List; 27 import java.util.Objects; 28 29 /** Class to hold input to measurement trigger registration calls from web context. */ 30 public final class WebTriggerRegistrationRequest implements Parcelable { 31 private static final int WEB_TRIGGER_PARAMS_MAX_COUNT = 80; 32 33 /** Creator for Paracelable (via reflection). */ 34 @NonNull 35 public static final Parcelable.Creator<WebTriggerRegistrationRequest> CREATOR = 36 new Parcelable.Creator<WebTriggerRegistrationRequest>() { 37 @Override 38 public WebTriggerRegistrationRequest createFromParcel(Parcel in) { 39 return new WebTriggerRegistrationRequest(in); 40 } 41 42 @Override 43 public WebTriggerRegistrationRequest[] newArray(int size) { 44 return new WebTriggerRegistrationRequest[size]; 45 } 46 }; 47 /** Registration info to fetch sources. */ 48 @NonNull private final List<WebTriggerParams> mWebTriggerParams; 49 50 /** Destination {@link Uri}. */ 51 @NonNull private final Uri mDestination; 52 WebTriggerRegistrationRequest(@onNull Builder builder)53 private WebTriggerRegistrationRequest(@NonNull Builder builder) { 54 mWebTriggerParams = builder.mWebTriggerParams; 55 mDestination = builder.mDestination; 56 } 57 WebTriggerRegistrationRequest(Parcel in)58 private WebTriggerRegistrationRequest(Parcel in) { 59 Objects.requireNonNull(in); 60 ArrayList<WebTriggerParams> webTriggerParams = new ArrayList<>(); 61 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) { 62 in.readList(webTriggerParams, WebTriggerParams.class.getClassLoader()); 63 } else { 64 in.readList( 65 webTriggerParams, 66 WebTriggerParams.class.getClassLoader(), 67 WebTriggerParams.class); 68 } 69 mWebTriggerParams = webTriggerParams; 70 mDestination = Uri.CREATOR.createFromParcel(in); 71 } 72 73 @Override equals(Object o)74 public boolean equals(Object o) { 75 if (this == o) return true; 76 if (!(o instanceof WebTriggerRegistrationRequest)) return false; 77 WebTriggerRegistrationRequest that = (WebTriggerRegistrationRequest) o; 78 return Objects.equals(mWebTriggerParams, that.mWebTriggerParams) 79 && Objects.equals(mDestination, that.mDestination); 80 } 81 82 @Override hashCode()83 public int hashCode() { 84 return Objects.hash(mWebTriggerParams, mDestination); 85 } 86 87 /** Getter for trigger params. */ 88 @NonNull getTriggerParams()89 public List<WebTriggerParams> getTriggerParams() { 90 return mWebTriggerParams; 91 } 92 93 /** Getter for destination. */ 94 @NonNull getDestination()95 public Uri getDestination() { 96 return mDestination; 97 } 98 99 @Override describeContents()100 public int describeContents() { 101 return 0; 102 } 103 104 @Override writeToParcel(@onNull Parcel out, int flags)105 public void writeToParcel(@NonNull Parcel out, int flags) { 106 Objects.requireNonNull(out); 107 out.writeList(mWebTriggerParams); 108 mDestination.writeToParcel(out, flags); 109 } 110 111 /** Builder for {@link WebTriggerRegistrationRequest}. */ 112 public static final class Builder { 113 /** 114 * Registration info to fetch triggers. Maximum 80 registrations allowed at once, to be in 115 * sync with Chrome platform. 116 */ 117 @NonNull private List<WebTriggerParams> mWebTriggerParams; 118 /** Top level origin of publisher app. */ 119 @NonNull private final Uri mDestination; 120 121 /** 122 * Builder constructor for {@link WebTriggerRegistrationRequest}. 123 * 124 * @param webTriggerParams contains trigger registration parameters, the list should not be 125 * empty 126 * @param destination trigger destination {@link Uri} 127 */ Builder(@onNull List<WebTriggerParams> webTriggerParams, @NonNull Uri destination)128 public Builder(@NonNull List<WebTriggerParams> webTriggerParams, @NonNull Uri destination) { 129 Objects.requireNonNull(webTriggerParams); 130 if (webTriggerParams.isEmpty() 131 || webTriggerParams.size() > WEB_TRIGGER_PARAMS_MAX_COUNT) { 132 throw new IllegalArgumentException( 133 "web trigger params size is not within bounds, size: " 134 + webTriggerParams.size()); 135 } 136 137 Objects.requireNonNull(destination); 138 if (destination.getScheme() == null) { 139 throw new IllegalArgumentException("Destination origin must have a scheme."); 140 } 141 mWebTriggerParams = webTriggerParams; 142 mDestination = destination; 143 144 } 145 146 /** Pre-validates parameters and builds {@link WebTriggerRegistrationRequest}. */ 147 @NonNull build()148 public WebTriggerRegistrationRequest build() { 149 return new WebTriggerRegistrationRequest(this); 150 } 151 } 152 } 153