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 #include "components/rappor/rappor_service.h" | 5 #include "components/rappor/rappor_service.h" |
6 | 6 |
7 #include "base/base64.h" | 7 #include "base/base64.h" |
8 #include "base/metrics/field_trial.h" | 8 #include "base/metrics/field_trial.h" |
9 #include "base/prefs/pref_registry_simple.h" | 9 #include "base/prefs/pref_registry_simple.h" |
10 #include "base/prefs/pref_service.h" | 10 #include "base/prefs/pref_service.h" |
(...skipping 11 matching lines...) Expand all Loading... |
22 | 22 |
23 namespace { | 23 namespace { |
24 | 24 |
25 // Seconds before the initial log is generated. | 25 // Seconds before the initial log is generated. |
26 const int kInitialLogIntervalSeconds = 15; | 26 const int kInitialLogIntervalSeconds = 15; |
27 // Interval between ongoing logs. | 27 // Interval between ongoing logs. |
28 const int kLogIntervalSeconds = 30 * 60; | 28 const int kLogIntervalSeconds = 30 * 60; |
29 | 29 |
30 const char kMimeType[] = "application/vnd.chrome.rappor"; | 30 const char kMimeType[] = "application/vnd.chrome.rappor"; |
31 | 31 |
| 32 const char kRapporDailyEventHistogram[] = "Rappor.DailyEvent.IntervalType"; |
| 33 |
32 // Constants for the RAPPOR rollout field trial. | 34 // Constants for the RAPPOR rollout field trial. |
33 const char kRapporRolloutFieldTrialName[] = "RapporRollout"; | 35 const char kRapporRolloutFieldTrialName[] = "RapporRollout"; |
34 | 36 |
35 // Constant for the finch parameter name for the server URL | 37 // Constant for the finch parameter name for the server URL |
36 const char kRapporRolloutServerUrlParam[] = "ServerUrl"; | 38 const char kRapporRolloutServerUrlParam[] = "ServerUrl"; |
37 | 39 |
38 // Constant for the finch parameter name for the server URL | 40 // Constant for the finch parameter name for the server URL |
39 const char kRapporRolloutRequireUmaParam[] = "RequireUma"; | 41 const char kRapporRolloutRequireUmaParam[] = "RequireUma"; |
40 | 42 |
41 // The rappor server's URL. | 43 // The rappor server's URL. |
(...skipping 20 matching lines...) Expand all Loading... |
62 16 /* Bloom filter size bytes */, | 64 16 /* Bloom filter size bytes */, |
63 2 /* Bloom filter hash count */, | 65 2 /* Bloom filter hash count */, |
64 rappor::PROBABILITY_50 /* Fake data probability */, | 66 rappor::PROBABILITY_50 /* Fake data probability */, |
65 rappor::PROBABILITY_50 /* Fake one probability */, | 67 rappor::PROBABILITY_50 /* Fake one probability */, |
66 rappor::PROBABILITY_75 /* One coin probability */, | 68 rappor::PROBABILITY_75 /* One coin probability */, |
67 rappor::PROBABILITY_25 /* Zero coin probability */}, | 69 rappor::PROBABILITY_25 /* Zero coin probability */}, |
68 }; | 70 }; |
69 | 71 |
70 } // namespace | 72 } // namespace |
71 | 73 |
72 RapporService::RapporService() : cohort_(-1) {} | 74 RapporService::RapporService(PrefService* pref_service) |
| 75 : pref_service_(pref_service), |
| 76 cohort_(-1), |
| 77 daily_event_(pref_service, |
| 78 prefs::kRapporLastDailySample, |
| 79 kRapporDailyEventHistogram) { |
| 80 } |
73 | 81 |
74 RapporService::~RapporService() { | 82 RapporService::~RapporService() { |
75 STLDeleteValues(&metrics_map_); | 83 STLDeleteValues(&metrics_map_); |
76 } | 84 } |
77 | 85 |
78 void RapporService::Start(PrefService* pref_service, | 86 void RapporService::AddDailyObserver( |
79 net::URLRequestContextGetter* request_context, | 87 scoped_ptr<metrics::DailyEvent::Observer> observer) { |
| 88 daily_event_.AddObserver(observer.Pass()); |
| 89 } |
| 90 |
| 91 void RapporService::Start(net::URLRequestContextGetter* request_context, |
80 bool metrics_enabled) { | 92 bool metrics_enabled) { |
81 const GURL server_url = GetServerUrl(metrics_enabled); | 93 const GURL server_url = GetServerUrl(metrics_enabled); |
82 if (!server_url.is_valid()) { | 94 if (!server_url.is_valid()) { |
83 DVLOG(1) << server_url.spec() << " is invalid. " | 95 DVLOG(1) << server_url.spec() << " is invalid. " |
84 << "RapporService not started."; | 96 << "RapporService not started."; |
85 return; | 97 return; |
86 } | 98 } |
87 DVLOG(1) << "RapporService started. Reporting to " << server_url.spec(); | 99 DVLOG(1) << "RapporService started. Reporting to " << server_url.spec(); |
88 DCHECK(!uploader_); | 100 DCHECK(!uploader_); |
89 LoadSecret(pref_service); | 101 LoadSecret(); |
90 LoadCohort(pref_service); | 102 LoadCohort(); |
91 uploader_.reset(new LogUploader(server_url, kMimeType, request_context)); | 103 uploader_.reset(new LogUploader(server_url, kMimeType, request_context)); |
92 log_rotation_timer_.Start( | 104 log_rotation_timer_.Start( |
93 FROM_HERE, | 105 FROM_HERE, |
94 base::TimeDelta::FromSeconds(kInitialLogIntervalSeconds), | 106 base::TimeDelta::FromSeconds(kInitialLogIntervalSeconds), |
95 this, | 107 this, |
96 &RapporService::OnLogInterval); | 108 &RapporService::OnLogInterval); |
97 } | 109 } |
98 | 110 |
99 void RapporService::OnLogInterval() { | 111 void RapporService::OnLogInterval() { |
100 DCHECK(uploader_); | 112 DCHECK(uploader_); |
101 DVLOG(2) << "RapporService::OnLogInterval"; | 113 DVLOG(2) << "RapporService::OnLogInterval"; |
| 114 daily_event_.CheckInterval(); |
102 RapporReports reports; | 115 RapporReports reports; |
103 if (ExportMetrics(&reports)) { | 116 if (ExportMetrics(&reports)) { |
104 std::string log_text; | 117 std::string log_text; |
105 bool success = reports.SerializeToString(&log_text); | 118 bool success = reports.SerializeToString(&log_text); |
106 DCHECK(success); | 119 DCHECK(success); |
107 DVLOG(1) << "RapporService sending a report of " | 120 DVLOG(1) << "RapporService sending a report of " |
108 << reports.report_size() << " value(s)."; | 121 << reports.report_size() << " value(s)."; |
109 uploader_->QueueLog(log_text); | 122 uploader_->QueueLog(log_text); |
110 } | 123 } |
111 log_rotation_timer_.Start(FROM_HERE, | 124 log_rotation_timer_.Start(FROM_HERE, |
112 base::TimeDelta::FromSeconds(kLogIntervalSeconds), | 125 base::TimeDelta::FromSeconds(kLogIntervalSeconds), |
113 this, | 126 this, |
114 &RapporService::OnLogInterval); | 127 &RapporService::OnLogInterval); |
115 } | 128 } |
116 | 129 |
117 // static | 130 // static |
118 void RapporService::RegisterPrefs(PrefRegistrySimple* registry) { | 131 void RapporService::RegisterPrefs(PrefRegistrySimple* registry) { |
119 registry->RegisterStringPref(prefs::kRapporSecret, std::string()); | 132 registry->RegisterStringPref(prefs::kRapporSecret, std::string()); |
120 registry->RegisterIntegerPref(prefs::kRapporCohortDeprecated, -1); | 133 registry->RegisterIntegerPref(prefs::kRapporCohortDeprecated, -1); |
121 registry->RegisterIntegerPref(prefs::kRapporCohortSeed, -1); | 134 registry->RegisterIntegerPref(prefs::kRapporCohortSeed, -1); |
| 135 metrics::DailyEvent::RegisterPref(registry, |
| 136 prefs::kRapporLastDailySample); |
122 } | 137 } |
123 | 138 |
124 void RapporService::LoadCohort(PrefService* pref_service) { | 139 void RapporService::LoadCohort() { |
125 DCHECK(!IsInitialized()); | 140 DCHECK(!IsInitialized()); |
126 // Ignore and delete old cohort parameter. | 141 // Ignore and delete old cohort parameter. |
127 pref_service->ClearPref(prefs::kRapporCohortDeprecated); | 142 pref_service_->ClearPref(prefs::kRapporCohortDeprecated); |
128 | 143 |
129 cohort_ = pref_service->GetInteger(prefs::kRapporCohortSeed); | 144 cohort_ = pref_service_->GetInteger(prefs::kRapporCohortSeed); |
130 // If the user is already assigned to a valid cohort, we're done. | 145 // If the user is already assigned to a valid cohort, we're done. |
131 if (cohort_ >= 0 && cohort_ < RapporParameters::kMaxCohorts) | 146 if (cohort_ >= 0 && cohort_ < RapporParameters::kMaxCohorts) |
132 return; | 147 return; |
133 | 148 |
134 // This is the first time the client has started the service (or their | 149 // This is the first time the client has started the service (or their |
135 // preferences were corrupted). Randomly assign them to a cohort. | 150 // preferences were corrupted). Randomly assign them to a cohort. |
136 cohort_ = base::RandGenerator(RapporParameters::kMaxCohorts); | 151 cohort_ = base::RandGenerator(RapporParameters::kMaxCohorts); |
137 DVLOG(2) << "Selected a new Rappor cohort: " << cohort_; | 152 DVLOG(2) << "Selected a new Rappor cohort: " << cohort_; |
138 pref_service->SetInteger(prefs::kRapporCohortSeed, cohort_); | 153 pref_service_->SetInteger(prefs::kRapporCohortSeed, cohort_); |
139 } | 154 } |
140 | 155 |
141 void RapporService::LoadSecret(PrefService* pref_service) { | 156 void RapporService::LoadSecret() { |
142 DCHECK(secret_.empty()); | 157 DCHECK(secret_.empty()); |
143 std::string secret_base64 = pref_service->GetString(prefs::kRapporSecret); | 158 std::string secret_base64 = pref_service_->GetString(prefs::kRapporSecret); |
144 if (!secret_base64.empty()) { | 159 if (!secret_base64.empty()) { |
145 bool decoded = base::Base64Decode(secret_base64, &secret_); | 160 bool decoded = base::Base64Decode(secret_base64, &secret_); |
146 if (decoded && secret_.size() == HmacByteVectorGenerator::kEntropyInputSize) | 161 if (decoded && secret_.size() == HmacByteVectorGenerator::kEntropyInputSize) |
147 return; | 162 return; |
148 // If the preference fails to decode, or is the wrong size, it must be | 163 // If the preference fails to decode, or is the wrong size, it must be |
149 // corrupt, so continue as though it didn't exist yet and generate a new | 164 // corrupt, so continue as though it didn't exist yet and generate a new |
150 // one. | 165 // one. |
151 } | 166 } |
152 | 167 |
153 DVLOG(2) << "Generated a new Rappor secret."; | 168 DVLOG(2) << "Generated a new Rappor secret."; |
154 secret_ = HmacByteVectorGenerator::GenerateEntropyInput(); | 169 secret_ = HmacByteVectorGenerator::GenerateEntropyInput(); |
155 base::Base64Encode(secret_, &secret_base64); | 170 base::Base64Encode(secret_, &secret_base64); |
156 pref_service->SetString(prefs::kRapporSecret, secret_base64); | 171 pref_service_->SetString(prefs::kRapporSecret, secret_base64); |
157 } | 172 } |
158 | 173 |
159 bool RapporService::ExportMetrics(RapporReports* reports) { | 174 bool RapporService::ExportMetrics(RapporReports* reports) { |
160 if (metrics_map_.empty()) | 175 if (metrics_map_.empty()) |
161 return false; | 176 return false; |
162 | 177 |
163 DCHECK_GE(cohort_, 0); | 178 DCHECK_GE(cohort_, 0); |
164 reports->set_cohort(cohort_); | 179 reports->set_cohort(cohort_); |
165 | 180 |
166 for (std::map<std::string, RapporMetric*>::const_iterator it = | 181 for (std::map<std::string, RapporMetric*>::const_iterator it = |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
212 DCHECK_EQ(parameters.ToString(), metric->parameters().ToString()); | 227 DCHECK_EQ(parameters.ToString(), metric->parameters().ToString()); |
213 return metric; | 228 return metric; |
214 } | 229 } |
215 | 230 |
216 RapporMetric* new_metric = new RapporMetric(metric_name, parameters, cohort_); | 231 RapporMetric* new_metric = new RapporMetric(metric_name, parameters, cohort_); |
217 metrics_map_[metric_name] = new_metric; | 232 metrics_map_[metric_name] = new_metric; |
218 return new_metric; | 233 return new_metric; |
219 } | 234 } |
220 | 235 |
221 } // namespace rappor | 236 } // namespace rappor |
OLD | NEW |