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 package com.android.adservices.service.measurement.reporting;
17 
18 import android.annotation.NonNull;
19 import android.annotation.Nullable;
20 import android.net.Uri;
21 
22 import org.json.JSONException;
23 import org.json.JSONObject;
24 
25 import java.util.Objects;
26 
27 /** Debug Report. */
28 public final class DebugReport {
29     private final String mId;
30     private final String mType;
31     private final JSONObject mBody;
32     private final String mEnrollmentId;
33     private final Uri mRegistrationOrigin;
34     private final String mReferenceId;
35     private final Long mInsertionTime;
36     private final Uri mRegistrant;
37 
38     /** Create a new debug report object. */
DebugReport( @ullable String id, @NonNull String type, @NonNull JSONObject body, @NonNull String enrollmentId, @NonNull Uri registrationOrigin, @Nullable String referenceId, @Nullable Uri registrant, @Nullable Long insertionTime)39     private DebugReport(
40             @Nullable String id,
41             @NonNull String type,
42             @NonNull JSONObject body,
43             @NonNull String enrollmentId,
44             @NonNull Uri registrationOrigin,
45             @Nullable String referenceId,
46             @Nullable Uri registrant,
47             @Nullable Long insertionTime) {
48         mId = id;
49         mType = type;
50         mBody = body;
51         mEnrollmentId = enrollmentId;
52         mRegistrationOrigin = registrationOrigin;
53         mReferenceId = referenceId;
54         mInsertionTime = insertionTime;
55         mRegistrant = registrant;
56     }
57 
58     @Override
equals(Object obj)59     public boolean equals(Object obj) {
60         // TODO (b/300109438) Investigate DebugReport::equals
61         if (!(obj instanceof DebugReport key)) {
62             return false;
63         }
64         return Objects.equals(mType, key.mType)
65                 && Objects.equals(mBody, key.mBody)
66                 && Objects.equals(mEnrollmentId, key.mEnrollmentId)
67                 && Objects.equals(mRegistrationOrigin, key.mRegistrationOrigin)
68                 && Objects.equals(mReferenceId, key.mReferenceId)
69                 && Objects.equals(mRegistrant, key.mRegistrant)
70                 && Objects.equals(mInsertionTime, key.mInsertionTime);
71     }
72 
73     @Override
hashCode()74     public int hashCode() {
75         return Objects.hash(
76                 mType,
77                 mBody,
78                 mEnrollmentId,
79                 mRegistrationOrigin,
80                 mReferenceId,
81                 mRegistrant,
82                 mInsertionTime);
83     }
84 
85     /** Unique identifier for the {@link DebugReport}. */
getId()86     public String getId() {
87         return mId;
88     }
89 
90     /** Type of debug report. */
getType()91     public String getType() {
92         return mType;
93     }
94 
95     /** Body of debug report. */
getBody()96     public JSONObject getBody() {
97         return mBody;
98     }
99 
100     /** AdTech enrollment ID. */
getEnrollmentId()101     public String getEnrollmentId() {
102         return mEnrollmentId;
103     }
104     /** Registration Origin URL */
getRegistrationOrigin()105     public Uri getRegistrationOrigin() {
106         return mRegistrationOrigin;
107     }
108     /** Reference ID for grouping reports. */
getReferenceId()109     public String getReferenceId() {
110         return mReferenceId;
111     }
112     /** Datastore Insertion time */
getInsertionTime()113     public Long getInsertionTime() {
114         return mInsertionTime;
115     }
116     /** Associated Source App Identifier */
getRegistrant()117     public Uri getRegistrant() {
118         return mRegistrant;
119     }
120 
121     /** Generate the JSON serialization of the debug report payload. */
toPayloadJson()122     public JSONObject toPayloadJson() throws JSONException {
123         JSONObject jsonObject = new JSONObject();
124         jsonObject.put("type", mType);
125         jsonObject.put("body", mBody);
126         return jsonObject;
127     }
128 
129     /** A builder for {@link DebugReport}. */
130     public static final class Builder {
131         private String mId;
132         private String mType;
133         private JSONObject mBody;
134         private String mEnrollmentId;
135         private Uri mRegistrationOrigin;
136         private String mReferenceId;
137         private Long mInsertionTime;
138         private Uri mRegistrant;
139 
Builder()140         public Builder() {}
141 
142         /** See {@link DebugReport#getId()}. */
setId(String id)143         public Builder setId(String id) {
144             mId = id;
145             return this;
146         }
147 
148         /** See {@link DebugReport#getType}. */
149         @NonNull
setType(@onNull String type)150         public Builder setType(@NonNull String type) {
151             mType = type;
152             return this;
153         }
154 
155         /** See {@link DebugReport#getBody}. */
156         @NonNull
setBody(@onNull String body)157         public Builder setBody(@NonNull String body) {
158             try {
159                 mBody = new JSONObject(body);
160             } catch (JSONException e) {
161                 throw new IllegalArgumentException("Invalid debug report body json");
162             }
163             return this;
164         }
165 
166         /** See {@link DebugReport#getBody}. */
167         @NonNull
setBody(@onNull JSONObject body)168         public Builder setBody(@NonNull JSONObject body) {
169             mBody = body;
170             return this;
171         }
172 
173         /** See {@link DebugReport#getEnrollmentId()}. */
174         @NonNull
setEnrollmentId(String enrollmentId)175         public Builder setEnrollmentId(String enrollmentId) {
176             mEnrollmentId = enrollmentId;
177             return this;
178         }
179 
180         /** See {@link DebugReport#getRegistrationOrigin()} ()}. */
181         @NonNull
setRegistrationOrigin(Uri registrationOrigin)182         public Builder setRegistrationOrigin(Uri registrationOrigin) {
183             mRegistrationOrigin = registrationOrigin;
184             return this;
185         }
186 
187         /** See {@link DebugReport#getReferenceId()} ()}. */
188         @NonNull
setReferenceId(String referenceId)189         public Builder setReferenceId(String referenceId) {
190             mReferenceId = referenceId;
191             return this;
192         }
193 
194         /** See {@link DebugReport#getInsertionTime()} ()}. */
195         @NonNull
setInsertionTime(Long insertionTime)196         public Builder setInsertionTime(Long insertionTime) {
197             mInsertionTime = insertionTime;
198             return this;
199         }
200 
201         /** See {@link DebugReport#getRegistrant()} ()}. */
202         @NonNull
setRegistrant(Uri registrant)203         public Builder setRegistrant(Uri registrant) {
204             mRegistrant = registrant;
205             return this;
206         }
207 
208         /** Build the DebugReport. */
209         @NonNull
build()210         public DebugReport build() {
211             if (mType == null || mBody == null) {
212                 throw new IllegalArgumentException("Uninitialized fields");
213             }
214             return new DebugReport(
215                     mId,
216                     mType,
217                     mBody,
218                     mEnrollmentId,
219                     mRegistrationOrigin,
220                     mReferenceId,
221                     mRegistrant,
222                     mInsertionTime);
223         }
224     }
225 }
226