OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "chrome/browser/metrics/metrics_log_serializer.h" | 5 #include "chrome/browser/metrics/metrics_log_serializer.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 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
86 DCHECK(logs); | 86 DCHECK(logs); |
87 PrefService* local_state = g_browser_process->local_state(); | 87 PrefService* local_state = g_browser_process->local_state(); |
88 DCHECK(local_state); | 88 DCHECK(local_state); |
89 | 89 |
90 const char* pref; | 90 const char* pref; |
91 if (log_type == MetricsLogBase::INITIAL_LOG) | 91 if (log_type == MetricsLogBase::INITIAL_LOG) |
92 pref = prefs::kMetricsInitialLogs; | 92 pref = prefs::kMetricsInitialLogs; |
93 else | 93 else |
94 pref = prefs::kMetricsOngoingLogs; | 94 pref = prefs::kMetricsOngoingLogs; |
95 | 95 |
96 const ListValue* unsent_logs = local_state->GetList(pref); | 96 const base::ListValue* unsent_logs = local_state->GetList(pref); |
97 ReadLogsFromPrefList(*unsent_logs, logs); | 97 ReadLogsFromPrefList(*unsent_logs, logs); |
98 } | 98 } |
99 | 99 |
100 // static | 100 // static |
101 void MetricsLogSerializer::WriteLogsToPrefList( | 101 void MetricsLogSerializer::WriteLogsToPrefList( |
102 const std::vector<MetricsLogManager::SerializedLog>& local_list, | 102 const std::vector<MetricsLogManager::SerializedLog>& local_list, |
103 size_t list_length_limit, | 103 size_t list_length_limit, |
104 size_t byte_limit, | 104 size_t byte_limit, |
105 base::ListValue* list) { | 105 base::ListValue* list) { |
106 // One of the limit arguments must be non-zero. | 106 // One of the limit arguments must be non-zero. |
(...skipping 17 matching lines...) Expand all Loading... |
124 break; | 124 break; |
125 bytes_used += log_size; | 125 bytes_used += log_size; |
126 --start; | 126 --start; |
127 } | 127 } |
128 } | 128 } |
129 DCHECK_LT(start, local_list.size()); | 129 DCHECK_LT(start, local_list.size()); |
130 if (start >= local_list.size()) | 130 if (start >= local_list.size()) |
131 return; | 131 return; |
132 | 132 |
133 // Store size at the beginning of the list. | 133 // Store size at the beginning of the list. |
134 list->Append(Value::CreateIntegerValue(local_list.size() - start)); | 134 list->Append(base::Value::CreateIntegerValue(local_list.size() - start)); |
135 | 135 |
136 base::MD5Context ctx; | 136 base::MD5Context ctx; |
137 base::MD5Init(&ctx); | 137 base::MD5Init(&ctx); |
138 std::string encoded_log; | 138 std::string encoded_log; |
139 for (std::vector<MetricsLogManager::SerializedLog>::const_iterator it = | 139 for (std::vector<MetricsLogManager::SerializedLog>::const_iterator it = |
140 local_list.begin() + start; | 140 local_list.begin() + start; |
141 it != local_list.end(); ++it) { | 141 it != local_list.end(); ++it) { |
142 // We encode the compressed log as Value::CreateStringValue() expects to | 142 // We encode the compressed log as Value::CreateStringValue() expects to |
143 // take a valid UTF8 string. | 143 // take a valid UTF8 string. |
144 base::Base64Encode(it->log_text(), &encoded_log); | 144 base::Base64Encode(it->log_text(), &encoded_log); |
145 base::MD5Update(&ctx, encoded_log); | 145 base::MD5Update(&ctx, encoded_log); |
146 list->Append(Value::CreateStringValue(encoded_log)); | 146 list->Append(base::Value::CreateStringValue(encoded_log)); |
147 } | 147 } |
148 | 148 |
149 // Append hash to the end of the list. | 149 // Append hash to the end of the list. |
150 base::MD5Digest digest; | 150 base::MD5Digest digest; |
151 base::MD5Final(&digest, &ctx); | 151 base::MD5Final(&digest, &ctx); |
152 list->Append(Value::CreateStringValue(base::MD5DigestToBase16(digest))); | 152 list->Append(base::Value::CreateStringValue(base::MD5DigestToBase16(digest))); |
153 DCHECK(list->GetSize() >= 3); // Minimum of 3 elements (size, data, hash). | 153 DCHECK(list->GetSize() >= 3); // Minimum of 3 elements (size, data, hash). |
154 } | 154 } |
155 | 155 |
156 // static | 156 // static |
157 MetricsLogSerializer::LogReadStatus MetricsLogSerializer::ReadLogsFromPrefList( | 157 MetricsLogSerializer::LogReadStatus MetricsLogSerializer::ReadLogsFromPrefList( |
158 const ListValue& list, | 158 const base::ListValue& list, |
159 std::vector<MetricsLogManager::SerializedLog>* local_list) { | 159 std::vector<MetricsLogManager::SerializedLog>* local_list) { |
160 if (list.GetSize() == 0) | 160 if (list.GetSize() == 0) |
161 return MakeRecallStatusHistogram(LIST_EMPTY); | 161 return MakeRecallStatusHistogram(LIST_EMPTY); |
162 if (list.GetSize() < 3) | 162 if (list.GetSize() < 3) |
163 return MakeRecallStatusHistogram(LIST_SIZE_TOO_SMALL); | 163 return MakeRecallStatusHistogram(LIST_SIZE_TOO_SMALL); |
164 | 164 |
165 // The size is stored at the beginning of the list. | 165 // The size is stored at the beginning of the list. |
166 int size; | 166 int size; |
167 bool valid = (*list.begin())->GetAsInteger(&size); | 167 bool valid = (*list.begin())->GetAsInteger(&size); |
168 if (!valid) | 168 if (!valid) |
169 return MakeRecallStatusHistogram(LIST_SIZE_MISSING); | 169 return MakeRecallStatusHistogram(LIST_SIZE_MISSING); |
170 // Account for checksum and size included in the list. | 170 // Account for checksum and size included in the list. |
171 if (static_cast<unsigned int>(size) != | 171 if (static_cast<unsigned int>(size) != |
172 list.GetSize() - kChecksumEntryCount) { | 172 list.GetSize() - kChecksumEntryCount) { |
173 return MakeRecallStatusHistogram(LIST_SIZE_CORRUPTION); | 173 return MakeRecallStatusHistogram(LIST_SIZE_CORRUPTION); |
174 } | 174 } |
175 | 175 |
176 // Allocate strings for all of the logs we are going to read in. | 176 // Allocate strings for all of the logs we are going to read in. |
177 // Do this ahead of time so that we can decode the string values directly into | 177 // Do this ahead of time so that we can decode the string values directly into |
178 // the elements of |local_list|, and thereby avoid making copies of the | 178 // the elements of |local_list|, and thereby avoid making copies of the |
179 // serialized logs, which can be fairly large. | 179 // serialized logs, which can be fairly large. |
180 DCHECK(local_list->empty()); | 180 DCHECK(local_list->empty()); |
181 local_list->resize(size); | 181 local_list->resize(size); |
182 | 182 |
183 base::MD5Context ctx; | 183 base::MD5Context ctx; |
184 base::MD5Init(&ctx); | 184 base::MD5Init(&ctx); |
185 std::string encoded_log; | 185 std::string encoded_log; |
186 size_t local_index = 0; | 186 size_t local_index = 0; |
187 for (ListValue::const_iterator it = list.begin() + 1; | 187 for (base::ListValue::const_iterator it = list.begin() + 1; |
188 it != list.end() - 1; // Last element is the checksum. | 188 it != list.end() - 1; // Last element is the checksum. |
189 ++it, ++local_index) { | 189 ++it, ++local_index) { |
190 bool valid = (*it)->GetAsString(&encoded_log); | 190 bool valid = (*it)->GetAsString(&encoded_log); |
191 if (!valid) { | 191 if (!valid) { |
192 local_list->clear(); | 192 local_list->clear(); |
193 return MakeRecallStatusHistogram(LOG_STRING_CORRUPTION); | 193 return MakeRecallStatusHistogram(LOG_STRING_CORRUPTION); |
194 } | 194 } |
195 | 195 |
196 base::MD5Update(&ctx, encoded_log); | 196 base::MD5Update(&ctx, encoded_log); |
197 | 197 |
(...skipping 16 matching lines...) Expand all Loading... |
214 if (!valid) { | 214 if (!valid) { |
215 local_list->clear(); | 215 local_list->clear(); |
216 return MakeRecallStatusHistogram(CHECKSUM_STRING_CORRUPTION); | 216 return MakeRecallStatusHistogram(CHECKSUM_STRING_CORRUPTION); |
217 } | 217 } |
218 if (recovered_md5 != base::MD5DigestToBase16(digest)) { | 218 if (recovered_md5 != base::MD5DigestToBase16(digest)) { |
219 local_list->clear(); | 219 local_list->clear(); |
220 return MakeRecallStatusHistogram(CHECKSUM_CORRUPTION); | 220 return MakeRecallStatusHistogram(CHECKSUM_CORRUPTION); |
221 } | 221 } |
222 return MakeRecallStatusHistogram(RECALL_SUCCESS); | 222 return MakeRecallStatusHistogram(RECALL_SUCCESS); |
223 } | 223 } |
OLD | NEW |