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