blob: 8c42b719380e1c7bb556cba3198538680306083d [file] [log] [blame]
[email protected]2a172e42014-02-21 04:06:101// Copyright 2014 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 COMPONENTS_RAPPOR_RAPPOR_SERVICE_H_
6#define COMPONENTS_RAPPOR_RAPPOR_SERVICE_H_
7
avif57136c12015-12-25 23:27:458#include <stdint.h>
9
[email protected]ccb49262014-03-26 04:10:1710#include <map>
[email protected]2a172e42014-02-21 04:06:1011#include <string>
12
holte79705c82014-11-14 23:41:0413#include "base/callback.h"
[email protected]ccb49262014-03-26 04:10:1714#include "base/macros.h"
15#include "base/memory/scoped_ptr.h"
holte585f4662015-06-18 19:13:5216#include "base/threading/thread_checker.h"
[email protected]2a172e42014-02-21 04:06:1017#include "base/timer/timer.h"
holte07637b012014-09-22 19:15:0218#include "components/metrics/daily_event.h"
holteee8e7062014-11-04 22:27:1019#include "components/rappor/rappor_parameters.h"
holtefeb4e552015-04-28 01:05:0120#include "components/rappor/sample.h"
21#include "components/rappor/sampler.h"
[email protected]2a172e42014-02-21 04:06:1022
23class PrefRegistrySimple;
[email protected]ccb49262014-03-26 04:10:1724class PrefService;
25
26namespace net {
27class URLRequestContextGetter;
28}
[email protected]2a172e42014-02-21 04:06:1029
30namespace rappor {
31
holte5a7ed7c2015-01-09 23:52:4632class LogUploaderInterface;
[email protected]ccb49262014-03-26 04:10:1733class RapporMetric;
34class RapporReports;
[email protected]ccb49262014-03-26 04:10:1735
[email protected]2a172e42014-02-21 04:06:1036// This class provides an interface for recording samples for rappor metrics,
37// and periodically generates and uploads reports based on the collected data.
38class RapporService {
39 public:
holte07637b012014-09-22 19:15:0240 // Constructs a RapporService.
41 // Calling code is responsible for ensuring that the lifetime of
42 // |pref_service| is longer than the lifetime of RapporService.
holte79705c82014-11-14 23:41:0443 // |is_incognito_callback| will be called to test if incognito mode is active.
44 RapporService(PrefService* pref_service,
45 const base::Callback<bool(void)> is_incognito_callback);
[email protected]2a172e42014-02-21 04:06:1046 virtual ~RapporService();
47
holte07637b012014-09-22 19:15:0248 // Add an observer for collecting daily metrics.
49 void AddDailyObserver(scoped_ptr<metrics::DailyEvent::Observer> observer);
50
holte5a7ed7c2015-01-09 23:52:4651 // Initializes the rappor service, including loading the cohort and secret
52 // preferences from disk.
53 void Initialize(net::URLRequestContextGetter* context);
54
55 // Updates the settings for metric recording and uploading.
56 // The RapporService must be initialized before this method is called.
holte585f4662015-06-18 19:13:5257 // |recording_groups| should be set of flags, e.g.
58 // UMA_RECORDING_GROUP | SAFEBROWSING_RECORDING_GROUP
59 // If it contains any enabled groups, periodic reports will be
holte5a7ed7c2015-01-09 23:52:4660 // generated and queued for upload.
61 // If |may_upload| is true, reports will be uploaded from the queue.
holte585f4662015-06-18 19:13:5262 void Update(int recording_groups, bool may_upload);
[email protected]2a172e42014-02-21 04:06:1063
holtefeb4e552015-04-28 01:05:0164 // Constructs a Sample object for the caller to record fields in.
csharrisonb50320c32015-11-02 15:59:5765 virtual scoped_ptr<Sample> CreateSample(RapporType);
holtefeb4e552015-04-28 01:05:0166
67 // Records a Sample of rappor metric specified by |metric_name|.
68 //
69 // TODO(holte): Rename RecordSample to RecordString and then rename this
70 // to RecordSample.
71 //
72 // example:
73 // scoped_ptr<Sample> sample = rappor_service->CreateSample(MY_METRIC_TYPE);
74 // sample->SetStringField("Field1", "some string");
75 // sample->SetFlagsValue("Field2", SOME|FLAGS);
76 // rappor_service->RecordSample("MyMetric", sample.Pass());
77 //
78 // This will result in a report setting two metrics "MyMetric.Field1" and
79 // "MyMetric.Field2", and they will both be generated from the same sample,
80 // to allow for correllations to be computed.
csharrisonb50320c32015-11-02 15:59:5781 virtual void RecordSampleObj(const std::string& metric_name,
82 scoped_ptr<Sample> sample);
holtefeb4e552015-04-28 01:05:0183
[email protected]2a172e42014-02-21 04:06:1084 // Records a sample of the rappor metric specified by |metric_name|.
85 // Creates and initializes the metric, if it doesn't yet exist.
mathp7deb9df2015-03-13 14:44:1686 virtual void RecordSample(const std::string& metric_name,
87 RapporType type,
88 const std::string& sample);
[email protected]2a172e42014-02-21 04:06:1089
[email protected]2a172e42014-02-21 04:06:1090 // Registers the names of all of the preferences used by RapporService in the
91 // provided PrefRegistry. This should be called before calling Start().
92 static void RegisterPrefs(PrefRegistrySimple* registry);
93
94 protected:
holteee8e7062014-11-04 22:27:1095 // Initializes the state of the RapporService.
holte5a7ed7c2015-01-09 23:52:4696 void InitializeInternal(scoped_ptr<LogUploaderInterface> uploader,
97 int32_t cohort,
98 const std::string& secret);
99
holte5a7ed7c2015-01-09 23:52:46100 // Cancels the next call to OnLogInterval.
101 virtual void CancelNextLogRotation();
102
103 // Schedules the next call to OnLogInterval.
104 virtual void ScheduleNextLogRotation(base::TimeDelta interval);
105
[email protected]ccb49262014-03-26 04:10:17106 // Logs all of the collected metrics to the reports proto message and clears
holte07637b012014-09-22 19:15:02107 // the internal map. Exposed for tests. Returns true if any metrics were
[email protected]ccb49262014-03-26 04:10:17108 // recorded.
[email protected]2a172e42014-02-21 04:06:10109 bool ExportMetrics(RapporReports* reports);
110
holte5a7ed7c2015-01-09 23:52:46111 private:
[email protected]2a172e42014-02-21 04:06:10112 // Records a sample of the rappor metric specified by |parameters|.
113 // Creates and initializes the metric, if it doesn't yet exist.
114 // Exposed for tests.
115 void RecordSampleInternal(const std::string& metric_name,
116 const RapporParameters& parameters,
117 const std::string& sample);
118
holte5a7ed7c2015-01-09 23:52:46119 // Checks if the service has been started successfully.
[email protected]2a172e42014-02-21 04:06:10120 bool IsInitialized() const;
121
[email protected]2a172e42014-02-21 04:06:10122 // Called whenever the logging interval elapses to generate a new log of
123 // reports and pass it to the uploader.
124 void OnLogInterval();
125
holte585f4662015-06-18 19:13:52126 // Check if recording of the metric is allowed, given it's parameters.
127 // This will check that we are not in incognito mode, and that the
128 // appropriate recording group is enabled.
129 bool RecordingAllowed(const RapporParameters& parameters);
130
[email protected]2a172e42014-02-21 04:06:10131 // Finds a metric in the metrics_map_, creating it if it doesn't already
132 // exist.
133 RapporMetric* LookUpMetric(const std::string& metric_name,
134 const RapporParameters& parameters);
135
holte07637b012014-09-22 19:15:02136 // A weak pointer to the PrefService used to read and write preferences.
137 PrefService* pref_service_;
138
holte79705c82014-11-14 23:41:04139 // A callback for testing if incognito mode is active;
140 const base::Callback<bool(void)> is_incognito_callback_;
141
[email protected]2a172e42014-02-21 04:06:10142 // Client-side secret used to generate fake bits.
143 std::string secret_;
144
145 // The cohort this client is assigned to. -1 is uninitialized.
146 int32_t cohort_;
147
[email protected]ccb49262014-03-26 04:10:17148 // Timer which schedules calls to OnLogInterval().
danakj8c3eb802015-09-24 07:53:00149 base::OneShotTimer log_rotation_timer_;
[email protected]2a172e42014-02-21 04:06:10150
holte07637b012014-09-22 19:15:02151 // A daily event for collecting metrics once a day.
152 metrics::DailyEvent daily_event_;
153
[email protected]2a172e42014-02-21 04:06:10154 // A private LogUploader instance for sending reports to the server.
holte5a7ed7c2015-01-09 23:52:46155 scoped_ptr<LogUploaderInterface> uploader_;
[email protected]2a172e42014-02-21 04:06:10156
holte585f4662015-06-18 19:13:52157 // The set of recording groups that metrics are being recorded, e.g.
158 // UMA_RECORDING_GROUP | SAFEBROWSING_RECORDING_GROUP
159 int recording_groups_;
holteee8e7062014-11-04 22:27:10160
[email protected]2a172e42014-02-21 04:06:10161 // We keep all registered metrics in a map, from name to metric.
162 // The map owns the metrics it contains.
163 std::map<std::string, RapporMetric*> metrics_map_;
164
holtefeb4e552015-04-28 01:05:01165 internal::Sampler sampler_;
166
holte585f4662015-06-18 19:13:52167 base::ThreadChecker thread_checker_;
168
[email protected]2a172e42014-02-21 04:06:10169 DISALLOW_COPY_AND_ASSIGN(RapporService);
170};
171
172} // namespace rappor
173
174#endif // COMPONENTS_RAPPOR_RAPPOR_SERVICE_H_