OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef COMPONENTS_RAPPOR_RAPPOR_SERVICE_H_ | 5 #ifndef COMPONENTS_RAPPOR_RAPPOR_SERVICE_H_ |
6 #define COMPONENTS_RAPPOR_RAPPOR_SERVICE_H_ | 6 #define COMPONENTS_RAPPOR_RAPPOR_SERVICE_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
12 #include "base/macros.h" | 12 #include "base/macros.h" |
13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
14 #include "base/timer/timer.h" | 14 #include "base/timer/timer.h" |
15 #include "components/metrics/daily_event.h" | 15 #include "components/metrics/daily_event.h" |
| 16 #include "components/rappor/rappor_parameters.h" |
16 | 17 |
17 class PrefRegistrySimple; | 18 class PrefRegistrySimple; |
18 class PrefService; | 19 class PrefService; |
19 | 20 |
20 namespace net { | 21 namespace net { |
21 class URLRequestContextGetter; | 22 class URLRequestContextGetter; |
22 } | 23 } |
23 | 24 |
24 namespace rappor { | 25 namespace rappor { |
25 | 26 |
26 class LogUploader; | 27 class LogUploader; |
27 class RapporMetric; | 28 class RapporMetric; |
28 class RapporReports; | 29 class RapporReports; |
29 struct RapporParameters; | |
30 | 30 |
31 // The type of data stored in a metric. | 31 // The type of data stored in a metric. |
32 enum RapporType { | 32 enum RapporType { |
33 // For sampling the eTLD+1 of a URL. | 33 // For sampling the eTLD+1 of a URL. |
34 ETLD_PLUS_ONE_RAPPOR_TYPE = 0, | 34 ETLD_PLUS_ONE_RAPPOR_TYPE = 0, |
| 35 COARSE_RAPPOR_TYPE, |
35 NUM_RAPPOR_TYPES | 36 NUM_RAPPOR_TYPES |
36 }; | 37 }; |
37 | 38 |
38 // This class provides an interface for recording samples for rappor metrics, | 39 // This class provides an interface for recording samples for rappor metrics, |
39 // and periodically generates and uploads reports based on the collected data. | 40 // and periodically generates and uploads reports based on the collected data. |
40 class RapporService { | 41 class RapporService { |
41 public: | 42 public: |
42 // Constructs a RapporService. | 43 // Constructs a RapporService. |
43 // Calling code is responsible for ensuring that the lifetime of | 44 // Calling code is responsible for ensuring that the lifetime of |
44 // |pref_service| is longer than the lifetime of RapporService. | 45 // |pref_service| is longer than the lifetime of RapporService. |
45 explicit RapporService(PrefService* pref_service); | 46 explicit RapporService(PrefService* pref_service); |
46 virtual ~RapporService(); | 47 virtual ~RapporService(); |
47 | 48 |
48 // Add an observer for collecting daily metrics. | 49 // Add an observer for collecting daily metrics. |
49 void AddDailyObserver(scoped_ptr<metrics::DailyEvent::Observer> observer); | 50 void AddDailyObserver(scoped_ptr<metrics::DailyEvent::Observer> observer); |
50 | 51 |
51 // Starts the periodic generation of reports and upload attempts. | 52 // Starts the periodic generation of reports and upload attempts. |
| 53 // |metrics enabled| should be true if UMA users have opted in. |
52 void Start(net::URLRequestContextGetter* context, | 54 void Start(net::URLRequestContextGetter* context, |
53 bool metrics_enabled); | 55 bool metrics_enabled); |
54 | 56 |
55 // Records a sample of the rappor metric specified by |metric_name|. | 57 // Records a sample of the rappor metric specified by |metric_name|. |
56 // Creates and initializes the metric, if it doesn't yet exist. | 58 // Creates and initializes the metric, if it doesn't yet exist. |
57 void RecordSample(const std::string& metric_name, | 59 void RecordSample(const std::string& metric_name, |
58 RapporType type, | 60 RapporType type, |
59 const std::string& sample); | 61 const std::string& sample); |
60 | 62 |
61 // Sets the cohort value. For use by tests only. | |
62 void SetCohortForTesting(uint32_t cohort) { cohort_ = cohort; } | |
63 | |
64 // Sets the secret value. For use by tests only. | |
65 void SetSecretForTesting(const std::string& secret) { secret_ = secret; } | |
66 | |
67 // Registers the names of all of the preferences used by RapporService in the | 63 // Registers the names of all of the preferences used by RapporService in the |
68 // provided PrefRegistry. This should be called before calling Start(). | 64 // provided PrefRegistry. This should be called before calling Start(). |
69 static void RegisterPrefs(PrefRegistrySimple* registry); | 65 static void RegisterPrefs(PrefRegistrySimple* registry); |
70 | 66 |
71 protected: | 67 protected: |
| 68 // Initializes the state of the RapporService. |
| 69 void Initialize(int32_t cohort, |
| 70 const std::string& secret, |
| 71 const ReportingLevel& reporting_level); |
| 72 |
72 // Retrieves the cohort number this client was assigned to, generating it if | 73 // Retrieves the cohort number this client was assigned to, generating it if |
73 // doesn't already exist. The cohort should be persistent. | 74 // doesn't already exist. The cohort should be persistent. |
74 void LoadCohort(); | 75 int32_t LoadCohort(); |
75 | 76 |
76 // Retrieves the value for secret_ from preferences, generating it if doesn't | 77 // Retrieves the value for secret_ from preferences, generating it if doesn't |
77 // already exist. The secret should be persistent, so that additional bits | 78 // already exist. The secret should be persistent, so that additional bits |
78 // from the client do not get exposed over time. | 79 // from the client do not get exposed over time. |
79 void LoadSecret(); | 80 std::string LoadSecret(); |
80 | 81 |
81 // Logs all of the collected metrics to the reports proto message and clears | 82 // Logs all of the collected metrics to the reports proto message and clears |
82 // the internal map. Exposed for tests. Returns true if any metrics were | 83 // the internal map. Exposed for tests. Returns true if any metrics were |
83 // recorded. | 84 // recorded. |
84 bool ExportMetrics(RapporReports* reports); | 85 bool ExportMetrics(RapporReports* reports); |
85 | 86 |
86 // Records a sample of the rappor metric specified by |parameters|. | 87 // Records a sample of the rappor metric specified by |parameters|. |
87 // Creates and initializes the metric, if it doesn't yet exist. | 88 // Creates and initializes the metric, if it doesn't yet exist. |
88 // Exposed for tests. | 89 // Exposed for tests. |
89 void RecordSampleInternal(const std::string& metric_name, | 90 void RecordSampleInternal(const std::string& metric_name, |
(...skipping 24 matching lines...) Expand all Loading... |
114 | 115 |
115 // Timer which schedules calls to OnLogInterval(). | 116 // Timer which schedules calls to OnLogInterval(). |
116 base::OneShotTimer<RapporService> log_rotation_timer_; | 117 base::OneShotTimer<RapporService> log_rotation_timer_; |
117 | 118 |
118 // A daily event for collecting metrics once a day. | 119 // A daily event for collecting metrics once a day. |
119 metrics::DailyEvent daily_event_; | 120 metrics::DailyEvent daily_event_; |
120 | 121 |
121 // A private LogUploader instance for sending reports to the server. | 122 // A private LogUploader instance for sending reports to the server. |
122 scoped_ptr<LogUploader> uploader_; | 123 scoped_ptr<LogUploader> uploader_; |
123 | 124 |
| 125 // What reporting level of metrics are being reported. |
| 126 ReportingLevel reporting_level_; |
| 127 |
124 // We keep all registered metrics in a map, from name to metric. | 128 // We keep all registered metrics in a map, from name to metric. |
125 // The map owns the metrics it contains. | 129 // The map owns the metrics it contains. |
126 std::map<std::string, RapporMetric*> metrics_map_; | 130 std::map<std::string, RapporMetric*> metrics_map_; |
127 | 131 |
128 DISALLOW_COPY_AND_ASSIGN(RapporService); | 132 DISALLOW_COPY_AND_ASSIGN(RapporService); |
129 }; | 133 }; |
130 | 134 |
131 } // namespace rappor | 135 } // namespace rappor |
132 | 136 |
133 #endif // COMPONENTS_RAPPOR_RAPPOR_SERVICE_H_ | 137 #endif // COMPONENTS_RAPPOR_RAPPOR_SERVICE_H_ |
OLD | NEW |