blob: 11c91f89f9d4c7f80cb5bfb9e611ae6e189bf74b [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
nzolghadrd87a308d2016-12-07 15:45:565#ifndef COMPONENTS_RAPPOR_RAPPOR_SERVICE_IMPL_H_
6#define COMPONENTS_RAPPOR_RAPPOR_SERVICE_IMPL_H_
[email protected]2a172e42014-02-21 04:06:107
avif57136c12015-12-25 23:27:458#include <stdint.h>
9
[email protected]ccb49262014-03-26 04:10:1710#include <map>
dcheng82beb4f2016-04-26 00:35:0211#include <memory>
[email protected]2a172e42014-02-21 04:06:1012#include <string>
13
holte79705c82014-11-14 23:41:0414#include "base/callback.h"
[email protected]ccb49262014-03-26 04:10:1715#include "base/macros.h"
Mark Pilgrimedc1d15632018-05-22 17:38:2316#include "base/memory/scoped_refptr.h"
holte585f4662015-06-18 19:13:5217#include "base/threading/thread_checker.h"
[email protected]2a172e42014-02-21 04:06:1018#include "base/timer/timer.h"
holte07637b012014-09-22 19:15:0219#include "components/metrics/daily_event.h"
nzolghadrd87a308d2016-12-07 15:45:5620#include "components/rappor/public/rappor_parameters.h"
21#include "components/rappor/public/rappor_service.h"
22#include "components/rappor/public/sample.h"
holtefeb4e552015-04-28 01:05:0123#include "components/rappor/sampler.h"
[email protected]2a172e42014-02-21 04:06:1024
25class PrefRegistrySimple;
[email protected]ccb49262014-03-26 04:10:1726class PrefService;
27
Mark Pilgrimedc1d15632018-05-22 17:38:2328namespace network {
29class SharedURLLoaderFactory;
[email protected]ccb49262014-03-26 04:10:1730}
[email protected]2a172e42014-02-21 04:06:1031
32namespace rappor {
33
holte5a7ed7c2015-01-09 23:52:4634class LogUploaderInterface;
[email protected]ccb49262014-03-26 04:10:1735class RapporMetric;
36class RapporReports;
[email protected]ccb49262014-03-26 04:10:1737
[email protected]2a172e42014-02-21 04:06:1038// This class provides an interface for recording samples for rappor metrics,
39// and periodically generates and uploads reports based on the collected data.
nzolghadrd87a308d2016-12-07 15:45:5640class RapporServiceImpl : public RapporService {
[email protected]2a172e42014-02-21 04:06:1041 public:
nzolghadrd87a308d2016-12-07 15:45:5642 // Constructs a RapporServiceImpl.
holte07637b012014-09-22 19:15:0243 // Calling code is responsible for ensuring that the lifetime of
nzolghadrd87a308d2016-12-07 15:45:5644 // |pref_service| is longer than the lifetime of RapporServiceImpl.
holte79705c82014-11-14 23:41:0445 // |is_incognito_callback| will be called to test if incognito mode is active.
nzolghadrd87a308d2016-12-07 15:45:5646 RapporServiceImpl(PrefService* pref_service,
47 const base::Callback<bool(void)> is_incognito_callback);
48 virtual ~RapporServiceImpl();
[email protected]2a172e42014-02-21 04:06:1049
holte07637b012014-09-22 19:15:0250 // Add an observer for collecting daily metrics.
dcheng82beb4f2016-04-26 00:35:0251 void AddDailyObserver(
52 std::unique_ptr<metrics::DailyEvent::Observer> observer);
holte07637b012014-09-22 19:15:0253
holte5a7ed7c2015-01-09 23:52:4654 // Initializes the rappor service, including loading the cohort and secret
55 // preferences from disk.
Mark Pilgrimedc1d15632018-05-22 17:38:2356 void Initialize(
57 scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory);
holte5a7ed7c2015-01-09 23:52:4658
59 // Updates the settings for metric recording and uploading.
nzolghadrd87a308d2016-12-07 15:45:5660 // The RapporServiceImpl must be initialized before this method is called.
holte0036d9b2017-03-15 21:49:1661 // If |may_record| is true, data will be recorded and periodic reports will
62 // be generated and queued for upload.
holte5a7ed7c2015-01-09 23:52:4663 // If |may_upload| is true, reports will be uploaded from the queue.
holte0036d9b2017-03-15 21:49:1664 void Update(bool may_record, bool may_upload);
[email protected]2a172e42014-02-21 04:06:1065
holtefeb4e552015-04-28 01:05:0166 // Constructs a Sample object for the caller to record fields in.
nzolghadrd87a308d2016-12-07 15:45:5667 std::unique_ptr<Sample> CreateSample(RapporType) override;
holtefeb4e552015-04-28 01:05:0168
69 // Records a Sample of rappor metric specified by |metric_name|.
70 //
holtefeb4e552015-04-28 01:05:0171 // example:
dcheng82beb4f2016-04-26 00:35:0272 // std::unique_ptr<Sample> sample =
73 // rappor_service->CreateSample(MY_METRIC_TYPE);
holtefeb4e552015-04-28 01:05:0174 // sample->SetStringField("Field1", "some string");
75 // sample->SetFlagsValue("Field2", SOME|FLAGS);
dcheng7061e5f2016-03-04 01:21:4776 // rappor_service->RecordSample("MyMetric", std::move(sample));
holtefeb4e552015-04-28 01:05:0177 //
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,
avi0a1f54b2016-10-25 21:09:3880 // to allow for correlations to be computed.
nzolghadrd87a308d2016-12-07 15:45:5681 void RecordSample(const std::string& metric_name,
82 std::unique_ptr<Sample> sample) override;
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.
nzolghadrd87a308d2016-12-07 15:45:5686 void RecordSampleString(const std::string& metric_name,
87 RapporType type,
88 const std::string& sample) override;
[email protected]2a172e42014-02-21 04:06:1089
nzolghadrd87a308d2016-12-07 15:45:5690 // Registers the names of all of the preferences used by RapporServiceImpl in
91 // the provided PrefRegistry. This should be called before calling Start().
[email protected]2a172e42014-02-21 04:06:1092 static void RegisterPrefs(PrefRegistrySimple* registry);
93
94 protected:
nzolghadrd87a308d2016-12-07 15:45:5695 // Initializes the state of the RapporServiceImpl.
dcheng82beb4f2016-04-26 00:35:0296 void InitializeInternal(std::unique_ptr<LogUploaderInterface> uploader,
holte5a7ed7c2015-01-09 23:52:4697 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.
dcheng82beb4f2016-04-26 00:35:02155 std::unique_ptr<LogUploaderInterface> uploader_;
[email protected]2a172e42014-02-21 04:06:10156
holte0036d9b2017-03-15 21:49:16157 // Whether new data can be recorded.
158 bool recording_enabled_;
holteee8e7062014-11-04 22:27:10159
[email protected]2a172e42014-02-21 04:06:10160 // We keep all registered metrics in a map, from name to metric.
avi0a1f54b2016-10-25 21:09:38161 std::map<std::string, std::unique_ptr<RapporMetric>> metrics_map_;
[email protected]2a172e42014-02-21 04:06:10162
holtefeb4e552015-04-28 01:05:01163 internal::Sampler sampler_;
164
holte585f4662015-06-18 19:13:52165 base::ThreadChecker thread_checker_;
166
nzolghadrd87a308d2016-12-07 15:45:56167 DISALLOW_COPY_AND_ASSIGN(RapporServiceImpl);
[email protected]2a172e42014-02-21 04:06:10168};
169
170} // namespace rappor
171
nzolghadrd87a308d2016-12-07 15:45:56172#endif // COMPONENTS_RAPPOR_RAPPOR_SERVICE_IMPL_H_