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/common/metrics/metrics_log_manager.h" | 5 #include "chrome/common/metrics/metrics_log_manager.h" |
6 | 6 |
7 #if defined(USE_SYSTEM_LIBBZ2) | 7 #if defined(USE_SYSTEM_LIBBZ2) |
8 #include <bzlib.h> | 8 #include <bzlib.h> |
9 #else | 9 #else |
10 #include "third_party/bzip2/bzlib.h" | 10 #include "third_party/bzip2/bzlib.h" |
(...skipping 13 matching lines...) Expand all Loading... | |
24 } | 24 } |
25 | 25 |
26 void MetricsLogManager::StageCurrentLogForUpload() { | 26 void MetricsLogManager::StageCurrentLogForUpload() { |
27 DCHECK(current_log_.get()); | 27 DCHECK(current_log_.get()); |
28 current_log_->CloseLog(); | 28 current_log_->CloseLog(); |
29 staged_log_.reset(current_log_.release()); | 29 staged_log_.reset(current_log_.release()); |
30 CompressStagedLog(); | 30 CompressStagedLog(); |
31 } | 31 } |
32 | 32 |
33 bool MetricsLogManager::has_staged_log() const { | 33 bool MetricsLogManager::has_staged_log() const { |
34 return staged_log_.get() || !compressed_staged_log_text_.empty(); | 34 DCHECK_EQ(staged_log_text_xml().empty(), staged_log_text_proto().empty()); |
35 return staged_log_.get() || !staged_log_text_xml().empty(); | |
35 } | 36 } |
36 | 37 |
37 void MetricsLogManager::DiscardStagedLog() { | 38 void MetricsLogManager::DiscardStagedLog() { |
38 staged_log_.reset(); | 39 staged_log_.reset(); |
39 compressed_staged_log_text_.clear(); | 40 staged_log_text_.first.clear(); |
41 staged_log_text_.second.clear(); | |
40 } | 42 } |
41 | 43 |
42 void MetricsLogManager::DiscardCurrentLog() { | 44 void MetricsLogManager::DiscardCurrentLog() { |
43 current_log_->CloseLog(); | 45 current_log_->CloseLog(); |
44 current_log_.reset(); | 46 current_log_.reset(); |
45 } | 47 } |
46 | 48 |
47 void MetricsLogManager::PauseCurrentLog() { | 49 void MetricsLogManager::PauseCurrentLog() { |
48 DCHECK(!paused_log_.get()); | 50 DCHECK(!paused_log_.get()); |
49 paused_log_.reset(current_log_.release()); | 51 paused_log_.reset(current_log_.release()); |
50 } | 52 } |
51 | 53 |
52 void MetricsLogManager::ResumePausedLog() { | 54 void MetricsLogManager::ResumePausedLog() { |
53 DCHECK(!current_log_.get()); | 55 DCHECK(!current_log_.get()); |
54 current_log_.reset(paused_log_.release()); | 56 current_log_.reset(paused_log_.release()); |
55 } | 57 } |
56 | 58 |
57 void MetricsLogManager::StoreStagedLogAsUnsent(LogType log_type) { | 59 void MetricsLogManager::StoreStagedLogAsUnsent(LogType log_type) { |
58 DCHECK(has_staged_log()); | 60 DCHECK(has_staged_log()); |
61 | |
59 // If compressing the log failed, there's nothing to store. | 62 // If compressing the log failed, there's nothing to store. |
60 if (compressed_staged_log_text_.empty()) | 63 DCHECK_EQ(staged_log_text_xml().empty(), |
64 staged_log_text_proto().empty()); | |
65 if (staged_log_text_xml().empty()) | |
61 return; | 66 return; |
62 | 67 |
63 if (log_type == INITIAL_LOG) { | 68 if (log_type == INITIAL_LOG) { |
64 unsent_initial_logs_.push_back(compressed_staged_log_text_); | 69 unsent_initial_logs_.push_back(staged_log_text_); |
65 } else { | 70 } else { |
66 // If it's too large, just note that and discard it. | 71 // If it's too large, just note that and discard it. |
67 if (max_ongoing_log_store_size_ && | 72 if (max_ongoing_log_store_size_ && |
68 compressed_staged_log_text_.length() > max_ongoing_log_store_size_) { | 73 staged_log_text_xml().length() > max_ongoing_log_store_size_) { |
74 // TODO(isherman): We probably want a similar check for protobufs, but we | |
75 // don't want to prevent XML upload just because the protobuf version is | |
76 // too long. In practice, I'm pretty sure the XML version should always | |
77 // be longer, or at least on the same order of magnitude in length. | |
Ilya Sherman
2012/01/28 08:17:20
Reviewers, does this seem fine?
| |
69 UMA_HISTOGRAM_COUNTS( | 78 UMA_HISTOGRAM_COUNTS( |
70 "UMA.Large Accumulated Log Not Persisted", | 79 "UMA.Large Accumulated Log Not Persisted", |
71 static_cast<int>(compressed_staged_log_text_.length())); | 80 static_cast<int>(staged_log_text_xml().length())); |
72 } else { | 81 } else { |
73 unsent_ongoing_logs_.push_back(compressed_staged_log_text_); | 82 unsent_ongoing_logs_.push_back(staged_log_text_); |
74 } | 83 } |
75 } | 84 } |
76 DiscardStagedLog(); | 85 DiscardStagedLog(); |
77 } | 86 } |
78 | 87 |
79 void MetricsLogManager::StageNextStoredLogForUpload() { | 88 void MetricsLogManager::StageNextStoredLogForUpload() { |
80 // Prioritize initial logs for uploading. | 89 // Prioritize initial logs for uploading. |
81 std::vector<std::string>* source_list = unsent_initial_logs_.empty() ? | 90 std::vector<std::pair<std::string, std::string> >* source_list = |
82 &unsent_ongoing_logs_ : &unsent_initial_logs_; | 91 unsent_initial_logs_.empty() ? |
92 &unsent_ongoing_logs_ : | |
93 &unsent_initial_logs_; | |
83 DCHECK(!source_list->empty()); | 94 DCHECK(!source_list->empty()); |
84 DCHECK(compressed_staged_log_text_.empty()); | 95 DCHECK(staged_log_text_xml().empty()); |
85 compressed_staged_log_text_ = source_list->back(); | 96 DCHECK(staged_log_text_proto().empty()); |
97 staged_log_text_ = source_list->back(); | |
86 source_list->pop_back(); | 98 source_list->pop_back(); |
87 } | 99 } |
88 | 100 |
89 void MetricsLogManager::PersistUnsentLogs() { | 101 void MetricsLogManager::PersistUnsentLogs() { |
90 DCHECK(log_serializer_.get()); | 102 DCHECK(log_serializer_.get()); |
91 if (!log_serializer_.get()) | 103 if (!log_serializer_.get()) |
92 return; | 104 return; |
93 log_serializer_->SerializeLogs(unsent_initial_logs_, INITIAL_LOG); | 105 log_serializer_->SerializeLogs(unsent_initial_logs_, INITIAL_LOG); |
94 log_serializer_->SerializeLogs(unsent_ongoing_logs_, ONGOING_LOG); | 106 log_serializer_->SerializeLogs(unsent_ongoing_logs_, ONGOING_LOG); |
95 } | 107 } |
96 | 108 |
97 void MetricsLogManager::LoadPersistedUnsentLogs() { | 109 void MetricsLogManager::LoadPersistedUnsentLogs() { |
98 DCHECK(log_serializer_.get()); | 110 DCHECK(log_serializer_.get()); |
99 if (!log_serializer_.get()) | 111 if (!log_serializer_.get()) |
100 return; | 112 return; |
101 log_serializer_->DeserializeLogs(INITIAL_LOG, &unsent_initial_logs_); | 113 log_serializer_->DeserializeLogs(INITIAL_LOG, &unsent_initial_logs_); |
102 log_serializer_->DeserializeLogs(ONGOING_LOG, &unsent_ongoing_logs_); | 114 log_serializer_->DeserializeLogs(ONGOING_LOG, &unsent_ongoing_logs_); |
103 } | 115 } |
104 | 116 |
105 void MetricsLogManager::CompressStagedLog() { | 117 void MetricsLogManager::CompressStagedLog() { |
106 int text_size = staged_log_->GetEncodedLogSize(); | 118 int text_size = staged_log_->GetEncodedLogSizeXml(); |
107 std::string staged_log_text; | 119 std::string staged_log_text; |
108 DCHECK_GT(text_size, 0); | 120 DCHECK_GT(text_size, 0); |
109 staged_log_->GetEncodedLog(WriteInto(&staged_log_text, text_size + 1), | 121 staged_log_->GetEncodedLogXml(WriteInto(&staged_log_text, text_size + 1), |
110 text_size); | 122 text_size); |
111 | 123 |
112 bool success = Bzip2Compress(staged_log_text, &compressed_staged_log_text_); | 124 bool success = Bzip2Compress(staged_log_text, &staged_log_text_.first); |
113 if (success) { | 125 if (success) { |
114 // Allow security-conscious users to see all metrics logs that we send. | 126 // Allow security-conscious users to see all metrics logs that we send. |
115 DVLOG(1) << "METRICS LOG: " << staged_log_text; | 127 DVLOG(1) << "METRICS LOG: " << staged_log_text; |
128 | |
129 // Note that we only save the protobuf version if we succeeded in | |
130 // compressing the XML, so that the two data streams are the same. | |
131 staged_log_text_.second = staged_log_->GetEncodedLogProto(); | |
116 } else { | 132 } else { |
117 NOTREACHED() << "Failed to compress log for transmission."; | 133 NOTREACHED() << "Failed to compress log for transmission."; |
118 } | 134 } |
119 } | 135 } |
120 | 136 |
121 // static | 137 // static |
122 // This implementation is based on the Firefox MetricsService implementation. | 138 // This implementation is based on the Firefox MetricsService implementation. |
123 bool MetricsLogManager::Bzip2Compress(const std::string& input, | 139 bool MetricsLogManager::Bzip2Compress(const std::string& input, |
124 std::string* output) { | 140 std::string* output) { |
125 bz_stream stream = {0}; | 141 bz_stream stream = {0}; |
(...skipping 27 matching lines...) Expand all Loading... | |
153 // TODO(jar): See if it would be better to do a CHECK() here. | 169 // TODO(jar): See if it would be better to do a CHECK() here. |
154 return false; | 170 return false; |
155 } | 171 } |
156 result = BZ2_bzCompressEnd(&stream); | 172 result = BZ2_bzCompressEnd(&stream); |
157 DCHECK(result == BZ_OK); | 173 DCHECK(result == BZ_OK); |
158 | 174 |
159 output->resize(stream.total_out_lo32); | 175 output->resize(stream.total_out_lo32); |
160 | 176 |
161 return true; | 177 return true; |
162 } | 178 } |
OLD | NEW |