File: identifiability_study_group_settings.cc

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (151 lines) | stat: -rw-r--r-- 5,454 bytes parent folder | download | duplicates (3)
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
// 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 "chrome/browser/privacy_budget/identifiability_study_group_settings.h"

#include <algorithm>
#include <numeric>

#include "base/containers/flat_map.h"
#include "chrome/common/privacy_budget/privacy_budget_features.h"
#include "chrome/common/privacy_budget/types.h"
#include "third_party/blink/public/common/privacy_budget/identifiable_surface.h"

// static
IdentifiabilityStudyGroupSettings
IdentifiabilityStudyGroupSettings::InitFromFeatureParams() {
  return InitFrom(base::FeatureList::IsEnabled(features::kIdentifiabilityStudy),
                  features::kIdentifiabilityStudyExpectedSurfaceCount.Get(),
                  features::kIdentifiabilityStudyActiveSurfaceBudget.Get(),
                  features::kIdentifiabilityStudyBlocks.Get(),
                  features::kIdentifiabilityStudyBlockWeights.Get(),
                  features::kIdentifiabilityStudyAllowedRandomTypes.Get());
}

// static
IdentifiabilityStudyGroupSettings IdentifiabilityStudyGroupSettings::InitFrom(
    bool enabled,
    int expected_surface_count,
    int surface_budget,
    const std::string& blocks,
    const std::string& blocks_weights,
    const std::string& allowed_random_types) {
  return IdentifiabilityStudyGroupSettings(
      enabled, expected_surface_count, surface_budget,
      DecodeIdentifiabilityFieldTrialParam<IdentifiableSurfaceBlocks>(blocks),
      DecodeIdentifiabilityFieldTrialParam<std::vector<double>>(blocks_weights),
      DecodeIdentifiabilityFieldTrialParam<
          std::vector<blink::IdentifiableSurface::Type>>(allowed_random_types));
}

IdentifiabilityStudyGroupSettings::IdentifiabilityStudyGroupSettings(
    bool enabled,
    int expected_surface_count,
    int surface_budget,
    IdentifiableSurfaceBlocks blocks,
    std::vector<double> blocks_weights,
    std::vector<blink::IdentifiableSurface::Type> allowed_random_types)
    : enabled_(enabled),
      expected_surface_count_(std::clamp<int>(
          expected_surface_count,
          0,
          features::kMaxIdentifiabilityStudyExpectedSurfaceCount)),
      surface_budget_(std::clamp<int>(
          surface_budget,
          0,
          features::kMaxIdentifiabilityStudyActiveSurfaceBudget)),
      blocks_(std::move(blocks)),
      blocks_weights_(std::move(blocks_weights)),
      allowed_random_types_(std::move(allowed_random_types)) {
  if (!Validate()) {
    enabled_ = false;
  }
}

IdentifiabilityStudyGroupSettings::~IdentifiabilityStudyGroupSettings() =
    default;
IdentifiabilityStudyGroupSettings::IdentifiabilityStudyGroupSettings(
    IdentifiabilityStudyGroupSettings&&) = default;

bool IdentifiabilityStudyGroupSettings::Validate() {
  // Disabling the Identifiability Study feature flag is a valid configuration.
  if (!enabled_)
    return true;
  // If the study is enabled, at least one of assigned-block-sampling or
  // random-surface-assignment should be enabled.
  if (!IsUsingAssignedBlockSampling() && !IsUsingRandomSampling()) {
    return false;
  }
  if (IsUsingAssignedBlockSampling() && IsUsingRandomSampling())
    return false;
  if (IsUsingAssignedBlockSampling() && !ValidateAssignedBlockSampling())
    return false;

  return true;
}

bool IdentifiabilityStudyGroupSettings::ValidateAssignedBlockSampling() {
  // For every block there should be a weight.
  if (blocks_weights_.size() != blocks_.size())
    return false;

  // Weights should be positive.
  for (double weight : blocks_weights_) {
    if (weight < 0)
      return false;
  }

  // Each single surface should have probability lower than the threshold to be
  // selected.
  base::flat_map<blink::IdentifiableSurface, double> weight_per_surface;
  for (size_t i = 0; i < blocks_.size(); ++i) {
    for (const blink::IdentifiableSurface& surface : blocks_[i]) {
      auto el = weight_per_surface.find(surface);
      if (el != weight_per_surface.end()) {
        weight_per_surface.insert_or_assign(surface,
                                            el->second + blocks_weights_[i]);
      } else {
        weight_per_surface.insert_or_assign(surface, blocks_weights_[i]);
      }
    }
  }
  double sum_weights =
      std::accumulate(blocks_weights_.begin(), blocks_weights_.end(), 0.0);
  for (const auto& iter : weight_per_surface) {
    double surface_probability = iter.second / sum_weights;
    if (surface_probability > features::kMaxProbabilityPerSurface)
      return false;
  }

  return true;
}

const IdentifiableSurfaceBlocks& IdentifiabilityStudyGroupSettings::blocks()
    const {
  return blocks_;
}

const std::vector<double>& IdentifiabilityStudyGroupSettings::blocks_weights()
    const {
  return blocks_weights_;
}

const std::vector<blink::IdentifiableSurface::Type>&
IdentifiabilityStudyGroupSettings::allowed_random_types() const {
  return allowed_random_types_;
}

bool IdentifiabilityStudyGroupSettings::IsUsingAssignedBlockSampling() const {
  return !blocks().empty();
}

bool IdentifiabilityStudyGroupSettings::IsUsingRandomSampling() const {
  return expected_surface_count() > 0;
}

bool IdentifiabilityStudyGroupSettings::IsUsingSamplingOfSurfaces() const {
  // Random and assigned block sampling are mutually exclusive.
  DCHECK(!IsUsingRandomSampling() || !IsUsingAssignedBlockSampling());
  return IsUsingRandomSampling() || IsUsingAssignedBlockSampling();
}