1 | /* |
2 | * Copyright (C) 2019 Apple Inc. All rights reserved. |
3 | * |
4 | * Redistribution and use in source and binary forms, with or without |
5 | * modification, are permitted provided that the following conditions |
6 | * are met: |
7 | * 1. Redistributions of source code must retain the above copyright |
8 | * notice, this list of conditions and the following disclaimer. |
9 | * 2. Redistributions in binary form must reproduce the above copyright |
10 | * notice, this list of conditions and the following disclaimer in the |
11 | * documentation and/or other materials provided with the distribution. |
12 | * |
13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
14 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
15 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
17 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
18 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
19 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
20 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
21 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
22 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
23 | * THE POSSIBILITY OF SUCH DAMAGE. |
24 | */ |
25 | |
26 | #pragma once |
27 | |
28 | #include "NetworkResourceLoadParameters.h" |
29 | #include <WebCore/AdClickAttribution.h> |
30 | #include <WebCore/RegistrableDomain.h> |
31 | #include <WebCore/ResourceError.h> |
32 | #include <WebCore/ResourceResponse.h> |
33 | #include <WebCore/Timer.h> |
34 | #include <wtf/CompletionHandler.h> |
35 | #include <wtf/HashMap.h> |
36 | #include <wtf/WeakPtr.h> |
37 | #include <wtf/text/WTFString.h> |
38 | |
39 | namespace WebKit { |
40 | |
41 | class AdClickAttributionManager : public CanMakeWeakPtr<AdClickAttributionManager> { |
42 | public: |
43 | |
44 | using RegistrableDomain = WebCore::RegistrableDomain; |
45 | using AdClickAttribution = WebCore::AdClickAttribution; |
46 | using Source = WebCore::AdClickAttribution::Source; |
47 | using Destination = WebCore::AdClickAttribution::Destination; |
48 | using Conversion = WebCore::AdClickAttribution::Conversion; |
49 | |
50 | explicit AdClickAttributionManager(PAL::SessionID sessionID) |
51 | : m_firePendingConversionRequestsTimer(*this, &AdClickAttributionManager::firePendingConversionRequests) |
52 | , m_pingLoadFunction([](NetworkResourceLoadParameters&& params, CompletionHandler<void(const WebCore::ResourceError&, const WebCore::ResourceResponse&)>&& completionHandler) { |
53 | UNUSED_PARAM(params); |
54 | completionHandler(WebCore::ResourceError(), WebCore::ResourceResponse()); |
55 | }) |
56 | , m_sessionID(sessionID) |
57 | { |
58 | } |
59 | |
60 | void storeUnconverted(AdClickAttribution&&); |
61 | void handleConversion(Conversion&&, const URL& requestURL, const WebCore::ResourceRequest& redirectRequest); |
62 | void clear(); |
63 | void clearForRegistrableDomain(const RegistrableDomain&); |
64 | void toString(CompletionHandler<void(String)>&&) const; |
65 | void setPingLoadFunction(Function<void(NetworkResourceLoadParameters&&, CompletionHandler<void(const WebCore::ResourceError&, const WebCore::ResourceResponse&)>&&)>&& pingLoadFunction) { m_pingLoadFunction = WTFMove(pingLoadFunction); } |
66 | void setOverrideTimerForTesting(bool value) { m_isRunningTest = value; } |
67 | void setConversionURLForTesting(URL&&); |
68 | void markAllUnconvertedAsExpiredForTesting(); |
69 | |
70 | private: |
71 | void startTimer(Seconds); |
72 | void convert(const Source&, const Destination&, Conversion&&); |
73 | void fireConversionRequest(const AdClickAttribution&); |
74 | void firePendingConversionRequests(); |
75 | void clearExpired(); |
76 | bool debugModeEnabled() const; |
77 | |
78 | HashMap<std::pair<Source, Destination>, AdClickAttribution> m_unconvertedAdClickAttributionMap; |
79 | HashMap<std::pair<Source, Destination>, AdClickAttribution> m_convertedAdClickAttributionMap; |
80 | WebCore::Timer m_firePendingConversionRequestsTimer; |
81 | Function<void(NetworkResourceLoadParameters&&, CompletionHandler<void(const WebCore::ResourceError&, const WebCore::ResourceResponse&)>&&)> m_pingLoadFunction; |
82 | bool m_isRunningTest { false }; |
83 | Optional<URL> m_conversionBaseURLForTesting; |
84 | PAL::SessionID m_sessionID; |
85 | }; |
86 | |
87 | } // namespace WebKit |
88 | |