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