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.IntDef;
20 import android.annotation.NonNull;
21 import android.annotation.Nullable;
22 import android.net.Uri;
23 
24 import java.lang.annotation.Retention;
25 import java.lang.annotation.RetentionPolicy;
26 import java.time.Instant;
27 import java.util.ArrayList;
28 import java.util.List;
29 import java.util.Objects;
30 
31 /** Deletion Request. */
32 public class DeletionRequest {
33 
34     /**
35      * Deletion modes for matched records.
36      *
37      * @hide
38      */
39     @IntDef(value = {DELETION_MODE_ALL, DELETION_MODE_EXCLUDE_INTERNAL_DATA})
40     @Retention(RetentionPolicy.SOURCE)
41     public @interface DeletionMode {}
42 
43     /**
44      * Matching Behaviors for params.
45      *
46      * @hide
47      */
48     @IntDef(value = {MATCH_BEHAVIOR_DELETE, MATCH_BEHAVIOR_PRESERVE})
49     @Retention(RetentionPolicy.SOURCE)
50     public @interface MatchBehavior {}
51 
52     /** Deletion mode to delete all data associated with the selected records. */
53     public static final int DELETION_MODE_ALL = 0;
54 
55     /**
56      * Deletion mode to delete all data except the internal data (e.g. rate limits) for the selected
57      * records.
58      */
59     public static final int DELETION_MODE_EXCLUDE_INTERNAL_DATA = 1;
60 
61     /** Match behavior option to delete the supplied params (Origin/Domains). */
62     public static final int MATCH_BEHAVIOR_DELETE = 0;
63 
64     /**
65      * Match behavior option to preserve the supplied params (Origin/Domains) and delete everything
66      * else.
67      */
68     public static final int MATCH_BEHAVIOR_PRESERVE = 1;
69 
70     private final Instant mStart;
71     private final Instant mEnd;
72     private final List<Uri> mOriginUris;
73     private final List<Uri> mDomainUris;
74     private final @MatchBehavior int mMatchBehavior;
75     private final @DeletionMode int mDeletionMode;
76 
DeletionRequest(@onNull Builder builder)77     private DeletionRequest(@NonNull Builder builder) {
78         mOriginUris = builder.mOriginUris;
79         mDomainUris = builder.mDomainUris;
80         mMatchBehavior = builder.mMatchBehavior;
81         mDeletionMode = builder.mDeletionMode;
82         mStart = builder.mStart;
83         mEnd = builder.mEnd;
84     }
85 
86     /** Get the list of origin URIs. */
87     @NonNull
getOriginUris()88     public List<Uri> getOriginUris() {
89         return mOriginUris;
90     }
91 
92     /** Get the list of domain URIs. */
93     @NonNull
getDomainUris()94     public List<Uri> getDomainUris() {
95         return mDomainUris;
96     }
97 
98     /** Get the deletion mode. */
getDeletionMode()99     public @DeletionMode int getDeletionMode() {
100         return mDeletionMode;
101     }
102 
103     /** Get the match behavior. */
getMatchBehavior()104     public @MatchBehavior int getMatchBehavior() {
105         return mMatchBehavior;
106     }
107 
108     /** Get the start of the deletion range. */
109     @NonNull
getStart()110     public Instant getStart() {
111         return mStart;
112     }
113 
114     /** Get the end of the deletion range. */
115     @NonNull
getEnd()116     public Instant getEnd() {
117         return mEnd;
118     }
119 
120     /** Builder for {@link DeletionRequest} objects. */
121     public static final class Builder {
122         private Instant mStart = Instant.MIN;
123         private Instant mEnd = Instant.MAX;
124         private List<Uri> mOriginUris;
125         private List<Uri> mDomainUris;
126         @MatchBehavior private int mMatchBehavior;
127         @DeletionMode private int mDeletionMode;
128 
Builder()129         public Builder() {}
130 
131         /**
132          * Set the list of origin URI which will be used for matching. These will be matched with
133          * records using the same origin only, i.e. subdomains won't match. E.g. If originUri is
134          * {@code https://a.example.com}, then {@code https://a.example.com} will match; {@code
135          * https://example.com}, {@code https://b.example.com} and {@code https://abcexample.com}
136          * will NOT match.
137          */
setOriginUris(@ullable List<Uri> originUris)138         public @NonNull Builder setOriginUris(@Nullable List<Uri> originUris) {
139             mOriginUris = originUris;
140             return this;
141         }
142 
143         /**
144          * Set the list of domain URI which will be used for matching. These will be matched with
145          * records using the same domain or any subdomains. E.g. If domainUri is {@code
146          * https://example.com}, then {@code https://a.example.com}, {@code https://example.com} and
147          * {@code https://b.example.com} will match; {@code https://abcexample.com} will NOT match.
148          */
setDomainUris(@ullable List<Uri> domainUris)149         public @NonNull Builder setDomainUris(@Nullable List<Uri> domainUris) {
150             mDomainUris = domainUris;
151             return this;
152         }
153 
154         /**
155          * Set the match behavior for the supplied params. {@link #MATCH_BEHAVIOR_DELETE}: This
156          * option will use the supplied params (Origin URIs & Domain URIs) for selecting records for
157          * deletion. {@link #MATCH_BEHAVIOR_PRESERVE}: This option will preserve the data associated
158          * with the supplied params (Origin URIs & Domain URIs) and select remaining records for
159          * deletion.
160          */
setMatchBehavior(@atchBehavior int matchBehavior)161         public @NonNull Builder setMatchBehavior(@MatchBehavior int matchBehavior) {
162             mMatchBehavior = matchBehavior;
163             return this;
164         }
165 
166         /**
167          * Set the match behavior for the supplied params. {@link #DELETION_MODE_ALL}: All data
168          * associated with the selected records will be deleted. {@link
169          * #DELETION_MODE_EXCLUDE_INTERNAL_DATA}: All data except the internal system data (e.g.
170          * rate limits) associated with the selected records will be deleted.
171          */
setDeletionMode(@eletionMode int deletionMode)172         public @NonNull Builder setDeletionMode(@DeletionMode int deletionMode) {
173             mDeletionMode = deletionMode;
174             return this;
175         }
176 
177         /**
178          * Set the start of the deletion range. Passing in {@link java.time.Instant#MIN} will cause
179          * everything from the oldest record to the specified end be deleted. No set start will
180          * default to {@link java.time.Instant#MIN}.
181          */
setStart(@onNull Instant start)182         public @NonNull Builder setStart(@NonNull Instant start) {
183             Objects.requireNonNull(start);
184             mStart = start;
185             return this;
186         }
187 
188         /**
189          * Set the end of the deletion range. Passing in {@link java.time.Instant#MAX} will cause
190          * everything from the specified start until the newest record to be deleted. No set end
191          * will default to {@link java.time.Instant#MAX}.
192          */
setEnd(@onNull Instant end)193         public @NonNull Builder setEnd(@NonNull Instant end) {
194             Objects.requireNonNull(end);
195             mEnd = end;
196             return this;
197         }
198 
199         /** Builds a {@link DeletionRequest} instance. */
build()200         public @NonNull DeletionRequest build() {
201             if (mDomainUris == null) {
202                 mDomainUris = new ArrayList<>();
203             }
204             if (mOriginUris == null) {
205                 mOriginUris = new ArrayList<>();
206             }
207             return new DeletionRequest(this);
208         }
209     }
210 }
211