Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(430)

Side by Side Diff: components/metrics/persisted_logs.cc

Issue 2610653003: Remove old-format PersistedLogs support (Closed)
Patch Set: Remove old-format PersistedLogs support Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « components/metrics/persisted_logs.h ('k') | components/metrics/persisted_logs_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 <memory> 7 #include <memory>
8 #include <string> 8 #include <string>
9 #include <utility> 9 #include <utility>
10 10
11 #include "base/base64.h" 11 #include "base/base64.h"
12 #include "base/md5.h" 12 #include "base/md5.h"
13 #include "base/metrics/histogram_macros.h" 13 #include "base/metrics/histogram_macros.h"
14 #include "base/sha1.h" 14 #include "base/sha1.h"
15 #include "base/strings/string_number_conversions.h" 15 #include "base/strings/string_number_conversions.h"
16 #include "base/timer/elapsed_timer.h" 16 #include "base/timer/elapsed_timer.h"
17 #include "components/metrics/persisted_logs_metrics.h" 17 #include "components/metrics/persisted_logs_metrics.h"
18 #include "components/prefs/pref_service.h" 18 #include "components/prefs/pref_service.h"
19 #include "components/prefs/scoped_user_pref_update.h" 19 #include "components/prefs/scoped_user_pref_update.h"
20 #include "third_party/zlib/google/compression_utils.h" 20 #include "third_party/zlib/google/compression_utils.h"
21 21
22 namespace metrics { 22 namespace metrics {
23 23
24 namespace { 24 namespace {
25 25
26 const char kLogHashKey[] = "hash"; 26 const char kLogHashKey[] = "hash";
27 const char kLogTimestampKey[] = "timestamp"; 27 const char kLogTimestampKey[] = "timestamp";
28 const char kLogDataKey[] = "data"; 28 const char kLogDataKey[] = "data";
29 29
30 // Reads the value at |index| from |list_value| as a string and Base64-decodes
31 // it into |result|. Returns true on success.
32 bool ReadBase64String(const base::ListValue& list_value,
33 size_t index,
34 std::string* result) {
35 std::string base64_result;
36 if (!list_value.GetString(index, &base64_result))
37 return false;
38 return base::Base64Decode(base64_result, result);
39 }
40
41 std::string EncodeToBase64(const std::string& to_convert) { 30 std::string EncodeToBase64(const std::string& to_convert) {
42 std::string base64_result; 31 std::string base64_result;
43 base::Base64Encode(to_convert, &base64_result); 32 base::Base64Encode(to_convert, &base64_result);
44 return base64_result; 33 return base64_result;
45 } 34 }
46 35
47 std::string DecodeFromBase64(const std::string& to_convert) { 36 std::string DecodeFromBase64(const std::string& to_convert) {
48 std::string result; 37 std::string result;
49 base::Base64Decode(to_convert, &result); 38 base::Base64Decode(to_convert, &result);
50 return result; 39 return result;
(...skipping 13 matching lines...) Expand all
64 53
65 metrics->RecordCompressionRatio(compressed_log_data.size(), log_data.size()); 54 metrics->RecordCompressionRatio(compressed_log_data.size(), log_data.size());
66 55
67 hash = base::SHA1HashString(log_data); 56 hash = base::SHA1HashString(log_data);
68 timestamp = log_timestamp; 57 timestamp = log_timestamp;
69 } 58 }
70 59
71 PersistedLogs::PersistedLogs(std::unique_ptr<PersistedLogsMetrics> metrics, 60 PersistedLogs::PersistedLogs(std::unique_ptr<PersistedLogsMetrics> metrics,
72 PrefService* local_state, 61 PrefService* local_state,
73 const char* pref_name, 62 const char* pref_name,
74 const char* outdated_pref_name,
75 size_t min_log_count, 63 size_t min_log_count,
76 size_t min_log_bytes, 64 size_t min_log_bytes,
77 size_t max_log_size) 65 size_t max_log_size)
78 : metrics_(std::move(metrics)), 66 : metrics_(std::move(metrics)),
79 local_state_(local_state), 67 local_state_(local_state),
80 pref_name_(pref_name), 68 pref_name_(pref_name),
81 outdated_pref_name_(outdated_pref_name),
82 min_log_count_(min_log_count), 69 min_log_count_(min_log_count),
83 min_log_bytes_(min_log_bytes), 70 min_log_bytes_(min_log_bytes),
84 max_log_size_(max_log_size != 0 ? max_log_size : static_cast<size_t>(-1)), 71 max_log_size_(max_log_size != 0 ? max_log_size : static_cast<size_t>(-1)),
85 staged_log_index_(-1) { 72 staged_log_index_(-1) {
86 DCHECK(local_state_); 73 DCHECK(local_state_);
87 // One of the limit arguments must be non-zero. 74 // One of the limit arguments must be non-zero.
88 DCHECK(min_log_count_ > 0 || min_log_bytes_ > 0); 75 DCHECK(min_log_count_ > 0 || min_log_bytes_ > 0);
89 } 76 }
90 77
91 PersistedLogs::~PersistedLogs() {} 78 PersistedLogs::~PersistedLogs() {}
92 79
93 void PersistedLogs::SerializeLogs() const { 80 void PersistedLogs::SerializeLogs() const {
94 ListPrefUpdate update(local_state_, pref_name_); 81 ListPrefUpdate update(local_state_, pref_name_);
95 WriteLogsToPrefList(update.Get()); 82 WriteLogsToPrefList(update.Get());
96
97 // After writing all the logs to the new pref remove old outdated pref.
98 // TODO(gayane): Remove when all users are migrated. crbug.com/649440
99 if (local_state_->HasPrefPath(outdated_pref_name_))
100 local_state_->ClearPref(outdated_pref_name_);
101 } 83 }
102 84
103 PersistedLogs::LogReadStatus PersistedLogs::DeserializeLogs() { 85 PersistedLogs::LogReadStatus PersistedLogs::DeserializeLogs() {
104 // TODO(gayane): Remove the code for reading logs from outdated pref when all
105 // users are migrated. crbug.com/649440
106 if (local_state_->HasPrefPath(outdated_pref_name_)) {
107 return ReadLogsFromOldFormatPrefList(
108 *local_state_->GetList(outdated_pref_name_));
109 }
110 return ReadLogsFromPrefList(*local_state_->GetList(pref_name_)); 86 return ReadLogsFromPrefList(*local_state_->GetList(pref_name_));
111 } 87 }
112 88
113 void PersistedLogs::StoreLog(const std::string& log_data) { 89 void PersistedLogs::StoreLog(const std::string& log_data) {
114 list_.push_back(LogInfo()); 90 list_.push_back(LogInfo());
115 list_.back().Init(metrics_.get(), 91 list_.back().Init(metrics_.get(),
116 log_data, 92 log_data,
117 base::Int64ToString(base::Time::Now().ToTimeT())); 93 base::Int64ToString(base::Time::Now().ToTimeT()));
118 } 94 }
119 95
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
200 dict_value->SetString(kLogHashKey, EncodeToBase64(list_[i].hash)); 176 dict_value->SetString(kLogHashKey, EncodeToBase64(list_[i].hash));
201 dict_value->SetString(kLogDataKey, 177 dict_value->SetString(kLogDataKey,
202 EncodeToBase64(list_[i].compressed_log_data)); 178 EncodeToBase64(list_[i].compressed_log_data));
203 dict_value->SetString(kLogTimestampKey, list_[i].timestamp); 179 dict_value->SetString(kLogTimestampKey, list_[i].timestamp);
204 list_value->Append(std::move(dict_value)); 180 list_value->Append(std::move(dict_value));
205 } 181 }
206 if (dropped_logs_num > 0) 182 if (dropped_logs_num > 0)
207 metrics_->RecordDroppedLogsNum(dropped_logs_num); 183 metrics_->RecordDroppedLogsNum(dropped_logs_num);
208 } 184 }
209 185
210 PersistedLogs::LogReadStatus PersistedLogs::ReadLogsFromOldFormatPrefList(
211 const base::ListValue& list_value) {
212 if (list_value.empty())
213 return metrics_->RecordLogReadStatus(LIST_EMPTY);
214
215 // For each log, there's two entries in the list (the data and the hash).
216 DCHECK_EQ(0U, list_value.GetSize() % 2);
217 const size_t log_count = list_value.GetSize() / 2;
218
219 // Resize |list_| ahead of time, so that values can be decoded directly into
220 // the elements of the list.
221 DCHECK(list_.empty());
222 list_.resize(log_count);
223
224 for (size_t i = 0; i < log_count; ++i) {
225 if (!ReadBase64String(list_value, i * 2, &list_[i].compressed_log_data) ||
226 !ReadBase64String(list_value, i * 2 + 1, &list_[i].hash)) {
227 list_.clear();
228 return metrics_->RecordLogReadStatus(LOG_STRING_CORRUPTION);
229 }
230 }
231
232 return metrics_->RecordLogReadStatus(RECALL_SUCCESS);
233 }
234
235 } // namespace metrics 186 } // namespace metrics
OLDNEW
« no previous file with comments | « components/metrics/persisted_logs.h ('k') | components/metrics/persisted_logs_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698