File: privacy_budget_browsertest_util.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 (143 lines) | stat: -rw-r--r-- 5,549 bytes parent folder | download | duplicates (5)
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
// 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 "chrome/browser/privacy_budget/privacy_budget_browsertest_util.h"

#include "base/memory/raw_ptr.h"
#include "chrome/browser/metrics/chrome_metrics_service_accessor.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/test/base/chrome_test_utils.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/test_utils.h"

PrivacyBudgetBrowserTestBaseWithTestRecorder::
    PrivacyBudgetBrowserTestBaseWithTestRecorder() = default;
PrivacyBudgetBrowserTestBaseWithTestRecorder::
    ~PrivacyBudgetBrowserTestBaseWithTestRecorder() = default;

void PrivacyBudgetBrowserTestBaseWithTestRecorder::SetUpOnMainThread() {
  PlatformBrowserTest::SetUpOnMainThread();
  // Do an initial empty navigation then create the recorder to make sure we
  // start on a clean slate. This clears the platform differences in between
  // Android and Desktop.
  content::NavigateToURLBlockUntilNavigationsComplete(web_contents(),
                                                      GURL("about:blank"), 1);

  // Ensure that the actively sampled surfaces reported at browser startup go
  // through before we set up the test recorder.
  content::RunAllTasksUntilIdle();
  ukm_recorder_ = std::make_unique<ukm::TestAutoSetUkmRecorder>();
}

base::flat_set<uint64_t>
PrivacyBudgetBrowserTestBaseWithTestRecorder::GetReportedSurfaceKeys(
    std::vector<uint64_t> expected_keys) {
  std::map<ukm::SourceId, ukm::mojom::UkmEntryPtr> merged_entries =
      ukm_recorder_->GetMergedEntriesByName(
          ukm::builders::Identifiability::kEntryName);

  base::flat_set<uint64_t> reported_surface_keys;
  for (const auto& entry : merged_entries) {
    for (const auto& metric : entry.second->metrics) {
      if (base::Contains(expected_keys, metric.first))
        reported_surface_keys.insert(metric.first);
    }
  }
  return reported_surface_keys;
}

int PrivacyBudgetBrowserTestBaseWithTestRecorder::GetSurfaceKeyCount(
    uint64_t expected_key) {
  std::vector<raw_ptr<const ukm::mojom::UkmEntry, VectorExperimental>> entries =
      ukm_recorder_->GetEntriesByName(
          ukm::builders::Identifiability::kEntryName);

  int count = 0;
  for (const ukm::mojom::UkmEntry* entry : entries) {
    for (const auto& metric : entry->metrics) {
      if (expected_key == metric.first)
        count++;
    }
  }
  return count;
}

content::WebContents*
PrivacyBudgetBrowserTestBaseWithTestRecorder::web_contents() {
  return chrome_test_utils::GetActiveWebContents(this);
}

const std::string& PrivacyBudgetBrowserTestBaseWithTestRecorder::FilePathXYZ() {
  return GetParam();
}

PrivacyBudgetBrowserTestBaseWithUkmRecording::
    PrivacyBudgetBrowserTestBaseWithUkmRecording()
    : SyncTest(SINGLE_CLIENT) {}

PrivacyBudgetBrowserTestBaseWithUkmRecording::
    ~PrivacyBudgetBrowserTestBaseWithUkmRecording() = default;

ukm::UkmService* PrivacyBudgetBrowserTestBaseWithUkmRecording::ukm_service() {
  return g_browser_process->GetMetricsServicesManager()->GetUkmService();
}

PrefService* PrivacyBudgetBrowserTestBaseWithUkmRecording::local_state() {
  return g_browser_process->local_state();
}

bool PrivacyBudgetBrowserTestBaseWithUkmRecording::EnableUkmRecording() {
  // 1. Enable sync.
  Profile* profile = ProfileManager::GetLastUsedProfileIfLoaded();
  sync_test_harness_ = metrics::test::InitializeProfileForSync(
      profile, GetFakeServer()->AsWeakPtr());
  EXPECT_TRUE(sync_test_harness_->SetupSync());

  // 2. Signal consent for UKM reporting.
  unified_consent::UnifiedConsentService* consent_service =
      UnifiedConsentServiceFactory::GetForProfile(profile);
  if (consent_service != nullptr)
    consent_service->SetUrlKeyedAnonymizedDataCollectionEnabled(true);

  // 3. Enable metrics reporting.
  is_metrics_reporting_enabled_ = true;
  ChromeMetricsServiceAccessor::SetMetricsAndCrashReportingForTesting(
      &is_metrics_reporting_enabled_);

  // UpdateUploadPermissions causes the MetricsServicesManager to look at the
  // consent signals and re-evaluate whether reporting should be enabled.
  g_browser_process->GetMetricsServicesManager()->UpdateUploadPermissions();

  // The following sequence synchronously completes UkmService initialization
  // (if it wasn't initialized yet) and flushes any accumulated metrics.
  ukm::UkmTestHelper ukm_test_helper(ukm_service());
  ukm_test_helper.BuildAndStoreLog();
  std::unique_ptr<ukm::Report> report_to_discard =
      ukm_test_helper.GetUkmReport();

  ukm_service()->SetSamplingForTesting(1);
  return ukm::UkmTestHelper(ukm_service()).IsRecordingEnabled();
}

bool PrivacyBudgetBrowserTestBaseWithUkmRecording::DisableUkmRecording() {
  EXPECT_TRUE(is_metrics_reporting_enabled_)
      << "DisableUkmRecording() should only be called after "
         "EnableUkmRecording()";
  is_metrics_reporting_enabled_ = false;
  g_browser_process->GetMetricsServicesManager()->UpdateUploadPermissions();
  ChromeMetricsServiceAccessor::SetMetricsAndCrashReportingForTesting(nullptr);
  return !ukm::UkmTestHelper(ukm_service()).IsRecordingEnabled();
}

content::WebContents*
PrivacyBudgetBrowserTestBaseWithUkmRecording::web_contents() {
  return chrome_test_utils::GetActiveWebContents(this);
}

void PrivacyBudgetBrowserTestBaseWithUkmRecording::TearDown() {
  if (is_metrics_reporting_enabled_) {
    ChromeMetricsServiceAccessor::SetMetricsAndCrashReportingForTesting(
        nullptr);
  }
}