| 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) | |
| 8 #include <bzlib.h> | |
| 9 #else | |
| 10 #include "third_party/bzip2/bzlib.h" | |
| 11 #endif | |
| 12 | |
| 13 #include <algorithm> | 7 #include <algorithm> |
| 14 | 8 |
| 15 #include "base/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
| 16 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| 11 #include "base/strings/string_compress.h" |
| 17 #include "chrome/common/metrics/metrics_log_base.h" | 12 #include "chrome/common/metrics/metrics_log_base.h" |
| 18 | 13 |
| 19 namespace { | 14 namespace { |
| 20 | 15 |
| 21 // Used to keep track of discarded protobuf logs without having to track xml and | 16 // Used to keep track of discarded protobuf logs without having to track xml and |
| 22 // protobuf logs in separate lists. | 17 // protobuf logs in separate lists. |
| 23 const char kDiscardedLog[] = "Log discarded"; | 18 const char kDiscardedLog[] = "Log discarded"; |
| 24 | 19 |
| 25 } // anonymous namespace | 20 } // anonymous namespace |
| 26 | 21 |
| (...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 226 log_serializer_->DeserializeLogs(ONGOING_LOG, &unsent_ongoing_logs_); | 221 log_serializer_->DeserializeLogs(ONGOING_LOG, &unsent_ongoing_logs_); |
| 227 } | 222 } |
| 228 | 223 |
| 229 void MetricsLogManager::CompressCurrentLog(SerializedLog* compressed_log) { | 224 void MetricsLogManager::CompressCurrentLog(SerializedLog* compressed_log) { |
| 230 int text_size = current_log_->GetEncodedLogSizeXml(); | 225 int text_size = current_log_->GetEncodedLogSizeXml(); |
| 231 DCHECK_GT(text_size, 0); | 226 DCHECK_GT(text_size, 0); |
| 232 std::string log_text; | 227 std::string log_text; |
| 233 current_log_->GetEncodedLogXml(WriteInto(&log_text, text_size + 1), | 228 current_log_->GetEncodedLogXml(WriteInto(&log_text, text_size + 1), |
| 234 text_size); | 229 text_size); |
| 235 | 230 |
| 236 bool success = Bzip2Compress(log_text, &(compressed_log->xml)); | 231 bool success = base::Bzip2Compress(log_text, &(compressed_log->xml)); |
| 237 if (success) { | 232 if (success) { |
| 238 // Allow security-conscious users to see all metrics logs that we send. | 233 // Allow security-conscious users to see all metrics logs that we send. |
| 239 DVLOG(1) << "METRICS LOG: " << log_text; | 234 DVLOG(1) << "METRICS LOG: " << log_text; |
| 240 | 235 |
| 241 // Note that we only save the protobuf version if we succeeded in | 236 // Note that we only save the protobuf version if we succeeded in |
| 242 // compressing the XML, so that the two data streams are the same. | 237 // compressing the XML, so that the two data streams are the same. |
| 243 current_log_->GetEncodedLogProto(&(compressed_log->proto)); | 238 current_log_->GetEncodedLogProto(&(compressed_log->proto)); |
| 244 } else { | 239 } else { |
| 245 NOTREACHED() << "Failed to compress log for transmission."; | 240 NOTREACHED() << "Failed to compress log for transmission."; |
| 246 } | 241 } |
| 247 } | 242 } |
| 248 | |
| 249 // static | |
| 250 // This implementation is based on the Firefox MetricsService implementation. | |
| 251 bool MetricsLogManager::Bzip2Compress(const std::string& input, | |
| 252 std::string* output) { | |
| 253 bz_stream stream = {0}; | |
| 254 // As long as our input is smaller than the bzip2 block size, we should get | |
| 255 // the best compression. For example, if your input was 250k, using a block | |
| 256 // size of 300k or 500k should result in the same compression ratio. Since | |
| 257 // our data should be under 100k, using the minimum block size of 100k should | |
| 258 // allocate less temporary memory, but result in the same compression ratio. | |
| 259 int result = BZ2_bzCompressInit(&stream, | |
| 260 1, // 100k (min) block size | |
| 261 0, // quiet | |
| 262 0); // default "work factor" | |
| 263 if (result != BZ_OK) { // out of memory? | |
| 264 return false; | |
| 265 } | |
| 266 | |
| 267 output->clear(); | |
| 268 | |
| 269 stream.next_in = const_cast<char*>(input.data()); | |
| 270 stream.avail_in = static_cast<int>(input.size()); | |
| 271 // NOTE: we don't need a BZ_RUN phase since our input buffer contains | |
| 272 // the entire input | |
| 273 do { | |
| 274 output->resize(output->size() + 1024); | |
| 275 stream.next_out = &((*output)[stream.total_out_lo32]); | |
| 276 stream.avail_out = static_cast<int>(output->size()) - stream.total_out_lo32; | |
| 277 result = BZ2_bzCompress(&stream, BZ_FINISH); | |
| 278 } while (result == BZ_FINISH_OK); | |
| 279 if (result != BZ_STREAM_END) { // unknown failure? | |
| 280 output->clear(); | |
| 281 // TODO(jar): See if it would be better to do a CHECK() here. | |
| 282 return false; | |
| 283 } | |
| 284 result = BZ2_bzCompressEnd(&stream); | |
| 285 DCHECK(result == BZ_OK); | |
| 286 | |
| 287 output->resize(stream.total_out_lo32); | |
| 288 | |
| 289 return true; | |
| 290 } | |
| OLD | NEW |