Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(322)

Side by Side Diff: components/rappor/rappor_service.h

Issue 2510803003: Pass RapporService to content/browser/ (Closed)
Patch Set: Fix more compile errors in JNI files Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // 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
8 #include <stdint.h>
9
10 #include <map>
11 #include <memory>
12 #include <string>
13
14 #include "base/callback.h"
15 #include "base/macros.h"
16 #include "base/memory/weak_ptr.h"
17 #include "base/threading/thread_checker.h"
18 #include "base/timer/timer.h"
19 #include "components/metrics/daily_event.h"
20 #include "components/rappor/rappor_parameters.h"
21 #include "components/rappor/sample.h"
22 #include "components/rappor/sampler.h"
23
24 class PrefRegistrySimple;
25 class PrefService;
26
27 namespace net {
28 class URLRequestContextGetter;
29 }
30
31 namespace rappor {
32
33 class LogUploaderInterface;
34 class RapporMetric;
35 class RapporReports;
36
37 // This class provides an interface for recording samples for rappor metrics,
38 // and periodically generates and uploads reports based on the collected data.
39 class RapporService : public base::SupportsWeakPtr<RapporService> {
40 public:
41 // Constructs a RapporService.
42 // Calling code is responsible for ensuring that the lifetime of
43 // |pref_service| is longer than the lifetime of RapporService.
44 // |is_incognito_callback| will be called to test if incognito mode is active.
45 RapporService(PrefService* pref_service,
46 const base::Callback<bool(void)> is_incognito_callback);
47 virtual ~RapporService();
48
49 // Add an observer for collecting daily metrics.
50 void AddDailyObserver(
51 std::unique_ptr<metrics::DailyEvent::Observer> observer);
52
53 // Initializes the rappor service, including loading the cohort and secret
54 // preferences from disk.
55 void Initialize(net::URLRequestContextGetter* context);
56
57 // Updates the settings for metric recording and uploading.
58 // The RapporService must be initialized before this method is called.
59 // |recording_groups| should be set of flags, e.g.
60 // UMA_RECORDING_GROUP | SAFEBROWSING_RECORDING_GROUP
61 // If it contains any enabled groups, periodic reports will be
62 // generated and queued for upload.
63 // If |may_upload| is true, reports will be uploaded from the queue.
64 void Update(int recording_groups, bool may_upload);
65
66 // Constructs a Sample object for the caller to record fields in.
67 virtual std::unique_ptr<Sample> CreateSample(RapporType);
68
69 // Records a Sample of rappor metric specified by |metric_name|.
70 //
71 // TODO(holte): Rename RecordSample to RecordString and then rename this
72 // to RecordSample.
73 //
74 // example:
75 // std::unique_ptr<Sample> sample =
76 // rappor_service->CreateSample(MY_METRIC_TYPE);
77 // sample->SetStringField("Field1", "some string");
78 // sample->SetFlagsValue("Field2", SOME|FLAGS);
79 // rappor_service->RecordSample("MyMetric", std::move(sample));
80 //
81 // This will result in a report setting two metrics "MyMetric.Field1" and
82 // "MyMetric.Field2", and they will both be generated from the same sample,
83 // to allow for correlations to be computed.
84 virtual void RecordSampleObj(const std::string& metric_name,
85 std::unique_ptr<Sample> sample);
86
87 // Records a sample of the rappor metric specified by |metric_name|.
88 // Creates and initializes the metric, if it doesn't yet exist.
89 virtual void RecordSample(const std::string& metric_name,
90 RapporType type,
91 const std::string& sample);
92
93 // Registers the names of all of the preferences used by RapporService in the
94 // provided PrefRegistry. This should be called before calling Start().
95 static void RegisterPrefs(PrefRegistrySimple* registry);
96
97 protected:
98 // Initializes the state of the RapporService.
99 void InitializeInternal(std::unique_ptr<LogUploaderInterface> uploader,
100 int32_t cohort,
101 const std::string& secret);
102
103 // Cancels the next call to OnLogInterval.
104 virtual void CancelNextLogRotation();
105
106 // Schedules the next call to OnLogInterval.
107 virtual void ScheduleNextLogRotation(base::TimeDelta interval);
108
109 // Logs all of the collected metrics to the reports proto message and clears
110 // the internal map. Exposed for tests. Returns true if any metrics were
111 // recorded.
112 bool ExportMetrics(RapporReports* reports);
113
114 private:
115 // Records a sample of the rappor metric specified by |parameters|.
116 // Creates and initializes the metric, if it doesn't yet exist.
117 // Exposed for tests.
118 void RecordSampleInternal(const std::string& metric_name,
119 const RapporParameters& parameters,
120 const std::string& sample);
121
122 // Checks if the service has been started successfully.
123 bool IsInitialized() const;
124
125 // Called whenever the logging interval elapses to generate a new log of
126 // reports and pass it to the uploader.
127 void OnLogInterval();
128
129 // Check if recording of the metric is allowed, given it's parameters.
130 // This will check that we are not in incognito mode, and that the
131 // appropriate recording group is enabled.
132 bool RecordingAllowed(const RapporParameters& parameters);
133
134 // Finds a metric in the metrics_map_, creating it if it doesn't already
135 // exist.
136 RapporMetric* LookUpMetric(const std::string& metric_name,
137 const RapporParameters& parameters);
138
139 // A weak pointer to the PrefService used to read and write preferences.
140 PrefService* pref_service_;
141
142 // A callback for testing if incognito mode is active;
143 const base::Callback<bool(void)> is_incognito_callback_;
144
145 // Client-side secret used to generate fake bits.
146 std::string secret_;
147
148 // The cohort this client is assigned to. -1 is uninitialized.
149 int32_t cohort_;
150
151 // Timer which schedules calls to OnLogInterval().
152 base::OneShotTimer log_rotation_timer_;
153
154 // A daily event for collecting metrics once a day.
155 metrics::DailyEvent daily_event_;
156
157 // A private LogUploader instance for sending reports to the server.
158 std::unique_ptr<LogUploaderInterface> uploader_;
159
160 // The set of recording groups that metrics are being recorded, e.g.
161 // UMA_RECORDING_GROUP | SAFEBROWSING_RECORDING_GROUP
162 int recording_groups_;
163
164 // We keep all registered metrics in a map, from name to metric.
165 std::map<std::string, std::unique_ptr<RapporMetric>> metrics_map_;
166
167 internal::Sampler sampler_;
168
169 base::ThreadChecker thread_checker_;
170
171 DISALLOW_COPY_AND_ASSIGN(RapporService);
172 };
173
174 } // namespace rappor
175
176 #endif // COMPONENTS_RAPPOR_RAPPOR_SERVICE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698