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