[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 | |
| 5 | #ifndef COMPONENTS_RAPPOR_RAPPOR_SERVICE_H_ |
| 6 | #define COMPONENTS_RAPPOR_RAPPOR_SERVICE_H_ |
| 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> |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 11 | #include <string> |
| 12 | |
holte | 79705c8 | 2014-11-14 23:41:04 | [diff] [blame] | 13 | #include "base/callback.h" |
[email protected] | ccb4926 | 2014-03-26 04:10:17 | [diff] [blame] | 14 | #include "base/macros.h" |
| 15 | #include "base/memory/scoped_ptr.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" |
holte | ee8e706 | 2014-11-04 22:27:10 | [diff] [blame] | 19 | #include "components/rappor/rappor_parameters.h" |
holte | feb4e55 | 2015-04-28 01:05:01 | [diff] [blame] | 20 | #include "components/rappor/sample.h" |
| 21 | #include "components/rappor/sampler.h" |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 22 | |
| 23 | class PrefRegistrySimple; |
[email protected] | ccb4926 | 2014-03-26 04:10:17 | [diff] [blame] | 24 | class PrefService; |
| 25 | |
| 26 | namespace net { |
| 27 | class URLRequestContextGetter; |
| 28 | } |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 29 | |
| 30 | namespace rappor { |
| 31 | |
holte | 5a7ed7c | 2015-01-09 23:52:46 | [diff] [blame] | 32 | class LogUploaderInterface; |
[email protected] | ccb4926 | 2014-03-26 04:10:17 | [diff] [blame] | 33 | class RapporMetric; |
| 34 | class RapporReports; |
[email protected] | ccb4926 | 2014-03-26 04:10:17 | [diff] [blame] | 35 | |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 36 | // This class provides an interface for recording samples for rappor metrics, |
| 37 | // and periodically generates and uploads reports based on the collected data. |
| 38 | class RapporService { |
| 39 | public: |
holte | 07637b01 | 2014-09-22 19:15:02 | [diff] [blame] | 40 | // Constructs a RapporService. |
| 41 | // Calling code is responsible for ensuring that the lifetime of |
| 42 | // |pref_service| is longer than the lifetime of RapporService. |
holte | 79705c8 | 2014-11-14 23:41:04 | [diff] [blame] | 43 | // |is_incognito_callback| will be called to test if incognito mode is active. |
| 44 | RapporService(PrefService* pref_service, |
| 45 | const base::Callback<bool(void)> is_incognito_callback); |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 46 | virtual ~RapporService(); |
| 47 | |
holte | 07637b01 | 2014-09-22 19:15:02 | [diff] [blame] | 48 | // Add an observer for collecting daily metrics. |
| 49 | void AddDailyObserver(scoped_ptr<metrics::DailyEvent::Observer> observer); |
| 50 | |
holte | 5a7ed7c | 2015-01-09 23:52:46 | [diff] [blame] | 51 | // Initializes the rappor service, including loading the cohort and secret |
| 52 | // preferences from disk. |
| 53 | void Initialize(net::URLRequestContextGetter* context); |
| 54 | |
| 55 | // Updates the settings for metric recording and uploading. |
| 56 | // The RapporService must be initialized before this method is called. |
holte | 585f466 | 2015-06-18 19:13:52 | [diff] [blame] | 57 | // |recording_groups| should be set of flags, e.g. |
| 58 | // UMA_RECORDING_GROUP | SAFEBROWSING_RECORDING_GROUP |
| 59 | // If it contains any enabled groups, periodic reports will be |
holte | 5a7ed7c | 2015-01-09 23:52:46 | [diff] [blame] | 60 | // generated and queued for upload. |
| 61 | // If |may_upload| is true, reports will be uploaded from the queue. |
holte | 585f466 | 2015-06-18 19:13:52 | [diff] [blame] | 62 | void Update(int recording_groups, 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. |
csharrison | b50320c3 | 2015-11-02 15:59:57 | [diff] [blame] | 65 | virtual scoped_ptr<Sample> CreateSample(RapporType); |
holte | feb4e55 | 2015-04-28 01:05:01 | [diff] [blame] | 66 | |
| 67 | // Records a Sample of rappor metric specified by |metric_name|. |
| 68 | // |
| 69 | // TODO(holte): Rename RecordSample to RecordString and then rename this |
| 70 | // to RecordSample. |
| 71 | // |
| 72 | // example: |
| 73 | // scoped_ptr<Sample> sample = rappor_service->CreateSample(MY_METRIC_TYPE); |
| 74 | // sample->SetStringField("Field1", "some string"); |
| 75 | // sample->SetFlagsValue("Field2", SOME|FLAGS); |
| 76 | // rappor_service->RecordSample("MyMetric", sample.Pass()); |
| 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, |
| 80 | // to allow for correllations to be computed. |
csharrison | b50320c3 | 2015-11-02 15:59:57 | [diff] [blame] | 81 | virtual void RecordSampleObj(const std::string& metric_name, |
| 82 | scoped_ptr<Sample> sample); |
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. |
mathp | 7deb9df | 2015-03-13 14:44:16 | [diff] [blame] | 86 | virtual void RecordSample(const std::string& metric_name, |
| 87 | RapporType type, |
| 88 | const std::string& sample); |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 89 | |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 90 | // Registers the names of all of the preferences used by RapporService in the |
| 91 | // provided PrefRegistry. This should be called before calling Start(). |
| 92 | static void RegisterPrefs(PrefRegistrySimple* registry); |
| 93 | |
| 94 | protected: |
holte | ee8e706 | 2014-11-04 22:27:10 | [diff] [blame] | 95 | // Initializes the state of the RapporService. |
holte | 5a7ed7c | 2015-01-09 23:52:46 | [diff] [blame] | 96 | void InitializeInternal(scoped_ptr<LogUploaderInterface> uploader, |
| 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. |
holte | 5a7ed7c | 2015-01-09 23:52:46 | [diff] [blame] | 155 | scoped_ptr<LogUploaderInterface> uploader_; |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 156 | |
holte | 585f466 | 2015-06-18 19:13:52 | [diff] [blame] | 157 | // The set of recording groups that metrics are being recorded, e.g. |
| 158 | // UMA_RECORDING_GROUP | SAFEBROWSING_RECORDING_GROUP |
| 159 | int recording_groups_; |
holte | ee8e706 | 2014-11-04 22:27:10 | [diff] [blame] | 160 | |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 161 | // We keep all registered metrics in a map, from name to metric. |
| 162 | // The map owns the metrics it contains. |
| 163 | std::map<std::string, RapporMetric*> metrics_map_; |
| 164 | |
holte | feb4e55 | 2015-04-28 01:05:01 | [diff] [blame] | 165 | internal::Sampler sampler_; |
| 166 | |
holte | 585f466 | 2015-06-18 19:13:52 | [diff] [blame] | 167 | base::ThreadChecker thread_checker_; |
| 168 | |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 169 | DISALLOW_COPY_AND_ASSIGN(RapporService); |
| 170 | }; |
| 171 | |
| 172 | } // namespace rappor |
| 173 | |
| 174 | #endif // COMPONENTS_RAPPOR_RAPPOR_SERVICE_H_ |