blob: 564c7f52df8343e2955b29a60968eb98711013e0 [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
[email protected]ccb49262014-03-26 04:10:178#include <map>
[email protected]2a172e42014-02-21 04:06:109#include <string>
10
11#include "base/basictypes.h"
holte79705c82014-11-14 23:41:0412#include "base/callback.h"
[email protected]ccb49262014-03-26 04:10:1713#include "base/macros.h"
14#include "base/memory/scoped_ptr.h"
[email protected]2a172e42014-02-21 04:06:1015#include "base/timer/timer.h"
holte07637b012014-09-22 19:15:0216#include "components/metrics/daily_event.h"
holteee8e7062014-11-04 22:27:1017#include "components/rappor/rappor_parameters.h"
holtefeb4e552015-04-28 01:05:0118#include "components/rappor/sample.h"
19#include "components/rappor/sampler.h"
[email protected]2a172e42014-02-21 04:06:1020
21class PrefRegistrySimple;
[email protected]ccb49262014-03-26 04:10:1722class PrefService;
23
24namespace net {
25class URLRequestContextGetter;
26}
[email protected]2a172e42014-02-21 04:06:1027
28namespace rappor {
29
holte5a7ed7c2015-01-09 23:52:4630class LogUploaderInterface;
[email protected]ccb49262014-03-26 04:10:1731class RapporMetric;
32class RapporReports;
[email protected]ccb49262014-03-26 04:10:1733
[email protected]2a172e42014-02-21 04:06:1034// The type of data stored in a metric.
35enum RapporType {
[email protected]ccb49262014-03-26 04:10:1736 // For sampling the eTLD+1 of a URL.
[email protected]2a172e42014-02-21 04:06:1037 ETLD_PLUS_ONE_RAPPOR_TYPE = 0,
holteee8e7062014-11-04 22:27:1038 COARSE_RAPPOR_TYPE,
[email protected]2a172e42014-02-21 04:06:1039 NUM_RAPPOR_TYPES
40};
41
42// This class provides an interface for recording samples for rappor metrics,
43// and periodically generates and uploads reports based on the collected data.
44class RapporService {
45 public:
holte07637b012014-09-22 19:15:0246 // Constructs a RapporService.
47 // Calling code is responsible for ensuring that the lifetime of
48 // |pref_service| is longer than the lifetime of RapporService.
holte79705c82014-11-14 23:41:0449 // |is_incognito_callback| will be called to test if incognito mode is active.
50 RapporService(PrefService* pref_service,
51 const base::Callback<bool(void)> is_incognito_callback);
[email protected]2a172e42014-02-21 04:06:1052 virtual ~RapporService();
53
holte07637b012014-09-22 19:15:0254 // Add an observer for collecting daily metrics.
55 void AddDailyObserver(scoped_ptr<metrics::DailyEvent::Observer> observer);
56
holte5a7ed7c2015-01-09 23:52:4657 // Initializes the rappor service, including loading the cohort and secret
58 // preferences from disk.
59 void Initialize(net::URLRequestContextGetter* context);
60
61 // Updates the settings for metric recording and uploading.
62 // The RapporService must be initialized before this method is called.
63 // If |recording_level| > REPORTING_DISABLED, periodic reports will be
64 // generated and queued for upload.
65 // If |may_upload| is true, reports will be uploaded from the queue.
66 void Update(RecordingLevel recording_level, bool may_upload);
[email protected]2a172e42014-02-21 04:06:1067
holtefeb4e552015-04-28 01:05:0168 // Constructs a Sample object for the caller to record fields in.
69 scoped_ptr<Sample> CreateSample(RapporType);
70
71 // Records a Sample of rappor metric specified by |metric_name|.
72 //
73 // TODO(holte): Rename RecordSample to RecordString and then rename this
74 // to RecordSample.
75 //
76 // example:
77 // scoped_ptr<Sample> sample = rappor_service->CreateSample(MY_METRIC_TYPE);
78 // sample->SetStringField("Field1", "some string");
79 // sample->SetFlagsValue("Field2", SOME|FLAGS);
80 // rappor_service->RecordSample("MyMetric", sample.Pass());
81 //
82 // This will result in a report setting two metrics "MyMetric.Field1" and
83 // "MyMetric.Field2", and they will both be generated from the same sample,
84 // to allow for correllations to be computed.
85 void RecordSampleObj(const std::string& metric_name,
86 scoped_ptr<Sample> sample);
87
[email protected]2a172e42014-02-21 04:06:1088 // Records a sample of the rappor metric specified by |metric_name|.
89 // Creates and initializes the metric, if it doesn't yet exist.
mathp7deb9df2015-03-13 14:44:1690 virtual void RecordSample(const std::string& metric_name,
91 RapporType type,
92 const std::string& sample);
[email protected]2a172e42014-02-21 04:06:1093
[email protected]2a172e42014-02-21 04:06:1094 // Registers the names of all of the preferences used by RapporService in the
95 // provided PrefRegistry. This should be called before calling Start().
96 static void RegisterPrefs(PrefRegistrySimple* registry);
97
98 protected:
holteee8e7062014-11-04 22:27:1099 // Initializes the state of the RapporService.
holte5a7ed7c2015-01-09 23:52:46100 void InitializeInternal(scoped_ptr<LogUploaderInterface> uploader,
101 int32_t cohort,
102 const std::string& secret);
103
104 // Sets the recording level.
105 void SetRecordingLevel(RecordingLevel parameters);
holteee8e7062014-11-04 22:27:10106
holte5a7ed7c2015-01-09 23:52:46107 // Cancels the next call to OnLogInterval.
108 virtual void CancelNextLogRotation();
109
110 // Schedules the next call to OnLogInterval.
111 virtual void ScheduleNextLogRotation(base::TimeDelta interval);
112
[email protected]ccb49262014-03-26 04:10:17113 // Logs all of the collected metrics to the reports proto message and clears
holte07637b012014-09-22 19:15:02114 // the internal map. Exposed for tests. Returns true if any metrics were
[email protected]ccb49262014-03-26 04:10:17115 // recorded.
[email protected]2a172e42014-02-21 04:06:10116 bool ExportMetrics(RapporReports* reports);
117
holte5a7ed7c2015-01-09 23:52:46118 private:
[email protected]2a172e42014-02-21 04:06:10119 // Records a sample of the rappor metric specified by |parameters|.
120 // Creates and initializes the metric, if it doesn't yet exist.
121 // Exposed for tests.
122 void RecordSampleInternal(const std::string& metric_name,
123 const RapporParameters& parameters,
124 const std::string& sample);
125
holte5a7ed7c2015-01-09 23:52:46126 // Checks if the service has been started successfully.
[email protected]2a172e42014-02-21 04:06:10127 bool IsInitialized() const;
128
[email protected]2a172e42014-02-21 04:06:10129 // Called whenever the logging interval elapses to generate a new log of
130 // reports and pass it to the uploader.
131 void OnLogInterval();
132
133 // Finds a metric in the metrics_map_, creating it if it doesn't already
134 // exist.
135 RapporMetric* LookUpMetric(const std::string& metric_name,
136 const RapporParameters& parameters);
137
holte07637b012014-09-22 19:15:02138 // A weak pointer to the PrefService used to read and write preferences.
139 PrefService* pref_service_;
140
holte79705c82014-11-14 23:41:04141 // A callback for testing if incognito mode is active;
142 const base::Callback<bool(void)> is_incognito_callback_;
143
[email protected]2a172e42014-02-21 04:06:10144 // Client-side secret used to generate fake bits.
145 std::string secret_;
146
147 // The cohort this client is assigned to. -1 is uninitialized.
148 int32_t cohort_;
149
[email protected]ccb49262014-03-26 04:10:17150 // Timer which schedules calls to OnLogInterval().
[email protected]2a172e42014-02-21 04:06:10151 base::OneShotTimer<RapporService> log_rotation_timer_;
152
holte07637b012014-09-22 19:15:02153 // A daily event for collecting metrics once a day.
154 metrics::DailyEvent daily_event_;
155
[email protected]2a172e42014-02-21 04:06:10156 // A private LogUploader instance for sending reports to the server.
holte5a7ed7c2015-01-09 23:52:46157 scoped_ptr<LogUploaderInterface> uploader_;
[email protected]2a172e42014-02-21 04:06:10158
holteee8e7062014-11-04 22:27:10159 // What reporting level of metrics are being reported.
holte5a7ed7c2015-01-09 23:52:46160 RecordingLevel recording_level_;
holteee8e7062014-11-04 22:27:10161
[email protected]2a172e42014-02-21 04:06:10162 // We keep all registered metrics in a map, from name to metric.
163 // The map owns the metrics it contains.
164 std::map<std::string, RapporMetric*> metrics_map_;
165
holtefeb4e552015-04-28 01:05:01166 internal::Sampler sampler_;
167
[email protected]2a172e42014-02-21 04:06:10168 DISALLOW_COPY_AND_ASSIGN(RapporService);
169};
170
171} // namespace rappor
172
173#endif // COMPONENTS_RAPPOR_RAPPOR_SERVICE_H_