Give names to all utility processes.
[chromium-blink-merge.git] / components / rappor / rappor_service.h
blobd50b91e9ca2edda4878a7314e5344b94e16e6c7d
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.
5 #ifndef COMPONENTS_RAPPOR_RAPPOR_SERVICE_H_
6 #define COMPONENTS_RAPPOR_RAPPOR_SERVICE_H_
8 #include <map>
9 #include <string>
11 #include "base/basictypes.h"
12 #include "base/callback.h"
13 #include "base/macros.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/timer/timer.h"
16 #include "components/metrics/daily_event.h"
17 #include "components/rappor/rappor_parameters.h"
19 class PrefRegistrySimple;
20 class PrefService;
22 namespace net {
23 class URLRequestContextGetter;
26 namespace rappor {
28 class LogUploaderInterface;
29 class RapporMetric;
30 class RapporReports;
32 // The type of data stored in a metric.
33 enum RapporType {
34 // For sampling the eTLD+1 of a URL.
35 ETLD_PLUS_ONE_RAPPOR_TYPE = 0,
36 COARSE_RAPPOR_TYPE,
37 NUM_RAPPOR_TYPES
40 // This class provides an interface for recording samples for rappor metrics,
41 // and periodically generates and uploads reports based on the collected data.
42 class RapporService {
43 public:
44 // Constructs a RapporService.
45 // Calling code is responsible for ensuring that the lifetime of
46 // |pref_service| is longer than the lifetime of RapporService.
47 // |is_incognito_callback| will be called to test if incognito mode is active.
48 RapporService(PrefService* pref_service,
49 const base::Callback<bool(void)> is_incognito_callback);
50 virtual ~RapporService();
52 // Add an observer for collecting daily metrics.
53 void AddDailyObserver(scoped_ptr<metrics::DailyEvent::Observer> observer);
55 // Initializes the rappor service, including loading the cohort and secret
56 // preferences from disk.
57 void Initialize(net::URLRequestContextGetter* context);
59 // Updates the settings for metric recording and uploading.
60 // The RapporService must be initialized before this method is called.
61 // If |recording_level| > REPORTING_DISABLED, 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(RecordingLevel recording_level, bool may_upload);
66 // Records a sample of the rappor metric specified by |metric_name|.
67 // Creates and initializes the metric, if it doesn't yet exist.
68 virtual void RecordSample(const std::string& metric_name,
69 RapporType type,
70 const std::string& sample);
72 // Registers the names of all of the preferences used by RapporService in the
73 // provided PrefRegistry. This should be called before calling Start().
74 static void RegisterPrefs(PrefRegistrySimple* registry);
76 protected:
77 // Initializes the state of the RapporService.
78 void InitializeInternal(scoped_ptr<LogUploaderInterface> uploader,
79 int32_t cohort,
80 const std::string& secret);
82 // Sets the recording level.
83 void SetRecordingLevel(RecordingLevel parameters);
85 // Cancels the next call to OnLogInterval.
86 virtual void CancelNextLogRotation();
88 // Schedules the next call to OnLogInterval.
89 virtual void ScheduleNextLogRotation(base::TimeDelta interval);
91 // Logs all of the collected metrics to the reports proto message and clears
92 // the internal map. Exposed for tests. Returns true if any metrics were
93 // recorded.
94 bool ExportMetrics(RapporReports* reports);
96 private:
97 // Records a sample of the rappor metric specified by |parameters|.
98 // Creates and initializes the metric, if it doesn't yet exist.
99 // Exposed for tests.
100 void RecordSampleInternal(const std::string& metric_name,
101 const RapporParameters& parameters,
102 const std::string& sample);
104 // Checks if the service has been started successfully.
105 bool IsInitialized() const;
107 // Called whenever the logging interval elapses to generate a new log of
108 // reports and pass it to the uploader.
109 void OnLogInterval();
111 // Finds a metric in the metrics_map_, creating it if it doesn't already
112 // exist.
113 RapporMetric* LookUpMetric(const std::string& metric_name,
114 const RapporParameters& parameters);
116 // A weak pointer to the PrefService used to read and write preferences.
117 PrefService* pref_service_;
119 // A callback for testing if incognito mode is active;
120 const base::Callback<bool(void)> is_incognito_callback_;
122 // Client-side secret used to generate fake bits.
123 std::string secret_;
125 // The cohort this client is assigned to. -1 is uninitialized.
126 int32_t cohort_;
128 // Timer which schedules calls to OnLogInterval().
129 base::OneShotTimer<RapporService> log_rotation_timer_;
131 // A daily event for collecting metrics once a day.
132 metrics::DailyEvent daily_event_;
134 // A private LogUploader instance for sending reports to the server.
135 scoped_ptr<LogUploaderInterface> uploader_;
137 // What reporting level of metrics are being reported.
138 RecordingLevel recording_level_;
140 // We keep all registered metrics in a map, from name to metric.
141 // The map owns the metrics it contains.
142 std::map<std::string, RapporMetric*> metrics_map_;
144 DISALLOW_COPY_AND_ASSIGN(RapporService);
147 } // namespace rappor
149 #endif // COMPONENTS_RAPPOR_RAPPOR_SERVICE_H_