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