1 // Copyright 2017 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "components/metrics/environment_recorder.h"
7 #include "base/base64.h"
8 #include "base/hash/sha1.h"
9 #include "base/strings/string_number_conversions.h"
10 #include "components/metrics/metrics_pref_names.h"
11 #include "components/prefs/pref_registry_simple.h"
12 #include "components/prefs/pref_service.h"
13 #include "third_party/metrics_proto/system_profile.pb.h"
19 // Computes a SHA-1 hash of |data| and returns it as a hex string.
20 std::string ComputeSHA1(const std::string& data) {
21 const std::string sha1 = base::SHA1HashString(data);
22 return base::HexEncode(sha1.data(), sha1.size());
27 EnvironmentRecorder::EnvironmentRecorder(PrefService* local_state)
28 : local_state_(local_state) {}
30 EnvironmentRecorder::~EnvironmentRecorder() = default;
32 std::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));
46 return serialized_system_profile;
49 bool EnvironmentRecorder::LoadEnvironmentFromPrefs(
50 SystemProfileProto* system_profile) {
51 DCHECK(system_profile);
53 const std::string base64_system_profile =
54 local_state_->GetString(prefs::kStabilitySavedSystemProfile);
55 if (base64_system_profile.empty())
57 const std::string system_profile_hash =
58 local_state_->GetString(prefs::kStabilitySavedSystemProfileHash);
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);
67 void EnvironmentRecorder::ClearEnvironmentFromPrefs() {
68 local_state_->ClearPref(prefs::kStabilitySavedSystemProfile);
69 local_state_->ClearPref(prefs::kStabilitySavedSystemProfileHash);
72 int64_t EnvironmentRecorder::GetLastBuildtime() {
73 return local_state_->GetInt64(prefs::kStabilityStatsBuildTime);
76 std::string EnvironmentRecorder::GetLastVersion() {
77 return local_state_->GetString(prefs::kStabilityStatsVersion);
80 void EnvironmentRecorder::SetBuildtimeAndVersion(int64_t buildtime,
81 const std::string& version) {
82 local_state_->SetInt64(prefs::kStabilityStatsBuildTime, buildtime);
83 local_state_->SetString(prefs::kStabilityStatsVersion, version);
87 void EnvironmentRecorder::RegisterPrefs(PrefRegistrySimple* registry) {
88 registry->RegisterStringPref(prefs::kStabilitySavedSystemProfile,
90 registry->RegisterStringPref(prefs::kStabilitySavedSystemProfileHash,
92 registry->RegisterStringPref(prefs::kStabilityStatsVersion, std::string());
93 registry->RegisterInt64Pref(prefs::kStabilityStatsBuildTime, 0);
96 } // namespace metrics