blob: 6aaacce4025806b3806827f581b50401e6ab6a32 [file] [log] [blame]
Xi Han59bb8c22019-05-23 21:05:501// Copyright 2019 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include <sstream>
6
7#include "base/android/jni_android.h"
8#include "base/files/file.h"
9#include "base/files/file_path.h"
10#include "base/files/file_util.h"
11#include "base/files/memory_mapped_file.h"
Xi Han095f39342019-08-08 20:00:5412#include "base/metrics/field_trial.h"
Xi Han59bb8c22019-05-23 21:05:5013#include "base/metrics/persistent_histogram_allocator.h"
14#include "base/system/sys_info.h"
Andrew Grieve4a42c22e2019-06-24 16:14:2915#include "chrome/android/test_support_jni_headers/ServicificationBackgroundService_jni.h"
Xi Hanb06d11b2020-04-02 14:37:1616#include "chrome/browser/android/metrics/uma_session_stats.h"
Xi Han59bb8c22019-05-23 21:05:5017#include "components/metrics/persistent_system_profile.h"
Xi Han095f39342019-08-08 20:00:5418#include "components/variations/active_field_trials.h"
Xi Han59bb8c22019-05-23 21:05:5019#include "third_party/metrics_proto/system_profile.pb.h"
20
21// Verifies that the memory-mapped file for persistent histograms data exists
22// and contains a valid SystemProfile.
23jboolean
24JNI_ServicificationBackgroundService_TestPersistentHistogramsOnDiskSystemProfile(
25 JNIEnv* env) {
26 base::GlobalHistogramAllocator* allocator =
27 base::GlobalHistogramAllocator::Get();
Xi Hanb2371fe2020-07-15 20:05:5928 if (!allocator) {
29 LOG(ERROR) << "The GlobalHistogramAllocator is null!";
Xi Han59bb8c22019-05-23 21:05:5030 return false;
Xi Hanb2371fe2020-07-15 20:05:5931 }
Xi Han59bb8c22019-05-23 21:05:5032
33 const base::FilePath& persistent_file_path =
34 allocator->GetPersistentLocation();
Xi Hanb2371fe2020-07-15 20:05:5935 if (persistent_file_path.empty()) {
36 LOG(ERROR) << "The path of the persistent file is empty!";
Xi Han59bb8c22019-05-23 21:05:5037 return false;
Xi Hanb2371fe2020-07-15 20:05:5938 }
Xi Han59bb8c22019-05-23 21:05:5039
40 base::File file(persistent_file_path,
41 base::File::FLAG_OPEN | base::File::FLAG_READ);
Xi Hanb2371fe2020-07-15 20:05:5942 if (!file.IsValid()) {
43 LOG(ERROR) << "The persistent file isn't valid!";
Xi Han59bb8c22019-05-23 21:05:5044 return false;
Xi Hanb2371fe2020-07-15 20:05:5945 }
Xi Han59bb8c22019-05-23 21:05:5046
47 std::unique_ptr<base::MemoryMappedFile> mapped(new base::MemoryMappedFile());
Xi Hanb2371fe2020-07-15 20:05:5948 if (!mapped->Initialize(std::move(file), base::MemoryMappedFile::READ_ONLY)) {
49 LOG(ERROR) << "Fails to Initialize the memory mapped file!";
Xi Han59bb8c22019-05-23 21:05:5050 return false;
Xi Hanb2371fe2020-07-15 20:05:5951 }
Xi Han59bb8c22019-05-23 21:05:5052
53 if (!base::FilePersistentMemoryAllocator::IsFileAcceptable(
54 *mapped, true /* read_only */)) {
Xi Hanb2371fe2020-07-15 20:05:5955 LOG(ERROR) << "The memory mapped file isn't acceptable!";
Xi Han59bb8c22019-05-23 21:05:5056 return false;
57 }
58
59 // Map the file and validate it.
60 std::unique_ptr<base::FilePersistentMemoryAllocator> memory_allocator =
61 std::make_unique<base::FilePersistentMemoryAllocator>(
62 std::move(mapped), 0, 0, base::StringPiece(), /* read_only */ true);
63 if (memory_allocator->GetMemoryState() ==
64 base::PersistentMemoryAllocator::MEMORY_DELETED) {
Xi Hana0fb7712019-08-13 17:19:3565 LOG(ERROR) << "The memory allocator state shouldn't be MEMORY_DELETED!";
Xi Han59bb8c22019-05-23 21:05:5066 return false;
67 }
Xi Han59bb8c22019-05-23 21:05:5068
Xi Hana0fb7712019-08-13 17:19:3569 if (memory_allocator->IsCorrupt()) {
70 LOG(ERROR) << "The memory allocator is corrupt!";
Xi Han59bb8c22019-05-23 21:05:5071 return false;
Xi Hana0fb7712019-08-13 17:19:3572 }
73
74 if (!metrics::PersistentSystemProfile::HasSystemProfile(*memory_allocator)) {
75 LOG(ERROR) << "There isn't a System Profile!";
76 return false;
77 }
Xi Han59bb8c22019-05-23 21:05:5078
79 metrics::SystemProfileProto system_profile_proto;
80 if (!metrics::PersistentSystemProfile::GetSystemProfile(
Xi Hana0fb7712019-08-13 17:19:3581 *memory_allocator, &system_profile_proto)) {
82 LOG(ERROR) << "Failed to get the System Profile!";
Xi Han59bb8c22019-05-23 21:05:5083 return false;
Xi Hana0fb7712019-08-13 17:19:3584 }
Xi Han59bb8c22019-05-23 21:05:5085
Xi Hana0fb7712019-08-13 17:19:3586 if (!system_profile_proto.has_os()) {
87 LOG(ERROR) << "The os info isn't set!";
Xi Han59bb8c22019-05-23 21:05:5088 return false;
Xi Hana0fb7712019-08-13 17:19:3589 }
Xi Han59bb8c22019-05-23 21:05:5090
91 const metrics::SystemProfileProto_OS& os = system_profile_proto.os();
Xi Hana0fb7712019-08-13 17:19:3592 if (!os.has_version()) {
93 LOG(ERROR) << "The os version isn't set!";
Xi Han59bb8c22019-05-23 21:05:5094 return false;
Xi Hana0fb7712019-08-13 17:19:3595 }
Xi Han59bb8c22019-05-23 21:05:5096
Xi Hana0fb7712019-08-13 17:19:3597 if (base::SysInfo::OperatingSystemVersion().compare(os.version()) != 0) {
98 LOG(ERROR) << "The os version doesn't match!";
Xi Han095f39342019-08-08 20:00:5499 return false;
Xi Hana0fb7712019-08-13 17:19:35100 }
Xi Han095f39342019-08-08 20:00:54101
102 std::vector<variations::ActiveGroupId> field_trial_ids;
103 variations::GetFieldTrialActiveGroupIds("", &field_trial_ids);
Xi Hana0fb7712019-08-13 17:19:35104 int expected_size = static_cast<int>(field_trial_ids.size());
Xi Han095f39342019-08-08 20:00:54105
Xi Hana0fb7712019-08-13 17:19:35106 // The active field trial "Foo" is guaranteed in the list.
107 if (expected_size <= 0) {
108 LOG(ERROR) << "Expect at least one active field trial!";
Xi Han095f39342019-08-08 20:00:54109 return false;
Xi Hana0fb7712019-08-13 17:19:35110 }
Xi Han095f39342019-08-08 20:00:54111
Xi Hana0fb7712019-08-13 17:19:35112 if (system_profile_proto.field_trial_size() != expected_size) {
113 LOG(ERROR) << "The size of field trials recorded in the System Profile"
114 << " doesn't match the size of active field trials!";
115 return false;
116 }
117
118 return true;
Xi Han59bb8c22019-05-23 21:05:50119}
Xi Hanb06d11b2020-04-02 14:37:16120
121// Returns whether a background session has started.
122jboolean JNI_ServicificationBackgroundService_IsBackgroundSessionStart(
123 JNIEnv* env) {
124 return UmaSessionStats::IsBackgroundSessionStartForTesting();
125}