File: servicification_background_service_jni.cc

package info (click to toggle)
chromium 135.0.7049.95-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 5,959,392 kB
  • sloc: cpp: 34,198,526; ansic: 7,100,035; javascript: 3,985,800; python: 1,395,489; asm: 896,754; xml: 722,891; pascal: 180,504; sh: 94,909; perl: 88,388; objc: 79,739; sql: 53,020; cs: 41,358; fortran: 24,137; makefile: 22,501; php: 13,699; tcl: 10,142; yacc: 8,822; ruby: 7,350; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; awk: 197; sed: 36
file content (129 lines) | stat: -rw-r--r-- 4,475 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
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <sstream>
#include <string_view>

#include "base/android/jni_android.h"
#include "base/files/file.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/files/memory_mapped_file.h"
#include "base/metrics/field_trial.h"
#include "base/metrics/persistent_histogram_allocator.h"
#include "base/system/sys_info.h"
#include "chrome/android/test_support_jni_headers/ServicificationBackgroundService_jni.h"
#include "chrome/browser/android/metrics/uma_session_stats.h"
#include "components/metrics/persistent_system_profile.h"
#include "components/variations/active_field_trials.h"
#include "third_party/metrics_proto/system_profile.pb.h"

// Verifies that the memory-mapped file for persistent histograms data exists
// and contains a valid SystemProfile.
jboolean
JNI_ServicificationBackgroundService_TestPersistentHistogramsOnDiskSystemProfile(
    JNIEnv* env) {
  base::GlobalHistogramAllocator* allocator =
      base::GlobalHistogramAllocator::Get();
  if (!allocator) {
    LOG(ERROR) << "The GlobalHistogramAllocator is null!";
    return false;
  }

  const base::FilePath& persistent_file_path =
      allocator->GetPersistentLocation();
  if (persistent_file_path.empty()) {
    LOG(ERROR) << "The path of the persistent file is empty!";
    return false;
  }

  base::File file(persistent_file_path,
                  base::File::FLAG_OPEN | base::File::FLAG_READ);
  if (!file.IsValid()) {
    LOG(ERROR) << "The persistent file isn't valid!";
    return false;
  }

  std::unique_ptr<base::MemoryMappedFile> mapped(new base::MemoryMappedFile());
  if (!mapped->Initialize(std::move(file), base::MemoryMappedFile::READ_ONLY)) {
    LOG(ERROR) << "Fails to Initialize the memory mapped file!";
    return false;
  }

  if (!base::FilePersistentMemoryAllocator::IsFileAcceptable(
          *mapped, true /* read_only */)) {
    LOG(ERROR) << "The memory mapped file isn't acceptable!";
    return false;
  }

  // Map the file and validate it.
  std::unique_ptr<base::FilePersistentMemoryAllocator> memory_allocator =
      std::make_unique<base::FilePersistentMemoryAllocator>(
          std::move(mapped), 0, 0, std::string_view(),
          base::FilePersistentMemoryAllocator::kReadOnly);
  if (memory_allocator->GetMemoryState() ==
      base::PersistentMemoryAllocator::MEMORY_DELETED) {
    LOG(ERROR) << "The memory allocator state shouldn't be MEMORY_DELETED!";
    return false;
  }

  if (memory_allocator->IsCorrupt()) {
    LOG(ERROR) << "The memory allocator is corrupt!";
    return false;
  }

  if (!metrics::PersistentSystemProfile::HasSystemProfile(*memory_allocator)) {
    LOG(ERROR) << "There isn't a System Profile!";
    return false;
  }

  metrics::SystemProfileProto system_profile_proto;
  if (!metrics::PersistentSystemProfile::GetSystemProfile(
          *memory_allocator, &system_profile_proto)) {
    LOG(ERROR) << "Failed to get the System Profile!";
    return false;
  }

  if (!system_profile_proto.has_os()) {
    LOG(ERROR) << "The os info isn't set!";
    return false;
  }

  const metrics::SystemProfileProto_OS& os = system_profile_proto.os();
  if (!os.has_version()) {
    LOG(ERROR) << "The os version isn't set!";
    return false;
  }

  if (base::SysInfo::OperatingSystemVersion().compare(os.version()) != 0) {
    LOG(ERROR) << "The os version doesn't match!";
    return false;
  }

  std::vector<variations::ActiveGroupId> field_trial_ids;
  // The persistent histograms on disk do not include low anonymity trials (as
  // we do not include those in metrics reports).
  variations::GetFieldTrialActiveGroupIds("", &field_trial_ids);
  int expected_size = static_cast<int>(field_trial_ids.size());

  // The active field trial "Foo" is guaranteed in the list.
  if (expected_size <= 0) {
    LOG(ERROR) << "Expect at least one active field trial!";
    return false;
  }

  if (system_profile_proto.field_trial_size() != expected_size) {
    LOG(ERROR) << "The size of field trials recorded in the System Profile"
               << " doesn't match the size of active field trials!";
    return false;
  }

  return true;
}

// Returns whether a background session has started.
jboolean JNI_ServicificationBackgroundService_IsBackgroundSessionStart(
    JNIEnv* env) {
  return UmaSessionStats::IsBackgroundSessionStartForTesting();
}