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" |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
58 "UMA.ProtoCompressionRatio", | 58 "UMA.ProtoCompressionRatio", |
59 static_cast<int>(100 * compressed_log_data.size() / log_data.size())); | 59 static_cast<int>(100 * compressed_log_data.size() / log_data.size())); |
60 UMA_HISTOGRAM_CUSTOM_COUNTS( | 60 UMA_HISTOGRAM_CUSTOM_COUNTS( |
61 "UMA.ProtoGzippedKBSaved", | 61 "UMA.ProtoGzippedKBSaved", |
62 static_cast<int>((log_data.size() - compressed_log_data.size()) / 1024), | 62 static_cast<int>((log_data.size() - compressed_log_data.size()) / 1024), |
63 1, 2000, 50); | 63 1, 2000, 50); |
64 | 64 |
65 hash = base::SHA1HashString(log_data); | 65 hash = base::SHA1HashString(log_data); |
66 } | 66 } |
67 | 67 |
68 void PersistedLogs::LogHashPair::Clear() { | |
69 compressed_log_data.clear(); | |
70 hash.clear(); | |
71 } | |
72 | |
73 void PersistedLogs::LogHashPair::Swap(PersistedLogs::LogHashPair* input) { | |
74 compressed_log_data.swap(input->compressed_log_data); | |
75 hash.swap(input->hash); | |
76 } | |
77 | |
78 PersistedLogs::PersistedLogs(PrefService* local_state, | 68 PersistedLogs::PersistedLogs(PrefService* local_state, |
79 const char* pref_name, | 69 const char* pref_name, |
80 const char* old_pref_name, | 70 const char* old_pref_name, |
81 size_t min_log_count, | 71 size_t min_log_count, |
82 size_t min_log_bytes, | 72 size_t min_log_bytes, |
83 size_t max_log_size) | 73 size_t max_log_size) |
84 : local_state_(local_state), | 74 : local_state_(local_state), |
85 pref_name_(pref_name), | 75 pref_name_(pref_name), |
86 old_pref_name_(old_pref_name), | 76 old_pref_name_(old_pref_name), |
87 min_log_count_(min_log_count), | 77 min_log_count_(min_log_count), |
88 min_log_bytes_(min_log_bytes), | 78 min_log_bytes_(min_log_bytes), |
89 max_log_size_(max_log_size != 0 ? max_log_size : static_cast<size_t>(-1)), | 79 max_log_size_(max_log_size != 0 ? max_log_size : static_cast<size_t>(-1)), |
90 last_provisional_store_index_(-1) { | 80 staged_log_index_(-1) { |
91 DCHECK(local_state_); | 81 DCHECK(local_state_); |
92 // One of the limit arguments must be non-zero. | 82 // One of the limit arguments must be non-zero. |
93 DCHECK(min_log_count_ > 0 || min_log_bytes_ > 0); | 83 DCHECK(min_log_count_ > 0 || min_log_bytes_ > 0); |
94 } | 84 } |
95 | 85 |
96 PersistedLogs::~PersistedLogs() {} | 86 PersistedLogs::~PersistedLogs() {} |
97 | 87 |
98 void PersistedLogs::SerializeLogs() const { | 88 void PersistedLogs::SerializeLogs() const { |
99 ListPrefUpdate update(local_state_, pref_name_); | 89 ListPrefUpdate update(local_state_, pref_name_); |
100 WriteLogsToPrefList(update.Get()); | 90 WriteLogsToPrefList(update.Get()); |
(...skipping 17 matching lines...) Expand all Loading... |
118 void PersistedLogs::StoreLog(const std::string& log_data) { | 108 void PersistedLogs::StoreLog(const std::string& log_data) { |
119 list_.push_back(LogHashPair()); | 109 list_.push_back(LogHashPair()); |
120 list_.back().Init(log_data); | 110 list_.back().Init(log_data); |
121 } | 111 } |
122 | 112 |
123 void PersistedLogs::StageLog() { | 113 void PersistedLogs::StageLog() { |
124 // CHECK, rather than DCHECK, because swap()ing with an empty list causes | 114 // CHECK, rather than DCHECK, because swap()ing with an empty list causes |
125 // hard-to-identify crashes much later. | 115 // hard-to-identify crashes much later. |
126 CHECK(!list_.empty()); | 116 CHECK(!list_.empty()); |
127 DCHECK(!has_staged_log()); | 117 DCHECK(!has_staged_log()); |
128 staged_log_.Swap(&list_.back()); | 118 staged_log_index_ = list_.size() - 1; |
129 list_.pop_back(); | |
130 | |
131 // If the staged log was the last provisional store, clear that. | |
132 if (static_cast<size_t>(last_provisional_store_index_) == list_.size()) | |
133 last_provisional_store_index_ = -1; | |
134 DCHECK(has_staged_log()); | 119 DCHECK(has_staged_log()); |
135 } | 120 } |
136 | 121 |
137 void PersistedLogs::DiscardStagedLog() { | 122 void PersistedLogs::DiscardStagedLog() { |
138 DCHECK(has_staged_log()); | 123 DCHECK(has_staged_log()); |
139 staged_log_.Clear(); | 124 DCHECK_LT(static_cast<size_t>(staged_log_index_), list_.size()); |
140 } | 125 list_.erase(list_.begin() + staged_log_index_); |
141 | 126 staged_log_index_ = -1; |
142 void PersistedLogs::StoreStagedLogAsUnsent(StoreType store_type) { | |
143 list_.push_back(LogHashPair()); | |
144 list_.back().Swap(&staged_log_); | |
145 if (store_type == PROVISIONAL_STORE) | |
146 last_provisional_store_index_ = list_.size() - 1; | |
147 } | |
148 | |
149 void PersistedLogs::DiscardLastProvisionalStore() { | |
150 if (last_provisional_store_index_ == -1) | |
151 return; | |
152 DCHECK_LT(static_cast<size_t>(last_provisional_store_index_), list_.size()); | |
153 list_.erase(list_.begin() + last_provisional_store_index_); | |
154 last_provisional_store_index_ = -1; | |
155 } | 127 } |
156 | 128 |
157 void PersistedLogs::WriteLogsToPrefList(base::ListValue* list_value) const { | 129 void PersistedLogs::WriteLogsToPrefList(base::ListValue* list_value) const { |
158 list_value->Clear(); | 130 list_value->Clear(); |
159 | 131 |
160 // Keep the most recent logs which are smaller than |max_log_size_|. | 132 // Keep the most recent logs which are smaller than |max_log_size_|. |
161 // We keep at least |min_log_bytes_| and |min_log_count_| of logs before | 133 // We keep at least |min_log_bytes_| and |min_log_count_| of logs before |
162 // discarding older logs. | 134 // discarding older logs. |
163 size_t start = list_.size(); | 135 size_t start = list_.size(); |
164 size_t saved_log_count = 0; | 136 size_t saved_log_count = 0; |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
276 return MakeRecallStatusHistogram(CHECKSUM_STRING_CORRUPTION); | 248 return MakeRecallStatusHistogram(CHECKSUM_STRING_CORRUPTION); |
277 } | 249 } |
278 if (recovered_md5 != base::MD5DigestToBase16(digest)) { | 250 if (recovered_md5 != base::MD5DigestToBase16(digest)) { |
279 list_.clear(); | 251 list_.clear(); |
280 return MakeRecallStatusHistogram(CHECKSUM_CORRUPTION); | 252 return MakeRecallStatusHistogram(CHECKSUM_CORRUPTION); |
281 } | 253 } |
282 return MakeRecallStatusHistogram(RECALL_SUCCESS); | 254 return MakeRecallStatusHistogram(RECALL_SUCCESS); |
283 } | 255 } |
284 | 256 |
285 } // namespace metrics | 257 } // namespace metrics |
OLD | NEW |