summaryrefslogtreecommitdiffstats
path: root/chrome/browser/metrics/metrics_log_serializer.h
blob: 9a1e630415015ac855101cc519c9a675da161096 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_METRICS_METRICS_LOG_SERIALIZER_H_
#define CHROME_BROWSER_METRICS_METRICS_LOG_SERIALIZER_H_
#pragma once

#include "base/basictypes.h"
#include "base/gtest_prod_util.h"
#include "chrome/common/metrics/metrics_log_manager.h"

namespace base {
class ListValue;
}

// Serializer for persisting metrics logs to prefs.
class MetricsLogSerializer : public MetricsLogManager::LogSerializer {
 public:
  // Used to produce a histogram that keeps track of the status of recalling
  // persisted per logs.
  enum LogReadStatus {
    RECALL_SUCCESS,         // We were able to correctly recall a persisted log.
    LIST_EMPTY,             // Attempting to recall from an empty list.
    LIST_SIZE_MISSING,      // Failed to recover list size using GetAsInteger().
    LIST_SIZE_TOO_SMALL,    // Too few elements in the list (less than 3).
    LIST_SIZE_CORRUPTION,   // List size is not as expected.
    LOG_STRING_CORRUPTION,  // Failed to recover log string using GetAsString().
    CHECKSUM_CORRUPTION,    // Failed to verify checksum.
    CHECKSUM_STRING_CORRUPTION,  // Failed to recover checksum string using
                                 // GetAsString().
    DECODE_FAIL,            // Failed to decode log.
    XML_PROTO_MISMATCH,     // The XML and protobuf logs have inconsistent data.
    END_RECALL_STATUS       // Number of bins to use to create the histogram.
  };

  MetricsLogSerializer();
  virtual ~MetricsLogSerializer();

  // Implementation of MetricsLogManager::LogSerializer
  virtual void SerializeLogs(
      const std::vector<MetricsLogManager::SerializedLog>& logs,
      MetricsLogManager::LogType log_type) OVERRIDE;
  virtual void DeserializeLogs(
      MetricsLogManager::LogType log_type,
      std::vector<MetricsLogManager::SerializedLog>* logs) OVERRIDE;

 private:
  // Encodes the textual log data from |local_list| and writes it to the given
  // pref list, along with list size and checksum.  If |is_xml| is true, writes
  // the XML data from |local_list|; otherwise writes the protobuf data.
  static void WriteLogsToPrefList(
      const std::vector<MetricsLogManager::SerializedLog>& local_list,
      bool is_xml,
      size_t max_list_size,
      base::ListValue* list);

  // Decodes and verifies the textual log data from |list|, populating
  // |local_list| and returning a status code.  If |is_xml| is true, populates
  // the XML data in |local_list|; otherwise populates the protobuf data.
  static LogReadStatus ReadLogsFromPrefList(
      const base::ListValue& list,
      bool is_xml,
      std::vector<MetricsLogManager::SerializedLog>* local_list);

  FRIEND_TEST_ALL_PREFIXES(MetricsLogSerializerTest, EmptyLogList);
  FRIEND_TEST_ALL_PREFIXES(MetricsLogSerializerTest, SingleElementLogList);
  FRIEND_TEST_ALL_PREFIXES(MetricsLogSerializerTest, OverLimitLogList);
  FRIEND_TEST_ALL_PREFIXES(MetricsLogSerializerTest, SmallRecoveredListSize);
  FRIEND_TEST_ALL_PREFIXES(MetricsLogSerializerTest, RemoveSizeFromLogList);
  FRIEND_TEST_ALL_PREFIXES(MetricsLogSerializerTest, CorruptSizeOfLogList);
  FRIEND_TEST_ALL_PREFIXES(MetricsLogSerializerTest, CorruptChecksumOfLogList);

  DISALLOW_COPY_AND_ASSIGN(MetricsLogSerializer);
};

#endif  // CHROME_BROWSER_METRICS_METRICS_LOG_SERIALIZER_H_