| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "base/base64.h" | 7 #include "base/base64.h" |
| 8 #include "base/md5.h" | 8 #include "base/md5.h" |
| 9 #include "base/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
| 10 #include "chrome/browser/browser_process.h" | 10 #include "chrome/browser/browser_process.h" |
| 11 #include "chrome/browser/prefs/pref_service.h" | 11 #include "chrome/browser/prefs/pref_service.h" |
| 12 #include "chrome/browser/prefs/scoped_user_pref_update.h" | 12 #include "chrome/browser/prefs/scoped_user_pref_update.h" |
| 13 #include "chrome/common/pref_names.h" | 13 #include "chrome/common/pref_names.h" |
| 14 | 14 |
| 15 namespace { | 15 namespace { |
| 16 | 16 |
| 17 // The number of "initial" logs we're willing to save, and hope to send during | 17 // The number of "initial" logs to save, and hope to send during a future Chrome |
| 18 // a future Chrome session. Initial logs contain crash stats, and are pretty | 18 // session. Initial logs contain crash stats, and are pretty small. |
| 19 // small. | 19 const size_t kInitialLogsPersistLimit = 20; |
| 20 const size_t kMaxInitialLogsPersisted = 20; | |
| 21 | 20 |
| 22 // The number of ongoing logs we're willing to save persistently, and hope to | 21 // The number of ongoing logs to save persistently, and hope to |
| 23 // send during a this or future sessions. Note that each log may be pretty | 22 // send during a this or future sessions. Note that each log may be pretty |
| 24 // large, as presumably the related "initial" log wasn't sent (probably nothing | 23 // large, as presumably the related "initial" log wasn't sent (probably nothing |
| 25 // was, as the user was probably off-line). As a result, the log probably kept | 24 // was, as the user was probably off-line). As a result, the log probably kept |
| 26 // accumulating while the "initial" log was stalled, and couldn't be sent. As a | 25 // accumulating while the "initial" log was stalled, and couldn't be sent. As a |
| 27 // result, we don't want to save too many of these mega-logs. | 26 // result, we don't want to save too many of these mega-logs. |
| 28 // A "standard shutdown" will create a small log, including just the data that | 27 // A "standard shutdown" will create a small log, including just the data that |
| 29 // was not yet been transmitted, and that is normal (to have exactly one | 28 // was not yet been transmitted, and that is normal (to have exactly one |
| 30 // ongoing_log_ at startup). | 29 // ongoing_log_ at startup). |
| 31 const size_t kMaxOngoingLogsPersisted = 8; | 30 const size_t kOngoingLogsPersistLimit = 8; |
| 31 |
| 32 // The number of bytes each of initial and ongoing logs that must be stored. |
| 33 // This ensures that a reasonable amount of history will be stored even if there |
| 34 // is a long series of very small logs. |
| 35 const size_t kStorageByteLimitPerLogType = 300000; |
| 32 | 36 |
| 33 // We append (2) more elements to persisted lists: the size of the list and a | 37 // We append (2) more elements to persisted lists: the size of the list and a |
| 34 // checksum of the elements. | 38 // checksum of the elements. |
| 35 const size_t kChecksumEntryCount = 2; | 39 const size_t kChecksumEntryCount = 2; |
| 36 | 40 |
| 37 // TODO(isherman): Remove this histogram once it's confirmed that there are no | 41 // TODO(isherman): Remove this histogram once it's confirmed that there are no |
| 38 // encoding failures for protobuf logs. | 42 // encoding failures for protobuf logs. |
| 39 enum LogStoreStatus { | 43 enum LogStoreStatus { |
| 40 STORE_SUCCESS, // Successfully presisted log. | 44 STORE_SUCCESS, // Successfully presisted log. |
| 41 ENCODE_FAIL, // Failed to encode log. | 45 ENCODE_FAIL, // Failed to encode log. |
| (...skipping 26 matching lines...) Expand all Loading... |
| 68 | 72 |
| 69 MetricsLogSerializer::~MetricsLogSerializer() {} | 73 MetricsLogSerializer::~MetricsLogSerializer() {} |
| 70 | 74 |
| 71 void MetricsLogSerializer::SerializeLogs( | 75 void MetricsLogSerializer::SerializeLogs( |
| 72 const std::vector<MetricsLogManager::SerializedLog>& logs, | 76 const std::vector<MetricsLogManager::SerializedLog>& logs, |
| 73 MetricsLogManager::LogType log_type) { | 77 MetricsLogManager::LogType log_type) { |
| 74 PrefService* local_state = g_browser_process->local_state(); | 78 PrefService* local_state = g_browser_process->local_state(); |
| 75 DCHECK(local_state); | 79 DCHECK(local_state); |
| 76 const char* pref_xml = NULL; | 80 const char* pref_xml = NULL; |
| 77 const char* pref_proto = NULL; | 81 const char* pref_proto = NULL; |
| 78 size_t max_store_count = 0; | 82 size_t store_length_limit = 0; |
| 79 switch (log_type) { | 83 switch (log_type) { |
| 80 case MetricsLogManager::INITIAL_LOG: | 84 case MetricsLogManager::INITIAL_LOG: |
| 81 pref_xml = prefs::kMetricsInitialLogsXml; | 85 pref_xml = prefs::kMetricsInitialLogsXml; |
| 82 pref_proto = prefs::kMetricsInitialLogsProto; | 86 pref_proto = prefs::kMetricsInitialLogsProto; |
| 83 max_store_count = kMaxInitialLogsPersisted; | 87 store_length_limit = kInitialLogsPersistLimit; |
| 84 break; | 88 break; |
| 85 case MetricsLogManager::ONGOING_LOG: | 89 case MetricsLogManager::ONGOING_LOG: |
| 86 pref_xml = prefs::kMetricsOngoingLogsXml; | 90 pref_xml = prefs::kMetricsOngoingLogsXml; |
| 87 pref_proto = prefs::kMetricsOngoingLogsProto; | 91 pref_proto = prefs::kMetricsOngoingLogsProto; |
| 88 max_store_count = kMaxOngoingLogsPersisted; | 92 store_length_limit = kOngoingLogsPersistLimit; |
| 89 break; | 93 break; |
| 90 default: | 94 default: |
| 91 NOTREACHED(); | 95 NOTREACHED(); |
| 92 return; | 96 return; |
| 93 }; | 97 }; |
| 94 | 98 |
| 95 // Write the XML version. | 99 // Write the XML version. |
| 96 ListPrefUpdate update_xml(local_state, pref_xml); | 100 ListPrefUpdate update_xml(local_state, pref_xml); |
| 97 WriteLogsToPrefList(logs, true, max_store_count, update_xml.Get()); | 101 WriteLogsToPrefList(logs, true, store_length_limit, |
| 102 kStorageByteLimitPerLogType, update_xml.Get()); |
| 98 | 103 |
| 99 // Write the protobuf version. | 104 // Write the protobuf version. |
| 100 ListPrefUpdate update_proto(local_state, pref_proto); | 105 ListPrefUpdate update_proto(local_state, pref_proto); |
| 101 WriteLogsToPrefList(logs, false, max_store_count, update_proto.Get()); | 106 WriteLogsToPrefList(logs, false, store_length_limit, |
| 107 kStorageByteLimitPerLogType, update_proto.Get()); |
| 102 } | 108 } |
| 103 | 109 |
| 104 void MetricsLogSerializer::DeserializeLogs( | 110 void MetricsLogSerializer::DeserializeLogs( |
| 105 MetricsLogManager::LogType log_type, | 111 MetricsLogManager::LogType log_type, |
| 106 std::vector<MetricsLogManager::SerializedLog>* logs) { | 112 std::vector<MetricsLogManager::SerializedLog>* logs) { |
| 107 DCHECK(logs); | 113 DCHECK(logs); |
| 108 PrefService* local_state = g_browser_process->local_state(); | 114 PrefService* local_state = g_browser_process->local_state(); |
| 109 DCHECK(local_state); | 115 DCHECK(local_state); |
| 110 | 116 |
| 111 const char* pref_xml; | 117 const char* pref_xml; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 124 // In order to try to keep the data sent to both servers roughly in sync, | 130 // In order to try to keep the data sent to both servers roughly in sync, |
| 125 // only read the protobuf data if we read the XML data successfully. | 131 // only read the protobuf data if we read the XML data successfully. |
| 126 ReadLogsFromPrefList(*unsent_logs_proto, false, logs); | 132 ReadLogsFromPrefList(*unsent_logs_proto, false, logs); |
| 127 } | 133 } |
| 128 } | 134 } |
| 129 | 135 |
| 130 // static | 136 // static |
| 131 void MetricsLogSerializer::WriteLogsToPrefList( | 137 void MetricsLogSerializer::WriteLogsToPrefList( |
| 132 const std::vector<MetricsLogManager::SerializedLog>& local_list, | 138 const std::vector<MetricsLogManager::SerializedLog>& local_list, |
| 133 bool is_xml, | 139 bool is_xml, |
| 134 size_t max_list_size, | 140 size_t list_length_limit, |
| 141 size_t byte_limit, |
| 135 base::ListValue* list) { | 142 base::ListValue* list) { |
| 143 // One of the limit arguments must be non-zero. |
| 144 DCHECK(list_length_limit > 0 || byte_limit > 0); |
| 145 |
| 136 list->Clear(); | 146 list->Clear(); |
| 147 if (local_list.size() == 0) |
| 148 return; |
| 149 |
| 137 size_t start = 0; | 150 size_t start = 0; |
| 138 if (local_list.size() > max_list_size) | 151 // If there are too many logs, keep the most recent logs up to the length |
| 139 start = local_list.size() - max_list_size; | 152 // limit, and at least to the minimum number of bytes. |
| 140 DCHECK_LE(start, local_list.size()); | 153 if (local_list.size() > list_length_limit) { |
| 141 if (local_list.size() <= start) | 154 start = local_list.size(); |
| 155 size_t bytes_used = 0; |
| 156 for (std::vector<MetricsLogManager::SerializedLog>::const_reverse_iterator |
| 157 it = local_list.rbegin(); it != local_list.rend(); ++it) { |
| 158 // TODO(isherman): Always uses XML length so both formats of a given log |
| 159 // will be saved; switch to proto once that's the primary format. |
| 160 size_t log_size = it->xml.length(); |
| 161 if (bytes_used >= byte_limit && |
| 162 (local_list.size() - start) >= list_length_limit) |
| 163 break; |
| 164 bytes_used += log_size; |
| 165 --start; |
| 166 } |
| 167 } |
| 168 DCHECK_LT(start, local_list.size()); |
| 169 if (start >= local_list.size()) |
| 142 return; | 170 return; |
| 143 | 171 |
| 144 // Store size at the beginning of the list. | 172 // Store size at the beginning of the list. |
| 145 list->Append(Value::CreateIntegerValue(local_list.size() - start)); | 173 list->Append(Value::CreateIntegerValue(local_list.size() - start)); |
| 146 | 174 |
| 147 base::MD5Context ctx; | 175 base::MD5Context ctx; |
| 148 base::MD5Init(&ctx); | 176 base::MD5Init(&ctx); |
| 149 std::string encoded_log; | 177 std::string encoded_log; |
| 150 for (std::vector<MetricsLogManager::SerializedLog>::const_iterator it = | 178 for (std::vector<MetricsLogManager::SerializedLog>::const_iterator it = |
| 151 local_list.begin() + start; | 179 local_list.begin() + start; |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 236 if (!valid) { | 264 if (!valid) { |
| 237 local_list->clear(); | 265 local_list->clear(); |
| 238 return MakeRecallStatusHistogram(CHECKSUM_STRING_CORRUPTION, is_xml); | 266 return MakeRecallStatusHistogram(CHECKSUM_STRING_CORRUPTION, is_xml); |
| 239 } | 267 } |
| 240 if (recovered_md5 != base::MD5DigestToBase16(digest)) { | 268 if (recovered_md5 != base::MD5DigestToBase16(digest)) { |
| 241 local_list->clear(); | 269 local_list->clear(); |
| 242 return MakeRecallStatusHistogram(CHECKSUM_CORRUPTION, is_xml); | 270 return MakeRecallStatusHistogram(CHECKSUM_CORRUPTION, is_xml); |
| 243 } | 271 } |
| 244 return MakeRecallStatusHistogram(RECALL_SUCCESS, is_xml); | 272 return MakeRecallStatusHistogram(RECALL_SUCCESS, is_xml); |
| 245 } | 273 } |
| OLD | NEW |