[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" |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 15 | |
| 16 | class PrefRegistrySimple; |
[email protected] | ccb4926 | 2014-03-26 04:10:17 | [diff] [blame^] | 17 | class PrefService; |
| 18 | |
| 19 | namespace net { |
| 20 | class URLRequestContextGetter; |
| 21 | } |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 22 | |
| 23 | namespace rappor { |
| 24 | |
[email protected] | ccb4926 | 2014-03-26 04:10:17 | [diff] [blame^] | 25 | class LogUploader; |
| 26 | class RapporMetric; |
| 27 | class RapporReports; |
| 28 | struct RapporParameters; |
| 29 | |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 30 | // The type of data stored in a metric. |
| 31 | enum RapporType { |
[email protected] | ccb4926 | 2014-03-26 04:10:17 | [diff] [blame^] | 32 | // For sampling the eTLD+1 of a URL. |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 33 | ETLD_PLUS_ONE_RAPPOR_TYPE = 0, |
| 34 | NUM_RAPPOR_TYPES |
| 35 | }; |
| 36 | |
| 37 | // This class provides an interface for recording samples for rappor metrics, |
| 38 | // and periodically generates and uploads reports based on the collected data. |
| 39 | class RapporService { |
| 40 | public: |
| 41 | RapporService(); |
| 42 | virtual ~RapporService(); |
| 43 | |
| 44 | // Starts the periodic generation of reports and upload attempts. |
| 45 | void Start(PrefService* pref_service, net::URLRequestContextGetter* context); |
| 46 | |
| 47 | // Records a sample of the rappor metric specified by |metric_name|. |
| 48 | // Creates and initializes the metric, if it doesn't yet exist. |
| 49 | void RecordSample(const std::string& metric_name, |
| 50 | RapporType type, |
| 51 | const std::string& sample); |
| 52 | |
| 53 | // Sets the cohort value. For use by tests only. |
| 54 | void SetCohortForTesting(uint32_t cohort) { cohort_ = cohort; } |
| 55 | |
| 56 | // Sets the secret value. For use by tests only. |
| 57 | void SetSecretForTesting(const std::string& secret) { secret_ = secret; } |
| 58 | |
| 59 | // Registers the names of all of the preferences used by RapporService in the |
| 60 | // provided PrefRegistry. This should be called before calling Start(). |
| 61 | static void RegisterPrefs(PrefRegistrySimple* registry); |
| 62 | |
| 63 | protected: |
[email protected] | ccb4926 | 2014-03-26 04:10:17 | [diff] [blame^] | 64 | // Logs all of the collected metrics to the reports proto message and clears |
| 65 | // the internal map. Exposed for tests. Returns true if any metrics were |
| 66 | // recorded. |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 67 | bool ExportMetrics(RapporReports* reports); |
| 68 | |
| 69 | // Records a sample of the rappor metric specified by |parameters|. |
| 70 | // Creates and initializes the metric, if it doesn't yet exist. |
| 71 | // Exposed for tests. |
| 72 | void RecordSampleInternal(const std::string& metric_name, |
| 73 | const RapporParameters& parameters, |
| 74 | const std::string& sample); |
| 75 | |
| 76 | private: |
| 77 | // Check if the service has been started successfully. |
| 78 | bool IsInitialized() const; |
| 79 | |
| 80 | // Retrieves the cohort number this client was assigned to, generating it if |
| 81 | // doesn't already exist. The cohort should be persistent. |
| 82 | void LoadCohort(PrefService* pref_service); |
| 83 | |
| 84 | // Retrieves the value for secret_ from preferences, generating it if doesn't |
| 85 | // already exist. The secret should be persistent, so that additional bits |
| 86 | // from the client do not get exposed over time. |
| 87 | void LoadSecret(PrefService* pref_service); |
| 88 | |
| 89 | // Called whenever the logging interval elapses to generate a new log of |
| 90 | // reports and pass it to the uploader. |
| 91 | void OnLogInterval(); |
| 92 | |
| 93 | // Finds a metric in the metrics_map_, creating it if it doesn't already |
| 94 | // exist. |
| 95 | RapporMetric* LookUpMetric(const std::string& metric_name, |
| 96 | const RapporParameters& parameters); |
| 97 | |
| 98 | // Client-side secret used to generate fake bits. |
| 99 | std::string secret_; |
| 100 | |
| 101 | // The cohort this client is assigned to. -1 is uninitialized. |
| 102 | int32_t cohort_; |
| 103 | |
[email protected] | ccb4926 | 2014-03-26 04:10:17 | [diff] [blame^] | 104 | // Timer which schedules calls to OnLogInterval(). |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 105 | base::OneShotTimer<RapporService> log_rotation_timer_; |
| 106 | |
| 107 | // A private LogUploader instance for sending reports to the server. |
| 108 | scoped_ptr<LogUploader> uploader_; |
| 109 | |
| 110 | // We keep all registered metrics in a map, from name to metric. |
| 111 | // The map owns the metrics it contains. |
| 112 | std::map<std::string, RapporMetric*> metrics_map_; |
| 113 | |
| 114 | DISALLOW_COPY_AND_ASSIGN(RapporService); |
| 115 | }; |
| 116 | |
| 117 | } // namespace rappor |
| 118 | |
| 119 | #endif // COMPONENTS_RAPPOR_RAPPOR_SERVICE_H_ |