Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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" | |
| 8 #include "base/sha1.h" | |
| 9 #include "base/strings/string_number_conversions.h" | |
| 10 #include "components/metrics/proto/system_profile.pb.h" | |
| 11 #include "components/metrics/stability_pref_names.h" | |
| 12 #include "components/prefs/pref_registry_simple.h" | |
| 13 #include "components/prefs/pref_service.h" | |
| 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 | |
|
Alexei Svitkine (slow)
2017/02/17 16:02:22
Nit: Remove blank line.
Steven Holte
2017/02/17 20:21:32
Done.
| |
| 62 return base::Base64Decode(base64_system_profile, | |
| 63 &serialized_system_profile) && | |
| 64 ComputeSHA1(serialized_system_profile) == system_profile_hash && | |
| 65 system_profile->ParseFromString(serialized_system_profile); | |
| 66 } | |
| 67 | |
| 68 void EnvironmentRecorder::ClearEnvironmentFromPrefs() { | |
| 69 local_state_->ClearPref(prefs::kStabilitySavedSystemProfile); | |
| 70 local_state_->ClearPref(prefs::kStabilitySavedSystemProfileHash); | |
| 71 } | |
| 72 | |
| 73 int64_t EnvironmentRecorder::GetLastBuildtime() { | |
| 74 return local_state_->GetInt64(prefs::kStabilityStatsBuildTime); | |
| 75 } | |
| 76 | |
| 77 std::string EnvironmentRecorder::GetLastVersion() { | |
| 78 return local_state_->GetString(prefs::kStabilityStatsVersion); | |
| 79 } | |
| 80 | |
| 81 void EnvironmentRecorder::SetBuildtimeAndVersion(int64_t buildtime, | |
| 82 const std::string& version) { | |
| 83 local_state_->SetInt64(prefs::kStabilityStatsBuildTime, buildtime); | |
| 84 local_state_->SetString(prefs::kStabilityStatsVersion, version); | |
| 85 } | |
| 86 | |
| 87 // static | |
| 88 void EnvironmentRecorder::RegisterPrefs(PrefRegistrySimple* registry) { | |
| 89 registry->RegisterStringPref(prefs::kStabilitySavedSystemProfile, | |
| 90 std::string()); | |
| 91 registry->RegisterStringPref(prefs::kStabilitySavedSystemProfileHash, | |
| 92 std::string()); | |
| 93 registry->RegisterStringPref(prefs::kStabilityStatsVersion, std::string()); | |
| 94 registry->RegisterInt64Pref(prefs::kStabilityStatsBuildTime, 0); | |
| 95 } | |
| 96 | |
| 97 } // namespace metrics | |
| OLD | NEW |