blob: 8445104d6701163bc4d6730a9b665d60d7a6b200 (
plain)
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
|
// Copyright 2020 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/variations/variations_layers.h"
#include "base/metrics/field_trial.h"
namespace variations {
namespace {
// Iterates through the members of the given layer proto definition, and
// returns the member which contains that slot (if any).
const Layer::LayerMember* FindActiveMemberBySlot(uint32_t chosen_slot,
const Layer& layer_proto) {
for (const Layer::LayerMember& member : layer_proto.members()) {
if (!member.id())
continue;
for (const Layer::LayerMember::SlotRange& slot : member.slots()) {
if (slot.start() <= chosen_slot && chosen_slot <= slot.end())
return &member;
}
}
return nullptr;
}
} // namespace
VariationsLayers::VariationsLayers(const VariationsSeed& seed,
const EntropyProviders& entropy_providers) {
// TODO(crbug.com/1154033): Support a way to expire old/unused layers so they
// no longer get processed by the clients.
for (const Layer& layer_proto : seed.layers())
ConstructLayer(entropy_providers, layer_proto);
}
VariationsLayers::VariationsLayers() = default;
VariationsLayers::~VariationsLayers() = default;
void VariationsLayers::ConstructLayer(const EntropyProviders& entropy_providers,
const Layer& layer_proto) {
if (layer_proto.id() == 0 || layer_proto.num_slots() == 0 ||
layer_proto.members_size() == 0) {
return;
}
if (layer_proto.entropy_mode() != Layer::LOW &&
layer_proto.entropy_mode() != Layer::DEFAULT) {
return;
}
const auto& entropy_provider = (layer_proto.entropy_mode() != Layer::LOW)
? entropy_providers.default_entropy()
: entropy_providers.low_entropy();
uint32_t chosen_slot = entropy_provider.GetPseudorandomValue(
layer_proto.salt(), layer_proto.num_slots());
const auto* chosen_member = FindActiveMemberBySlot(chosen_slot, layer_proto);
if (!chosen_member)
return;
active_member_for_layer_.emplace(
layer_proto.id(), LayerInfo{/*.active_member_id =*/ chosen_member->id(),
/* .entropy_mode =*/ layer_proto.entropy_mode()});
}
bool VariationsLayers::IsLayerMemberActive(uint32_t layer_id,
uint32_t member_id) const {
auto layer_iter = active_member_for_layer_.find(layer_id);
if (layer_iter == active_member_for_layer_.end())
return false;
return layer_iter->second.active_member_id &&
(member_id == layer_iter->second.active_member_id);
}
bool VariationsLayers::ActiveLayerMemberDependsOnHighEntropy(
uint32_t layer_id) const {
auto layer_iter = active_member_for_layer_.find(layer_id);
if (layer_iter == active_member_for_layer_.end())
return false;
return layer_iter->second.entropy_mode == Layer::DEFAULT;
}
} // namespace variations
|