summaryrefslogtreecommitdiff
path: root/chromium/components/rappor/sample.h
blob: 3f143f3f1b0e1bafe0f06febae298fc55296e947 (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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Copyright 2015 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 COMPONENTS_RAPPOR_SAMPLE_H_
#define COMPONENTS_RAPPOR_SAMPLE_H_

#include <stddef.h>
#include <stdint.h>

#include <map>
#include <string>

#include "base/macros.h"
#include "components/rappor/rappor_parameters.h"

namespace rappor {

class RapporReports;
class RapporService;
class TestSamplerFactory;

// Sample is a container for information about a single instance of some event
// we are sending Rappor data about.  It may contain multiple different fields,
// which describe different details of the event, and they will be sent in the
// same Rappor report, enabling analysis of correlations between those fields.
class Sample {
 public:
  virtual ~Sample();

  // Sets a string value field in this sample.
  virtual void SetStringField(const std::string& field_name,
                              const std::string& value);

  // TODO(holte): Move all callers to the version with NoiseLevel.
  virtual void SetFlagsField(const std::string& field_name,
                             uint64_t flags,
                             size_t num_flags);

  // Sets a group of boolean flags as a field in this sample, with the
  // specified noise level.
  // |flags| should be a set of boolean flags stored in the lowest |num_flags|
  // bits of |flags|.
  virtual void SetFlagsField(const std::string& field_name,
                             uint64_t flags,
                             size_t num_flags,
                             NoiseLevel noise_level);

  // Sets an integer value field in this sample, at the given noise level.
  virtual void SetUInt64Field(const std::string& field_name,
                              uint64_t value,
                              NoiseLevel noise_level);

  // Generate randomized reports and store them in |reports|.
  virtual void ExportMetrics(const std::string& secret,
                             const std::string& metric_name,
                             RapporReports* reports) const;

  const RapporParameters& parameters() { return parameters_; }

 private:
  friend class TestSamplerFactory;
  friend class RapporService;
  friend class TestSample;

  // Constructs a sample.  Instead of calling this directly, call
  // RapporService::MakeSampleObj to create a sample.
  Sample(int32_t cohort_seed, const RapporParameters& parameters);

  const RapporParameters parameters_;

  // Offset used for bloom filter hash functions.
  uint32_t bloom_offset_;

  struct FieldInfo {
    // Size of the field, in bytes.
    size_t size;
    // The non-randomized report value for the field.
    uint64_t value;
    // The noise level to use when creating a report for the field.
    NoiseLevel noise_level;
  };

  // Information about all recorded fields.
  std::map<std::string, FieldInfo> field_info_;

  DISALLOW_COPY_AND_ASSIGN(Sample);
};

}  // namespace rappor

#endif  // COMPONENTS_RAPPOR_SAMPLE_H_