forked from WebKit/WebKit-http
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdClickAttributionManager.cpp
342 lines (285 loc) · 16.3 KB
/
AdClickAttributionManager.cpp
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*
* Copyright (C) 2019 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "AdClickAttributionManager.h"
#include "Logging.h"
#include <JavaScriptCore/ConsoleTypes.h>
#include <WebCore/FetchOptions.h>
#include <WebCore/FormData.h>
#include <WebCore/ResourceError.h>
#include <WebCore/ResourceRequest.h>
#include <WebCore/ResourceResponse.h>
#include <WebCore/RuntimeApplicationChecks.h>
#include <WebCore/RuntimeEnabledFeatures.h>
#include <wtf/text/StringBuilder.h>
#include <wtf/text/StringHash.h>
namespace WebKit {
using namespace WebCore;
using Source = AdClickAttribution::Source;
using Destination = AdClickAttribution::Destination;
using DestinationMap = HashMap<Destination, AdClickAttribution>;
using Conversion = AdClickAttribution::Conversion;
constexpr Seconds debugModeSecondsUntilSend { 60_s };
void AdClickAttributionManager::storeUnconverted(AdClickAttribution&& attribution)
{
clearExpired();
if (UNLIKELY(debugModeEnabled())) {
RELEASE_LOG_INFO(AdClickAttribution, "Storing an ad click.");
m_networkProcess->broadcastConsoleMessage(m_sessionID, MessageSource::AdClickAttribution, MessageLevel::Error, "[Ad Click Attribution] Storing an ad click."_s);
}
m_unconvertedAdClickAttributionMap.set(std::make_pair(attribution.source(), attribution.destination()), WTFMove(attribution));
}
void AdClickAttributionManager::handleConversion(Conversion&& conversion, const URL& requestURL, const WebCore::ResourceRequest& redirectRequest)
{
if (m_sessionID.isEphemeral())
return;
RegistrableDomain redirectDomain { redirectRequest.url() };
auto& firstPartyURL = redirectRequest.firstPartyForCookies();
if (!redirectDomain.matches(requestURL)) {
if (UNLIKELY(debugModeEnabled())) {
RELEASE_LOG_INFO(AdClickAttribution, "Conversion was not accepted because the HTTP redirect was not same-site.");
m_networkProcess->broadcastConsoleMessage(m_sessionID, MessageSource::AdClickAttribution, MessageLevel::Error, "[Ad Click Attribution] Conversion was not accepted because the HTTP redirect was not same-site."_s);
}
return;
}
if (redirectDomain.matches(firstPartyURL)) {
if (UNLIKELY(debugModeEnabled())) {
RELEASE_LOG_INFO(AdClickAttribution, "Conversion was not accepted because it was requested in an HTTP redirect that is same-site as the first-party.");
m_networkProcess->broadcastConsoleMessage(m_sessionID, MessageSource::AdClickAttribution, MessageLevel::Error, "[Ad Click Attribution] Conversion was not accepted because it was requested in an HTTP redirect that is same-site as the first-party."_s);
}
return;
}
convert(AdClickAttribution::Source { WTFMove(redirectDomain) }, AdClickAttribution::Destination { firstPartyURL }, WTFMove(conversion));
}
void AdClickAttributionManager::startTimer(Seconds seconds)
{
m_firePendingConversionRequestsTimer.startOneShot(m_isRunningTest ? 0_s : seconds);
}
void AdClickAttributionManager::convert(const Source& source, const Destination& destination, Conversion&& conversion)
{
clearExpired();
if (!conversion.isValid()) {
if (UNLIKELY(debugModeEnabled())) {
RELEASE_LOG_INFO(AdClickAttribution, "Got an invalid conversion.");
m_networkProcess->broadcastConsoleMessage(m_sessionID, MessageSource::AdClickAttribution, MessageLevel::Error, "[Ad Click Attribution] Got an invalid conversion."_s);
}
return;
}
auto conversionData = conversion.data;
auto conversionPriority = conversion.priority;
if (UNLIKELY(debugModeEnabled())) {
RELEASE_LOG_INFO(AdClickAttribution, "Got a conversion with conversion data: %{public}u and priority: %{public}u.", conversionData, conversionPriority);
m_networkProcess->broadcastConsoleMessage(m_sessionID, MessageSource::AdClickAttribution, MessageLevel::Info, makeString("[Ad Click Attribution] Got a conversion with conversion data: '"_s, conversionData, "' and priority: '"_s, conversionPriority, "'."_s));
}
auto secondsUntilSend = Seconds::infinity();
auto pair = std::make_pair(source, destination);
auto previouslyUnconvertedAttribution = m_unconvertedAdClickAttributionMap.take(pair);
auto previouslyConvertedAttributionIter = m_convertedAdClickAttributionMap.find(pair);
if (!previouslyUnconvertedAttribution.isEmpty()) {
// Always convert the pending attribution and remove it from the unconverted map.
if (auto optionalSecondsUntilSend = previouslyUnconvertedAttribution.convertAndGetEarliestTimeToSend(WTFMove(conversion))) {
secondsUntilSend = *optionalSecondsUntilSend;
ASSERT(secondsUntilSend != Seconds::infinity());
if (UNLIKELY(debugModeEnabled())) {
RELEASE_LOG_INFO(AdClickAttribution, "Converted a stored ad click with conversion data: %{public}u and priority: %{public}u.", conversionData, conversionPriority);
m_networkProcess->broadcastConsoleMessage(m_sessionID, MessageSource::AdClickAttribution, MessageLevel::Info, makeString("[Ad Click Attribution] Converted a stored ad click with conversion data: '"_s, conversionData, "' and priority: '"_s, conversionPriority, "'."_s));
}
}
if (previouslyConvertedAttributionIter == m_convertedAdClickAttributionMap.end())
m_convertedAdClickAttributionMap.add(pair, WTFMove(previouslyUnconvertedAttribution));
else if (previouslyUnconvertedAttribution.hasHigherPriorityThan(previouslyConvertedAttributionIter->value)) {
// If the newly converted attribution has higher priority, replace the old one.
m_convertedAdClickAttributionMap.set(pair, WTFMove(previouslyUnconvertedAttribution));
if (UNLIKELY(debugModeEnabled())) {
RELEASE_LOG_INFO(AdClickAttribution, "Replaced a previously converted ad click with a new one with conversion data: %{public}u and priority: %{public}u because it had higher priority.", conversionData, conversionPriority);
m_networkProcess->broadcastConsoleMessage(m_sessionID, MessageSource::AdClickAttribution, MessageLevel::Info, makeString("[Ad Click Attribution] Replaced a previously converted ad click with a new one with conversion data: '"_s, conversionData, "' and priority: '"_s, conversionPriority, "' because it had higher priority."_s));
}
}
} else if (previouslyConvertedAttributionIter != m_convertedAdClickAttributionMap.end()) {
// If we have no newly converted attribution, re-convert the old one to respect the new priority.
if (auto optionalSecondsUntilSend = previouslyConvertedAttributionIter->value.convertAndGetEarliestTimeToSend(WTFMove(conversion))) {
secondsUntilSend = *optionalSecondsUntilSend;
ASSERT(secondsUntilSend != Seconds::infinity());
if (UNLIKELY(debugModeEnabled())) {
RELEASE_LOG_INFO(AdClickAttribution, "Re-converted an ad click with a new one with conversion data: %{public}u and priority: %{public}u because it had higher priority.", conversionData, conversionPriority);
m_networkProcess->broadcastConsoleMessage(m_sessionID, MessageSource::AdClickAttribution, MessageLevel::Info, makeString("[Ad Click Attribution] Re-converted an ad click with a new one with conversion data: '"_s, conversionData, "' and priority: '"_s, conversionPriority, "'' because it had higher priority."_s));
}
}
}
if (secondsUntilSend == Seconds::infinity())
return;
if (m_firePendingConversionRequestsTimer.isActive() && m_firePendingConversionRequestsTimer.nextFireInterval() < secondsUntilSend)
return;
if (UNLIKELY(debugModeEnabled())) {
RELEASE_LOG_INFO(AdClickAttribution, "Setting timer for firing conversion requests to the debug mode timeout of %{public}f seconds where the regular timeout would have been %{public}f seconds.", debugModeSecondsUntilSend.seconds(), secondsUntilSend.seconds());
m_networkProcess->broadcastConsoleMessage(m_sessionID, MessageSource::AdClickAttribution, MessageLevel::Info, makeString("[Ad Click Attribution] Setting timer for firing conversion requests to the debug mode timeout of "_s, debugModeSecondsUntilSend.seconds(), " seconds where the regular timeout would have been "_s, secondsUntilSend.seconds(), " seconds."_s));
secondsUntilSend = debugModeSecondsUntilSend;
}
startTimer(secondsUntilSend);
}
void AdClickAttributionManager::fireConversionRequest(const AdClickAttribution& attribution)
{
auto conversionURL = m_conversionBaseURLForTesting ? attribution.urlForTesting(*m_conversionBaseURLForTesting) : attribution.url();
if (conversionURL.isEmpty() || !conversionURL.isValid())
return;
auto conversionReferrerURL = attribution.referrer();
if (conversionReferrerURL.isEmpty() || !conversionReferrerURL.isValid())
return;
ResourceRequest request { conversionURL };
request.setHTTPMethod("POST"_s);
request.setHTTPHeaderField(HTTPHeaderName::CacheControl, "max-age=0"_s);
request.setHTTPReferrer(conversionReferrerURL.string());
FetchOptions options;
options.credentials = FetchOptions::Credentials::Omit;
options.redirect = FetchOptions::Redirect::Error;
static uint64_t identifier = 0;
NetworkResourceLoadParameters loadParameters;
loadParameters.identifier = ++identifier;
loadParameters.request = request;
loadParameters.sourceOrigin = SecurityOrigin::create(conversionReferrerURL);
loadParameters.parentPID = presentingApplicationPID();
loadParameters.storedCredentialsPolicy = StoredCredentialsPolicy::EphemeralStateless;
loadParameters.options = options;
loadParameters.shouldClearReferrerOnHTTPSToHTTPRedirect = true;
loadParameters.shouldRestrictHTTPResponseAccess = false;
#if ENABLE(CONTENT_EXTENSIONS)
loadParameters.mainDocumentURL = WTFMove(conversionReferrerURL);
#endif
if (UNLIKELY(debugModeEnabled())) {
RELEASE_LOG_INFO(AdClickAttribution, "About to fire an attribution request for a conversion.");
m_networkProcess->broadcastConsoleMessage(m_sessionID, MessageSource::AdClickAttribution, MessageLevel::Error, "[Ad Click Attribution] About to fire an attribution request for a conversion."_s);
}
m_pingLoadFunction(WTFMove(loadParameters), [weakThis = makeWeakPtr(*this)](const WebCore::ResourceError& error, const WebCore::ResourceResponse& response) {
if (!weakThis)
return;
if (UNLIKELY(weakThis->debugModeEnabled())) {
if (!error.isNull()) {
#if PLATFORM(COCOA)
RELEASE_LOG_ERROR(AdClickAttribution, "Received error: '%{public}s' for ad click attribution request.", error.localizedDescription().utf8().data());
#else
RELEASE_LOG_ERROR(AdClickAttribution, "Received error: '%s' for ad click attribution request.", error.localizedDescription().utf8().data());
#endif
weakThis->m_networkProcess->broadcastConsoleMessage(weakThis->m_sessionID, MessageSource::AdClickAttribution, MessageLevel::Error, makeString("[Ad Click Attribution] Received error: '"_s, error.localizedDescription(), "' for ad click attribution request."_s));
}
}
});
}
void AdClickAttributionManager::firePendingConversionRequests()
{
auto nextTimeToFire = Seconds::infinity();
for (auto& attribution : m_convertedAdClickAttributionMap.values()) {
if (attribution.wasConversionSent()) {
ASSERT_NOT_REACHED();
continue;
}
auto earliestTimeToSend = attribution.earliestTimeToSend();
if (!earliestTimeToSend) {
ASSERT_NOT_REACHED();
continue;
}
auto now = WallTime::now();
if (*earliestTimeToSend <= now || m_isRunningTest || debugModeEnabled()) {
fireConversionRequest(attribution);
attribution.markConversionAsSent();
continue;
}
auto seconds = *earliestTimeToSend - now;
nextTimeToFire = std::min(nextTimeToFire, seconds);
}
m_convertedAdClickAttributionMap.removeIf([](auto& keyAndValue) {
return keyAndValue.value.wasConversionSent();
});
if (nextTimeToFire < Seconds::infinity())
startTimer(nextTimeToFire);
}
void AdClickAttributionManager::clear()
{
m_firePendingConversionRequestsTimer.stop();
m_unconvertedAdClickAttributionMap.clear();
m_convertedAdClickAttributionMap.clear();
}
void AdClickAttributionManager::clearForRegistrableDomain(const RegistrableDomain& domain)
{
m_unconvertedAdClickAttributionMap.removeIf([&domain](auto& keyAndValue) {
return keyAndValue.key.first.registrableDomain == domain || keyAndValue.key.second.registrableDomain == domain;
});
m_convertedAdClickAttributionMap.removeIf([&domain](auto& keyAndValue) {
return keyAndValue.key.first.registrableDomain == domain || keyAndValue.key.second.registrableDomain == domain;
});
}
void AdClickAttributionManager::clearExpired()
{
m_unconvertedAdClickAttributionMap.removeIf([](auto& keyAndValue) {
return keyAndValue.value.hasExpired();
});
}
void AdClickAttributionManager::toString(CompletionHandler<void(String)>&& completionHandler) const
{
if (m_unconvertedAdClickAttributionMap.isEmpty() && m_convertedAdClickAttributionMap.isEmpty())
return completionHandler("\nNo stored Ad Click Attribution data.\n"_s);
unsigned unconvertedAttributionNumber = 0;
StringBuilder builder;
for (auto& attribution : m_unconvertedAdClickAttributionMap.values()) {
if (!unconvertedAttributionNumber)
builder.appendLiteral("Unconverted Ad Click Attributions:\n");
else
builder.append('\n');
builder.appendLiteral("WebCore::AdClickAttribution ");
builder.appendNumber(++unconvertedAttributionNumber);
builder.append('\n');
builder.append(attribution.toString());
}
unsigned convertedAttributionNumber = 0;
for (auto& attribution : m_convertedAdClickAttributionMap.values()) {
if (unconvertedAttributionNumber)
builder.append('\n');
if (!convertedAttributionNumber)
builder.appendLiteral("Converted Ad Click Attributions:\n");
else
builder.append('\n');
builder.appendLiteral("WebCore::AdClickAttribution ");
builder.appendNumber(++convertedAttributionNumber + unconvertedAttributionNumber);
builder.append('\n');
builder.append(attribution.toString());
}
completionHandler(builder.toString());
}
void AdClickAttributionManager::setConversionURLForTesting(URL&& testURL)
{
if (testURL.isEmpty())
m_conversionBaseURLForTesting = { };
else
m_conversionBaseURLForTesting = WTFMove(testURL);
}
void AdClickAttributionManager::markAllUnconvertedAsExpiredForTesting()
{
for (auto& attribution : m_unconvertedAdClickAttributionMap.values())
attribution.markAsExpired();
}
bool AdClickAttributionManager::debugModeEnabled() const
{
return RuntimeEnabledFeatures::sharedFeatures().adClickAttributionDebugModeEnabled() && !m_sessionID.isEphemeral();
}
} // namespace WebKit