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