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
|
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "third_party/blink/public/common/use_counter/use_counter_feature_tracker.h"
namespace blink {
namespace {
template <size_t N>
bool BitsetContains(const std::bitset<N>& lhs, const std::bitset<N>& rhs) {
return (lhs & rhs) == rhs;
}
} // namespace
using FeatureType = mojom::UseCounterFeatureType;
bool UseCounterFeatureTracker::Test(const UseCounterFeature& feature) const {
switch (feature.type()) {
case FeatureType::kWebFeature:
return web_features_.test(feature.value());
case FeatureType::kWebDXFeature:
return webdx_features_.test(feature.value());
case FeatureType::kCssProperty:
return css_properties_.test(feature.value());
case FeatureType::kAnimatedCssProperty:
return animated_css_properties_.test(feature.value());
case FeatureType::kPermissionsPolicyViolationEnforce:
return violated_permissions_policy_features_.test(feature.value());
case FeatureType::kPermissionsPolicyIframeAttribute:
return iframe_permissions_policy_features_.test(feature.value());
case FeatureType::kPermissionsPolicyHeader:
return header_permissions_policy_features_.test(feature.value());
case FeatureType::kPermissionsPolicyEnabledPrivacySensitive:
return private_permissions_policy_features_.test(feature.value());
}
}
bool UseCounterFeatureTracker::TestAndSet(const UseCounterFeature& feature) {
bool has_record = Test(feature);
Set(feature, true);
return has_record;
}
std::vector<UseCounterFeature> UseCounterFeatureTracker::GetRecordedFeatures()
const {
std::vector<UseCounterFeature> ret;
for (uint32_t i = 0; i < web_features_.size(); i++) {
if (web_features_.test(i))
ret.emplace_back(FeatureType::kWebFeature, i);
}
for (uint32_t i = 0; i < css_properties_.size(); i++) {
if (css_properties_.test(i))
ret.emplace_back(FeatureType::kCssProperty, i);
}
for (uint32_t i = 0; i < animated_css_properties_.size(); i++) {
if (animated_css_properties_.test(i))
ret.emplace_back(FeatureType::kAnimatedCssProperty, i);
}
for (uint32_t i = 0; i < violated_permissions_policy_features_.size(); i++) {
if (violated_permissions_policy_features_.test(i))
ret.emplace_back(FeatureType::kPermissionsPolicyViolationEnforce, i);
}
for (uint32_t i = 0; i < iframe_permissions_policy_features_.size(); i++) {
if (iframe_permissions_policy_features_.test(i))
ret.emplace_back(FeatureType::kPermissionsPolicyIframeAttribute, i);
}
for (uint32_t i = 0; i < header_permissions_policy_features_.size(); i++) {
if (header_permissions_policy_features_.test(i))
ret.emplace_back(FeatureType::kPermissionsPolicyHeader, i);
}
for (uint32_t i = 0; i < private_permissions_policy_features_.size(); i++) {
if (private_permissions_policy_features_.test(i)) {
ret.emplace_back(FeatureType::kPermissionsPolicyEnabledPrivacySensitive,
i);
}
}
return ret;
}
void UseCounterFeatureTracker::ResetForTesting(
const UseCounterFeature& feature) {
Set(feature, false);
}
bool UseCounterFeatureTracker::ContainsForTesting(
const UseCounterFeatureTracker& other) const {
return BitsetContains(web_features_, other.web_features_) &&
BitsetContains(css_properties_, other.css_properties_) &&
BitsetContains(animated_css_properties_,
other.animated_css_properties_) &&
BitsetContains(private_permissions_policy_features_,
other.private_permissions_policy_features_);
}
void UseCounterFeatureTracker::Set(const UseCounterFeature& feature,
bool value) {
switch (feature.type()) {
case FeatureType::kWebFeature:
web_features_[feature.value()] = value;
break;
case FeatureType::kWebDXFeature:
webdx_features_[feature.value()] = value;
break;
case FeatureType::kCssProperty:
css_properties_[feature.value()] = value;
break;
case FeatureType::kAnimatedCssProperty:
animated_css_properties_[feature.value()] = value;
break;
case FeatureType::kPermissionsPolicyViolationEnforce:
violated_permissions_policy_features_[feature.value()] = value;
break;
case FeatureType::kPermissionsPolicyIframeAttribute:
iframe_permissions_policy_features_[feature.value()] = value;
break;
case FeatureType::kPermissionsPolicyHeader:
header_permissions_policy_features_[feature.value()] = value;
break;
case FeatureType::kPermissionsPolicyEnabledPrivacySensitive:
private_permissions_policy_features_[feature.value()] = value;
break;
}
}
} // namespace blink
|