blob: 540eb44a0d295b76ef53de05cfee25925f37fc8b [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"
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"
nzolghadrd87a308d2016-12-07 15:45:5619#include "components/rappor/public/rappor_parameters.h"
20#include "components/rappor/public/rappor_service.h"
21#include "components/rappor/public/sample.h"
holtefeb4e552015-04-28 01:05:0122#include "components/rappor/sampler.h"
[email protected]2a172e42014-02-21 04:06:1023
24class PrefRegistrySimple;
[email protected]ccb49262014-03-26 04:10:1725class PrefService;
26
27namespace net {
28class URLRequestContextGetter;
29}
[email protected]2a172e42014-02-21 04:06:1030
31namespace rappor {
32
holte5a7ed7c2015-01-09 23:52:4633class LogUploaderInterface;
[email protected]ccb49262014-03-26 04:10:1734class RapporMetric;
35class RapporReports;
[email protected]ccb49262014-03-26 04:10:1736
[email protected]2a172e42014-02-21 04:06:1037// This class provides an interface for recording samples for rappor metrics,
38// and periodically generates and uploads reports based on the collected data.
nzolghadrd87a308d2016-12-07 15:45:5639class RapporServiceImpl : public RapporService {
[email protected]2a172e42014-02-21 04:06:1040 public:
nzolghadrd87a308d2016-12-07 15:45:5641 // Constructs a RapporServiceImpl.
holte07637b012014-09-22 19:15:0242 // Calling code is responsible for ensuring that the lifetime of
nzolghadrd87a308d2016-12-07 15:45:5643 // |pref_service| is longer than the lifetime of RapporServiceImpl.
holte79705c82014-11-14 23:41:0444 // |is_incognito_callback| will be called to test if incognito mode is active.
nzolghadrd87a308d2016-12-07 15:45:5645 RapporServiceImpl(PrefService* pref_service,
46 const base::Callback<bool(void)> is_incognito_callback);
47 virtual ~RapporServiceImpl();
[email protected]2a172e42014-02-21 04:06:1048
holte07637b012014-09-22 19:15:0249 // Add an observer for collecting daily metrics.
dcheng82beb4f2016-04-26 00:35:0250 void AddDailyObserver(
51 std::unique_ptr<metrics::DailyEvent::Observer> observer);
holte07637b012014-09-22 19:15:0252
holte5a7ed7c2015-01-09 23:52:4653 // 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.
nzolghadrd87a308d2016-12-07 15:45:5658 // The RapporServiceImpl must be initialized before this method is called.
holte0036d9b2017-03-15 21:49:1659 // If |may_record| is true, data will be recorded and periodic reports will
60 // be generated and queued for upload.
holte5a7ed7c2015-01-09 23:52:4661 // If |may_upload| is true, reports will be uploaded from the queue.
holte0036d9b2017-03-15 21:49:1662 void Update(bool may_record, 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.
nzolghadrd87a308d2016-12-07 15:45:5665 std::unique_ptr<Sample> CreateSample(RapporType) override;
holtefeb4e552015-04-28 01:05:0166
67 // Records a Sample of rappor metric specified by |metric_name|.
68 //
holtefeb4e552015-04-28 01:05:0169 // example:
dcheng82beb4f2016-04-26 00:35:0270 // std::unique_ptr<Sample> sample =
71 // rappor_service->CreateSample(MY_METRIC_TYPE);
holtefeb4e552015-04-28 01:05:0172 // sample->SetStringField("Field1", "some string");
73 // sample->SetFlagsValue("Field2", SOME|FLAGS);
dcheng7061e5f2016-03-04 01:21:4774 // rappor_service->RecordSample("MyMetric", std::move(sample));
holtefeb4e552015-04-28 01:05:0175 //
76 // This will result in a report setting two metrics "MyMetric.Field1" and
77 // "MyMetric.Field2", and they will both be generated from the same sample,
avi0a1f54b2016-10-25 21:09:3878 // to allow for correlations to be computed.
nzolghadrd87a308d2016-12-07 15:45:5679 void RecordSample(const std::string& metric_name,
80 std::unique_ptr<Sample> sample) override;
holtefeb4e552015-04-28 01:05:0181
[email protected]2a172e42014-02-21 04:06:1082 // Records a sample of the rappor metric specified by |metric_name|.
83 // Creates and initializes the metric, if it doesn't yet exist.
nzolghadrd87a308d2016-12-07 15:45:5684 void RecordSampleString(const std::string& metric_name,
85 RapporType type,
86 const std::string& sample) override;
[email protected]2a172e42014-02-21 04:06:1087
nzolghadrd87a308d2016-12-07 15:45:5688 // Registers the names of all of the preferences used by RapporServiceImpl in
89 // the provided PrefRegistry. This should be called before calling Start().
[email protected]2a172e42014-02-21 04:06:1090 static void RegisterPrefs(PrefRegistrySimple* registry);
91
92 protected:
nzolghadrd87a308d2016-12-07 15:45:5693 // Initializes the state of the RapporServiceImpl.
dcheng82beb4f2016-04-26 00:35:0294 void InitializeInternal(std::unique_ptr<LogUploaderInterface> uploader,
holte5a7ed7c2015-01-09 23:52:4695 int32_t cohort,
96 const std::string& secret);
97
holte5a7ed7c2015-01-09 23:52:4698 // Cancels the next call to OnLogInterval.
99 virtual void CancelNextLogRotation();
100
101 // Schedules the next call to OnLogInterval.
102 virtual void ScheduleNextLogRotation(base::TimeDelta interval);
103
[email protected]ccb49262014-03-26 04:10:17104 // Logs all of the collected metrics to the reports proto message and clears
holte07637b012014-09-22 19:15:02105 // the internal map. Exposed for tests. Returns true if any metrics were
[email protected]ccb49262014-03-26 04:10:17106 // recorded.
[email protected]2a172e42014-02-21 04:06:10107 bool ExportMetrics(RapporReports* reports);
108
holte5a7ed7c2015-01-09 23:52:46109 private:
[email protected]2a172e42014-02-21 04:06:10110 // Records a sample of the rappor metric specified by |parameters|.
111 // Creates and initializes the metric, if it doesn't yet exist.
112 // Exposed for tests.
113 void RecordSampleInternal(const std::string& metric_name,
114 const RapporParameters& parameters,
115 const std::string& sample);
116
holte5a7ed7c2015-01-09 23:52:46117 // Checks if the service has been started successfully.
[email protected]2a172e42014-02-21 04:06:10118 bool IsInitialized() const;
119
[email protected]2a172e42014-02-21 04:06:10120 // Called whenever the logging interval elapses to generate a new log of
121 // reports and pass it to the uploader.
122 void OnLogInterval();
123
holte585f4662015-06-18 19:13:52124 // Check if recording of the metric is allowed, given it's parameters.
125 // This will check that we are not in incognito mode, and that the
126 // appropriate recording group is enabled.
127 bool RecordingAllowed(const RapporParameters& parameters);
128
[email protected]2a172e42014-02-21 04:06:10129 // Finds a metric in the metrics_map_, creating it if it doesn't already
130 // exist.
131 RapporMetric* LookUpMetric(const std::string& metric_name,
132 const RapporParameters& parameters);
133
holte07637b012014-09-22 19:15:02134 // A weak pointer to the PrefService used to read and write preferences.
135 PrefService* pref_service_;
136
holte79705c82014-11-14 23:41:04137 // A callback for testing if incognito mode is active;
138 const base::Callback<bool(void)> is_incognito_callback_;
139
[email protected]2a172e42014-02-21 04:06:10140 // Client-side secret used to generate fake bits.
141 std::string secret_;
142
143 // The cohort this client is assigned to. -1 is uninitialized.
144 int32_t cohort_;
145
[email protected]ccb49262014-03-26 04:10:17146 // Timer which schedules calls to OnLogInterval().
danakj8c3eb802015-09-24 07:53:00147 base::OneShotTimer log_rotation_timer_;
[email protected]2a172e42014-02-21 04:06:10148
holte07637b012014-09-22 19:15:02149 // A daily event for collecting metrics once a day.
150 metrics::DailyEvent daily_event_;
151
[email protected]2a172e42014-02-21 04:06:10152 // A private LogUploader instance for sending reports to the server.
dcheng82beb4f2016-04-26 00:35:02153 std::unique_ptr<LogUploaderInterface> uploader_;
[email protected]2a172e42014-02-21 04:06:10154
holte0036d9b2017-03-15 21:49:16155 // Whether new data can be recorded.
156 bool recording_enabled_;
holteee8e7062014-11-04 22:27:10157
[email protected]2a172e42014-02-21 04:06:10158 // We keep all registered metrics in a map, from name to metric.
avi0a1f54b2016-10-25 21:09:38159 std::map<std::string, std::unique_ptr<RapporMetric>> metrics_map_;
[email protected]2a172e42014-02-21 04:06:10160
holtefeb4e552015-04-28 01:05:01161 internal::Sampler sampler_;
162
holte585f4662015-06-18 19:13:52163 base::ThreadChecker thread_checker_;
164
nzolghadrd87a308d2016-12-07 15:45:56165 DISALLOW_COPY_AND_ASSIGN(RapporServiceImpl);
[email protected]2a172e42014-02-21 04:06:10166};
167
168} // namespace rappor
169
nzolghadrd87a308d2016-12-07 15:45:56170#endif // COMPONENTS_RAPPOR_RAPPOR_SERVICE_IMPL_H_