1/*
2 * Copyright (C) 2016-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. ``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 "PageIdentifier.h"
30#include "ResourceLoadStatistics.h"
31#include "Timer.h"
32#include <wtf/HashMap.h>
33#include <wtf/HashSet.h>
34#include <wtf/NeverDestroyed.h>
35#include <wtf/text/WTFString.h>
36
37namespace WTF {
38class Lock;
39class WorkQueue;
40class WallTime;
41}
42
43namespace PAL {
44class SessionID;
45}
46
47namespace WebCore {
48
49class Document;
50class Frame;
51class Page;
52class RegistrableDomain;
53class ResourceRequest;
54class ResourceResponse;
55class ScriptExecutionContext;
56
57struct ResourceLoadStatistics;
58
59class ResourceLoadObserver {
60 friend class WTF::NeverDestroyed<ResourceLoadObserver>;
61public:
62 WEBCORE_EXPORT static ResourceLoadObserver& shared();
63
64 void logSubresourceLoading(const Frame*, const ResourceRequest& newRequest, const ResourceResponse& redirectResponse);
65 void logWebSocketLoading(const URL& targetURL, const URL& mainFrameURL, PAL::SessionID);
66 void logUserInteractionWithReducedTimeResolution(const Document&);
67
68 void logFontLoad(const Document&, const String& familyName, bool loadStatus);
69 void logCanvasRead(const Document&);
70 void logCanvasWriteOrMeasure(const Document&, const String& textWritten);
71 void logNavigatorAPIAccessed(const Document&, const ResourceLoadStatistics::NavigatorAPI);
72 void logScreenAPIAccessed(const Document&, const ResourceLoadStatistics::ScreenAPI);
73
74 WEBCORE_EXPORT String statisticsForURL(const URL&);
75
76 WEBCORE_EXPORT void setStatisticsUpdatedCallback(WTF::Function<void(Vector<ResourceLoadStatistics>&&)>&&);
77 WEBCORE_EXPORT void setRequestStorageAccessUnderOpenerCallback(Function<void(PAL::SessionID, const RegistrableDomain&, PageIdentifier, const RegistrableDomain&)>&&);
78 WEBCORE_EXPORT void setLogUserInteractionNotificationCallback(Function<void(PAL::SessionID, const RegistrableDomain&)>&&);
79 WEBCORE_EXPORT void setLogWebSocketLoadingNotificationCallback(Function<void(PAL::SessionID, const RegistrableDomain&, const RegistrableDomain&, WallTime)>&&);
80 WEBCORE_EXPORT void setLogSubresourceLoadingNotificationCallback(Function<void(PAL::SessionID, const RegistrableDomain&, const RegistrableDomain&, WallTime)>&&);
81 WEBCORE_EXPORT void setLogSubresourceRedirectNotificationCallback(Function<void(PAL::SessionID, const RegistrableDomain&, const RegistrableDomain&)>&&);
82
83 WEBCORE_EXPORT void updateCentralStatisticsStore();
84 WEBCORE_EXPORT void clearState();
85
86#if ENABLE(RESOURCE_LOAD_STATISTICS) && !RELEASE_LOG_DISABLED
87 bool shouldLogUserInteraction() const { return m_shouldLogUserInteraction; }
88 void setShouldLogUserInteraction(bool shouldLogUserInteraction) { m_shouldLogUserInteraction = shouldLogUserInteraction; }
89#endif
90
91private:
92 bool shouldLog(bool usesEphemeralSession) const;
93 ResourceLoadStatistics& ensureResourceStatisticsForRegistrableDomain(const RegistrableDomain&);
94
95 Vector<ResourceLoadStatistics> takeStatistics();
96
97#if ENABLE(RESOURCE_LOAD_STATISTICS)
98 void requestStorageAccessUnderOpener(PAL::SessionID, const RegistrableDomain& domainInNeedOfStorageAccess, PageIdentifier openerPageID, Document& openerDocument);
99#endif
100
101 HashMap<RegistrableDomain, ResourceLoadStatistics> m_resourceStatisticsMap;
102 HashMap<RegistrableDomain, WTF::WallTime> m_lastReportedUserInteractionMap;
103 Function<void(Vector<ResourceLoadStatistics>&&)> m_notificationCallback;
104 Function<void(PAL::SessionID, const RegistrableDomain&, PageIdentifier, const RegistrableDomain&)> m_requestStorageAccessUnderOpenerCallback;
105 Function<void(PAL::SessionID, const RegistrableDomain&)> m_logUserInteractionNotificationCallback;
106 Function<void(PAL::SessionID, const RegistrableDomain&, const RegistrableDomain&, WallTime)> m_logWebSocketLoadingNotificationCallback;
107 Function<void(PAL::SessionID, const RegistrableDomain&, const RegistrableDomain&, WallTime)> m_logSubresourceLoadingNotificationCallback;
108 Function<void(PAL::SessionID, const RegistrableDomain&, const RegistrableDomain&)> m_logSubresourceRedirectNotificationCallback;
109#if ENABLE(RESOURCE_LOAD_STATISTICS) && !RELEASE_LOG_DISABLED
110 uint64_t m_loggingCounter { 0 };
111 bool m_shouldLogUserInteraction { false };
112#endif
113
114 URL nonNullOwnerURL(const Document&) const;
115};
116
117} // namespace WebCore
118