File: scoped_iph_feature_list.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (127 lines) | stat: -rw-r--r-- 3,866 bytes parent folder | download | duplicates (6)
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
// Copyright 2022 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/feature_engagement/test/scoped_iph_feature_list.h"

#include "base/metrics/field_trial_params.h"
#include "base/test/scoped_feature_list.h"
#include "components/feature_engagement/internal/blocked_iph_features.h"
#include "components/feature_engagement/public/feature_constants.h"
#include "components/feature_engagement/public/feature_list.h"

namespace feature_engagement::test {

ScopedIphFeatureList::ScopedIphFeatureList() = default;

ScopedIphFeatureList::~ScopedIphFeatureList() {
  Reset();
}

void ScopedIphFeatureList::InitWithNoFeaturesAllowed() {
  InitWithExistingFeatures({});
}

void ScopedIphFeatureList::InitWithExistingFeatures(
    const std::vector<base::test::FeatureRef>& allow_features) {
  CHECK(!active_);

  std::vector<const base::Feature*> features;
  for (auto& ref : allow_features) {
    features.push_back(&*ref);
  }

  InitCommon(features);
}

void ScopedIphFeatureList::InitAndEnableFeatures(
    const std::vector<base::test::FeatureRef>& allow_and_enable_features,
    const std::vector<base::test::FeatureRef>& disable_features) {
  CHECK(!active_);

  feature_list_.InitWithFeatures(allow_and_enable_features, disable_features);

  std::vector<const base::Feature*> features;
  for (auto& ref : allow_and_enable_features) {
    features.push_back(&*ref);
  }

  InitCommon(features);
}

void ScopedIphFeatureList::InitAndEnableFeaturesWithParameters(
    const std::vector<base::test::FeatureRefAndParams>&
        allow_and_enable_features,
    const std::vector<base::test::FeatureRef>& disable_features) {
  CHECK(!active_);

  feature_list_.InitWithFeaturesAndParameters(allow_and_enable_features,
                                              disable_features);

  std::vector<const base::Feature*> features;
  for (auto& ref : allow_and_enable_features) {
    features.push_back(&ref.feature.get());
  }

  InitCommon(features);
}

void ScopedIphFeatureList::InitForDemo(
    const base::Feature& feature,
    const std::vector<base::test::FeatureRefAndParams>& additional_features) {
  std::vector<base::test::FeatureRefAndParams> actual_features =
      additional_features;
  actual_features.emplace_back(
      kIPHDemoMode,
      base::FieldTrialParams{{kIPHDemoModeFeatureChoiceParam, feature.name}});
  actual_features.emplace_back(feature, base::FieldTrialParams{});
  InitAndEnableFeaturesWithParameters(actual_features);
}

void ScopedIphFeatureList::Reset() {
  if (!active_) {
    return;
  }

  active_ = false;

  auto* const allowed = BlockedIphFeatures::GetInstance();
  base::AutoLock lock(allowed->GetLock());

  // Decrement the individual feature refcounts for each feature that was
  // previously added.
  for (const base::Feature* feature : added_features_) {
    allowed->DecrementFeatureAllowedCount(feature->name);
  }
  added_features_.clear();

  // Decrement total count of entities requesting blocks.
  allowed->DecrementGlobalBlockCount();

  // Reset the feature list in case any features were enabled.
  feature_list_.Reset();
}

void ScopedIphFeatureList::InitCommon(
    const std::vector<const base::Feature*>& features) {
  CHECK(!active_);
  CHECK(added_features_.empty());

  active_ = true;

  auto* const allowed = BlockedIphFeatures::GetInstance();
  base::AutoLock lock(allowed->GetLock());

  // Increment the total number of entities requesting a block.
  allowed->IncrementGlobalBlockCount();

  // Increment the refcount for each unique feature and add it to the saved
  // list for when they will be removed.
  for (auto* feature : features) {
    if (added_features_.insert(feature).second) {
      allowed->IncrementFeatureAllowedCount(feature->name);
    }
  }
}

}  // namespace feature_engagement::test