| 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 | #if ENABLE(RESOURCE_LOAD_STATISTICS) |
| 29 | |
| 30 | #include "ResourceLoadStatisticsClassifier.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 | |
| 37 | #if HAVE(CORE_PREDICTION) |
| 38 | #include "ResourceLoadStatisticsClassifierCocoa.h" |
| 39 | #endif |
| 40 | |
| 41 | namespace WebCore { |
| 42 | class KeyedDecoder; |
| 43 | class KeyedEncoder; |
| 44 | enum class StorageAccessPromptWasShown : bool; |
| 45 | enum class StorageAccessWasGranted : bool; |
| 46 | struct ResourceLoadStatistics; |
| 47 | } |
| 48 | |
| 49 | namespace WebKit { |
| 50 | |
| 51 | class ResourceLoadStatisticsPersistentStorage; |
| 52 | |
| 53 | class OperatingDate { |
| 54 | public: |
| 55 | OperatingDate() = default; |
| 56 | |
| 57 | static OperatingDate fromWallTime(WallTime); |
| 58 | static OperatingDate today(); |
| 59 | Seconds secondsSinceEpoch() const; |
| 60 | bool operator==(const OperatingDate& other) const; |
| 61 | bool operator<(const OperatingDate& other) const; |
| 62 | bool operator<=(const OperatingDate& other) const; |
| 63 | |
| 64 | private: |
| 65 | OperatingDate(int year, int month, int monthDay) |
| 66 | : m_year(year) |
| 67 | , m_month(month) |
| 68 | , m_monthDay(monthDay) |
| 69 | { } |
| 70 | |
| 71 | int m_year { 0 }; |
| 72 | int m_month { 0 }; // [0, 11]. |
| 73 | int m_monthDay { 0 }; // [1, 31]. |
| 74 | }; |
| 75 | |
| 76 | enum class OperatingDatesWindow : bool { Long, Short }; |
| 77 | |
| 78 | // This is always constructed / used / destroyed on the WebResourceLoadStatisticsStore's statistics queue. |
| 79 | class ResourceLoadStatisticsStore : public CanMakeWeakPtr<ResourceLoadStatisticsStore> { |
| 80 | public: |
| 81 | using ResourceLoadStatistics = WebCore::ResourceLoadStatistics; |
| 82 | using RegistrableDomain = WebCore::RegistrableDomain; |
| 83 | using TopFrameDomain = WebCore::RegistrableDomain; |
| 84 | using SubFrameDomain = WebCore::RegistrableDomain; |
| 85 | using SubResourceDomain = WebCore::RegistrableDomain; |
| 86 | using RedirectDomain = WebCore::RegistrableDomain; |
| 87 | using RedirectedFromDomain = WebCore::RegistrableDomain; |
| 88 | using RedirectedToDomain = WebCore::RegistrableDomain; |
| 89 | using NavigatedFromDomain = WebCore::RegistrableDomain; |
| 90 | using NavigatedToDomain = WebCore::RegistrableDomain; |
| 91 | using DomainInNeedOfStorageAccess = WebCore::RegistrableDomain; |
| 92 | using OpenerDomain = WebCore::RegistrableDomain; |
| 93 | using OpenerPageID = uint64_t; |
| 94 | using PageID = uint64_t; |
| 95 | using FrameID = uint64_t; |
| 96 | |
| 97 | virtual ~ResourceLoadStatisticsStore(); |
| 98 | |
| 99 | virtual void clear(CompletionHandler<void()>&&) = 0; |
| 100 | virtual bool isEmpty() const = 0; |
| 101 | |
| 102 | virtual void updateCookieBlocking(CompletionHandler<void()>&&) = 0; |
| 103 | void updateCookieBlockingForDomains(const Vector<RegistrableDomain>& domainsToBlock, CompletionHandler<void()>&&); |
| 104 | void clearBlockingStateForDomains(const Vector<RegistrableDomain>& domains, CompletionHandler<void()>&&); |
| 105 | |
| 106 | void includeTodayAsOperatingDateIfNecessary(); |
| 107 | void processStatisticsAndDataRecords(); |
| 108 | |
| 109 | virtual void classifyPrevalentResources() = 0; |
| 110 | virtual void syncStorageIfNeeded() = 0; |
| 111 | virtual void syncStorageImmediately() = 0; |
| 112 | |
| 113 | virtual void requestStorageAccessUnderOpener(DomainInNeedOfStorageAccess&&, OpenerPageID, OpenerDomain&&) = 0; |
| 114 | void removeAllStorageAccess(CompletionHandler<void()>&&); |
| 115 | |
| 116 | void grandfatherExistingWebsiteData(CompletionHandler<void()>&&); |
| 117 | void cancelPendingStatisticsProcessingRequest(); |
| 118 | |
| 119 | virtual bool isRegisteredAsSubresourceUnder(const SubResourceDomain&, const TopFrameDomain&) const = 0; |
| 120 | virtual bool isRegisteredAsSubFrameUnder(const SubFrameDomain&, const TopFrameDomain&) const = 0; |
| 121 | virtual bool isRegisteredAsRedirectingTo(const RedirectedFromDomain&, const RedirectedToDomain&) const = 0; |
| 122 | |
| 123 | virtual void clearPrevalentResource(const RegistrableDomain&) = 0; |
| 124 | virtual String dumpResourceLoadStatistics() const = 0; |
| 125 | virtual bool isPrevalentResource(const RegistrableDomain&) const = 0; |
| 126 | virtual bool isVeryPrevalentResource(const RegistrableDomain&) const = 0; |
| 127 | virtual void setPrevalentResource(const RegistrableDomain&) = 0; |
| 128 | virtual void setVeryPrevalentResource(const RegistrableDomain&) = 0; |
| 129 | |
| 130 | virtual void setGrandfathered(const RegistrableDomain&, bool value) = 0; |
| 131 | virtual bool isGrandfathered(const RegistrableDomain&) const = 0; |
| 132 | |
| 133 | virtual void incrementRecordsDeletedCountForDomains(HashSet<RegistrableDomain>&&) = 0; |
| 134 | virtual void grandfatherDataForDomains(const HashSet<RegistrableDomain>&) = 0; |
| 135 | |
| 136 | virtual void setSubframeUnderTopFrameDomain(const SubFrameDomain&, const TopFrameDomain&) = 0; |
| 137 | virtual void setSubresourceUnderTopFrameDomain(const SubResourceDomain&, const TopFrameDomain&) = 0; |
| 138 | virtual void setSubresourceUniqueRedirectTo(const SubResourceDomain&, const RedirectDomain&) = 0; |
| 139 | virtual void setSubresourceUniqueRedirectFrom(const SubResourceDomain&, const RedirectDomain&) = 0; |
| 140 | virtual void setTopFrameUniqueRedirectTo(const TopFrameDomain&, const RedirectDomain&) = 0; |
| 141 | virtual void setTopFrameUniqueRedirectFrom(const TopFrameDomain&, const RedirectDomain&) = 0; |
| 142 | |
| 143 | void logTestingEvent(const String&); |
| 144 | |
| 145 | void setMaxStatisticsEntries(size_t maximumEntryCount); |
| 146 | void setPruneEntriesDownTo(size_t pruneTargetCount); |
| 147 | void resetParametersToDefaultValues(); |
| 148 | Optional<Seconds> statisticsEpirationTime() const; |
| 149 | |
| 150 | virtual void calculateAndSubmitTelemetry() const = 0; |
| 151 | |
| 152 | void setNotifyPagesWhenDataRecordsWereScanned(bool); |
| 153 | void setIsRunningTest(bool); |
| 154 | bool shouldSkip(const RegistrableDomain&) const; |
| 155 | void setShouldClassifyResourcesBeforeDataRecordsRemoval(bool); |
| 156 | void setShouldSubmitTelemetry(bool); |
| 157 | void setTimeToLiveUserInteraction(Seconds); |
| 158 | void setMinimumTimeBetweenDataRecordsRemoval(Seconds); |
| 159 | void setGrandfatheringTime(Seconds); |
| 160 | void setResourceLoadStatisticsDebugMode(bool); |
| 161 | bool isDebugModeEnabled() const { return m_debugModeEnabled; }; |
| 162 | void setPrevalentResourceForDebugMode(const RegistrableDomain&); |
| 163 | |
| 164 | virtual void hasStorageAccess(const SubFrameDomain&, const TopFrameDomain&, Optional<FrameID>, PageID, CompletionHandler<void(bool)>&&) = 0; |
| 165 | virtual void requestStorageAccess(SubFrameDomain&&, TopFrameDomain&&, FrameID, PageID, CompletionHandler<void(StorageAccessStatus)>&&) = 0; |
| 166 | virtual void grantStorageAccess(SubFrameDomain&&, TopFrameDomain&&, FrameID, PageID, WebCore::StorageAccessPromptWasShown, CompletionHandler<void(WebCore::StorageAccessWasGranted)>&&) = 0; |
| 167 | |
| 168 | virtual void logFrameNavigation(const NavigatedToDomain&, const TopFrameDomain&, const NavigatedFromDomain&, bool isRedirect, bool isMainFrame) = 0; |
| 169 | virtual void logUserInteraction(const TopFrameDomain&) = 0; |
| 170 | virtual void logSubresourceLoading(const SubResourceDomain&, const TopFrameDomain&, WallTime lastSeen) = 0; |
| 171 | virtual void logSubresourceRedirect(const RedirectedFromDomain&, const RedirectedToDomain&) = 0; |
| 172 | virtual void logCrossSiteLoadWithLinkDecoration(const NavigatedFromDomain&, const NavigatedToDomain&) = 0; |
| 173 | |
| 174 | virtual void clearUserInteraction(const RegistrableDomain&) = 0; |
| 175 | virtual bool hasHadUserInteraction(const RegistrableDomain&, OperatingDatesWindow) = 0; |
| 176 | |
| 177 | virtual void setLastSeen(const RegistrableDomain& primaryDomain, Seconds) = 0; |
| 178 | |
| 179 | void didCreateNetworkProcess(); |
| 180 | |
| 181 | const WebResourceLoadStatisticsStore& store() const { return m_store; } |
| 182 | |
| 183 | static constexpr unsigned maxImportance { 3 }; |
| 184 | |
| 185 | virtual bool isMemoryStore() const { return false; } |
| 186 | virtual bool isDatabaseStore()const { return false; } |
| 187 | |
| 188 | protected: |
| 189 | static unsigned computeImportance(const WebCore::ResourceLoadStatistics&); |
| 190 | static Vector<OperatingDate> mergeOperatingDates(const Vector<OperatingDate>& existingDates, Vector<OperatingDate>&& newDates); |
| 191 | static void debugLogDomainsInBatches(const char* action, const Vector<RegistrableDomain>& domains); |
| 192 | |
| 193 | ResourceLoadStatisticsStore(WebResourceLoadStatisticsStore&, WorkQueue&, ShouldIncludeLocalhost); |
| 194 | |
| 195 | bool hasStatisticsExpired(const ResourceLoadStatistics&, OperatingDatesWindow) const; |
| 196 | bool hasStatisticsExpired(WallTime mostRecentUserInteractionTime, OperatingDatesWindow) const; |
| 197 | void scheduleStatisticsProcessingRequestIfNecessary(); |
| 198 | void mergeOperatingDates(Vector<OperatingDate>&&); |
| 199 | virtual Vector<RegistrableDomain> ensurePrevalentResourcesForDebugMode() = 0; |
| 200 | virtual HashMap<RegistrableDomain, WebsiteDataToRemove> registrableDomainsToRemoveWebsiteDataFor() = 0; |
| 201 | virtual void pruneStatisticsIfNeeded() = 0; |
| 202 | |
| 203 | WebResourceLoadStatisticsStore& store() { return m_store; } |
| 204 | Ref<WorkQueue>& workQueue() { return m_workQueue; } |
| 205 | #if HAVE(CORE_PREDICTION) |
| 206 | ResourceLoadStatisticsClassifierCocoa& classifier() { return m_resourceLoadStatisticsClassifier; } |
| 207 | #else |
| 208 | ResourceLoadStatisticsClassifier& classifier() { return m_resourceLoadStatisticsClassifier; } |
| 209 | #endif |
| 210 | |
| 211 | struct Parameters { |
| 212 | size_t pruneEntriesDownTo { 800 }; |
| 213 | size_t maxStatisticsEntries { 1000 }; |
| 214 | Optional<Seconds> timeToLiveUserInteraction; |
| 215 | Seconds minimumTimeBetweenDataRecordsRemoval { 1_h }; |
| 216 | Seconds grandfatheringTime { 24_h * 7 }; |
| 217 | Seconds cacheMaxAgeCapTime { 24_h * 7 }; |
| 218 | Seconds clientSideCookiesAgeCapTime { 24_h * 7 }; |
| 219 | bool shouldNotifyPagesWhenDataRecordsWereScanned { false }; |
| 220 | bool shouldClassifyResourcesBeforeDataRecordsRemoval { true }; |
| 221 | bool shouldSubmitTelemetry { true }; |
| 222 | bool isRunningTest { false }; |
| 223 | }; |
| 224 | const Parameters& parameters() const { return m_parameters; } |
| 225 | const Vector<OperatingDate>& operatingDates() const { return m_operatingDates; } |
| 226 | void clearOperatingDates() { m_operatingDates.clear(); } |
| 227 | WallTime& endOfGrandfatheringTimestamp() { return m_endOfGrandfatheringTimestamp; } |
| 228 | const WallTime& endOfGrandfatheringTimestamp() const { return m_endOfGrandfatheringTimestamp; } |
| 229 | void setEndOfGrandfatheringTimestamp(const WallTime& grandfatheringTime) { m_endOfGrandfatheringTimestamp = grandfatheringTime; } |
| 230 | void clearEndOfGrandfatheringTimeStamp() { m_endOfGrandfatheringTimestamp = { }; } |
| 231 | const RegistrableDomain& debugManualPrevalentResource() const { return m_debugManualPrevalentResource; } |
| 232 | const RegistrableDomain& debugStaticPrevalentResource() const { return m_debugStaticPrevalentResource; } |
| 233 | bool debugLoggingEnabled() const { return m_debugLoggingEnabled; }; |
| 234 | bool debugModeEnabled() const { return m_debugModeEnabled; } |
| 235 | |
| 236 | static constexpr unsigned maxNumberOfRecursiveCallsInRedirectTraceBack { 50 }; |
| 237 | |
| 238 | private: |
| 239 | bool shouldRemoveDataRecords() const; |
| 240 | void setDebugLogggingEnabled(bool enabled) { m_debugLoggingEnabled = enabled; } |
| 241 | void setDataRecordsBeingRemoved(bool); |
| 242 | void removeDataRecords(CompletionHandler<void()>&&); |
| 243 | void setCacheMaxAgeCap(Seconds); |
| 244 | void updateCacheMaxAgeCap(); |
| 245 | void setAgeCapForClientSideCookies(Seconds); |
| 246 | void updateClientSideCookiesAgeCap(); |
| 247 | |
| 248 | WebResourceLoadStatisticsStore& m_store; |
| 249 | Ref<WorkQueue> m_workQueue; |
| 250 | #if HAVE(CORE_PREDICTION) |
| 251 | ResourceLoadStatisticsClassifierCocoa m_resourceLoadStatisticsClassifier; |
| 252 | #else |
| 253 | ResourceLoadStatisticsClassifier m_resourceLoadStatisticsClassifier; |
| 254 | #endif |
| 255 | #if ENABLE(NETSCAPE_PLUGIN_API) |
| 256 | HashSet<uint64_t> m_activePluginTokens; |
| 257 | #endif |
| 258 | Parameters m_parameters; |
| 259 | Vector<OperatingDate> m_operatingDates; |
| 260 | WallTime m_endOfGrandfatheringTimestamp; |
| 261 | RegistrableDomain m_debugManualPrevalentResource; |
| 262 | MonotonicTime m_lastTimeDataRecordsWereRemoved; |
| 263 | uint64_t m_lastStatisticsProcessingRequestIdentifier { 0 }; |
| 264 | Optional<uint64_t> m_pendingStatisticsProcessingRequestIdentifier; |
| 265 | const RegistrableDomain m_debugStaticPrevalentResource { URL { URL(), "https://3rdpartytestwebkit.org"_s } }; |
| 266 | bool m_debugLoggingEnabled { false }; |
| 267 | bool m_debugModeEnabled { false }; |
| 268 | bool m_dataRecordsBeingRemoved { false }; |
| 269 | ShouldIncludeLocalhost m_shouldIncludeLocalhost { ShouldIncludeLocalhost::Yes }; |
| 270 | }; |
| 271 | |
| 272 | } // namespace WebKit |
| 273 | |
| 274 | #endif |
| 275 | |