1/*
2 * Copyright (C) 2017-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#if ENABLE(RESOURCE_LOAD_STATISTICS)
29
30#include "ResourceLoadStatisticsStore.h"
31#include "WebResourceLoadStatisticsStore.h"
32#include <wtf/CompletionHandler.h>
33#include <wtf/Vector.h>
34#include <wtf/WeakPtr.h>
35#include <wtf/WorkQueue.h>
36
37namespace WebCore {
38class KeyedDecoder;
39class KeyedEncoder;
40enum class StorageAccessPromptWasShown : bool;
41enum class StorageAccessWasGranted : bool;
42struct ResourceLoadStatistics;
43}
44
45namespace WebKit {
46
47class ResourceLoadStatisticsPersistentStorage;
48
49// This is always constructed / used / destroyed on the WebResourceLoadStatisticsStore's statistics queue.
50class ResourceLoadStatisticsMemoryStore final : public ResourceLoadStatisticsStore {
51public:
52 ResourceLoadStatisticsMemoryStore(WebResourceLoadStatisticsStore&, WorkQueue&, ShouldIncludeLocalhost);
53
54 void setPersistentStorage(ResourceLoadStatisticsPersistentStorage&);
55
56 void clear(CompletionHandler<void()>&&) override;
57 bool isEmpty() const override;
58
59 const HashMap<RegistrableDomain, WebCore::ResourceLoadStatistics>& data() const { return m_resourceStatisticsMap; }
60
61 std::unique_ptr<WebCore::KeyedEncoder> createEncoderFromData() const;
62 void mergeWithDataFromDecoder(WebCore::KeyedDecoder&);
63
64 void mergeStatistics(Vector<ResourceLoadStatistics>&&);
65 void processStatistics(const Function<void(const ResourceLoadStatistics&)>&) const;
66
67 void updateCookieBlocking(CompletionHandler<void()>&&) override;
68
69 void classifyPrevalentResources() override;
70 void syncStorageIfNeeded() override;
71 void syncStorageImmediately() override;
72
73 void requestStorageAccessUnderOpener(DomainInNeedOfStorageAccess&&, WebCore::PageIdentifier, OpenerDomain&&) override;
74
75 void grandfatherDataForDomains(const HashSet<RegistrableDomain>&) override;
76
77 bool isRegisteredAsSubresourceUnder(const SubResourceDomain&, const TopFrameDomain&) const override;
78 bool isRegisteredAsSubFrameUnder(const SubFrameDomain&, const TopFrameDomain&) const override;
79 bool isRegisteredAsRedirectingTo(const RedirectedFromDomain&, const RedirectedToDomain&) const override;
80
81 void clearPrevalentResource(const RegistrableDomain&) override;
82 String dumpResourceLoadStatistics() const override;
83 bool isPrevalentResource(const RegistrableDomain&) const override;
84 bool isVeryPrevalentResource(const RegistrableDomain&) const override;
85 void setPrevalentResource(const RegistrableDomain&) override;
86 void setVeryPrevalentResource(const RegistrableDomain&) override;
87
88 void setGrandfathered(const RegistrableDomain&, bool value) override;
89 bool isGrandfathered(const RegistrableDomain&) const override;
90
91 void setSubframeUnderTopFrameDomain(const SubFrameDomain&, const TopFrameDomain&) override;
92 void setSubresourceUnderTopFrameDomain(const SubResourceDomain&, const TopFrameDomain&) override;
93 void setSubresourceUniqueRedirectTo(const SubResourceDomain&, const RedirectDomain&) override;
94 void setSubresourceUniqueRedirectFrom(const SubResourceDomain&, const RedirectDomain&) override;
95 void setTopFrameUniqueRedirectTo(const TopFrameDomain&, const RedirectDomain&) override;
96 void setTopFrameUniqueRedirectFrom(const TopFrameDomain&, const RedirectDomain&) override;
97
98 void calculateAndSubmitTelemetry() const override;
99
100 void hasStorageAccess(const SubFrameDomain&, const TopFrameDomain&, Optional<FrameID>, WebCore::PageIdentifier, CompletionHandler<void(bool)>&&) override;
101 void requestStorageAccess(SubFrameDomain&&, TopFrameDomain&&, FrameID, WebCore::PageIdentifier, CompletionHandler<void(StorageAccessStatus)>&&) override;
102 void grantStorageAccess(SubFrameDomain&&, TopFrameDomain&&, FrameID, WebCore::PageIdentifier, WebCore::StorageAccessPromptWasShown, CompletionHandler<void(WebCore::StorageAccessWasGranted)>&&) override;
103
104 void logFrameNavigation(const NavigatedToDomain&, const TopFrameDomain&, const NavigatedFromDomain&, bool isRedirect, bool isMainFrame) override;
105 void logUserInteraction(const TopFrameDomain&) override;
106 void logSubresourceLoading(const SubResourceDomain&, const TopFrameDomain&, WallTime lastSeen) override;
107 void logSubresourceRedirect(const RedirectedFromDomain&, const RedirectedToDomain&) override;
108 void logCrossSiteLoadWithLinkDecoration(const NavigatedFromDomain&, const NavigatedToDomain&) override;
109
110 void clearUserInteraction(const RegistrableDomain&) override;
111 bool hasHadUserInteraction(const RegistrableDomain&, OperatingDatesWindow) override;
112
113 void setLastSeen(const RegistrableDomain&, Seconds) override;
114
115private:
116 static bool shouldBlockAndKeepCookies(const ResourceLoadStatistics&);
117 static bool shouldBlockAndPurgeCookies(const ResourceLoadStatistics&);
118 static WebCore::StorageAccessPromptWasShown hasUserGrantedStorageAccessThroughPrompt(const ResourceLoadStatistics&, const RegistrableDomain&);
119 bool hasHadUnexpiredRecentUserInteraction(ResourceLoadStatistics&, OperatingDatesWindow) const;
120 bool shouldRemoveAllWebsiteDataFor(ResourceLoadStatistics&, bool shouldCheckForGrandfathering) const;
121 bool shouldRemoveAllButCookiesFor(ResourceLoadStatistics&, bool shouldCheckForGrandfathering) const;
122 bool wasAccessedAsFirstPartyDueToUserInteraction(const ResourceLoadStatistics& current, const ResourceLoadStatistics& updated) const;
123 void incrementRecordsDeletedCountForDomains(HashSet<RegistrableDomain>&&) override;
124 void setPrevalentResource(ResourceLoadStatistics&, ResourceLoadPrevalence);
125 unsigned recursivelyGetAllDomainsThatHaveRedirectedToThisDomain(const ResourceLoadStatistics&, HashSet<RedirectedToDomain>&, unsigned numberOfRecursiveCalls) const;
126 void grantStorageAccessInternal(SubFrameDomain&&, TopFrameDomain&&, Optional<FrameID>, WebCore::PageIdentifier, WebCore::StorageAccessPromptWasShown, CompletionHandler<void(WebCore::StorageAccessWasGranted)>&&);
127 void markAsPrevalentIfHasRedirectedToPrevalent(ResourceLoadStatistics&);
128 bool isPrevalentDueToDebugMode(ResourceLoadStatistics&);
129 Vector<RegistrableDomain> ensurePrevalentResourcesForDebugMode() override;
130 void removeDataRecords(CompletionHandler<void()>&&);
131 void pruneStatisticsIfNeeded() override;
132 ResourceLoadStatistics& ensureResourceStatisticsForRegistrableDomain(const RegistrableDomain&);
133 HashMap<RegistrableDomain, WebsiteDataToRemove> registrableDomainsToRemoveWebsiteDataFor() override;
134 bool isMemoryStore() const final { return true; }
135
136 WeakPtr<ResourceLoadStatisticsPersistentStorage> m_persistentStorage;
137 HashMap<RegistrableDomain, ResourceLoadStatistics> m_resourceStatisticsMap;
138};
139
140} // namespace WebKit
141
142SPECIALIZE_TYPE_TRAITS_BEGIN(WebKit::ResourceLoadStatisticsMemoryStore)
143 static bool isType(const WebKit::ResourceLoadStatisticsStore& store) { return store.isMemoryStore(); }
144SPECIALIZE_TYPE_TRAITS_END()
145
146#endif
147