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/callback.h" | 12 #include "base/callback.h" |
13 #include "base/macros.h" | 13 #include "base/macros.h" |
14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
15 #include "base/timer/timer.h" | 15 #include "base/timer/timer.h" |
16 #include "components/metrics/daily_event.h" | 16 #include "components/metrics/daily_event.h" |
| 17 #include "components/rappor/rappor_metric.h" |
17 #include "components/rappor/rappor_parameters.h" | 18 #include "components/rappor/rappor_parameters.h" |
18 | 19 |
19 class PrefRegistrySimple; | 20 class PrefRegistrySimple; |
20 class PrefService; | 21 class PrefService; |
21 | 22 |
22 namespace net { | 23 namespace net { |
23 class URLRequestContextGetter; | 24 class URLRequestContextGetter; |
24 } | 25 } |
25 | 26 |
26 namespace rappor { | 27 namespace rappor { |
27 | 28 |
28 class LogUploaderInterface; | 29 class LogUploaderInterface; |
| 30 class RapporReports; |
| 31 |
| 32 namespace internal { |
29 class RapporMetric; | 33 class RapporMetric; |
30 class RapporReports; | 34 } |
31 | 35 |
32 // The type of data stored in a metric. | 36 // The type of data stored in a metric. |
33 enum RapporType { | 37 enum RapporType { |
34 // For sampling the eTLD+1 of a URL. | 38 // For sampling strings, such as domain and registry. |
35 ETLD_PLUS_ONE_RAPPOR_TYPE = 0, | 39 UMA_STRING_RAPPOR_TYPE = 0, |
36 COARSE_RAPPOR_TYPE, | 40 // For sampling a string with some flag bits for correllation analysis. |
37 NUM_RAPPOR_TYPES | 41 UMA_STRING_AND_FLAGS_RAPPOR_TYPE, |
| 42 // For safebrowsing controlled metrics. |
| 43 SB_STRING_RAPPOR_TYPE, |
| 44 SB_STRING_AND_FLAGS_RAPPOR_TYPE, |
| 45 NUM_RAPPOR_TYPES, |
| 46 // TODO(holte): Update callers and remove these aliases. |
| 47 ETLD_PLUS_ONE_RAPPOR_TYPE = UMA_STRING_RAPPOR_TYPE, |
| 48 COARSE_RAPPOR_TYPE = SB_STRING_RAPPOR_TYPE, |
38 }; | 49 }; |
39 | 50 |
40 // This class provides an interface for recording samples for rappor metrics, | 51 // This class provides an interface for recording samples for rappor metrics, |
41 // and periodically generates and uploads reports based on the collected data. | 52 // and periodically generates and uploads reports based on the collected data. |
42 class RapporService { | 53 class RapporService { |
43 public: | 54 public: |
44 // Constructs a RapporService. | 55 // Constructs a RapporService. |
45 // Calling code is responsible for ensuring that the lifetime of | 56 // Calling code is responsible for ensuring that the lifetime of |
46 // |pref_service| is longer than the lifetime of RapporService. | 57 // |pref_service| is longer than the lifetime of RapporService. |
47 // |is_incognito_callback| will be called to test if incognito mode is active. | 58 // |is_incognito_callback| will be called to test if incognito mode is active. |
(...skipping 14 matching lines...) Expand all Loading... |
62 // generated and queued for upload. | 73 // generated and queued for upload. |
63 // If |may_upload| is true, reports will be uploaded from the queue. | 74 // If |may_upload| is true, reports will be uploaded from the queue. |
64 void Update(RecordingLevel recording_level, bool may_upload); | 75 void Update(RecordingLevel recording_level, bool may_upload); |
65 | 76 |
66 // Records a sample of the rappor metric specified by |metric_name|. | 77 // Records a sample of the rappor metric specified by |metric_name|. |
67 // Creates and initializes the metric, if it doesn't yet exist. | 78 // Creates and initializes the metric, if it doesn't yet exist. |
68 virtual void RecordSample(const std::string& metric_name, | 79 virtual void RecordSample(const std::string& metric_name, |
69 RapporType type, | 80 RapporType type, |
70 const std::string& sample); | 81 const std::string& sample); |
71 | 82 |
| 83 // Record a sample of the rappor metric specified by |metric_name|. |
| 84 // Creates and initializes the metric, if it doesn't yet exist. |
| 85 // This should be used for metric types that contain flag bytes. |
| 86 void RecordStringAndFlags(const std::string& metric_name, |
| 87 RapporType type, |
| 88 const std::string& str, |
| 89 uint64_t flags); |
| 90 |
72 // Registers the names of all of the preferences used by RapporService in the | 91 // Registers the names of all of the preferences used by RapporService in the |
73 // provided PrefRegistry. This should be called before calling Start(). | 92 // provided PrefRegistry. This should be called before calling Start(). |
74 static void RegisterPrefs(PrefRegistrySimple* registry); | 93 static void RegisterPrefs(PrefRegistrySimple* registry); |
75 | 94 |
76 protected: | 95 protected: |
77 // Initializes the state of the RapporService. | 96 // Initializes the state of the RapporService. |
78 void InitializeInternal(scoped_ptr<LogUploaderInterface> uploader, | 97 void InitializeInternal(scoped_ptr<LogUploaderInterface> uploader, |
79 int32_t cohort, | 98 int32_t cohort, |
80 const std::string& secret); | 99 const std::string& secret); |
81 | 100 |
(...skipping 10 matching lines...) Expand all Loading... |
92 // the internal map. Exposed for tests. Returns true if any metrics were | 111 // the internal map. Exposed for tests. Returns true if any metrics were |
93 // recorded. | 112 // recorded. |
94 bool ExportMetrics(RapporReports* reports); | 113 bool ExportMetrics(RapporReports* reports); |
95 | 114 |
96 private: | 115 private: |
97 // Records a sample of the rappor metric specified by |parameters|. | 116 // Records a sample of the rappor metric specified by |parameters|. |
98 // Creates and initializes the metric, if it doesn't yet exist. | 117 // Creates and initializes the metric, if it doesn't yet exist. |
99 // Exposed for tests. | 118 // Exposed for tests. |
100 void RecordSampleInternal(const std::string& metric_name, | 119 void RecordSampleInternal(const std::string& metric_name, |
101 const RapporParameters& parameters, | 120 const RapporParameters& parameters, |
102 const std::string& sample); | 121 const internal::Sample& sample); |
103 | 122 |
104 // Checks if the service has been started successfully. | 123 // Checks if the service has been started successfully. |
105 bool IsInitialized() const; | 124 bool IsInitialized() const; |
106 | 125 |
107 // Called whenever the logging interval elapses to generate a new log of | 126 // Called whenever the logging interval elapses to generate a new log of |
108 // reports and pass it to the uploader. | 127 // reports and pass it to the uploader. |
109 void OnLogInterval(); | 128 void OnLogInterval(); |
110 | 129 |
111 // Finds a metric in the metrics_map_, creating it if it doesn't already | 130 // Finds a metric in the metrics_map_, creating it if it doesn't already |
112 // exist. | 131 // exist. |
113 RapporMetric* LookUpMetric(const std::string& metric_name, | 132 internal::RapporMetric* LookUpMetric(const std::string& metric_name, |
114 const RapporParameters& parameters); | 133 const RapporParameters& parameters); |
115 | 134 |
116 // A weak pointer to the PrefService used to read and write preferences. | 135 // A weak pointer to the PrefService used to read and write preferences. |
117 PrefService* pref_service_; | 136 PrefService* pref_service_; |
118 | 137 |
119 // A callback for testing if incognito mode is active; | 138 // A callback for testing if incognito mode is active; |
120 const base::Callback<bool(void)> is_incognito_callback_; | 139 const base::Callback<bool(void)> is_incognito_callback_; |
121 | 140 |
122 // Client-side secret used to generate fake bits. | 141 // Client-side secret used to generate fake bits. |
123 std::string secret_; | 142 std::string secret_; |
124 | 143 |
125 // The cohort this client is assigned to. -1 is uninitialized. | 144 // The cohort this client is assigned to. -1 is uninitialized. |
126 int32_t cohort_; | 145 int32_t cohort_; |
127 | 146 |
128 // Timer which schedules calls to OnLogInterval(). | 147 // Timer which schedules calls to OnLogInterval(). |
129 base::OneShotTimer<RapporService> log_rotation_timer_; | 148 base::OneShotTimer<RapporService> log_rotation_timer_; |
130 | 149 |
131 // A daily event for collecting metrics once a day. | 150 // A daily event for collecting metrics once a day. |
132 metrics::DailyEvent daily_event_; | 151 metrics::DailyEvent daily_event_; |
133 | 152 |
134 // A private LogUploader instance for sending reports to the server. | 153 // A private LogUploader instance for sending reports to the server. |
135 scoped_ptr<LogUploaderInterface> uploader_; | 154 scoped_ptr<LogUploaderInterface> uploader_; |
136 | 155 |
137 // What reporting level of metrics are being reported. | 156 // What reporting level of metrics are being reported. |
138 RecordingLevel recording_level_; | 157 RecordingLevel recording_level_; |
139 | 158 |
140 // We keep all registered metrics in a map, from name to metric. | 159 // We keep all registered metrics in a map, from name to metric. |
141 // The map owns the metrics it contains. | 160 // The map owns the metrics it contains. |
142 std::map<std::string, RapporMetric*> metrics_map_; | 161 std::map<std::string, internal::RapporMetric*> metrics_map_; |
143 | 162 |
144 DISALLOW_COPY_AND_ASSIGN(RapporService); | 163 DISALLOW_COPY_AND_ASSIGN(RapporService); |
145 }; | 164 }; |
146 | 165 |
147 } // namespace rappor | 166 } // namespace rappor |
148 | 167 |
149 #endif // COMPONENTS_RAPPOR_RAPPOR_SERVICE_H_ | 168 #endif // COMPONENTS_RAPPOR_RAPPOR_SERVICE_H_ |
OLD | NEW |