Index: components/metrics/persisted_logs.cc |
diff --git a/components/metrics/persisted_logs.cc b/components/metrics/persisted_logs.cc |
index 7213b59806131c66868b7f4f651e1c1bd151c106..f3cc5fdf679cf78d4d4cd683cdf5396897a6533d 100644 |
--- a/components/metrics/persisted_logs.cc |
+++ b/components/metrics/persisted_logs.cc |
@@ -86,7 +86,7 @@ PersistedLogs::PersistedLogs(PrefService* local_state, |
old_pref_name_(old_pref_name), |
min_log_count_(min_log_count), |
min_log_bytes_(min_log_bytes), |
- max_log_size_(max_log_size), |
+ max_log_size_(max_log_size != 0 ? max_log_size : static_cast<size_t>(-1)), |
last_provisional_store_index_(-1) { |
DCHECK(local_state_); |
// One of the limit arguments must be non-zero. |
@@ -95,22 +95,7 @@ PersistedLogs::PersistedLogs(PrefService* local_state, |
PersistedLogs::~PersistedLogs() {} |
-void PersistedLogs::SerializeLogs() { |
- // Remove any logs that are over the serialization size limit. |
- if (max_log_size_) { |
- for (std::vector<LogHashPair>::iterator it = list_.begin(); |
- it != list_.end();) { |
- size_t log_size = it->compressed_log_data.length(); |
- if (log_size > max_log_size_) { |
- UMA_HISTOGRAM_COUNTS("UMA.Large Accumulated Log Not Persisted", |
- static_cast<int>(log_size)); |
- it = list_.erase(it); |
- } else { |
- ++it; |
- } |
- } |
- } |
- |
+void PersistedLogs::SerializeLogs() const { |
ListPrefUpdate update(local_state_, pref_name_); |
WriteLogsToPrefList(update.Get()); |
@@ -169,33 +154,35 @@ void PersistedLogs::DiscardLastProvisionalStore() { |
last_provisional_store_index_ = -1; |
} |
-void PersistedLogs::WriteLogsToPrefList(base::ListValue* list_value) { |
+void PersistedLogs::WriteLogsToPrefList(base::ListValue* list_value) const { |
list_value->Clear(); |
- // Leave the list completely empty if there are no storable values. |
- if (list_.empty()) |
- return; |
- size_t start = 0; |
- // If there are too many logs, keep the most recent logs up to the length |
- // limit, and at least to the minimum number of bytes. |
- if (list_.size() > min_log_count_) { |
- start = list_.size(); |
- size_t bytes_used = 0; |
- std::vector<LogHashPair>::const_reverse_iterator end = list_.rend(); |
- for (std::vector<LogHashPair>::const_reverse_iterator it = list_.rbegin(); |
- it != end; ++it) { |
- const size_t log_size = it->compressed_log_data.length(); |
- if (bytes_used >= min_log_bytes_ && |
- (list_.size() - start) >= min_log_count_) { |
- break; |
- } |
- bytes_used += log_size; |
- --start; |
+ // Keep the most recent logs which are smaller than |max_log_size_|. |
+ // We keep at least |min_log_bytes_| and |min_log_count_| of logs before |
+ // discarding older logs. |
+ size_t start = list_.size(); |
+ size_t saved_log_count = 0; |
+ size_t bytes_used = 0; |
+ for (; start > 0; --start) { |
+ size_t log_size = list_[start - 1].compressed_log_data.length(); |
+ if (bytes_used >= min_log_bytes_ && |
+ saved_log_count >= min_log_count_) { |
+ break; |
} |
+ // Oversized logs won't be persisted, so don't count them. |
+ if (log_size > max_log_size_) |
+ continue; |
+ bytes_used += log_size; |
+ ++saved_log_count; |
} |
- DCHECK_LT(start, list_.size()); |
for (size_t i = start; i < list_.size(); ++i) { |
+ size_t log_size = list_[i].compressed_log_data.length(); |
+ if (log_size > max_log_size_) { |
+ UMA_HISTOGRAM_COUNTS("UMA.Large Accumulated Log Not Persisted", |
+ static_cast<int>(log_size)); |
+ continue; |
+ } |
AppendBase64String(list_[i].compressed_log_data, list_value); |
AppendBase64String(list_[i].hash, list_value); |
} |