holte | 1bf273c | 2017-02-23 00:22:28 | [diff] [blame] | 1 | // 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 Cheng | 1b96a69 | 2019-03-28 21:29:02 | [diff] [blame] | 8 | #include "base/hash/sha1.h" |
holte | 1bf273c | 2017-02-23 00:22:28 | [diff] [blame] | 9 | #include "base/strings/string_number_conversions.h" |
| 10 | #include "components/metrics/metrics_pref_names.h" |
holte | 1bf273c | 2017-02-23 00:22:28 | [diff] [blame] | 11 | #include "components/prefs/pref_registry_simple.h" |
| 12 | #include "components/prefs/pref_service.h" |
Steven Holte | f9d5ed6 | 2017-10-21 02:02:30 | [diff] [blame] | 13 | #include "third_party/metrics_proto/system_profile.pb.h" |
holte | 1bf273c | 2017-02-23 00:22:28 | [diff] [blame] | 14 | |
| 15 | namespace metrics { |
| 16 | |
| 17 | namespace { |
| 18 | |
| 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()); |
| 23 | } |
| 24 | |
| 25 | } // namespace |
| 26 | |
| 27 | EnvironmentRecorder::EnvironmentRecorder(PrefService* local_state) |
| 28 | : local_state_(local_state) {} |
| 29 | |
| 30 | EnvironmentRecorder::~EnvironmentRecorder() = default; |
| 31 | |
| 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)); |
| 44 | } |
| 45 | |
| 46 | return serialized_system_profile; |
| 47 | } |
| 48 | |
| 49 | bool 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 | |
| 67 | void EnvironmentRecorder::ClearEnvironmentFromPrefs() { |
| 68 | local_state_->ClearPref(prefs::kStabilitySavedSystemProfile); |
| 69 | local_state_->ClearPref(prefs::kStabilitySavedSystemProfileHash); |
| 70 | } |
| 71 | |
| 72 | int64_t EnvironmentRecorder::GetLastBuildtime() { |
| 73 | return local_state_->GetInt64(prefs::kStabilityStatsBuildTime); |
| 74 | } |
| 75 | |
| 76 | std::string EnvironmentRecorder::GetLastVersion() { |
| 77 | return local_state_->GetString(prefs::kStabilityStatsVersion); |
| 78 | } |
| 79 | |
| 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); |
| 84 | } |
| 85 | |
| 86 | // static |
| 87 | void 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 |