summaryrefslogtreecommitdiffstats
path: root/chromium/components/variations/variations_layers.h
blob: cdc6b94b09cf053cc2f1d076d558f08d48e18bad (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
// 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.

#ifndef COMPONENTS_VARIATIONS_VARIATIONS_LAYERS_H_
#define COMPONENTS_VARIATIONS_VARIATIONS_LAYERS_H_

#include <map>

#include "base/component_export.h"
#include "base/metrics/field_trial.h"
#include "components/variations/entropy_provider.h"
#include "components/variations/proto/variations_seed.pb.h"
#include "third_party/abseil-cpp/absl/types/optional.h"

namespace variations {

enum class InvalidLayerReason {
  kInvalidId = 0,
  kNoSlots = 1,
  kNoMembers = 2,
  kInvalidEntropyMode = 3,
  kSlotsDoNotDivideLowEntropyDomain = 4,
  kInvalidSlotBounds = 5,
  kUnknownFields = 6,
  kMaxValue = kUnknownFields,
};

// A view over the layers defined within a variations seed.
//
// A layer defines a collection of mutually exclusive members. For each client,
// at most one member will be assigned as its active member. Studies may be
// conditioned on a particular member being active, in order to avoid overlap
// with studies that require a different member to be active.
class COMPONENT_EXPORT(VARIATIONS) VariationsLayers {
 public:
  VariationsLayers(const VariationsSeed& seed,
                   const EntropyProviders& entropy_providers);

  VariationsLayers();
  ~VariationsLayers();

  VariationsLayers(const VariationsLayers&) = delete;
  VariationsLayers& operator=(const VariationsLayers&) = delete;

  // Returns whether the given layer has the given member active.
  bool IsLayerMemberActive(uint32_t layer_id, uint32_t member_id) const;

  // Returns true if the layer has an active member and is configured to use
  // DEFAULT entropy, which means that any study conditioned on it would leak
  // information about the client's high entropy source (including whether or
  // not the client _has_ a high entropy source).
  bool ActiveLayerMemberDependsOnHighEntropy(uint32_t layer_id) const;

  // Gets an EntropyProvider for low entropy randomization of studies
  // conditioned on the layer's active member.
  const base::FieldTrial::EntropyProvider& GetRemainderEntropy(
      uint32_t layer_id) const;

 private:
  struct LayerInfo {
    // Which layer member is active in the layer.
    uint32_t active_member_id;
    // The type of entropy the layer was configured to use.
    Layer::EntropyMode entropy_mode;
    // If this layer has an active member, this is the remaining entropy from
    // that selection, which can be used for uniform randomization of studies
    // conditioned on that layer member.
    // See ComputeRemainderEntropy() for details.
    NormalizedMurmurHashEntropyProvider remainder_entropy;
  };

  void ConstructLayer(const EntropyProviders& entropy_providers,
                      const Layer& layer_proto);

  NormalizedMurmurHashEntropyProvider nil_entropy;
  std::map<uint32_t, LayerInfo> active_member_for_layer_;
};

}  // namespace variations

#endif  // COMPONENTS_VARIATIONS_VARIATIONS_LAYERS_H_