| 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 #ifndef CHROME_BROWSER_METRICS_METRICS_LOG_SERIALIZER_H_ | 5 #ifndef CHROME_BROWSER_METRICS_METRICS_LOG_SERIALIZER_H_ |
| 6 #define CHROME_BROWSER_METRICS_METRICS_LOG_SERIALIZER_H_ | 6 #define CHROME_BROWSER_METRICS_METRICS_LOG_SERIALIZER_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "base/gtest_prod_util.h" | 10 #include "base/gtest_prod_util.h" |
| 11 #include "chrome/common/metrics/metrics_log_manager.h" | 11 #include "chrome/common/metrics/metrics_log_manager.h" |
| 12 | 12 |
| 13 namespace base { | 13 namespace base { |
| 14 class ListValue; | 14 class ListValue; |
| 15 } | 15 } |
| 16 | 16 |
| 17 // Serializer for persisting metrics logs to prefs. | 17 // Serializer for persisting metrics logs to prefs. |
| 18 class MetricsLogSerializer : public MetricsLogManager::LogSerializer { | 18 class MetricsLogSerializer : public MetricsLogManager::LogSerializer { |
| 19 public: | 19 public: |
| 20 // Used to produce a historgram that keeps track of the status of recalling | 20 // Used to produce a histogram that keeps track of the status of recalling |
| 21 // persisted per logs. | 21 // persisted per logs. |
| 22 enum LogReadStatus { | 22 enum LogReadStatus { |
| 23 RECALL_SUCCESS, // We were able to correctly recall a persisted log. | 23 RECALL_SUCCESS, // We were able to correctly recall a persisted log. |
| 24 LIST_EMPTY, // Attempting to recall from an empty list. | 24 LIST_EMPTY, // Attempting to recall from an empty list. |
| 25 LIST_SIZE_MISSING, // Failed to recover list size using GetAsInteger(). | 25 LIST_SIZE_MISSING, // Failed to recover list size using GetAsInteger(). |
| 26 LIST_SIZE_TOO_SMALL, // Too few elements in the list (less than 3). | 26 LIST_SIZE_TOO_SMALL, // Too few elements in the list (less than 3). |
| 27 LIST_SIZE_CORRUPTION, // List size is not as expected. | 27 LIST_SIZE_CORRUPTION, // List size is not as expected. |
| 28 LOG_STRING_CORRUPTION, // Failed to recover log string using GetAsString(). | 28 LOG_STRING_CORRUPTION, // Failed to recover log string using GetAsString(). |
| 29 CHECKSUM_CORRUPTION, // Failed to verify checksum. | 29 CHECKSUM_CORRUPTION, // Failed to verify checksum. |
| 30 CHECKSUM_STRING_CORRUPTION, // Failed to recover checksum string using | 30 CHECKSUM_STRING_CORRUPTION, // Failed to recover checksum string using |
| 31 // GetAsString(). | 31 // GetAsString(). |
| 32 DECODE_FAIL, // Failed to decode log. | 32 DECODE_FAIL, // Failed to decode log. |
| 33 XML_PROTO_MISMATCH, // The XML and protobuf logs have inconsistent data. |
| 33 END_RECALL_STATUS // Number of bins to use to create the histogram. | 34 END_RECALL_STATUS // Number of bins to use to create the histogram. |
| 34 }; | 35 }; |
| 35 | 36 |
| 36 MetricsLogSerializer(); | 37 MetricsLogSerializer(); |
| 37 virtual ~MetricsLogSerializer(); | 38 virtual ~MetricsLogSerializer(); |
| 38 | 39 |
| 39 // Implementation of MetricsLogManager::LogSerializer | 40 // Implementation of MetricsLogManager::LogSerializer |
| 40 virtual void SerializeLogs(const std::vector<std::string>& logs, | 41 virtual void SerializeLogs( |
| 41 MetricsLogManager::LogType log_type) OVERRIDE; | 42 const std::vector<MetricsLogManager::SerializedLog>& logs, |
| 42 virtual void DeserializeLogs(MetricsLogManager::LogType log_type, | 43 MetricsLogManager::LogType log_type) OVERRIDE; |
| 43 std::vector<std::string>* logs) OVERRIDE; | 44 virtual void DeserializeLogs( |
| 45 MetricsLogManager::LogType log_type, |
| 46 std::vector<MetricsLogManager::SerializedLog>* logs) OVERRIDE; |
| 44 | 47 |
| 45 private: | 48 private: |
| 46 // Encodes the textual log data from |local_list| and writes it to the given | 49 // Encodes the textual log data from |local_list| and writes it to the given |
| 47 // pref list, along with list size and checksum. | 50 // pref list, along with list size and checksum. If |is_xml| is true, writes |
| 48 static void WriteLogsToPrefList(const std::vector<std::string>& local_list, | 51 // the XML data from |local_list|; otherwise writes the protobuf data. |
| 49 const size_t kMaxLocalListSize, | 52 static void WriteLogsToPrefList( |
| 50 base::ListValue* list); | 53 const std::vector<MetricsLogManager::SerializedLog>& local_list, |
| 54 bool is_xml, |
| 55 size_t max_list_size, |
| 56 base::ListValue* list); |
| 51 | 57 |
| 52 // Decodes and verifies the textual log data from |list|, populating | 58 // Decodes and verifies the textual log data from |list|, populating |
| 53 // |local_list| and returning a status code. | 59 // |local_list| and returning a status code. If |is_xml| is true, populates |
| 60 // the XML data in |local_list|; otherwise populates the protobuf data. |
| 54 static LogReadStatus ReadLogsFromPrefList( | 61 static LogReadStatus ReadLogsFromPrefList( |
| 55 const base::ListValue& list, | 62 const base::ListValue& list, |
| 56 std::vector<std::string>* local_list); | 63 bool is_xml, |
| 64 std::vector<MetricsLogManager::SerializedLog>* local_list); |
| 57 | 65 |
| 58 FRIEND_TEST_ALL_PREFIXES(MetricsLogSerializerTest, EmptyLogList); | 66 FRIEND_TEST_ALL_PREFIXES(MetricsLogSerializerTest, EmptyLogList); |
| 59 FRIEND_TEST_ALL_PREFIXES(MetricsLogSerializerTest, SingleElementLogList); | 67 FRIEND_TEST_ALL_PREFIXES(MetricsLogSerializerTest, SingleElementLogList); |
| 60 FRIEND_TEST_ALL_PREFIXES(MetricsLogSerializerTest, OverLimitLogList); | 68 FRIEND_TEST_ALL_PREFIXES(MetricsLogSerializerTest, OverLimitLogList); |
| 61 FRIEND_TEST_ALL_PREFIXES(MetricsLogSerializerTest, SmallRecoveredListSize); | 69 FRIEND_TEST_ALL_PREFIXES(MetricsLogSerializerTest, SmallRecoveredListSize); |
| 62 FRIEND_TEST_ALL_PREFIXES(MetricsLogSerializerTest, RemoveSizeFromLogList); | 70 FRIEND_TEST_ALL_PREFIXES(MetricsLogSerializerTest, RemoveSizeFromLogList); |
| 63 FRIEND_TEST_ALL_PREFIXES(MetricsLogSerializerTest, CorruptSizeOfLogList); | 71 FRIEND_TEST_ALL_PREFIXES(MetricsLogSerializerTest, CorruptSizeOfLogList); |
| 64 FRIEND_TEST_ALL_PREFIXES(MetricsLogSerializerTest, CorruptChecksumOfLogList); | 72 FRIEND_TEST_ALL_PREFIXES(MetricsLogSerializerTest, CorruptChecksumOfLogList); |
| 65 | 73 |
| 66 DISALLOW_COPY_AND_ASSIGN(MetricsLogSerializer); | 74 DISALLOW_COPY_AND_ASSIGN(MetricsLogSerializer); |
| 67 }; | 75 }; |
| 68 | 76 |
| 69 #endif // CHROME_BROWSER_METRICS_METRICS_LOG_SERIALIZER_H_ | 77 #endif // CHROME_BROWSER_METRICS_METRICS_LOG_SERIALIZER_H_ |
| OLD | NEW |