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
|
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/sync/service/trusted_vault_synthetic_field_trial.h"
#include <memory>
#include <ostream>
#include <string>
#include <string_view>
#include "base/metrics/histogram_functions.h"
#include "base/notreached.h"
#include "base/numerics/byte_conversions.h"
#include "base/rand_util.h"
#include "base/strings/strcat.h"
#include "base/strings/stringprintf.h"
#include "components/sync/protocol/nigori_specifics.pb.h"
#include "crypto/hash.h"
#include "google_apis/gaia/gaia_id.h"
namespace syncer {
namespace {
// Arbitrary and generious limit for the cohort ID.
constexpr int kMaxCohortId = 100;
// Arbitrary and generous limit for the group type index.
constexpr int kMaxGroupTypeIndex = 50;
const char* GetGroupTypeName(
sync_pb::TrustedVaultAutoUpgradeExperimentGroup::Type type) {
switch (type) {
case sync_pb::TrustedVaultAutoUpgradeExperimentGroup::TYPE_UNSPECIFIED:
return "";
case sync_pb::TrustedVaultAutoUpgradeExperimentGroup::TREATMENT:
return "Treatment";
case sync_pb::TrustedVaultAutoUpgradeExperimentGroup::CONTROL:
return "Control";
case sync_pb::TrustedVaultAutoUpgradeExperimentGroup::VALIDATION:
return "Validation";
}
NOTREACHED();
}
std::string GetGroupName(
int cohort,
sync_pb::TrustedVaultAutoUpgradeExperimentGroup::Type type,
int type_index) {
if (cohort <= 0 || cohort > kMaxCohortId) {
// Invalid cohort ID.
return std::string();
}
if (type_index < 0 || type_index > kMaxGroupTypeIndex) {
// Invalid type index.
return std::string();
}
const char* type_str = GetGroupTypeName(type);
if (!*type_str) {
// Invalid type.
return std::string();
}
std::string type_index_str;
if (type_index > 0) {
type_index_str = base::StringPrintf("%d", type_index);
}
return base::StringPrintf("Cohort%d_%s%s", cohort, type_str,
type_index_str.c_str());
}
// Returns a random-like float in range [0, 1) that is computed
// deterministically from `gaia_id` and `salt`.
float DeterministicFloatBetweenZeroAndOneFromGaiaId(const GaiaId& gaia_id,
std::string_view salt) {
CHECK(!gaia_id.empty());
const std::string_view kSuffix = "TrustedVaultAutoUpgrade";
crypto::hash::Hasher sha256(crypto::hash::HashKind::kSha256);
sha256.Update(gaia_id.ToString());
sha256.Update(salt);
sha256.Update(kSuffix);
std::array<uint8_t, crypto::hash::kSha256Size> full_hash;
sha256.Finish(full_hash);
uint64_t value = base::U64FromNativeEndian(base::span(full_hash).first<8>());
const int kResolution = 100000;
return 1.0f * (value % kResolution) / kResolution;
}
bool ShouldSampleGaiaIdWithTenPercentProbability(const GaiaId& gaia_id) {
const float kGaiaIdSamplingFactor = 0.1f;
const std::string_view kSaltForUserSampling = "UserSampling";
return DeterministicFloatBetweenZeroAndOneFromGaiaId(
gaia_id, kSaltForUserSampling) < kGaiaIdSamplingFactor;
}
} // namespace
// static
std::string TrustedVaultAutoUpgradeSyntheticFieldTrialGroup::
GetMultiProfileConflictGroupName() {
return "MultiProfileConflict";
}
// static
TrustedVaultAutoUpgradeSyntheticFieldTrialGroup
TrustedVaultAutoUpgradeSyntheticFieldTrialGroup::FromProto(
const sync_pb::TrustedVaultAutoUpgradeExperimentGroup& proto) {
TrustedVaultAutoUpgradeSyntheticFieldTrialGroup instance;
instance.name_ =
GetGroupName(proto.cohort(), proto.type(), proto.type_index());
instance.is_validation_group_type_ =
(proto.type() ==
sync_pb::TrustedVaultAutoUpgradeExperimentGroup::VALIDATION);
return instance;
}
TrustedVaultAutoUpgradeSyntheticFieldTrialGroup::
TrustedVaultAutoUpgradeSyntheticFieldTrialGroup() = default;
TrustedVaultAutoUpgradeSyntheticFieldTrialGroup::
TrustedVaultAutoUpgradeSyntheticFieldTrialGroup(
const TrustedVaultAutoUpgradeSyntheticFieldTrialGroup&) = default;
TrustedVaultAutoUpgradeSyntheticFieldTrialGroup::
TrustedVaultAutoUpgradeSyntheticFieldTrialGroup(
TrustedVaultAutoUpgradeSyntheticFieldTrialGroup&&) = default;
TrustedVaultAutoUpgradeSyntheticFieldTrialGroup::
~TrustedVaultAutoUpgradeSyntheticFieldTrialGroup() = default;
TrustedVaultAutoUpgradeSyntheticFieldTrialGroup&
TrustedVaultAutoUpgradeSyntheticFieldTrialGroup::operator=(
const TrustedVaultAutoUpgradeSyntheticFieldTrialGroup&) = default;
TrustedVaultAutoUpgradeSyntheticFieldTrialGroup&
TrustedVaultAutoUpgradeSyntheticFieldTrialGroup::operator=(
TrustedVaultAutoUpgradeSyntheticFieldTrialGroup&&) = default;
void TrustedVaultAutoUpgradeSyntheticFieldTrialGroup::
LogValidationMetricsUponOnProfileLoad(const GaiaId& gaia_id) const {
CHECK(is_valid());
if (gaia_id.empty()) {
// Shouldn't be reachable, but in edge cases (preferences corrupted) it
// could be.
return;
}
// This metrics gets logged for 10% gaia IDs to artifially reduce the volume
// of data.
if (!ShouldSampleGaiaIdWithTenPercentProbability(gaia_id)) {
return;
}
LogValidationMetrics(gaia_id, "OnProfileLoadSampled");
}
void TrustedVaultAutoUpgradeSyntheticFieldTrialGroup::LogValidationMetrics(
const GaiaId& gaia_id,
std::string_view short_metric_name) const {
const struct {
const std::string_view name_suffix;
float probability_default;
float probability_validation;
} boolean_metrics[] = {
{"Binary_C01_V01", 0.01f, 0.01f}, {"Binary_C01_V04", 0.01f, 0.04f},
{"Binary_C01_V06", 0.01f, 0.06f}, {"Binary_C01_V09", 0.01f, 0.09f},
{"Binary_C20_V20", 0.20f, 0.20f}, {"Binary_C20_V23", 0.20f, 0.23f},
{"Binary_C20_V25", 0.20f, 0.25f}, {"Binary_C20_V28", 0.20f, 0.28f},
{"Binary_C50_V50", 0.50f, 0.50f}, {"Binary_C50_V53", 0.50f, 0.53f},
{"Binary_C50_V55", 0.50f, 0.55f}, {"Binary_C50_V58", 0.50f, 0.58f},
};
for (bool use_account_consistency : {false, true}) {
const std::string_view metric_infix = use_account_consistency
? ".WithAccountConsistency."
: ".WithoutAccountConsistency.";
const float value_between_zero_and_one =
use_account_consistency ? DeterministicFloatBetweenZeroAndOneFromGaiaId(
gaia_id, /*salt=*/short_metric_name)
: base::RandFloat();
CHECK_GE(value_between_zero_and_one, 0.0f);
CHECK_LT(value_between_zero_and_one, 1.0f);
for (const auto& metric : boolean_metrics) {
const std::string full_metric_name =
base::StrCat({"Sync.TrustedVaultAutoUpgrade.Validation.",
short_metric_name, metric_infix, metric.name_suffix});
const float probability = is_validation_group_type_
? metric.probability_validation
: metric.probability_default;
const bool success = value_between_zero_and_one < probability;
base::UmaHistogramBoolean(full_metric_name, success);
}
}
}
// static
float TrustedVaultAutoUpgradeSyntheticFieldTrialGroup::
DeterministicFloatBetweenZeroAndOneFromGaiaIdForTest(
const GaiaId& gaia_id,
std::string_view salt) {
return DeterministicFloatBetweenZeroAndOneFromGaiaId(gaia_id, salt);
}
// static
bool TrustedVaultAutoUpgradeSyntheticFieldTrialGroup::
ShouldSampleGaiaIdWithTenPercentProbabilityForTest(const GaiaId& gaia_id) {
return ShouldSampleGaiaIdWithTenPercentProbability(gaia_id);
}
void PrintTo(const TrustedVaultAutoUpgradeSyntheticFieldTrialGroup& group,
std::ostream* os) {
if (group.is_valid()) {
*os << group.name();
} else {
*os << "<invalid-group>";
}
}
} // namespace syncer
|