[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" |
[email protected] | ccb4926 | 2014-03-26 04:10:17 | [diff] [blame] | 12 | #include "base/macros.h" |
| 13 | #include "base/memory/scoped_ptr.h" |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 14 | #include "base/timer/timer.h" |
holte | 07637b01 | 2014-09-22 19:15:02 | [diff] [blame] | 15 | #include "components/metrics/daily_event.h" |
holte | ee8e706 | 2014-11-04 22:27:10 | [diff] [blame^] | 16 | #include "components/rappor/rappor_parameters.h" |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 17 | |
| 18 | class PrefRegistrySimple; |
[email protected] | ccb4926 | 2014-03-26 04:10:17 | [diff] [blame] | 19 | class PrefService; |
| 20 | |
| 21 | namespace net { |
| 22 | class URLRequestContextGetter; |
| 23 | } |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 24 | |
| 25 | namespace rappor { |
| 26 | |
[email protected] | ccb4926 | 2014-03-26 04:10:17 | [diff] [blame] | 27 | class LogUploader; |
| 28 | class RapporMetric; |
| 29 | class RapporReports; |
[email protected] | ccb4926 | 2014-03-26 04:10:17 | [diff] [blame] | 30 | |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 31 | // The type of data stored in a metric. |
| 32 | enum RapporType { |
[email protected] | ccb4926 | 2014-03-26 04:10:17 | [diff] [blame] | 33 | // For sampling the eTLD+1 of a URL. |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 34 | ETLD_PLUS_ONE_RAPPOR_TYPE = 0, |
holte | ee8e706 | 2014-11-04 22:27:10 | [diff] [blame^] | 35 | COARSE_RAPPOR_TYPE, |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 36 | NUM_RAPPOR_TYPES |
| 37 | }; |
| 38 | |
| 39 | // This class provides an interface for recording samples for rappor metrics, |
| 40 | // and periodically generates and uploads reports based on the collected data. |
| 41 | class RapporService { |
| 42 | public: |
holte | 07637b01 | 2014-09-22 19:15:02 | [diff] [blame] | 43 | // Constructs a RapporService. |
| 44 | // Calling code is responsible for ensuring that the lifetime of |
| 45 | // |pref_service| is longer than the lifetime of RapporService. |
| 46 | explicit RapporService(PrefService* pref_service); |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 47 | virtual ~RapporService(); |
| 48 | |
holte | 07637b01 | 2014-09-22 19:15:02 | [diff] [blame] | 49 | // Add an observer for collecting daily metrics. |
| 50 | void AddDailyObserver(scoped_ptr<metrics::DailyEvent::Observer> observer); |
| 51 | |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 52 | // Starts the periodic generation of reports and upload attempts. |
holte | ee8e706 | 2014-11-04 22:27:10 | [diff] [blame^] | 53 | // |metrics enabled| should be true if UMA users have opted in. |
holte | 07637b01 | 2014-09-22 19:15:02 | [diff] [blame] | 54 | void Start(net::URLRequestContextGetter* context, |
[email protected] | f861eca | 2014-08-07 20:02:38 | [diff] [blame] | 55 | bool metrics_enabled); |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 56 | |
| 57 | // Records a sample of the rappor metric specified by |metric_name|. |
| 58 | // Creates and initializes the metric, if it doesn't yet exist. |
| 59 | void RecordSample(const std::string& metric_name, |
| 60 | RapporType type, |
| 61 | const std::string& sample); |
| 62 | |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 63 | // Registers the names of all of the preferences used by RapporService in the |
| 64 | // provided PrefRegistry. This should be called before calling Start(). |
| 65 | static void RegisterPrefs(PrefRegistrySimple* registry); |
| 66 | |
| 67 | protected: |
holte | ee8e706 | 2014-11-04 22:27:10 | [diff] [blame^] | 68 | // Initializes the state of the RapporService. |
| 69 | void Initialize(int32_t cohort, |
| 70 | const std::string& secret, |
| 71 | const ReportingLevel& reporting_level); |
| 72 | |
holte | 07637b01 | 2014-09-22 19:15:02 | [diff] [blame] | 73 | // Retrieves the cohort number this client was assigned to, generating it if |
| 74 | // doesn't already exist. The cohort should be persistent. |
holte | ee8e706 | 2014-11-04 22:27:10 | [diff] [blame^] | 75 | int32_t LoadCohort(); |
holte | 07637b01 | 2014-09-22 19:15:02 | [diff] [blame] | 76 | |
| 77 | // Retrieves the value for secret_ from preferences, generating it if doesn't |
| 78 | // already exist. The secret should be persistent, so that additional bits |
| 79 | // from the client do not get exposed over time. |
holte | ee8e706 | 2014-11-04 22:27:10 | [diff] [blame^] | 80 | std::string LoadSecret(); |
holte | 07637b01 | 2014-09-22 19:15:02 | [diff] [blame] | 81 | |
[email protected] | ccb4926 | 2014-03-26 04:10:17 | [diff] [blame] | 82 | // Logs all of the collected metrics to the reports proto message and clears |
holte | 07637b01 | 2014-09-22 19:15:02 | [diff] [blame] | 83 | // the internal map. Exposed for tests. Returns true if any metrics were |
[email protected] | ccb4926 | 2014-03-26 04:10:17 | [diff] [blame] | 84 | // recorded. |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 85 | bool ExportMetrics(RapporReports* reports); |
| 86 | |
| 87 | // Records a sample of the rappor metric specified by |parameters|. |
| 88 | // Creates and initializes the metric, if it doesn't yet exist. |
| 89 | // Exposed for tests. |
| 90 | void RecordSampleInternal(const std::string& metric_name, |
| 91 | const RapporParameters& parameters, |
| 92 | const std::string& sample); |
| 93 | |
| 94 | private: |
| 95 | // Check if the service has been started successfully. |
| 96 | bool IsInitialized() const; |
| 97 | |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 98 | // Called whenever the logging interval elapses to generate a new log of |
| 99 | // reports and pass it to the uploader. |
| 100 | void OnLogInterval(); |
| 101 | |
| 102 | // Finds a metric in the metrics_map_, creating it if it doesn't already |
| 103 | // exist. |
| 104 | RapporMetric* LookUpMetric(const std::string& metric_name, |
| 105 | const RapporParameters& parameters); |
| 106 | |
holte | 07637b01 | 2014-09-22 19:15:02 | [diff] [blame] | 107 | // A weak pointer to the PrefService used to read and write preferences. |
| 108 | PrefService* pref_service_; |
| 109 | |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 110 | // Client-side secret used to generate fake bits. |
| 111 | std::string secret_; |
| 112 | |
| 113 | // The cohort this client is assigned to. -1 is uninitialized. |
| 114 | int32_t cohort_; |
| 115 | |
[email protected] | ccb4926 | 2014-03-26 04:10:17 | [diff] [blame] | 116 | // Timer which schedules calls to OnLogInterval(). |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 117 | base::OneShotTimer<RapporService> log_rotation_timer_; |
| 118 | |
holte | 07637b01 | 2014-09-22 19:15:02 | [diff] [blame] | 119 | // A daily event for collecting metrics once a day. |
| 120 | metrics::DailyEvent daily_event_; |
| 121 | |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 122 | // A private LogUploader instance for sending reports to the server. |
| 123 | scoped_ptr<LogUploader> uploader_; |
| 124 | |
holte | ee8e706 | 2014-11-04 22:27:10 | [diff] [blame^] | 125 | // What reporting level of metrics are being reported. |
| 126 | ReportingLevel reporting_level_; |
| 127 | |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 128 | // We keep all registered metrics in a map, from name to metric. |
| 129 | // The map owns the metrics it contains. |
| 130 | std::map<std::string, RapporMetric*> metrics_map_; |
| 131 | |
| 132 | DISALLOW_COPY_AND_ASSIGN(RapporService); |
| 133 | }; |
| 134 | |
| 135 | } // namespace rappor |
| 136 | |
| 137 | #endif // COMPONENTS_RAPPOR_RAPPOR_SERVICE_H_ |