blob: 7b6d926959cd8e811c315f2843f4d8c9cf173397 [file] [log] [blame]
holte1bf273c2017-02-23 00:22:281// Copyright 2017 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 "components/metrics/environment_recorder.h"
6
7#include "base/base64.h"
Daniel Cheng1b96a692019-03-28 21:29:028#include "base/hash/sha1.h"
holte1bf273c2017-02-23 00:22:289#include "base/strings/string_number_conversions.h"
10#include "components/metrics/metrics_pref_names.h"
holte1bf273c2017-02-23 00:22:2811#include "components/prefs/pref_registry_simple.h"
12#include "components/prefs/pref_service.h"
Steven Holtef9d5ed62017-10-21 02:02:3013#include "third_party/metrics_proto/system_profile.pb.h"
holte1bf273c2017-02-23 00:22:2814
15namespace metrics {
16
17namespace {
18
19// Computes a SHA-1 hash of |data| and returns it as a hex string.
20std::string ComputeSHA1(const std::string& data) {
21 const std::string sha1 = base::SHA1HashString(data);
22 return base::HexEncode(sha1.data(), sha1.size());
23}
24
25} // namespace
26
27EnvironmentRecorder::EnvironmentRecorder(PrefService* local_state)
28 : local_state_(local_state) {}
29
30EnvironmentRecorder::~EnvironmentRecorder() = default;
31
32std::string EnvironmentRecorder::SerializeAndRecordEnvironmentToPrefs(
33 const SystemProfileProto& system_profile) {
34 std::string serialized_system_profile;
35 std::string base64_system_profile;
36 if (system_profile.SerializeToString(&serialized_system_profile)) {
37 // Persist the system profile to disk. In the event of an unclean shutdown,
38 // it will be used as part of the initial stability report.
39 base::Base64Encode(serialized_system_profile, &base64_system_profile);
40 local_state_->SetString(prefs::kStabilitySavedSystemProfile,
41 base64_system_profile);
42 local_state_->SetString(prefs::kStabilitySavedSystemProfileHash,
43 ComputeSHA1(serialized_system_profile));
44 }
45
46 return serialized_system_profile;
47}
48
49bool EnvironmentRecorder::LoadEnvironmentFromPrefs(
50 SystemProfileProto* system_profile) {
51 DCHECK(system_profile);
52
53 const std::string base64_system_profile =
54 local_state_->GetString(prefs::kStabilitySavedSystemProfile);
55 if (base64_system_profile.empty())
56 return false;
57 const std::string system_profile_hash =
58 local_state_->GetString(prefs::kStabilitySavedSystemProfileHash);
59
60 std::string serialized_system_profile;
61 return base::Base64Decode(base64_system_profile,
62 &serialized_system_profile) &&
63 ComputeSHA1(serialized_system_profile) == system_profile_hash &&
64 system_profile->ParseFromString(serialized_system_profile);
65}
66
67void EnvironmentRecorder::ClearEnvironmentFromPrefs() {
68 local_state_->ClearPref(prefs::kStabilitySavedSystemProfile);
69 local_state_->ClearPref(prefs::kStabilitySavedSystemProfileHash);
70}
71
72int64_t EnvironmentRecorder::GetLastBuildtime() {
73 return local_state_->GetInt64(prefs::kStabilityStatsBuildTime);
74}
75
76std::string EnvironmentRecorder::GetLastVersion() {
77 return local_state_->GetString(prefs::kStabilityStatsVersion);
78}
79
80void EnvironmentRecorder::SetBuildtimeAndVersion(int64_t buildtime,
81 const std::string& version) {
82 local_state_->SetInt64(prefs::kStabilityStatsBuildTime, buildtime);
83 local_state_->SetString(prefs::kStabilityStatsVersion, version);
84}
85
86// static
87void EnvironmentRecorder::RegisterPrefs(PrefRegistrySimple* registry) {
88 registry->RegisterStringPref(prefs::kStabilitySavedSystemProfile,
89 std::string());
90 registry->RegisterStringPref(prefs::kStabilitySavedSystemProfileHash,
91 std::string());
92 registry->RegisterStringPref(prefs::kStabilityStatsVersion, std::string());
93 registry->RegisterInt64Pref(prefs::kStabilityStatsBuildTime, 0);
94}
95
96} // namespace metrics