• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include "components/metrics/environment_recorder.h"
6 
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"
14 
15 namespace metrics {
16 
17 namespace {
18 
19 // Computes a SHA-1 hash of |data| and returns it as a hex string.
ComputeSHA1(const std::string & data)20 std::string ComputeSHA1(const std::string& data) {
21   return base::HexEncode(base::SHA1Hash(base::as_byte_span(data)));
22 }
23 
24 }  // namespace
25 
EnvironmentRecorder(PrefService * local_state)26 EnvironmentRecorder::EnvironmentRecorder(PrefService* local_state)
27     : local_state_(local_state) {}
28 
29 EnvironmentRecorder::~EnvironmentRecorder() = default;
30 
SerializeAndRecordEnvironmentToPrefs(const SystemProfileProto & system_profile)31 std::string EnvironmentRecorder::SerializeAndRecordEnvironmentToPrefs(
32     const SystemProfileProto& system_profile) {
33   std::string serialized_system_profile;
34   if (system_profile.SerializeToString(&serialized_system_profile)) {
35     // Persist the system profile to disk. In the event of an unclean shutdown,
36     // it will be used as part of the initial stability report.
37     const std::string base64_system_profile =
38         base::Base64Encode(serialized_system_profile);
39     local_state_->SetString(prefs::kStabilitySavedSystemProfile,
40                             base64_system_profile);
41     local_state_->SetString(prefs::kStabilitySavedSystemProfileHash,
42                             ComputeSHA1(serialized_system_profile));
43   }
44 
45   return serialized_system_profile;
46 }
47 
LoadEnvironmentFromPrefs(SystemProfileProto * system_profile)48 bool EnvironmentRecorder::LoadEnvironmentFromPrefs(
49     SystemProfileProto* system_profile) {
50   DCHECK(system_profile);
51 
52   const std::string base64_system_profile =
53       local_state_->GetString(prefs::kStabilitySavedSystemProfile);
54   if (base64_system_profile.empty())
55     return false;
56   const std::string system_profile_hash =
57       local_state_->GetString(prefs::kStabilitySavedSystemProfileHash);
58 
59   std::string serialized_system_profile;
60   return base::Base64Decode(base64_system_profile,
61                             &serialized_system_profile) &&
62          ComputeSHA1(serialized_system_profile) == system_profile_hash &&
63          system_profile->ParseFromString(serialized_system_profile);
64 }
65 
ClearEnvironmentFromPrefs()66 void EnvironmentRecorder::ClearEnvironmentFromPrefs() {
67   local_state_->ClearPref(prefs::kStabilitySavedSystemProfile);
68   local_state_->ClearPref(prefs::kStabilitySavedSystemProfileHash);
69 }
70 
GetLastBuildtime()71 int64_t EnvironmentRecorder::GetLastBuildtime() {
72   return local_state_->GetInt64(prefs::kStabilityStatsBuildTime);
73 }
74 
GetLastVersion()75 std::string EnvironmentRecorder::GetLastVersion() {
76   return local_state_->GetString(prefs::kStabilityStatsVersion);
77 }
78 
SetBuildtimeAndVersion(int64_t buildtime,const std::string & version)79 void EnvironmentRecorder::SetBuildtimeAndVersion(int64_t buildtime,
80                                                  const std::string& version) {
81   local_state_->SetInt64(prefs::kStabilityStatsBuildTime, buildtime);
82   local_state_->SetString(prefs::kStabilityStatsVersion, version);
83 }
84 
85 // static
RegisterPrefs(PrefRegistrySimple * registry)86 void EnvironmentRecorder::RegisterPrefs(PrefRegistrySimple* registry) {
87   registry->RegisterStringPref(prefs::kStabilitySavedSystemProfile,
88                                std::string());
89   registry->RegisterStringPref(prefs::kStabilitySavedSystemProfileHash,
90                                std::string());
91   registry->RegisterStringPref(prefs::kStabilityStatsVersion, std::string());
92   registry->RegisterInt64Pref(prefs::kStabilityStatsBuildTime, 0);
93 }
94 
95 }  // namespace metrics
96