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
3 // found in the LICENSE file.
5 #include "components/metrics/persisted_logs.h"
9 #include "base/base64.h"
11 #include "base/metrics/histogram_macros.h"
12 #include "base/prefs/pref_service.h"
13 #include "base/prefs/scoped_user_pref_update.h"
14 #include "base/sha1.h"
15 #include "base/timer/elapsed_timer.h"
16 #include "components/compression/compression_utils.h"
22 PersistedLogs::LogReadStatus
MakeRecallStatusHistogram(
23 PersistedLogs::LogReadStatus status
) {
24 UMA_HISTOGRAM_ENUMERATION("PrefService.PersistentLogRecallProtobufs",
25 status
, PersistedLogs::END_RECALL_STATUS
);
29 // Reads the value at |index| from |list_value| as a string and Base64-decodes
30 // it into |result|. Returns true on success.
31 bool ReadBase64String(const base::ListValue
& list_value
,
33 std::string
* result
) {
34 std::string base64_result
;
35 if (!list_value
.GetString(index
, &base64_result
))
37 return base::Base64Decode(base64_result
, result
);
40 // Base64-encodes |str| and appends the result to |list_value|.
41 void AppendBase64String(const std::string
& str
, base::ListValue
* list_value
) {
42 std::string base64_str
;
43 base::Base64Encode(str
, &base64_str
);
44 list_value
->AppendString(base64_str
);
49 void PersistedLogs::LogHashPair::Init(const std::string
& log_data
) {
50 DCHECK(!log_data
.empty());
52 if (!compression::GzipCompress(log_data
, &compressed_log_data
)) {
57 UMA_HISTOGRAM_PERCENTAGE(
58 "UMA.ProtoCompressionRatio",
59 static_cast<int>(100 * compressed_log_data
.size() / log_data
.size()));
61 hash
= base::SHA1HashString(log_data
);
64 PersistedLogs::PersistedLogs(PrefService
* local_state
,
65 const char* pref_name
,
69 : local_state_(local_state
),
70 pref_name_(pref_name
),
71 min_log_count_(min_log_count
),
72 min_log_bytes_(min_log_bytes
),
73 max_log_size_(max_log_size
!= 0 ? max_log_size
: static_cast<size_t>(-1)),
74 staged_log_index_(-1) {
76 // One of the limit arguments must be non-zero.
77 DCHECK(min_log_count_
> 0 || min_log_bytes_
> 0);
80 PersistedLogs::~PersistedLogs() {}
82 void PersistedLogs::SerializeLogs() const {
83 ListPrefUpdate
update(local_state_
, pref_name_
);
84 WriteLogsToPrefList(update
.Get());
87 PersistedLogs::LogReadStatus
PersistedLogs::DeserializeLogs() {
88 return ReadLogsFromPrefList(*local_state_
->GetList(pref_name_
));
91 void PersistedLogs::StoreLog(const std::string
& log_data
) {
92 list_
.push_back(LogHashPair());
93 list_
.back().Init(log_data
);
96 void PersistedLogs::StageLog() {
97 // CHECK, rather than DCHECK, because swap()ing with an empty list causes
98 // hard-to-identify crashes much later.
99 CHECK(!list_
.empty());
100 DCHECK(!has_staged_log());
101 staged_log_index_
= list_
.size() - 1;
102 DCHECK(has_staged_log());
105 void PersistedLogs::DiscardStagedLog() {
106 DCHECK(has_staged_log());
107 DCHECK_LT(static_cast<size_t>(staged_log_index_
), list_
.size());
108 list_
.erase(list_
.begin() + staged_log_index_
);
109 staged_log_index_
= -1;
112 void PersistedLogs::WriteLogsToPrefList(base::ListValue
* list_value
) const {
115 // 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
117 // discarding older logs.
118 size_t start
= list_
.size();
119 size_t saved_log_count
= 0;
120 size_t bytes_used
= 0;
121 for (; start
> 0; --start
) {
122 size_t log_size
= list_
[start
- 1].compressed_log_data
.length();
123 if (bytes_used
>= min_log_bytes_
&&
124 saved_log_count
>= min_log_count_
) {
127 // Oversized logs won't be persisted, so don't count them.
128 if (log_size
> max_log_size_
)
130 bytes_used
+= log_size
;
134 for (size_t i
= start
; i
< list_
.size(); ++i
) {
135 size_t log_size
= list_
[i
].compressed_log_data
.length();
136 if (log_size
> max_log_size_
) {
137 UMA_HISTOGRAM_COUNTS("UMA.Large Accumulated Log Not Persisted",
138 static_cast<int>(log_size
));
141 AppendBase64String(list_
[i
].compressed_log_data
, list_value
);
142 AppendBase64String(list_
[i
].hash
, list_value
);
146 PersistedLogs::LogReadStatus
PersistedLogs::ReadLogsFromPrefList(
147 const base::ListValue
& list_value
) {
148 if (list_value
.empty())
149 return MakeRecallStatusHistogram(LIST_EMPTY
);
151 // For each log, there's two entries in the list (the data and the hash).
152 DCHECK_EQ(0U, list_value
.GetSize() % 2);
153 const size_t log_count
= list_value
.GetSize() / 2;
155 // Resize |list_| ahead of time, so that values can be decoded directly into
156 // the elements of the list.
157 DCHECK(list_
.empty());
158 list_
.resize(log_count
);
160 for (size_t i
= 0; i
< log_count
; ++i
) {
161 if (!ReadBase64String(list_value
, i
* 2, &list_
[i
].compressed_log_data
) ||
162 !ReadBase64String(list_value
, i
* 2 + 1, &list_
[i
].hash
)) {
164 return MakeRecallStatusHistogram(LOG_STRING_CORRUPTION
);
168 return MakeRecallStatusHistogram(RECALL_SUCCESS
);
171 } // namespace metrics