File: scoped_metrics_service_for_synthetic_trials.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; 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 (55 lines) | stat: -rw-r--r-- 2,320 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
// Copyright 2023 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/test/base/scoped_metrics_service_for_synthetic_trials.h"

ScopedMetricsServiceForSyntheticTrials::ScopedMetricsServiceForSyntheticTrials(
    TestingBrowserProcess* testing_browser_process)
    : browser_process_(testing_browser_process) {
  CHECK(browser_process_);

  auto* local_state = browser_process_->local_state();
  CHECK(local_state)
      << "Error: local state prefs are required. In a unit test, this can be "
         "set up using base::test::ScopedFeatureList.";

  // The `SyntheticTrialsActiveGroupIdProvider` needs to be notified of
  // changes from the registry for them to be used through the variations API.
  synthetic_trial_registry_observation_.Observe(&synthetic_trial_registry_);

  metrics_service_client_.set_synthetic_trial_registry(
      &synthetic_trial_registry_);

  metrics_state_manager_ = metrics::MetricsStateManager::Create(
      local_state, &enabled_state_provider_,
      /*backup_registry_key=*/std::wstring(),
      /*user_data_dir=*/base::FilePath());

  // Needs to be set up, will be updated at each synthetic trial change.
  variations::InitCrashKeys();

  // Required by `MetricsService` to record UserActions. We don't rely on
  // these here, since we never make it start recording metrics, but the task
  // runner is still required during the shutdown sequence.
  base::SetRecordActionTaskRunner(
      base::SingleThreadTaskRunner::GetCurrentDefault());

  metrics_service_ = std::make_unique<metrics::MetricsService>(
      metrics_state_manager_.get(), &metrics_service_client_, local_state);

  browser_process_->SetMetricsService(metrics_service_.get());
}

ScopedMetricsServiceForSyntheticTrials::
    ~ScopedMetricsServiceForSyntheticTrials() {
  // The scope is closing, undo the set up that was done in the constructor:
  // `MetricsService` and other necessary parts like crash keys.
  browser_process_->SetMetricsService(nullptr);
  variations::ClearCrashKeysInstanceForTesting();

  // Note: Clears all the synthetic trials, not juste the ones registered
  // during the lifetime of this object.
  variations::SyntheticTrialsActiveGroupIdProvider::GetInstance()
      ->ResetForTesting();
}