1 // Copyright (c) 2012 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.
5 #include "chrome/browser/metrics/metrics_log_serializer.h"
9 #include "base/base64.h"
11 #include "base/metrics/histogram.h"
12 #include "base/prefs/pref_service.h"
13 #include "base/prefs/scoped_user_pref_update.h"
14 #include "chrome/browser/browser_process.h"
15 #include "chrome/common/pref_names.h"
19 // The number of "initial" logs to save, and hope to send during a future Chrome
20 // session. Initial logs contain crash stats, and are pretty small.
21 const size_t kInitialLogsPersistLimit = 20;
23 // The number of ongoing logs to save persistently, and hope to
24 // send during a this or future sessions. Note that each log may be pretty
25 // large, as presumably the related "initial" log wasn't sent (probably nothing
26 // was, as the user was probably off-line). As a result, the log probably kept
27 // accumulating while the "initial" log was stalled, and couldn't be sent. As a
28 // result, we don't want to save too many of these mega-logs.
29 // A "standard shutdown" will create a small log, including just the data that
30 // was not yet been transmitted, and that is normal (to have exactly one
31 // ongoing_log_ at startup).
32 const size_t kOngoingLogsPersistLimit = 8;
34 // The number of bytes each of initial and ongoing logs that must be stored.
35 // This ensures that a reasonable amount of history will be stored even if there
36 // is a long series of very small logs.
37 const size_t kStorageByteLimitPerLogType = 300000;
39 // We append (2) more elements to persisted lists: the size of the list and a
40 // checksum of the elements.
41 const size_t kChecksumEntryCount = 2;
43 MetricsLogSerializer::LogReadStatus MakeRecallStatusHistogram(
44 MetricsLogSerializer::LogReadStatus status) {
45 UMA_HISTOGRAM_ENUMERATION("PrefService.PersistentLogRecallProtobufs",
46 status, MetricsLogSerializer::END_RECALL_STATUS);
53 MetricsLogSerializer::MetricsLogSerializer() {}
55 MetricsLogSerializer::~MetricsLogSerializer() {}
57 void MetricsLogSerializer::SerializeLogs(
58 const std::vector<MetricsLogManager::SerializedLog>& logs,
59 MetricsLogManager::LogType log_type) {
60 PrefService* local_state = g_browser_process->local_state();
62 const char* pref = NULL;
63 size_t store_length_limit = 0;
65 case MetricsLogManager::INITIAL_LOG:
66 pref = prefs::kMetricsInitialLogs;
67 store_length_limit = kInitialLogsPersistLimit;
69 case MetricsLogManager::ONGOING_LOG:
70 pref = prefs::kMetricsOngoingLogs;
71 store_length_limit = kOngoingLogsPersistLimit;
73 case MetricsLogManager::NO_LOG:
78 ListPrefUpdate update(local_state, pref);
79 WriteLogsToPrefList(logs, store_length_limit, kStorageByteLimitPerLogType,
83 void MetricsLogSerializer::DeserializeLogs(
84 MetricsLogManager::LogType log_type,
85 std::vector<MetricsLogManager::SerializedLog>* logs) {
87 PrefService* local_state = g_browser_process->local_state();
91 if (log_type == MetricsLogManager::INITIAL_LOG)
92 pref = prefs::kMetricsInitialLogs;
94 pref = prefs::kMetricsOngoingLogs;
96 const ListValue* unsent_logs = local_state->GetList(pref);
97 ReadLogsFromPrefList(*unsent_logs, logs);
101 void MetricsLogSerializer::WriteLogsToPrefList(
102 const std::vector<MetricsLogManager::SerializedLog>& local_list,
103 size_t list_length_limit,
105 base::ListValue* list) {
106 // One of the limit arguments must be non-zero.
107 DCHECK(list_length_limit > 0 || byte_limit > 0);
110 if (local_list.size() == 0)
114 // If there are too many logs, keep the most recent logs up to the length
115 // limit, and at least to the minimum number of bytes.
116 if (local_list.size() > list_length_limit) {
117 start = local_list.size();
118 size_t bytes_used = 0;
119 for (std::vector<MetricsLogManager::SerializedLog>::const_reverse_iterator
120 it = local_list.rbegin(); it != local_list.rend(); ++it) {
121 size_t log_size = it->log_text().length();
122 if (bytes_used >= byte_limit &&
123 (local_list.size() - start) >= list_length_limit)
125 bytes_used += log_size;
129 DCHECK_LT(start, local_list.size());
130 if (start >= local_list.size())
133 // Store size at the beginning of the list.
134 list->Append(Value::CreateIntegerValue(local_list.size() - start));
136 base::MD5Context ctx;
138 std::string encoded_log;
139 for (std::vector<MetricsLogManager::SerializedLog>::const_iterator it =
140 local_list.begin() + start;
141 it != local_list.end(); ++it) {
142 // We encode the compressed log as Value::CreateStringValue() expects to
143 // take a valid UTF8 string.
144 if (!base::Base64Encode(it->log_text(), &encoded_log)) {
148 base::MD5Update(&ctx, encoded_log);
149 list->Append(Value::CreateStringValue(encoded_log));
152 // Append hash to the end of the list.
153 base::MD5Digest digest;
154 base::MD5Final(&digest, &ctx);
155 list->Append(Value::CreateStringValue(base::MD5DigestToBase16(digest)));
156 DCHECK(list->GetSize() >= 3); // Minimum of 3 elements (size, data, hash).
160 MetricsLogSerializer::LogReadStatus MetricsLogSerializer::ReadLogsFromPrefList(
161 const ListValue& list,
162 std::vector<MetricsLogManager::SerializedLog>* local_list) {
163 if (list.GetSize() == 0)
164 return MakeRecallStatusHistogram(LIST_EMPTY);
165 if (list.GetSize() < 3)
166 return MakeRecallStatusHistogram(LIST_SIZE_TOO_SMALL);
168 // The size is stored at the beginning of the list.
170 bool valid = (*list.begin())->GetAsInteger(&size);
172 return MakeRecallStatusHistogram(LIST_SIZE_MISSING);
173 // Account for checksum and size included in the list.
174 if (static_cast<unsigned int>(size) !=
175 list.GetSize() - kChecksumEntryCount) {
176 return MakeRecallStatusHistogram(LIST_SIZE_CORRUPTION);
179 // Allocate strings for all of the logs we are going to read in.
180 // Do this ahead of time so that we can decode the string values directly into
181 // the elements of |local_list|, and thereby avoid making copies of the
182 // serialized logs, which can be fairly large.
183 DCHECK(local_list->empty());
184 local_list->resize(size);
186 base::MD5Context ctx;
188 std::string encoded_log;
189 size_t local_index = 0;
190 for (ListValue::const_iterator it = list.begin() + 1;
191 it != list.end() - 1; // Last element is the checksum.
192 ++it, ++local_index) {
193 bool valid = (*it)->GetAsString(&encoded_log);
196 return MakeRecallStatusHistogram(LOG_STRING_CORRUPTION);
199 base::MD5Update(&ctx, encoded_log);
201 std::string log_text;
202 if (!base::Base64Decode(encoded_log, &log_text)) {
204 return MakeRecallStatusHistogram(DECODE_FAIL);
207 DCHECK_LT(local_index, local_list->size());
208 (*local_list)[local_index].SwapLogText(&log_text);
212 base::MD5Digest digest;
213 base::MD5Final(&digest, &ctx);
214 std::string recovered_md5;
215 // We store the hash at the end of the list.
216 valid = (*(list.end() - 1))->GetAsString(&recovered_md5);
219 return MakeRecallStatusHistogram(CHECKSUM_STRING_CORRUPTION);
221 if (recovered_md5 != base::MD5DigestToBase16(digest)) {
223 return MakeRecallStatusHistogram(CHECKSUM_CORRUPTION);
225 return MakeRecallStatusHistogram(RECALL_SUCCESS);