Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/metrics/persisted_logs.h" | 5 #include "components/metrics/persisted_logs.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/base64.h" | 9 #include "base/base64.h" |
| 10 #include "base/md5.h" | 10 #include "base/md5.h" |
| 11 #include "base/metrics/histogram_macros.h" | 11 #include "base/metrics/histogram_macros.h" |
| 12 #include "base/sha1.h" | 12 #include "base/sha1.h" |
| 13 #include "base/timer/elapsed_timer.h" | 13 #include "base/timer/elapsed_timer.h" |
| 14 #include "components/prefs/pref_service.h" | 14 #include "components/prefs/pref_service.h" |
| 15 #include "components/prefs/scoped_user_pref_update.h" | 15 #include "components/prefs/scoped_user_pref_update.h" |
| 16 #include "third_party/zlib/google/compression_utils.h" | 16 #include "third_party/zlib/google/compression_utils.h" |
| 17 | 17 |
| 18 namespace metrics { | 18 namespace metrics { |
| 19 | 19 |
| 20 namespace { | 20 namespace { |
|
Alexei Svitkine (slow)
2016/09/28 18:50:30
Nit: add line after
gayane -on leave until 09-2017
2016/09/28 20:50:50
Done.
| |
| 21 const char kLogHashKey[] = "hash"; | |
| 22 const char kLogTimestampKey[] = "timestamp"; | |
| 23 const char kLogDataKey[] = "data"; | |
| 21 | 24 |
| 22 PersistedLogs::LogReadStatus MakeRecallStatusHistogram( | 25 PersistedLogs::LogReadStatus MakeRecallStatusHistogram( |
| 23 PersistedLogs::LogReadStatus status) { | 26 PersistedLogs::LogReadStatus status) { |
| 24 UMA_HISTOGRAM_ENUMERATION("PrefService.PersistentLogRecallProtobufs", | 27 UMA_HISTOGRAM_ENUMERATION("PrefService.PersistentLogRecallProtobufs", |
| 25 status, PersistedLogs::END_RECALL_STATUS); | 28 status, PersistedLogs::END_RECALL_STATUS); |
| 26 return status; | 29 return status; |
| 27 } | 30 } |
| 28 | 31 |
| 29 // Reads the value at |index| from |list_value| as a string and Base64-decodes | 32 // Reads the value at |index| from |list_value| as a string and Base64-decodes |
| 30 // it into |result|. Returns true on success. | 33 // it into |result|. Returns true on success. |
| 31 bool ReadBase64String(const base::ListValue& list_value, | 34 bool ReadBase64String(const base::ListValue& list_value, |
| 32 size_t index, | 35 size_t index, |
| 33 std::string* result) { | 36 std::string* result) { |
| 34 std::string base64_result; | 37 std::string base64_result; |
| 35 if (!list_value.GetString(index, &base64_result)) | 38 if (!list_value.GetString(index, &base64_result)) |
| 36 return false; | 39 return false; |
| 37 return base::Base64Decode(base64_result, result); | 40 return base::Base64Decode(base64_result, result); |
| 38 } | 41 } |
| 39 | 42 |
| 40 // Base64-encodes |str| and appends the result to |list_value|. | 43 std::string EncodeToBase64(const std::string& to_convert) { |
| 41 void AppendBase64String(const std::string& str, base::ListValue* list_value) { | 44 std::string base64_result; |
| 42 std::string base64_str; | 45 base::Base64Encode(to_convert, &base64_result); |
| 43 base::Base64Encode(str, &base64_str); | 46 return base64_result; |
| 44 list_value->AppendString(base64_str); | 47 } |
| 48 | |
| 49 std::string DecodeFromBase64(const std::string& to_convert) { | |
| 50 std::string result; | |
| 51 base::Base64Decode(to_convert, &result); | |
| 52 return result; | |
| 45 } | 53 } |
| 46 | 54 |
| 47 } // namespace | 55 } // namespace |
| 48 | 56 |
| 49 void PersistedLogs::LogHashPair::Init(const std::string& log_data) { | 57 void PersistedLogs::LogInfo::Init(const std::string& log_data) { |
| 50 DCHECK(!log_data.empty()); | 58 DCHECK(!log_data.empty()); |
| 51 | 59 |
| 52 if (!compression::GzipCompress(log_data, &compressed_log_data)) { | 60 if (!compression::GzipCompress(log_data, &compressed_log_data)) { |
| 53 NOTREACHED(); | 61 NOTREACHED(); |
| 54 return; | 62 return; |
| 55 } | 63 } |
| 56 | 64 |
| 57 UMA_HISTOGRAM_PERCENTAGE( | 65 UMA_HISTOGRAM_PERCENTAGE( |
| 58 "UMA.ProtoCompressionRatio", | 66 "UMA.ProtoCompressionRatio", |
| 59 static_cast<int>(100 * compressed_log_data.size() / log_data.size())); | 67 static_cast<int>(100 * compressed_log_data.size() / log_data.size())); |
| 60 | 68 |
| 61 hash = base::SHA1HashString(log_data); | 69 hash = base::SHA1HashString(log_data); |
| 70 timestamp = (base::Time::Now() - base::Time::UnixEpoch()).InSeconds(); | |
|
Alexei Svitkine (slow)
2016/09/28 18:50:30
Isn't return value int64_t but your field is an in
gayane -on leave until 09-2017
2016/09/28 20:50:49
hmm right. I had a look and dictionaries have only
Alexei Svitkine (slow)
2016/09/29 16:46:40
That's a 100 years since unix epoch? So 56 years a
| |
| 62 } | 71 } |
| 63 | 72 |
| 64 PersistedLogs::PersistedLogs(PrefService* local_state, | 73 PersistedLogs::PersistedLogs(PrefService* local_state, |
| 65 const char* pref_name, | 74 const char* pref_name, |
| 66 size_t min_log_count, | 75 size_t min_log_count, |
| 67 size_t min_log_bytes, | 76 size_t min_log_bytes, |
| 68 size_t max_log_size) | 77 size_t max_log_size) |
| 69 : local_state_(local_state), | 78 : local_state_(local_state), |
| 70 pref_name_(pref_name), | 79 pref_name_(pref_name), |
| 71 min_log_count_(min_log_count), | 80 min_log_count_(min_log_count), |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 82 void PersistedLogs::SerializeLogs() const { | 91 void PersistedLogs::SerializeLogs() const { |
| 83 ListPrefUpdate update(local_state_, pref_name_); | 92 ListPrefUpdate update(local_state_, pref_name_); |
| 84 WriteLogsToPrefList(update.Get()); | 93 WriteLogsToPrefList(update.Get()); |
| 85 } | 94 } |
| 86 | 95 |
| 87 PersistedLogs::LogReadStatus PersistedLogs::DeserializeLogs() { | 96 PersistedLogs::LogReadStatus PersistedLogs::DeserializeLogs() { |
| 88 return ReadLogsFromPrefList(*local_state_->GetList(pref_name_)); | 97 return ReadLogsFromPrefList(*local_state_->GetList(pref_name_)); |
| 89 } | 98 } |
| 90 | 99 |
| 91 void PersistedLogs::StoreLog(const std::string& log_data) { | 100 void PersistedLogs::StoreLog(const std::string& log_data) { |
| 92 list_.push_back(LogHashPair()); | 101 list_.push_back(LogInfo()); |
| 93 list_.back().Init(log_data); | 102 list_.back().Init(log_data); |
| 94 } | 103 } |
| 95 | 104 |
| 96 void PersistedLogs::StageLog() { | 105 void PersistedLogs::StageLog() { |
| 97 // CHECK, rather than DCHECK, because swap()ing with an empty list causes | 106 // CHECK, rather than DCHECK, because swap()ing with an empty list causes |
| 98 // hard-to-identify crashes much later. | 107 // hard-to-identify crashes much later. |
| 99 CHECK(!list_.empty()); | 108 CHECK(!list_.empty()); |
| 100 DCHECK(!has_staged_log()); | 109 DCHECK(!has_staged_log()); |
| 101 staged_log_index_ = list_.size() - 1; | 110 staged_log_index_ = list_.size() - 1; |
| 102 DCHECK(has_staged_log()); | 111 DCHECK(has_staged_log()); |
| 103 } | 112 } |
| 104 | 113 |
| 105 void PersistedLogs::DiscardStagedLog() { | 114 void PersistedLogs::DiscardStagedLog() { |
| 106 DCHECK(has_staged_log()); | 115 DCHECK(has_staged_log()); |
| 107 DCHECK_LT(static_cast<size_t>(staged_log_index_), list_.size()); | 116 DCHECK_LT(static_cast<size_t>(staged_log_index_), list_.size()); |
| 108 list_.erase(list_.begin() + staged_log_index_); | 117 list_.erase(list_.begin() + staged_log_index_); |
| 109 staged_log_index_ = -1; | 118 staged_log_index_ = -1; |
| 110 } | 119 } |
| 111 | 120 |
| 112 void PersistedLogs::WriteLogsToPrefList(base::ListValue* list_value) const { | 121 void PersistedLogs::MigrateFromOldFormat(const char* old_pref_name) { |
| 122 ReadLogsFromPrefList_OldFormat(*local_state_->GetList(old_pref_name)); | |
| 123 ListPrefUpdate update(local_state_, pref_name_); | |
| 124 WriteLogsToPrefList(update.Get()); | |
|
Alexei Svitkine (slow)
2016/09/28 18:50:30
I don't see where WriteLogsToPrefList is implement
gayane -on leave until 09-2017
2016/09/28 20:50:50
opps deleted that instead of WriteLogsToPrefList_O
| |
| 125 } | |
|
Alexei Svitkine (slow)
2016/09/28 18:50:30
I'm not sure we need an explicit migrate function.
gayane -on leave until 09-2017
2016/09/28 20:50:49
But then when we read we need to read from two dif
Alexei Svitkine (slow)
2016/09/29 16:46:40
Well, you're already reading from both prefs - jus
| |
| 126 | |
| 127 PersistedLogs::LogReadStatus PersistedLogs::ReadLogsFromPrefList( | |
| 128 const base::ListValue& list_value) { | |
| 129 if (list_value.empty()) | |
| 130 return MakeRecallStatusHistogram(LIST_EMPTY); | |
| 131 | |
| 132 const size_t log_count = list_value.GetSize(); | |
| 133 | |
| 134 DCHECK(list_.empty()); | |
| 135 list_.resize(log_count); | |
| 136 | |
| 137 for (size_t i = 0; i < log_count; ++i) { | |
| 138 const base::DictionaryValue* dict; | |
| 139 if (!list_value.GetDictionary(i, &dict) || | |
| 140 !dict->GetString(kLogDataKey, &list_[i].compressed_log_data) || | |
| 141 !dict->GetString(kLogHashKey, &list_[i].hash)) { | |
| 142 list_.clear(); | |
| 143 return MakeRecallStatusHistogram(LOG_STRING_CORRUPTION); | |
| 144 } | |
| 145 | |
| 146 list_[i].compressed_log_data = | |
| 147 DecodeFromBase64(list_[i].compressed_log_data); | |
| 148 list_[i].hash = DecodeFromBase64(list_[i].hash); | |
| 149 // Ignoring the success of this step as timestamp might not be there for | |
| 150 // older logs. | |
| 151 // NOTE: Should be added to the check with other fields once migration is | |
| 152 // over. | |
| 153 dict->GetInteger(kLogTimestampKey, &list_[i].timestamp); | |
| 154 } | |
| 155 | |
| 156 return MakeRecallStatusHistogram(RECALL_SUCCESS); | |
| 157 } | |
| 158 | |
| 159 void PersistedLogs::WriteLogsToPrefList_OldFormat( | |
|
Alexei Svitkine (slow)
2016/09/28 18:50:30
Do we need this anymore?
gayane -on leave until 09-2017
2016/09/28 20:50:49
Nope
| |
| 160 base::ListValue* list_value) const { | |
| 113 list_value->Clear(); | 161 list_value->Clear(); |
| 114 | 162 |
| 115 // Keep the most recent logs which are smaller than |max_log_size_|. | 163 // Keep the most recent logs which are smaller than |max_log_size_|. |
| 116 // We keep at least |min_log_bytes_| and |min_log_count_| of logs before | 164 // We keep at least |min_log_bytes_| and |min_log_count_| of logs before |
| 117 // discarding older logs. | 165 // discarding older logs. |
| 118 size_t start = list_.size(); | 166 size_t start = list_.size(); |
| 119 size_t saved_log_count = 0; | 167 size_t saved_log_count = 0; |
| 120 size_t bytes_used = 0; | 168 size_t bytes_used = 0; |
| 121 for (; start > 0; --start) { | 169 for (; start > 0; --start) { |
| 122 size_t log_size = list_[start - 1].compressed_log_data.length(); | 170 size_t log_size = list_[start - 1].compressed_log_data.length(); |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 140 dropped_logs_num++; | 188 dropped_logs_num++; |
| 141 continue; | 189 continue; |
| 142 } | 190 } |
| 143 AppendBase64String(list_[i].compressed_log_data, list_value); | 191 AppendBase64String(list_[i].compressed_log_data, list_value); |
| 144 AppendBase64String(list_[i].hash, list_value); | 192 AppendBase64String(list_[i].hash, list_value); |
| 145 } | 193 } |
| 146 if (dropped_logs_num > 0) | 194 if (dropped_logs_num > 0) |
| 147 UMA_HISTOGRAM_COUNTS("UMA.UnsentLogs.Dropped", dropped_logs_num); | 195 UMA_HISTOGRAM_COUNTS("UMA.UnsentLogs.Dropped", dropped_logs_num); |
| 148 } | 196 } |
| 149 | 197 |
| 150 PersistedLogs::LogReadStatus PersistedLogs::ReadLogsFromPrefList( | 198 PersistedLogs::LogReadStatus PersistedLogs::ReadLogsFromPrefList_OldFormat( |
|
Alexei Svitkine (slow)
2016/09/28 18:50:30
Nit: ReadLogsFromOldFormatPrefList
gayane -on leave until 09-2017
2016/09/28 20:50:49
Done.
| |
| 151 const base::ListValue& list_value) { | 199 const base::ListValue& list_value) { |
| 152 if (list_value.empty()) | 200 if (list_value.empty()) |
| 153 return MakeRecallStatusHistogram(LIST_EMPTY); | 201 return MakeRecallStatusHistogram(LIST_EMPTY); |
| 154 | 202 |
| 155 // For each log, there's two entries in the list (the data and the hash). | 203 // For each log, there's two entries in the list (the data and the hash). |
| 156 DCHECK_EQ(0U, list_value.GetSize() % 2); | 204 DCHECK_EQ(0U, list_value.GetSize() % 2); |
| 157 const size_t log_count = list_value.GetSize() / 2; | 205 const size_t log_count = list_value.GetSize() / 2; |
| 158 | 206 |
| 159 // Resize |list_| ahead of time, so that values can be decoded directly into | 207 // Resize |list_| ahead of time, so that values can be decoded directly into |
| 160 // the elements of the list. | 208 // the elements of the list. |
| 161 DCHECK(list_.empty()); | 209 DCHECK(list_.empty()); |
| 162 list_.resize(log_count); | 210 list_.resize(log_count); |
| 163 | 211 |
| 164 for (size_t i = 0; i < log_count; ++i) { | 212 for (size_t i = 0; i < log_count; ++i) { |
| 165 if (!ReadBase64String(list_value, i * 2, &list_[i].compressed_log_data) || | 213 if (!ReadBase64String(list_value, i * 2, &list_[i].compressed_log_data) || |
| 166 !ReadBase64String(list_value, i * 2 + 1, &list_[i].hash)) { | 214 !ReadBase64String(list_value, i * 2 + 1, &list_[i].hash)) { |
| 167 list_.clear(); | 215 list_.clear(); |
| 168 return MakeRecallStatusHistogram(LOG_STRING_CORRUPTION); | 216 return MakeRecallStatusHistogram(LOG_STRING_CORRUPTION); |
| 169 } | 217 } |
| 170 } | 218 } |
| 171 | 219 |
| 172 return MakeRecallStatusHistogram(RECALL_SUCCESS); | 220 return MakeRecallStatusHistogram(RECALL_SUCCESS); |
| 173 } | 221 } |
| 174 | 222 |
| 175 } // namespace metrics | 223 } // namespace metrics |
| OLD | NEW |