[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 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 | |
nzolghadr | d87a308d | 2016-12-07 15:45:56 | [diff] [blame] | 5 | #ifndef COMPONENTS_RAPPOR_RAPPOR_SERVICE_IMPL_H_ |
| 6 | #define COMPONENTS_RAPPOR_RAPPOR_SERVICE_IMPL_H_ |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 7 | |
avi | f57136c1 | 2015-12-25 23:27:45 | [diff] [blame] | 8 | #include <stdint.h> |
| 9 | |
[email protected] | ccb4926 | 2014-03-26 04:10:17 | [diff] [blame] | 10 | #include <map> |
dcheng | 82beb4f | 2016-04-26 00:35:02 | [diff] [blame] | 11 | #include <memory> |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 12 | #include <string> |
| 13 | |
holte | 79705c8 | 2014-11-14 23:41:04 | [diff] [blame] | 14 | #include "base/callback.h" |
[email protected] | ccb4926 | 2014-03-26 04:10:17 | [diff] [blame] | 15 | #include "base/macros.h" |
holte | 585f466 | 2015-06-18 19:13:52 | [diff] [blame] | 16 | #include "base/threading/thread_checker.h" |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 17 | #include "base/timer/timer.h" |
holte | 07637b01 | 2014-09-22 19:15:02 | [diff] [blame] | 18 | #include "components/metrics/daily_event.h" |
nzolghadr | d87a308d | 2016-12-07 15:45:56 | [diff] [blame] | 19 | #include "components/rappor/public/rappor_parameters.h" |
| 20 | #include "components/rappor/public/rappor_service.h" |
| 21 | #include "components/rappor/public/sample.h" |
holte | feb4e55 | 2015-04-28 01:05:01 | [diff] [blame] | 22 | #include "components/rappor/sampler.h" |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 23 | |
| 24 | class PrefRegistrySimple; |
[email protected] | ccb4926 | 2014-03-26 04:10:17 | [diff] [blame] | 25 | class PrefService; |
| 26 | |
| 27 | namespace net { |
| 28 | class URLRequestContextGetter; |
| 29 | } |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 30 | |
| 31 | namespace rappor { |
| 32 | |
holte | 5a7ed7c | 2015-01-09 23:52:46 | [diff] [blame] | 33 | class LogUploaderInterface; |
[email protected] | ccb4926 | 2014-03-26 04:10:17 | [diff] [blame] | 34 | class RapporMetric; |
| 35 | class RapporReports; |
[email protected] | ccb4926 | 2014-03-26 04:10:17 | [diff] [blame] | 36 | |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 37 | // This class provides an interface for recording samples for rappor metrics, |
| 38 | // and periodically generates and uploads reports based on the collected data. |
nzolghadr | d87a308d | 2016-12-07 15:45:56 | [diff] [blame] | 39 | class RapporServiceImpl : public RapporService { |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 40 | public: |
nzolghadr | d87a308d | 2016-12-07 15:45:56 | [diff] [blame] | 41 | // Constructs a RapporServiceImpl. |
holte | 07637b01 | 2014-09-22 19:15:02 | [diff] [blame] | 42 | // Calling code is responsible for ensuring that the lifetime of |
nzolghadr | d87a308d | 2016-12-07 15:45:56 | [diff] [blame] | 43 | // |pref_service| is longer than the lifetime of RapporServiceImpl. |
holte | 79705c8 | 2014-11-14 23:41:04 | [diff] [blame] | 44 | // |is_incognito_callback| will be called to test if incognito mode is active. |
nzolghadr | d87a308d | 2016-12-07 15:45:56 | [diff] [blame] | 45 | RapporServiceImpl(PrefService* pref_service, |
| 46 | const base::Callback<bool(void)> is_incognito_callback); |
| 47 | virtual ~RapporServiceImpl(); |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 48 | |
holte | 07637b01 | 2014-09-22 19:15:02 | [diff] [blame] | 49 | // Add an observer for collecting daily metrics. |
dcheng | 82beb4f | 2016-04-26 00:35:02 | [diff] [blame] | 50 | void AddDailyObserver( |
| 51 | std::unique_ptr<metrics::DailyEvent::Observer> observer); |
holte | 07637b01 | 2014-09-22 19:15:02 | [diff] [blame] | 52 | |
holte | 5a7ed7c | 2015-01-09 23:52:46 | [diff] [blame] | 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. |
nzolghadr | d87a308d | 2016-12-07 15:45:56 | [diff] [blame] | 58 | // The RapporServiceImpl must be initialized before this method is called. |
holte | 0036d9b | 2017-03-15 21:49:16 | [diff] [blame^] | 59 | // If |may_record| is true, data will be recorded and periodic reports will |
| 60 | // be generated and queued for upload. |
holte | 5a7ed7c | 2015-01-09 23:52:46 | [diff] [blame] | 61 | // If |may_upload| is true, reports will be uploaded from the queue. |
holte | 0036d9b | 2017-03-15 21:49:16 | [diff] [blame^] | 62 | void Update(bool may_record, bool may_upload); |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 63 | |
holte | feb4e55 | 2015-04-28 01:05:01 | [diff] [blame] | 64 | // Constructs a Sample object for the caller to record fields in. |
nzolghadr | d87a308d | 2016-12-07 15:45:56 | [diff] [blame] | 65 | std::unique_ptr<Sample> CreateSample(RapporType) override; |
holte | feb4e55 | 2015-04-28 01:05:01 | [diff] [blame] | 66 | |
| 67 | // Records a Sample of rappor metric specified by |metric_name|. |
| 68 | // |
holte | feb4e55 | 2015-04-28 01:05:01 | [diff] [blame] | 69 | // example: |
dcheng | 82beb4f | 2016-04-26 00:35:02 | [diff] [blame] | 70 | // std::unique_ptr<Sample> sample = |
| 71 | // rappor_service->CreateSample(MY_METRIC_TYPE); |
holte | feb4e55 | 2015-04-28 01:05:01 | [diff] [blame] | 72 | // sample->SetStringField("Field1", "some string"); |
| 73 | // sample->SetFlagsValue("Field2", SOME|FLAGS); |
dcheng | 7061e5f | 2016-03-04 01:21:47 | [diff] [blame] | 74 | // rappor_service->RecordSample("MyMetric", std::move(sample)); |
holte | feb4e55 | 2015-04-28 01:05:01 | [diff] [blame] | 75 | // |
| 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, |
avi | 0a1f54b | 2016-10-25 21:09:38 | [diff] [blame] | 78 | // to allow for correlations to be computed. |
nzolghadr | d87a308d | 2016-12-07 15:45:56 | [diff] [blame] | 79 | void RecordSample(const std::string& metric_name, |
| 80 | std::unique_ptr<Sample> sample) override; |
holte | feb4e55 | 2015-04-28 01:05:01 | [diff] [blame] | 81 | |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 82 | // Records a sample of the rappor metric specified by |metric_name|. |
| 83 | // Creates and initializes the metric, if it doesn't yet exist. |
nzolghadr | d87a308d | 2016-12-07 15:45:56 | [diff] [blame] | 84 | void RecordSampleString(const std::string& metric_name, |
| 85 | RapporType type, |
| 86 | const std::string& sample) override; |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 87 | |
nzolghadr | d87a308d | 2016-12-07 15:45:56 | [diff] [blame] | 88 | // 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] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 90 | static void RegisterPrefs(PrefRegistrySimple* registry); |
| 91 | |
| 92 | protected: |
nzolghadr | d87a308d | 2016-12-07 15:45:56 | [diff] [blame] | 93 | // Initializes the state of the RapporServiceImpl. |
dcheng | 82beb4f | 2016-04-26 00:35:02 | [diff] [blame] | 94 | void InitializeInternal(std::unique_ptr<LogUploaderInterface> uploader, |
holte | 5a7ed7c | 2015-01-09 23:52:46 | [diff] [blame] | 95 | int32_t cohort, |
| 96 | const std::string& secret); |
| 97 | |
holte | 5a7ed7c | 2015-01-09 23:52:46 | [diff] [blame] | 98 | // 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] | ccb4926 | 2014-03-26 04:10:17 | [diff] [blame] | 104 | // Logs all of the collected metrics to the reports proto message and clears |
holte | 07637b01 | 2014-09-22 19:15:02 | [diff] [blame] | 105 | // the internal map. Exposed for tests. Returns true if any metrics were |
[email protected] | ccb4926 | 2014-03-26 04:10:17 | [diff] [blame] | 106 | // recorded. |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 107 | bool ExportMetrics(RapporReports* reports); |
| 108 | |
holte | 5a7ed7c | 2015-01-09 23:52:46 | [diff] [blame] | 109 | private: |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 110 | // 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 | |
holte | 5a7ed7c | 2015-01-09 23:52:46 | [diff] [blame] | 117 | // Checks if the service has been started successfully. |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 118 | bool IsInitialized() const; |
| 119 | |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 120 | // Called whenever the logging interval elapses to generate a new log of |
| 121 | // reports and pass it to the uploader. |
| 122 | void OnLogInterval(); |
| 123 | |
holte | 585f466 | 2015-06-18 19:13:52 | [diff] [blame] | 124 | // 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] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 129 | // 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 | |
holte | 07637b01 | 2014-09-22 19:15:02 | [diff] [blame] | 134 | // A weak pointer to the PrefService used to read and write preferences. |
| 135 | PrefService* pref_service_; |
| 136 | |
holte | 79705c8 | 2014-11-14 23:41:04 | [diff] [blame] | 137 | // A callback for testing if incognito mode is active; |
| 138 | const base::Callback<bool(void)> is_incognito_callback_; |
| 139 | |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 140 | // 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] | ccb4926 | 2014-03-26 04:10:17 | [diff] [blame] | 146 | // Timer which schedules calls to OnLogInterval(). |
danakj | 8c3eb80 | 2015-09-24 07:53:00 | [diff] [blame] | 147 | base::OneShotTimer log_rotation_timer_; |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 148 | |
holte | 07637b01 | 2014-09-22 19:15:02 | [diff] [blame] | 149 | // A daily event for collecting metrics once a day. |
| 150 | metrics::DailyEvent daily_event_; |
| 151 | |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 152 | // A private LogUploader instance for sending reports to the server. |
dcheng | 82beb4f | 2016-04-26 00:35:02 | [diff] [blame] | 153 | std::unique_ptr<LogUploaderInterface> uploader_; |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 154 | |
holte | 0036d9b | 2017-03-15 21:49:16 | [diff] [blame^] | 155 | // Whether new data can be recorded. |
| 156 | bool recording_enabled_; |
holte | ee8e706 | 2014-11-04 22:27:10 | [diff] [blame] | 157 | |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 158 | // We keep all registered metrics in a map, from name to metric. |
avi | 0a1f54b | 2016-10-25 21:09:38 | [diff] [blame] | 159 | std::map<std::string, std::unique_ptr<RapporMetric>> metrics_map_; |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 160 | |
holte | feb4e55 | 2015-04-28 01:05:01 | [diff] [blame] | 161 | internal::Sampler sampler_; |
| 162 | |
holte | 585f466 | 2015-06-18 19:13:52 | [diff] [blame] | 163 | base::ThreadChecker thread_checker_; |
| 164 | |
nzolghadr | d87a308d | 2016-12-07 15:45:56 | [diff] [blame] | 165 | DISALLOW_COPY_AND_ASSIGN(RapporServiceImpl); |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 166 | }; |
| 167 | |
| 168 | } // namespace rappor |
| 169 | |
nzolghadr | d87a308d | 2016-12-07 15:45:56 | [diff] [blame] | 170 | #endif // COMPONENTS_RAPPOR_RAPPOR_SERVICE_IMPL_H_ |