1/*
2 * Copyright (C) 2016-2018 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. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#pragma once
27
28#include "CanvasActivityRecord.h"
29#include "RegistrableDomain.h"
30#include <wtf/HashCountedSet.h>
31#include <wtf/HashSet.h>
32#include <wtf/OptionSet.h>
33#include <wtf/URL.h>
34#include <wtf/WallTime.h>
35#include <wtf/text/StringHash.h>
36#include <wtf/text/WTFString.h>
37
38namespace WebCore {
39
40class KeyedDecoder;
41class KeyedEncoder;
42
43struct ResourceLoadStatistics {
44 explicit ResourceLoadStatistics(const RegistrableDomain& domain)
45 : registrableDomain { domain }
46 {
47 }
48
49 ResourceLoadStatistics() = default;
50
51 ResourceLoadStatistics(const ResourceLoadStatistics&) = delete;
52 ResourceLoadStatistics& operator=(const ResourceLoadStatistics&) = delete;
53 ResourceLoadStatistics(ResourceLoadStatistics&&) = default;
54 ResourceLoadStatistics& operator=(ResourceLoadStatistics&&) = default;
55
56 WEBCORE_EXPORT static WallTime reduceTimeResolution(WallTime);
57
58 WEBCORE_EXPORT void encode(KeyedEncoder&) const;
59 WEBCORE_EXPORT bool decode(KeyedDecoder&, unsigned modelVersion);
60
61 WEBCORE_EXPORT String toString() const;
62
63 WEBCORE_EXPORT void merge(const ResourceLoadStatistics&);
64
65 RegistrableDomain registrableDomain;
66
67 WallTime lastSeen;
68
69 // User interaction
70 bool hadUserInteraction { false };
71 // Timestamp. Default value is negative, 0 means it was reset.
72 WallTime mostRecentUserInteractionTime { WallTime::fromRawSeconds(-1) };
73 bool grandfathered { false };
74
75 // Storage access
76 HashSet<RegistrableDomain> storageAccessUnderTopFrameDomains;
77
78 // Top frame stats
79 HashSet<RegistrableDomain> topFrameUniqueRedirectsTo;
80 HashSet<RegistrableDomain> topFrameUniqueRedirectsFrom;
81 HashSet<RegistrableDomain> topFrameLinkDecorationsFrom;
82 bool gotLinkDecorationFromPrevalentResource { false };
83
84 // Subframe stats
85 HashSet<RegistrableDomain> subframeUnderTopFrameDomains;
86
87 // Subresource stats
88 HashSet<RegistrableDomain> subresourceUnderTopFrameDomains;
89 HashSet<RegistrableDomain> subresourceUniqueRedirectsTo;
90 HashSet<RegistrableDomain> subresourceUniqueRedirectsFrom;
91
92 // Prevalent resource stats
93 bool isPrevalentResource { false };
94 bool isVeryPrevalentResource { false };
95 unsigned dataRecordsRemoved { 0 };
96 unsigned timesAccessedAsFirstPartyDueToUserInteraction { 0 };
97 unsigned timesAccessedAsFirstPartyDueToStorageAccessAPI { 0 };
98
99 enum class NavigatorAPI : uint64_t {
100 AppVersion = 1 << 0,
101 UserAgent = 1 << 1,
102 Plugins = 1 << 2,
103 MimeTypes = 1 << 3,
104 CookieEnabled = 1 << 4,
105 JavaEnabled = 1 << 5,
106 };
107 enum class ScreenAPI : uint64_t {
108 Height = 1 << 0,
109 Width = 1 << 1,
110 ColorDepth = 1 << 2,
111 PixelDepth = 1 << 3,
112 AvailLeft = 1 << 4,
113 AvailTop = 1 << 5,
114 AvailHeight = 1 << 6,
115 AvailWidth = 1 << 7,
116 };
117#if ENABLE(WEB_API_STATISTICS)
118 // This set represents the registrable domain of the top frame where web API
119 // were used in the top frame or one of its subframes.
120 HashCountedSet<String> topFrameRegistrableDomainsWhichAccessedWebAPIs;
121 HashSet<String> fontsFailedToLoad;
122 HashSet<String> fontsSuccessfullyLoaded;
123 CanvasActivityRecord canvasActivityRecord;
124 OptionSet<NavigatorAPI> navigatorFunctionsAccessed;
125 OptionSet<ScreenAPI> screenFunctionsAccessed;
126#endif
127};
128
129} // namespace WebCore
130