1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
|
/*
* Copyright (C) 2016-2017 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "ResourceLoadObserver.h"
#include "Document.h"
#include "Frame.h"
#include "Logging.h"
#include "MainFrame.h"
#include "Page.h"
#include "ResourceLoadStatistics.h"
#include "ResourceRequest.h"
#include "ResourceResponse.h"
#include "SecurityOrigin.h"
#include "Settings.h"
#include "URL.h"
namespace WebCore {
template<typename T> static inline String primaryDomain(const T& value)
{
return ResourceLoadStatistics::primaryDomain(value);
}
static Seconds timestampResolution { 1_h };
static const Seconds minimumNotificationInterval { 5_s };
ResourceLoadObserver& ResourceLoadObserver::shared()
{
static NeverDestroyed<ResourceLoadObserver> resourceLoadObserver;
return resourceLoadObserver;
}
static bool shouldEnableSiteSpecificQuirks(Page* page)
{
#if PLATFORM(IOS)
UNUSED_PARAM(page);
// There is currently no way to toggle the needsSiteSpecificQuirks setting on iOS so we always enable
// the site-specific quirks on iOS.
return true;
#else
return page && page->settings().needsSiteSpecificQuirks();
#endif
}
// FIXME: Temporary fix for <rdar://problem/32343256> until content can be updated.
static bool areDomainsAssociated(Page* page, const String& firstDomain, const String& secondDomain)
{
static NeverDestroyed<HashMap<String, unsigned>> metaDomainIdentifiers = [] {
HashMap<String, unsigned> map;
// Domains owned by Dow Jones & Company, Inc.
const unsigned dowJonesIdentifier = 1;
map.add(ASCIILiteral("dowjones.com"), dowJonesIdentifier);
map.add(ASCIILiteral("wsj.com"), dowJonesIdentifier);
map.add(ASCIILiteral("barrons.com"), dowJonesIdentifier);
map.add(ASCIILiteral("marketwatch.com"), dowJonesIdentifier);
map.add(ASCIILiteral("wsjplus.com"), dowJonesIdentifier);
return map;
}();
if (firstDomain == secondDomain)
return true;
ASSERT(!equalIgnoringASCIICase(firstDomain, secondDomain));
if (!shouldEnableSiteSpecificQuirks(page))
return false;
unsigned firstMetaDomainIdentifier = metaDomainIdentifiers.get().get(firstDomain);
if (!firstMetaDomainIdentifier)
return false;
return firstMetaDomainIdentifier == metaDomainIdentifiers.get().get(secondDomain);
}
void ResourceLoadObserver::setNotificationCallback(WTF::Function<void (Vector<ResourceLoadStatistics>&&)>&& notificationCallback)
{
ASSERT(!m_notificationCallback);
m_notificationCallback = WTFMove(notificationCallback);
}
ResourceLoadObserver::ResourceLoadObserver()
: m_notificationTimer(*this, &ResourceLoadObserver::notifyObserver)
{
}
static inline bool is3xxRedirect(const ResourceResponse& response)
{
return response.httpStatusCode() >= 300 && response.httpStatusCode() <= 399;
}
bool ResourceLoadObserver::shouldLog(Page* page) const
{
// FIXME: Err on the safe side until we have sorted out what to do in worker contexts
if (!page)
return false;
return Settings::resourceLoadStatisticsEnabled() && !page->usesEphemeralSession() && m_notificationCallback;
}
static WallTime reduceToHourlyTimeResolution(WallTime time)
{
return WallTime::fromRawSeconds(std::floor(time.secondsSinceEpoch() / timestampResolution) * timestampResolution.seconds());
}
void ResourceLoadObserver::logFrameNavigation(const Frame& frame, const Frame& topFrame, const ResourceRequest& newRequest)
{
ASSERT(frame.document());
ASSERT(topFrame.document());
ASSERT(topFrame.page());
if (frame.isMainFrame())
return;
auto* page = topFrame.page();
if (!shouldLog(page))
return;
auto& sourceURL = frame.document()->url();
auto& targetURL = newRequest.url();
auto& mainFrameURL = topFrame.document()->url();
if (!targetURL.isValid() || !mainFrameURL.isValid())
return;
auto targetHost = targetURL.host();
auto mainFrameHost = mainFrameURL.host();
if (targetHost.isEmpty() || mainFrameHost.isEmpty() || targetHost == mainFrameHost || targetHost == sourceURL.host())
return;
auto targetPrimaryDomain = primaryDomain(targetURL);
auto mainFramePrimaryDomain = primaryDomain(mainFrameURL);
auto sourcePrimaryDomain = primaryDomain(sourceURL);
if (areDomainsAssociated(page, targetPrimaryDomain, mainFramePrimaryDomain) || areDomainsAssociated(page, targetPrimaryDomain, sourcePrimaryDomain))
return;
auto& targetStatistics = ensureResourceStatisticsForPrimaryDomain(targetPrimaryDomain);
targetStatistics.lastSeen = reduceToHourlyTimeResolution(WallTime::now());
auto subframeUnderTopFrameOriginsResult = targetStatistics.subframeUnderTopFrameOrigins.add(mainFramePrimaryDomain);
if (subframeUnderTopFrameOriginsResult.isNewEntry)
scheduleNotificationIfNeeded();
}
// FIXME: This quirk was added to address <rdar://problem/33325881> and should be removed once content is fixed.
static bool resourceNeedsSSOQuirk(Page* page, const URL& url)
{
if (!shouldEnableSiteSpecificQuirks(page))
return false;
return equalIgnoringASCIICase(url.host(), "sp.auth.adobe.com");
}
void ResourceLoadObserver::logSubresourceLoading(const Frame* frame, const ResourceRequest& newRequest, const ResourceResponse& redirectResponse)
{
ASSERT(frame->page());
auto* page = frame->page();
if (!shouldLog(page))
return;
bool isRedirect = is3xxRedirect(redirectResponse);
const URL& sourceURL = redirectResponse.url();
const URL& targetURL = newRequest.url();
const URL& mainFrameURL = frame ? frame->mainFrame().document()->url() : URL();
auto targetHost = targetURL.host();
auto mainFrameHost = mainFrameURL.host();
if (targetHost.isEmpty() || mainFrameHost.isEmpty() || targetHost == mainFrameHost || (isRedirect && targetHost == sourceURL.host()))
return;
auto targetPrimaryDomain = primaryDomain(targetURL);
auto mainFramePrimaryDomain = primaryDomain(mainFrameURL);
auto sourcePrimaryDomain = primaryDomain(sourceURL);
if (areDomainsAssociated(page, targetPrimaryDomain, mainFramePrimaryDomain) || (isRedirect && areDomainsAssociated(page, targetPrimaryDomain, sourcePrimaryDomain)))
return;
if (resourceNeedsSSOQuirk(page, targetURL))
return;
bool shouldCallNotificationCallback = false;
{
auto& targetStatistics = ensureResourceStatisticsForPrimaryDomain(targetPrimaryDomain);
targetStatistics.lastSeen = reduceToHourlyTimeResolution(WallTime::now());
if (targetStatistics.subresourceUnderTopFrameOrigins.add(mainFramePrimaryDomain).isNewEntry)
shouldCallNotificationCallback = true;
}
if (isRedirect) {
auto& redirectingOriginStatistics = ensureResourceStatisticsForPrimaryDomain(sourcePrimaryDomain);
if (redirectingOriginStatistics.subresourceUniqueRedirectsTo.add(targetPrimaryDomain).isNewEntry)
shouldCallNotificationCallback = true;
}
if (shouldCallNotificationCallback)
scheduleNotificationIfNeeded();
}
void ResourceLoadObserver::logWebSocketLoading(const Frame* frame, const URL& targetURL)
{
// FIXME: Web sockets can run in detached frames. Decide how to count such connections.
// See LayoutTests/http/tests/websocket/construct-in-detached-frame.html
if (!frame)
return;
auto* page = frame->page();
if (!shouldLog(page))
return;
auto& mainFrameURL = frame->mainFrame().document()->url();
auto targetHost = targetURL.host();
auto mainFrameHost = mainFrameURL.host();
if (targetHost.isEmpty() || mainFrameHost.isEmpty() || targetHost == mainFrameHost)
return;
auto targetPrimaryDomain = primaryDomain(targetURL);
auto mainFramePrimaryDomain = primaryDomain(mainFrameURL);
if (areDomainsAssociated(page, targetPrimaryDomain, mainFramePrimaryDomain))
return;
auto& targetStatistics = ensureResourceStatisticsForPrimaryDomain(targetPrimaryDomain);
targetStatistics.lastSeen = reduceToHourlyTimeResolution(WallTime::now());
if (targetStatistics.subresourceUnderTopFrameOrigins.add(mainFramePrimaryDomain).isNewEntry)
scheduleNotificationIfNeeded();
}
void ResourceLoadObserver::logUserInteractionWithReducedTimeResolution(const Document& document)
{
ASSERT(document.page());
if (!shouldLog(document.page()))
return;
auto& url = document.url();
if (url.isBlankURL() || url.isEmpty())
return;
auto domain = primaryDomain(url);
auto newTime = reduceToHourlyTimeResolution(WallTime::now());
auto lastReportedUserInteraction = m_lastReportedUserInteractionMap.get(domain);
if (newTime == lastReportedUserInteraction)
return;
m_lastReportedUserInteractionMap.set(domain, newTime);
auto& statistics = ensureResourceStatisticsForPrimaryDomain(domain);
statistics.hadUserInteraction = true;
statistics.lastSeen = newTime;
statistics.mostRecentUserInteractionTime = newTime;
m_notificationTimer.stop();
notifyObserver();
}
ResourceLoadStatistics& ResourceLoadObserver::ensureResourceStatisticsForPrimaryDomain(const String& primaryDomain)
{
auto addResult = m_resourceStatisticsMap.ensure(primaryDomain, [&primaryDomain] {
return ResourceLoadStatistics(primaryDomain);
});
return addResult.iterator->value;
}
void ResourceLoadObserver::scheduleNotificationIfNeeded()
{
ASSERT(m_notificationCallback);
if (m_resourceStatisticsMap.isEmpty()) {
m_notificationTimer.stop();
return;
}
if (!m_notificationTimer.isActive())
m_notificationTimer.startOneShot(minimumNotificationInterval);
}
void ResourceLoadObserver::notifyObserver()
{
ASSERT(m_notificationCallback);
m_notificationTimer.stop();
m_notificationCallback(takeStatistics());
}
String ResourceLoadObserver::statisticsForOrigin(const String& origin)
{
auto iter = m_resourceStatisticsMap.find(origin);
if (iter == m_resourceStatisticsMap.end())
return emptyString();
return "Statistics for " + origin + ":\n" + iter->value.toString();
}
Vector<ResourceLoadStatistics> ResourceLoadObserver::takeStatistics()
{
Vector<ResourceLoadStatistics> statistics;
statistics.reserveInitialCapacity(m_resourceStatisticsMap.size());
for (auto& statistic : m_resourceStatisticsMap.values())
statistics.uncheckedAppend(WTFMove(statistic));
m_resourceStatisticsMap.clear();
return statistics;
}
void ResourceLoadObserver::clearState()
{
m_notificationTimer.stop();
m_resourceStatisticsMap.clear();
m_lastReportedUserInteractionMap.clear();
}
} // namespace WebCore
|