[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 | |
[email protected] | ccb4926 | 2014-03-26 04:10:17 | [diff] [blame] | 8 | #include <map> |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 9 | #include <string> |
| 10 | |
| 11 | #include "base/basictypes.h" |
holte | 79705c8 | 2014-11-14 23:41:04 | [diff] [blame] | 12 | #include "base/callback.h" |
[email protected] | ccb4926 | 2014-03-26 04:10:17 | [diff] [blame] | 13 | #include "base/macros.h" |
| 14 | #include "base/memory/scoped_ptr.h" |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 15 | #include "base/timer/timer.h" |
holte | 07637b01 | 2014-09-22 19:15:02 | [diff] [blame] | 16 | #include "components/metrics/daily_event.h" |
holte | ee8e706 | 2014-11-04 22:27:10 | [diff] [blame] | 17 | #include "components/rappor/rappor_parameters.h" |
holte | feb4e55 | 2015-04-28 01:05:01 | [diff] [blame^] | 18 | #include "components/rappor/sample.h" |
| 19 | #include "components/rappor/sampler.h" |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 20 | |
| 21 | class PrefRegistrySimple; |
[email protected] | ccb4926 | 2014-03-26 04:10:17 | [diff] [blame] | 22 | class PrefService; |
| 23 | |
| 24 | namespace net { |
| 25 | class URLRequestContextGetter; |
| 26 | } |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 27 | |
| 28 | namespace rappor { |
| 29 | |
holte | 5a7ed7c | 2015-01-09 23:52:46 | [diff] [blame] | 30 | class LogUploaderInterface; |
[email protected] | ccb4926 | 2014-03-26 04:10:17 | [diff] [blame] | 31 | class RapporMetric; |
| 32 | class RapporReports; |
[email protected] | ccb4926 | 2014-03-26 04:10:17 | [diff] [blame] | 33 | |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 34 | // The type of data stored in a metric. |
| 35 | enum RapporType { |
[email protected] | ccb4926 | 2014-03-26 04:10:17 | [diff] [blame] | 36 | // For sampling the eTLD+1 of a URL. |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 37 | ETLD_PLUS_ONE_RAPPOR_TYPE = 0, |
holte | ee8e706 | 2014-11-04 22:27:10 | [diff] [blame] | 38 | COARSE_RAPPOR_TYPE, |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 39 | NUM_RAPPOR_TYPES |
| 40 | }; |
| 41 | |
| 42 | // This class provides an interface for recording samples for rappor metrics, |
| 43 | // and periodically generates and uploads reports based on the collected data. |
| 44 | class RapporService { |
| 45 | public: |
holte | 07637b01 | 2014-09-22 19:15:02 | [diff] [blame] | 46 | // Constructs a RapporService. |
| 47 | // Calling code is responsible for ensuring that the lifetime of |
| 48 | // |pref_service| is longer than the lifetime of RapporService. |
holte | 79705c8 | 2014-11-14 23:41:04 | [diff] [blame] | 49 | // |is_incognito_callback| will be called to test if incognito mode is active. |
| 50 | RapporService(PrefService* pref_service, |
| 51 | const base::Callback<bool(void)> is_incognito_callback); |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 52 | virtual ~RapporService(); |
| 53 | |
holte | 07637b01 | 2014-09-22 19:15:02 | [diff] [blame] | 54 | // Add an observer for collecting daily metrics. |
| 55 | void AddDailyObserver(scoped_ptr<metrics::DailyEvent::Observer> observer); |
| 56 | |
holte | 5a7ed7c | 2015-01-09 23:52:46 | [diff] [blame] | 57 | // Initializes the rappor service, including loading the cohort and secret |
| 58 | // preferences from disk. |
| 59 | void Initialize(net::URLRequestContextGetter* context); |
| 60 | |
| 61 | // Updates the settings for metric recording and uploading. |
| 62 | // The RapporService must be initialized before this method is called. |
| 63 | // If |recording_level| > REPORTING_DISABLED, periodic reports will be |
| 64 | // generated and queued for upload. |
| 65 | // If |may_upload| is true, reports will be uploaded from the queue. |
| 66 | void Update(RecordingLevel recording_level, bool may_upload); |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 67 | |
holte | feb4e55 | 2015-04-28 01:05:01 | [diff] [blame^] | 68 | // Constructs a Sample object for the caller to record fields in. |
| 69 | scoped_ptr<Sample> CreateSample(RapporType); |
| 70 | |
| 71 | // Records a Sample of rappor metric specified by |metric_name|. |
| 72 | // |
| 73 | // TODO(holte): Rename RecordSample to RecordString and then rename this |
| 74 | // to RecordSample. |
| 75 | // |
| 76 | // example: |
| 77 | // scoped_ptr<Sample> sample = rappor_service->CreateSample(MY_METRIC_TYPE); |
| 78 | // sample->SetStringField("Field1", "some string"); |
| 79 | // sample->SetFlagsValue("Field2", SOME|FLAGS); |
| 80 | // rappor_service->RecordSample("MyMetric", sample.Pass()); |
| 81 | // |
| 82 | // This will result in a report setting two metrics "MyMetric.Field1" and |
| 83 | // "MyMetric.Field2", and they will both be generated from the same sample, |
| 84 | // to allow for correllations to be computed. |
| 85 | void RecordSampleObj(const std::string& metric_name, |
| 86 | scoped_ptr<Sample> sample); |
| 87 | |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 88 | // Records a sample of the rappor metric specified by |metric_name|. |
| 89 | // Creates and initializes the metric, if it doesn't yet exist. |
mathp | 7deb9df | 2015-03-13 14:44:16 | [diff] [blame] | 90 | virtual void RecordSample(const std::string& metric_name, |
| 91 | RapporType type, |
| 92 | const std::string& sample); |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 93 | |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 94 | // Registers the names of all of the preferences used by RapporService in the |
| 95 | // provided PrefRegistry. This should be called before calling Start(). |
| 96 | static void RegisterPrefs(PrefRegistrySimple* registry); |
| 97 | |
| 98 | protected: |
holte | ee8e706 | 2014-11-04 22:27:10 | [diff] [blame] | 99 | // Initializes the state of the RapporService. |
holte | 5a7ed7c | 2015-01-09 23:52:46 | [diff] [blame] | 100 | void InitializeInternal(scoped_ptr<LogUploaderInterface> uploader, |
| 101 | int32_t cohort, |
| 102 | const std::string& secret); |
| 103 | |
| 104 | // Sets the recording level. |
| 105 | void SetRecordingLevel(RecordingLevel parameters); |
holte | ee8e706 | 2014-11-04 22:27:10 | [diff] [blame] | 106 | |
holte | 5a7ed7c | 2015-01-09 23:52:46 | [diff] [blame] | 107 | // Cancels the next call to OnLogInterval. |
| 108 | virtual void CancelNextLogRotation(); |
| 109 | |
| 110 | // Schedules the next call to OnLogInterval. |
| 111 | virtual void ScheduleNextLogRotation(base::TimeDelta interval); |
| 112 | |
[email protected] | ccb4926 | 2014-03-26 04:10:17 | [diff] [blame] | 113 | // Logs all of the collected metrics to the reports proto message and clears |
holte | 07637b01 | 2014-09-22 19:15:02 | [diff] [blame] | 114 | // the internal map. Exposed for tests. Returns true if any metrics were |
[email protected] | ccb4926 | 2014-03-26 04:10:17 | [diff] [blame] | 115 | // recorded. |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 116 | bool ExportMetrics(RapporReports* reports); |
| 117 | |
holte | 5a7ed7c | 2015-01-09 23:52:46 | [diff] [blame] | 118 | private: |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 119 | // Records a sample of the rappor metric specified by |parameters|. |
| 120 | // Creates and initializes the metric, if it doesn't yet exist. |
| 121 | // Exposed for tests. |
| 122 | void RecordSampleInternal(const std::string& metric_name, |
| 123 | const RapporParameters& parameters, |
| 124 | const std::string& sample); |
| 125 | |
holte | 5a7ed7c | 2015-01-09 23:52:46 | [diff] [blame] | 126 | // Checks if the service has been started successfully. |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 127 | bool IsInitialized() const; |
| 128 | |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 129 | // Called whenever the logging interval elapses to generate a new log of |
| 130 | // reports and pass it to the uploader. |
| 131 | void OnLogInterval(); |
| 132 | |
| 133 | // Finds a metric in the metrics_map_, creating it if it doesn't already |
| 134 | // exist. |
| 135 | RapporMetric* LookUpMetric(const std::string& metric_name, |
| 136 | const RapporParameters& parameters); |
| 137 | |
holte | 07637b01 | 2014-09-22 19:15:02 | [diff] [blame] | 138 | // A weak pointer to the PrefService used to read and write preferences. |
| 139 | PrefService* pref_service_; |
| 140 | |
holte | 79705c8 | 2014-11-14 23:41:04 | [diff] [blame] | 141 | // A callback for testing if incognito mode is active; |
| 142 | const base::Callback<bool(void)> is_incognito_callback_; |
| 143 | |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 144 | // Client-side secret used to generate fake bits. |
| 145 | std::string secret_; |
| 146 | |
| 147 | // The cohort this client is assigned to. -1 is uninitialized. |
| 148 | int32_t cohort_; |
| 149 | |
[email protected] | ccb4926 | 2014-03-26 04:10:17 | [diff] [blame] | 150 | // Timer which schedules calls to OnLogInterval(). |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 151 | base::OneShotTimer<RapporService> log_rotation_timer_; |
| 152 | |
holte | 07637b01 | 2014-09-22 19:15:02 | [diff] [blame] | 153 | // A daily event for collecting metrics once a day. |
| 154 | metrics::DailyEvent daily_event_; |
| 155 | |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 156 | // A private LogUploader instance for sending reports to the server. |
holte | 5a7ed7c | 2015-01-09 23:52:46 | [diff] [blame] | 157 | scoped_ptr<LogUploaderInterface> uploader_; |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 158 | |
holte | ee8e706 | 2014-11-04 22:27:10 | [diff] [blame] | 159 | // What reporting level of metrics are being reported. |
holte | 5a7ed7c | 2015-01-09 23:52:46 | [diff] [blame] | 160 | RecordingLevel recording_level_; |
holte | ee8e706 | 2014-11-04 22:27:10 | [diff] [blame] | 161 | |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 162 | // We keep all registered metrics in a map, from name to metric. |
| 163 | // The map owns the metrics it contains. |
| 164 | std::map<std::string, RapporMetric*> metrics_map_; |
| 165 | |
holte | feb4e55 | 2015-04-28 01:05:01 | [diff] [blame^] | 166 | internal::Sampler sampler_; |
| 167 | |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 168 | DISALLOW_COPY_AND_ASSIGN(RapporService); |
| 169 | }; |
| 170 | |
| 171 | } // namespace rappor |
| 172 | |
| 173 | #endif // COMPONENTS_RAPPOR_RAPPOR_SERVICE_H_ |