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 bytes each of initial and ongoing logs that will be stored. |
18 // a future Chrome session. Initial logs contain crash stats, and are pretty | 18 const size_t kMaxStorageBytesPerLogType = 300000; |
19 // small. | |
20 const size_t kMaxInitialLogsPersisted = 20; | |
21 | |
22 // The number of ongoing logs we're willing to save persistently, and hope to | |
23 // 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 | |
25 // 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 | |
27 // 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 | |
29 // was not yet been transmitted, and that is normal (to have exactly one | |
30 // ongoing_log_ at startup). | |
31 const size_t kMaxOngoingLogsPersisted = 8; | |
32 | 19 |
33 // We append (2) more elements to persisted lists: the size of the list and a | 20 // We append (2) more elements to persisted lists: the size of the list and a |
34 // checksum of the elements. | 21 // checksum of the elements. |
35 const size_t kChecksumEntryCount = 2; | 22 const size_t kChecksumEntryCount = 2; |
36 | 23 |
37 // TODO(isherman): Remove this histogram once it's confirmed that there are no | 24 // TODO(isherman): Remove this histogram once it's confirmed that there are no |
38 // encoding failures for protobuf logs. | 25 // encoding failures for protobuf logs. |
39 enum LogStoreStatus { | 26 enum LogStoreStatus { |
40 STORE_SUCCESS, // Successfully presisted log. | 27 STORE_SUCCESS, // Successfully presisted log. |
41 ENCODE_FAIL, // Failed to encode log. | 28 ENCODE_FAIL, // Failed to encode log. |
(...skipping 26 matching lines...) Expand all Loading... | |
68 | 55 |
69 MetricsLogSerializer::~MetricsLogSerializer() {} | 56 MetricsLogSerializer::~MetricsLogSerializer() {} |
70 | 57 |
71 void MetricsLogSerializer::SerializeLogs( | 58 void MetricsLogSerializer::SerializeLogs( |
72 const std::vector<MetricsLogManager::SerializedLog>& logs, | 59 const std::vector<MetricsLogManager::SerializedLog>& logs, |
73 MetricsLogManager::LogType log_type) { | 60 MetricsLogManager::LogType log_type) { |
74 PrefService* local_state = g_browser_process->local_state(); | 61 PrefService* local_state = g_browser_process->local_state(); |
75 DCHECK(local_state); | 62 DCHECK(local_state); |
76 const char* pref_xml = NULL; | 63 const char* pref_xml = NULL; |
77 const char* pref_proto = NULL; | 64 const char* pref_proto = NULL; |
78 size_t max_store_count = 0; | |
79 switch (log_type) { | 65 switch (log_type) { |
80 case MetricsLogManager::INITIAL_LOG: | 66 case MetricsLogManager::INITIAL_LOG: |
81 pref_xml = prefs::kMetricsInitialLogsXml; | 67 pref_xml = prefs::kMetricsInitialLogsXml; |
82 pref_proto = prefs::kMetricsInitialLogsProto; | 68 pref_proto = prefs::kMetricsInitialLogsProto; |
83 max_store_count = kMaxInitialLogsPersisted; | |
84 break; | 69 break; |
85 case MetricsLogManager::ONGOING_LOG: | 70 case MetricsLogManager::ONGOING_LOG: |
86 pref_xml = prefs::kMetricsOngoingLogsXml; | 71 pref_xml = prefs::kMetricsOngoingLogsXml; |
87 pref_proto = prefs::kMetricsOngoingLogsProto; | 72 pref_proto = prefs::kMetricsOngoingLogsProto; |
88 max_store_count = kMaxOngoingLogsPersisted; | |
89 break; | 73 break; |
90 default: | 74 default: |
91 NOTREACHED(); | 75 NOTREACHED(); |
92 return; | 76 return; |
93 }; | 77 }; |
94 | 78 |
95 // Write the XML version. | 79 // Write the XML version. |
96 ListPrefUpdate update_xml(local_state, pref_xml); | 80 ListPrefUpdate update_xml(local_state, pref_xml); |
97 WriteLogsToPrefList(logs, true, max_store_count, update_xml.Get()); | 81 WriteLogsToPrefList(logs, true, update_xml.Get()); |
98 | 82 |
99 // Write the protobuf version. | 83 // Write the protobuf version. |
100 ListPrefUpdate update_proto(local_state, pref_proto); | 84 ListPrefUpdate update_proto(local_state, pref_proto); |
101 WriteLogsToPrefList(logs, false, max_store_count, update_proto.Get()); | 85 WriteLogsToPrefList(logs, false, update_proto.Get()); |
102 } | 86 } |
103 | 87 |
104 void MetricsLogSerializer::DeserializeLogs( | 88 void MetricsLogSerializer::DeserializeLogs( |
105 MetricsLogManager::LogType log_type, | 89 MetricsLogManager::LogType log_type, |
106 std::vector<MetricsLogManager::SerializedLog>* logs) { | 90 std::vector<MetricsLogManager::SerializedLog>* logs) { |
107 DCHECK(logs); | 91 DCHECK(logs); |
108 PrefService* local_state = g_browser_process->local_state(); | 92 PrefService* local_state = g_browser_process->local_state(); |
109 DCHECK(local_state); | 93 DCHECK(local_state); |
110 | 94 |
111 const char* pref_xml; | 95 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, | 108 // 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. | 109 // only read the protobuf data if we read the XML data successfully. |
126 ReadLogsFromPrefList(*unsent_logs_proto, false, logs); | 110 ReadLogsFromPrefList(*unsent_logs_proto, false, logs); |
127 } | 111 } |
128 } | 112 } |
129 | 113 |
130 // static | 114 // static |
131 void MetricsLogSerializer::WriteLogsToPrefList( | 115 void MetricsLogSerializer::WriteLogsToPrefList( |
132 const std::vector<MetricsLogManager::SerializedLog>& local_list, | 116 const std::vector<MetricsLogManager::SerializedLog>& local_list, |
133 bool is_xml, | 117 bool is_xml, |
134 size_t max_list_size, | |
135 base::ListValue* list) { | 118 base::ListValue* list) { |
136 list->Clear(); | 119 list->Clear(); |
137 size_t start = 0; | 120 |
138 if (local_list.size() > max_list_size) | 121 // Keep the most recent logs, up to the size limit. |
139 start = local_list.size() - max_list_size; | 122 size_t start = local_list.size(); |
123 size_t bytes_used = 0; | |
124 for (std::vector<MetricsLogManager::SerializedLog>::const_reverse_iterator | |
125 it = local_list.rbegin(); it != local_list.rend(); ++it) { | |
126 // TODO(isherman): Always uses XML length so both formats of a given log | |
127 // will be saved; switch to proto once that's the primary format. | |
128 size_t log_size = it->xml.length(); | |
129 bytes_used += log_size; | |
130 if (bytes_used > kMaxStorageBytesPerLogType) | |
131 break; | |
132 --start; | |
133 } | |
134 // Logs big enough that not even one fits in the allowed size should never | |
135 // have reached this point, so at least one should always be written. | |
Ilya Sherman
2012/03/01 22:58:21
nit: This comment makes it sound like the line bel
jar (doing other things)
2012/03/02 17:47:00
In fact... I don't see why you are sure this is tr
| |
140 DCHECK_LE(start, local_list.size()); | 136 DCHECK_LE(start, local_list.size()); |
141 if (local_list.size() <= start) | 137 if (local_list.size() <= start) |
142 return; | 138 return; |
143 | 139 |
144 // Store size at the beginning of the list. | 140 // Store size at the beginning of the list. |
145 list->Append(Value::CreateIntegerValue(local_list.size() - start)); | 141 list->Append(Value::CreateIntegerValue(local_list.size() - start)); |
146 | 142 |
147 base::MD5Context ctx; | 143 base::MD5Context ctx; |
148 base::MD5Init(&ctx); | 144 base::MD5Init(&ctx); |
149 std::string encoded_log; | 145 std::string encoded_log; |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
236 if (!valid) { | 232 if (!valid) { |
237 local_list->clear(); | 233 local_list->clear(); |
238 return MakeRecallStatusHistogram(CHECKSUM_STRING_CORRUPTION, is_xml); | 234 return MakeRecallStatusHistogram(CHECKSUM_STRING_CORRUPTION, is_xml); |
239 } | 235 } |
240 if (recovered_md5 != base::MD5DigestToBase16(digest)) { | 236 if (recovered_md5 != base::MD5DigestToBase16(digest)) { |
241 local_list->clear(); | 237 local_list->clear(); |
242 return MakeRecallStatusHistogram(CHECKSUM_CORRUPTION, is_xml); | 238 return MakeRecallStatusHistogram(CHECKSUM_CORRUPTION, is_xml); |
243 } | 239 } |
244 return MakeRecallStatusHistogram(RECALL_SUCCESS, is_xml); | 240 return MakeRecallStatusHistogram(RECALL_SUCCESS, is_xml); |
245 } | 241 } |
OLD | NEW |