ResourceLoadStatistics.cpp [plain text]
#include "config.h"
#include "ResourceLoadStatistics.h"
#include "KeyedCoding.h"
#include "PublicSuffix.h"
#include <wtf/text/StringBuilder.h>
#include <wtf/text/StringHash.h>
namespace WebCore {
typedef WTF::HashMap<String, unsigned, StringHash, HashTraits<String>, HashTraits<unsigned>>::KeyValuePairType ResourceLoadStatisticsValue;
static void encodeHashCountedSet(KeyedEncoder& encoder, const String& label, const HashCountedSet<String>& hashCountedSet)
{
if (hashCountedSet.isEmpty())
return;
encoder.encodeObjects(label, hashCountedSet.begin(), hashCountedSet.end(), [](KeyedEncoder& encoderInner, const ResourceLoadStatisticsValue& origin) {
encoderInner.encodeString("origin", origin.key);
encoderInner.encodeUInt32("count", origin.value);
});
}
static void encodeHashSet(KeyedEncoder& encoder, const String& label, const HashSet<String>& hashSet)
{
if (hashSet.isEmpty())
return;
encoder.encodeObjects(label, hashSet.begin(), hashSet.end(), [](KeyedEncoder& encoderInner, const String& origin) {
encoderInner.encodeString("origin", origin);
});
}
void ResourceLoadStatistics::encode(KeyedEncoder& encoder) const
{
encoder.encodeString("PrevalentResourceOrigin", highLevelDomain);
encoder.encodeDouble("lastSeen", lastSeen.secondsSinceEpoch().value());
encoder.encodeBool("hadUserInteraction", hadUserInteraction);
encoder.encodeDouble("mostRecentUserInteraction", mostRecentUserInteractionTime.secondsSinceEpoch().value());
encoder.encodeBool("grandfathered", grandfathered);
encodeHashSet(encoder, "storageAccessUnderTopFrameOrigins", storageAccessUnderTopFrameOrigins);
encodeHashCountedSet(encoder, "subframeUnderTopFrameOrigins", subframeUnderTopFrameOrigins);
encodeHashCountedSet(encoder, "subresourceUnderTopFrameOrigins", subresourceUnderTopFrameOrigins);
encodeHashCountedSet(encoder, "subresourceUniqueRedirectsTo", subresourceUniqueRedirectsTo);
encoder.encodeBool("isPrevalentResource", isPrevalentResource);
encoder.encodeUInt32("dataRecordsRemoved", dataRecordsRemoved);
encoder.encodeUInt32("timesAccessedAsFirstPartyDueToUserInteraction", timesAccessedAsFirstPartyDueToUserInteraction);
encoder.encodeUInt32("timesAccessedAsFirstPartyDueToStorageAccessAPI", timesAccessedAsFirstPartyDueToStorageAccessAPI);
}
static void decodeHashCountedSet(KeyedDecoder& decoder, const String& label, HashCountedSet<String>& hashCountedSet)
{
Vector<String> ignore;
decoder.decodeObjects(label, ignore, [&hashCountedSet](KeyedDecoder& decoderInner, String& origin) {
if (!decoderInner.decodeString("origin", origin))
return false;
unsigned count;
if (!decoderInner.decodeUInt32("count", count))
return false;
hashCountedSet.add(origin, count);
return true;
});
}
static void decodeHashSet(KeyedDecoder& decoder, const String& label, HashSet<String>& hashSet)
{
Vector<String> ignore;
decoder.decodeObjects(label, ignore, [&hashSet](KeyedDecoder& decoderInner, String& origin) {
if (!decoderInner.decodeString("origin", origin))
return false;
hashSet.add(origin);
return true;
});
}
bool ResourceLoadStatistics::decode(KeyedDecoder& decoder)
{
if (!decoder.decodeString("PrevalentResourceOrigin", highLevelDomain))
return false;
if (!decoder.decodeBool("hadUserInteraction", hadUserInteraction))
return false;
decodeHashSet(decoder, "storageAccessUnderTopFrameOrigins", storageAccessUnderTopFrameOrigins);
decodeHashCountedSet(decoder, "subframeUnderTopFrameOrigins", subframeUnderTopFrameOrigins);
decodeHashCountedSet(decoder, "subresourceUnderTopFrameOrigins", subresourceUnderTopFrameOrigins);
decodeHashCountedSet(decoder, "subresourceUniqueRedirectsTo", subresourceUniqueRedirectsTo);
if (!decoder.decodeBool("isPrevalentResource", isPrevalentResource))
return false;
if (!decoder.decodeUInt32("dataRecordsRemoved", dataRecordsRemoved))
return false;
double mostRecentUserInteractionTimeAsDouble;
if (!decoder.decodeDouble("mostRecentUserInteraction", mostRecentUserInteractionTimeAsDouble))
return false;
mostRecentUserInteractionTime = WallTime::fromRawSeconds(mostRecentUserInteractionTimeAsDouble);
if (!decoder.decodeBool("grandfathered", grandfathered))
return false;
double lastSeenTimeAsDouble;
if (!decoder.decodeDouble("lastSeen", lastSeenTimeAsDouble))
return false;
lastSeen = WallTime::fromRawSeconds(lastSeenTimeAsDouble);
if (!decoder.decodeUInt32("timesAccessedAsFirstPartyDueToUserInteraction", timesAccessedAsFirstPartyDueToUserInteraction))
timesAccessedAsFirstPartyDueToUserInteraction = 0;
if (!decoder.decodeUInt32("timesAccessedAsFirstPartyDueToStorageAccessAPI", timesAccessedAsFirstPartyDueToStorageAccessAPI))
timesAccessedAsFirstPartyDueToStorageAccessAPI = 0;
return true;
}
static void appendBoolean(StringBuilder& builder, const String& label, bool flag)
{
builder.appendLiteral(" ");
builder.append(label);
builder.appendLiteral(": ");
builder.append(flag ? "Yes" : "No");
}
static void appendHashCountedSet(StringBuilder& builder, const String& label, const HashCountedSet<String>& hashCountedSet)
{
if (hashCountedSet.isEmpty())
return;
builder.appendLiteral(" ");
builder.append(label);
builder.appendLiteral(":\n");
for (auto& entry : hashCountedSet) {
builder.appendLiteral(" ");
builder.append(entry.key);
builder.appendLiteral(": ");
builder.appendNumber(entry.value);
builder.append('\n');
}
}
static void appendHashSet(StringBuilder& builder, const String& label, const HashSet<String>& hashSet)
{
if (hashSet.isEmpty())
return;
builder.appendLiteral(" ");
builder.append(label);
builder.appendLiteral(":\n");
for (auto& entry : hashSet) {
builder.appendLiteral(" ");
builder.append(entry);
builder.append('\n');
}
}
String ResourceLoadStatistics::toString() const
{
StringBuilder builder;
builder.appendLiteral("lastSeen");
builder.appendNumber(lastSeen.secondsSinceEpoch().value());
builder.append('\n');
appendBoolean(builder, "hadUserInteraction", hadUserInteraction);
builder.append('\n');
builder.appendLiteral(" mostRecentUserInteraction: ");
builder.appendNumber(mostRecentUserInteractionTime.secondsSinceEpoch().value());
builder.append('\n');
appendBoolean(builder, " grandfathered", grandfathered);
builder.append('\n');
appendHashSet(builder, "storageAccessUnderTopFrameOrigins", storageAccessUnderTopFrameOrigins);
appendHashCountedSet(builder, "subframeUnderTopFrameOrigins", subframeUnderTopFrameOrigins);
appendHashCountedSet(builder, "subresourceUnderTopFrameOrigins", subresourceUnderTopFrameOrigins);
appendHashCountedSet(builder, "subresourceUniqueRedirectsTo", subresourceUniqueRedirectsTo);
appendBoolean(builder, "isPrevalentResource", isPrevalentResource);
builder.appendLiteral(" dataRecordsRemoved: ");
builder.appendNumber(dataRecordsRemoved);
builder.append('\n');
appendBoolean(builder, "isMarkedForCookiePartitioning", isMarkedForCookiePartitioning);
appendBoolean(builder, "isMarkedForCookieBlocking", isMarkedForCookieBlocking);
builder.append('\n');
builder.append('\n');
return builder.toString();
}
template <typename T>
static void mergeHashCountedSet(HashCountedSet<T>& to, const HashCountedSet<T>& from)
{
for (auto& entry : from)
to.add(entry.key, entry.value);
}
template <typename T>
static void mergeHashSet(HashSet<T>& to, const HashSet<T>& from)
{
for (auto& entry : from)
to.add(entry);
}
void ResourceLoadStatistics::merge(const ResourceLoadStatistics& other)
{
ASSERT(other.highLevelDomain == highLevelDomain);
if (lastSeen < other.lastSeen)
lastSeen = other.lastSeen;
if (!other.hadUserInteraction) {
if (!other.mostRecentUserInteractionTime) {
hadUserInteraction = false;
mostRecentUserInteractionTime = { };
}
} else {
hadUserInteraction = true;
if (mostRecentUserInteractionTime < other.mostRecentUserInteractionTime)
mostRecentUserInteractionTime = other.mostRecentUserInteractionTime;
}
grandfathered |= other.grandfathered;
mergeHashSet(storageAccessUnderTopFrameOrigins, other.storageAccessUnderTopFrameOrigins);
mergeHashCountedSet(subframeUnderTopFrameOrigins, other.subframeUnderTopFrameOrigins);
mergeHashCountedSet(subresourceUnderTopFrameOrigins, other.subresourceUnderTopFrameOrigins);
mergeHashCountedSet(subresourceUniqueRedirectsTo, other.subresourceUniqueRedirectsTo);
isPrevalentResource |= other.isPrevalentResource;
dataRecordsRemoved = std::max(dataRecordsRemoved, other.dataRecordsRemoved);
isMarkedForCookiePartitioning |= other.isMarkedForCookiePartitioning;
isMarkedForCookieBlocking |= other.isMarkedForCookieBlocking;
}
String ResourceLoadStatistics::primaryDomain(const URL& url)
{
return primaryDomain(url.host());
}
String ResourceLoadStatistics::primaryDomain(const String& host)
{
if (host.isNull() || host.isEmpty())
return ASCIILiteral("nullOrigin");
#if ENABLE(PUBLIC_SUFFIX_LIST)
String primaryDomain = topPrivatelyControlledDomain(host);
if (!primaryDomain.isEmpty())
return primaryDomain;
#endif
return host;
}
}