summaryrefslogtreecommitdiff
path: root/adservices/framework/java/android/adservices/adselection/ReportEventRequest.java
blob: 1f0a0ca906bc85925fcc6e40dec1a38c89b751da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*
 * Copyright (C) 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.adservices.adselection;

import static android.adservices.adselection.AdSelectionOutcome.UNSET_AD_SELECTION_ID;
import static android.adservices.adselection.AdSelectionOutcome.UNSET_AD_SELECTION_ID_MESSAGE;

import android.annotation.IntDef;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.view.InputEvent;

import com.android.internal.util.Preconditions;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.nio.charset.StandardCharsets;
import java.util.Locale;
import java.util.Objects;

/**
 * Request object wrapping the required arguments needed to report an ad event.
 */
public class ReportEventRequest {
    public static final int FLAG_REPORTING_DESTINATION_SELLER = 1 << 0;
    public static final int FLAG_REPORTING_DESTINATION_BUYER = 1 << 1;
    private static final int UNSET_REPORTING_DESTINATIONS = 0;
    private static final String UNSET_REPORTING_DESTINATIONS_MESSAGE =
            "Reporting destinations bitfield not set.";
    private static final String INVALID_REPORTING_DESTINATIONS_MESSAGE =
            "Invalid reporting destinations bitfield!";

    /** @hide */
    public static final long REPORT_EVENT_MAX_INTERACTION_DATA_SIZE_B = 64 * 1024; // 64 KB

    private static final String EVENT_DATA_SIZE_MAX_EXCEEDED =
            String.format(
                    Locale.ENGLISH,
                    "Event data should not exceed %d bytes",
                    REPORT_EVENT_MAX_INTERACTION_DATA_SIZE_B);

    private final long mAdSelectionId;
    @NonNull private final String mEventKey;
    @Nullable private final InputEvent mInputEvent;
    @NonNull private final String mEventData;
    @ReportingDestination private final int mReportingDestinations; // buyer, seller, or both

    private ReportEventRequest(@NonNull Builder builder) {
        Objects.requireNonNull(builder);

        Preconditions.checkArgument(
                builder.mAdSelectionId != UNSET_AD_SELECTION_ID, UNSET_AD_SELECTION_ID_MESSAGE);
        Preconditions.checkArgument(
                builder.mReportingDestinations != UNSET_REPORTING_DESTINATIONS,
                UNSET_REPORTING_DESTINATIONS_MESSAGE);
        Preconditions.checkArgument(
                isValidDestination(builder.mReportingDestinations),
                INVALID_REPORTING_DESTINATIONS_MESSAGE);
        Preconditions.checkArgument(
                builder.mEventData.getBytes(StandardCharsets.UTF_8).length
                        <= REPORT_EVENT_MAX_INTERACTION_DATA_SIZE_B,
                EVENT_DATA_SIZE_MAX_EXCEEDED);

        this.mAdSelectionId = builder.mAdSelectionId;
        this.mEventKey = builder.mEventKey;
        this.mInputEvent = builder.mInputEvent;
        this.mEventData = builder.mEventData;
        this.mReportingDestinations = builder.mReportingDestinations;
    }

    /**
     * Returns the adSelectionId, the primary identifier of an ad selection process.
     */
    public long getAdSelectionId() {
        return mAdSelectionId;
    }

    /**
     * Returns the event key, the type of ad event to be reported.
     *
     * <p>This field will be used to fetch the {@code reportingUri} associated with the {@code
     * eventKey} registered in {@code registerAdBeacon} after ad selection.
     *
     * <p>This field should be an exact match to the {@code eventKey} registered in {@code
     * registerAdBeacon}. Specific details about {@code registerAdBeacon} can be found at the
     * documentation of {@link AdSelectionManager#reportImpression}
     *
     * <p>The event key (when inspecting its byte array with {@link String#getBytes()}) in {@code
     * UTF-8} format should not exceed 40 bytes. Any key exceeding this limit will not be registered
     * during the {@code registerAdBeacon} call.
     */
    @NonNull
    public String getKey() {
        return mEventKey;
    }

    /**
     * Returns the input event associated with the user interaction.
     *
     * <p>This field is either {@code null}, representing a <em>view</em> event, or has an {@link
     * InputEvent} object, representing a <em>click</em> event.
     */
    @Nullable
    public InputEvent getInputEvent() {
        return mInputEvent;
    }

    /**
     * Returns the ad event data.
     *
     * <p>After ad selection, this data is generated by the caller. The caller can then call {@link
     * AdSelectionManager#reportEvent}. This data will be attached in a POST request to the {@code
     * reportingUri} registered in {@code registerAdBeacon}.
     *
     * <p>The size of {@link String#getBytes()} in {@code UTF-8} format should be below 64KB.
     */
    @NonNull
    public String getData() {
        return mEventData;
    }

    /**
     * Returns the bitfield of reporting destinations to report to (buyer, seller, or both).
     *
     * <p>To create this bitfield, place an {@code |} bitwise operator between each {@code
     * reportingDestination} to be reported to. For example to only report to buyer, set the
     * reportingDestinations field to {@link #FLAG_REPORTING_DESTINATION_BUYER} To only report to
     * seller, set the reportingDestinations field to {@link #FLAG_REPORTING_DESTINATION_SELLER} To
     * report to both buyers and sellers, set the reportingDestinations field to {@link
     * #FLAG_REPORTING_DESTINATION_BUYER} | {@link #FLAG_REPORTING_DESTINATION_SELLER}
     */
    @ReportingDestination
    public int getReportingDestinations() {
        return mReportingDestinations;
    }

    /** @hide */
    @IntDef(
            flag = true,
            prefix = {"FLAG_REPORTING_DESTINATION"},
            value = {FLAG_REPORTING_DESTINATION_SELLER, FLAG_REPORTING_DESTINATION_BUYER})
    @Retention(RetentionPolicy.SOURCE)
    public @interface ReportingDestination {}

    private static boolean isValidDestination(@ReportingDestination int reportingDestinations) {
        return 0 < reportingDestinations
                && reportingDestinations
                        <= (FLAG_REPORTING_DESTINATION_SELLER | FLAG_REPORTING_DESTINATION_BUYER);
    }

    /** Builder for {@link ReportEventRequest} objects. */
    public static final class Builder {

        private long mAdSelectionId;
        @NonNull private String mEventKey;
        @Nullable private InputEvent mInputEvent;
        @NonNull private String mEventData;
        @ReportingDestination private int mReportingDestinations; // buyer, seller, or both

        public Builder(
                long adSelectionId,
                @NonNull String eventKey,
                @NonNull String eventData,
                @ReportingDestination int reportingDestinations) {
            Objects.requireNonNull(eventKey);
            Objects.requireNonNull(eventData);

            Preconditions.checkArgument(
                    adSelectionId != UNSET_AD_SELECTION_ID, UNSET_AD_SELECTION_ID_MESSAGE);
            Preconditions.checkArgument(
                    reportingDestinations != UNSET_REPORTING_DESTINATIONS,
                    UNSET_REPORTING_DESTINATIONS_MESSAGE);
            Preconditions.checkArgument(
                    isValidDestination(reportingDestinations),
                    INVALID_REPORTING_DESTINATIONS_MESSAGE);
            Preconditions.checkArgument(
                    eventData.getBytes(StandardCharsets.UTF_8).length
                            <= REPORT_EVENT_MAX_INTERACTION_DATA_SIZE_B,
                    EVENT_DATA_SIZE_MAX_EXCEEDED);

            this.mAdSelectionId = adSelectionId;
            this.mEventKey = eventKey;
            this.mEventData = eventData;
            this.mReportingDestinations = reportingDestinations;
        }

        /**
         * Sets the ad selection ID with which the rendered ad's events are associated.
         *
         * <p>See {@link #getAdSelectionId()} for more information.
         */
        @NonNull
        public Builder setAdSelectionId(long adSelectionId) {
            Preconditions.checkArgument(
                    adSelectionId != UNSET_AD_SELECTION_ID, UNSET_AD_SELECTION_ID_MESSAGE);
            mAdSelectionId = adSelectionId;
            return this;
        }

        /**
         * Sets the event key, the type of ad event to be reported.
         *
         * <p>See {@link #getKey()} for more information.
         */
        @NonNull
        public Builder setKey(@NonNull String eventKey) {
            Objects.requireNonNull(eventKey);

            mEventKey = eventKey;
            return this;
        }

        /**
         * Sets the input event associated with the user interaction.
         *
         * <p>See {@link #getInputEvent()} for more information.
         */
        @NonNull
        public Builder setInputEvent(@Nullable InputEvent inputEvent) {
            mInputEvent = inputEvent;
            return this;
        }

        /**
         * Sets the ad event data.
         *
         * <p>See {@link #getData()} for more information.
         */
        @NonNull
        public Builder setData(@NonNull String eventData) {
            Objects.requireNonNull(eventData);

            mEventData = eventData;
            return this;
        }

        /**
         * Sets the bitfield of reporting destinations to report to (buyer, seller, or both).
         *
         * <p>See {@link #getReportingDestinations()} for more information.
         */
        @NonNull
        public Builder setReportingDestinations(@ReportingDestination int reportingDestinations) {
            Preconditions.checkArgument(
                    isValidDestination(reportingDestinations),
                    INVALID_REPORTING_DESTINATIONS_MESSAGE);

            mReportingDestinations = reportingDestinations;
            return this;
        }

        /** Builds the {@link ReportEventRequest} object. */
        @NonNull
        public ReportEventRequest build() {
            return new ReportEventRequest(this);
        }
    }
}