-
Notifications
You must be signed in to change notification settings - Fork 11
/
seller_code_wrapper.h
206 lines (197 loc) · 9.32 KB
/
seller_code_wrapper.h
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
// Copyright 2023 Google LLC
//
// 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.
#ifndef FLEDGE_SERVICES_SELLER_CODE_WRAPPER_H_
#define FLEDGE_SERVICES_SELLER_CODE_WRAPPER_H_
#include <string>
#include "absl/container/flat_hash_map.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
namespace privacy_sandbox::bidding_auction_servers {
inline constexpr char kReportWinWrapperNamePlaceholder[] =
"$reportWinWrapperName";
inline constexpr char kExtraArgs[] = "$extraArgs";
inline constexpr char kSuffix[] = "$suffix";
inline constexpr char kProtectedAppSignalsTag[] = "ProtectedAppSignals";
inline constexpr char kEgressPayloadTag[] =
"egressPayload, temporaryUnlimitedEgressPayload";
inline constexpr char kReportWinCodePlaceholder[] = "$reportWinCode";
inline constexpr char kReportWinWrapperFunctionName[] = "reportWinWrapper";
// The function that will be called by Roma to generate reporting urls.
// The dispatch function name will be reportingEntryFunction.
// This wrapper supports the features below:
//- Event level and fenced frame reporting for seller: reportResult() function
// execution
//- Event level and fenced frame reporting for buyer: reportWin() function
// execution
//- Exporting console.logs from the AdTech script execution
inline constexpr absl::string_view kReportingEntryFunction =
R"JSCODE(
//Handler method to call adTech provided reportResult method and wrap the
// response with reportResult url and interaction reporting urls.
function reportingEntryFunction$suffix(auctionConfig, sellerReportingSignals, directFromSellerSignals, enable_logging, buyerReportingMetadata, $extraArgs) {
ps_signalsForWinner = ""
const ps_report_result_response = {
reportResultUrl : "",
interactionReportingUrls : {},
sendReportToInvoked : false,
registerAdBeaconInvoked : false,
}
const ps_logs = [];
const ps_errors = [];
const ps_warns = [];
if (enable_logging) {
console.log = (...args) => ps_logs.push(JSON.stringify(args));
console.warn = (...args) => ps_warns.push(JSON.stringify(args));
console.error = (...args) => ps_errors.push(JSON.stringify(args));
} else {
console.log = console.warn = console.error = function() {};
}
globalThis.sendReportTo = function sendReportTo(url){
if(ps_report_result_response.sendReportToInvoked) {
throw new Error("sendReportTo function invoked more than once");
}
ps_report_result_response.reportResultUrl = url;
ps_report_result_response.sendReportToInvoked = true;
}
globalThis.registerAdBeacon = function registerAdBeacon(eventUrlMap){
if(ps_report_result_response.registerAdBeaconInvoked) {
throw new Error("registerAdBeaconInvoked function invoked more than once");
}
ps_report_result_response.interactionReportingUrls=eventUrlMap;
ps_report_result_response.registerAdBeaconInvoked = true;
}
ps_signalsForWinner = reportResult(auctionConfig, sellerReportingSignals, directFromSellerSignals);
try{
if(buyerReportingMetadata.enableReportWinUrlGeneration){
var buyerOrigin = buyerReportingMetadata.buyerOrigin
var functionSuffix = buyerOrigin.replace(/[^a-zA-Z0-9 ]/g, "")
var auctionSignals = auctionConfig.auctionSignals
var buyerReportingSignals = sellerReportingSignals
delete buyerReportingSignals.desirability
if(buyerReportingMetadata.hasOwnProperty("interestGroupName")){
buyerReportingSignals.interestGroupName = buyerReportingMetadata.interestGroupName
}
buyerReportingSignals.madeHighestScoringOtherBid = buyerReportingMetadata.madeHighestScoringOtherBid
buyerReportingSignals.joinCount = buyerReportingMetadata.joinCount
buyerReportingSignals.recency = buyerReportingMetadata.recency
buyerReportingSignals.modelingSignals = buyerReportingMetadata.modelingSignals
perBuyerSignals = buyerReportingMetadata.perBuyerSignals
buyerReportingSignals.seller = buyerReportingMetadata.seller
buyerReportingSignals.adCost = buyerReportingMetadata.adCost
if(buyerReportingMetadata.hasOwnProperty("buyerReportingId")){
buyerReportingSignals.buyerReportingId = buyerReportingMetadata.buyerReportingId
}
// Absence of interest group indicates that this is a protected app
// signals ad.
if (buyerReportingMetadata.enableProtectedAppSignals &&
(buyerReportingSignals.interestGroupName === null ||
buyerReportingSignals.interestGroupName.trim() === "")) {
functionSuffix += "ProtectedAppSignals";
}
var reportWinFunction = "reportWinWrapper"+functionSuffix+"(auctionSignals, perBuyerSignals, ps_signalsForWinner, buyerReportingSignals,"+
"directFromSellerSignals, enable_logging, $extraArgs)"
var reportWinResponse = eval(reportWinFunction)
return {
reportResultResponse: ps_report_result_response,
sellerLogs: ps_logs,
sellerErrors: ps_errors,
sellerWarnings: ps_warns,
reportWinResponse: reportWinResponse.response,
buyerLogs: reportWinResponse.buyerLogs,
buyerErrors: reportWinResponse.buyerErrors,
buyerWarnings: reportWinResponse.buyerWarnings,
}
}
} catch(ex){
console.error(ex.message)
}
return {
reportResultResponse: ps_report_result_response,
sellerLogs: ps_logs,
sellerErrors: ps_errors,
sellerWarnings: ps_warns,
}
}
)JSCODE";
inline constexpr absl::string_view kReportingWinWrapperTemplate =
R"JSCODE(
// Handler method to call adTech provided reportWin method and wrap the
// response with reportWin url and interaction reporting urls.
function $reportWinWrapperName(auctionSignals, perBuyerSignals, signalsForWinner, buyerReportingSignals,
directFromSellerSignals, enable_logging, $extraArgs) {
const ps_report_win_response = {
reportWinUrl : "",
interactionReportingUrls : {},
sendReportToInvoked : false,
registerAdBeaconInvoked : false,
}
const ps_buyer_logs = [];
const ps_buyer_error_logs = [];
const ps_buyer_warning_logs = [];
if (enable_logging) {
console.log = (...args) => ps_buyer_logs.push(JSON.stringify(args));
console.warn = (...args) => ps_buyer_warning_logs.push(JSON.stringify(args));
console.error = (...args) => ps_buyer_error_logs.push(JSON.stringify(args));
} else {
console.log = console.warn = console.error = function() {};
}
globalThis.sendReportTo = function sendReportTo(url){
if(ps_report_win_response.sendReportToInvoked) {
throw new Error("sendReportTo function invoked more than once");
}
ps_report_win_response.reportWinUrl = url;
ps_report_win_response.sendReportToInvoked = true;
}
globalThis.registerAdBeacon = function registerAdBeacon(eventUrlMap){
if(ps_report_win_response.registerAdBeaconInvoked) {
throw new Error("registerAdBeaconInvoked function invoked more than once");
}
ps_report_win_response.interactionReportingUrls = eventUrlMap;
ps_report_win_response.registerAdBeaconInvoked = true;
}
{
$reportWinCode
}
try{
reportWin(auctionSignals, perBuyerSignals, signalsForWinner, buyerReportingSignals,
directFromSellerSignals, $extraArgs)
} catch(ex){
console.error(ex.message)
}
return {
response: ps_report_win_response,
buyerLogs: ps_buyer_logs,
buyerErrors: ps_buyer_error_logs,
buyerWarnings: ps_buyer_warning_logs
}
}
)JSCODE";
// Returns the complete wrapped code for Seller.
// The function adds wrappers to the Seller provided script. This enables:
// - Generation of event level reporting urls for Seller
// - Generation of event level reporting urls for all the Buyers
// - Generation of event level debug reporting
// - Exporting console.logs from the AdTech execution.
std::string GetSellerWrappedCode(
absl::string_view seller_js_code, bool enable_report_result_url_generation,
bool enable_report_win_url_generation,
const absl::flat_hash_map<std::string, std::string>& buyer_origin_code_map);
std::string GetSellerWrappedCode(
absl::string_view seller_js_code, bool enable_report_result_url_generation,
bool enable_protected_app_signals, bool enable_report_win_url_generation,
const absl::flat_hash_map<std::string, std::string>& buyer_origin_code_map,
const absl::flat_hash_map<std::string, std::string>&
protected_app_signals_buyer_origin_code_map);
} // namespace privacy_sandbox::bidding_auction_servers
#endif // FLEDGE_SERVICES_SELLER_CODE_WRAPPER_H_