| 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" |
| 11 #include "base/rand_util.h" | 11 #include "base/rand_util.h" |
| 12 #include "base/stl_util.h" | 12 #include "base/stl_util.h" |
| 13 #include "base/time/time.h" | 13 #include "base/time/time.h" |
| 14 #include "components/metrics/metrics_hashes.h" | 14 #include "components/metrics/metrics_hashes.h" |
| 15 #include "components/rappor/log_uploader.h" | 15 #include "components/rappor/log_uploader.h" |
| 16 #include "components/rappor/proto/rappor_metric.pb.h" | 16 #include "components/rappor/proto/rappor_metric.pb.h" |
| 17 #include "components/rappor/rappor_metric.h" | 17 #include "components/rappor/rappor_metric.h" |
| 18 #include "components/rappor/rappor_pref_names.h" | 18 #include "components/rappor/rappor_pref_names.h" |
| 19 #include "components/variations/variations_associated_data.h" | 19 #include "components/variations/variations_associated_data.h" |
| 20 | 20 |
| 21 namespace rappor { | 21 namespace rappor { |
| 22 | 22 |
| 23 namespace { | 23 namespace { |
| 24 | 24 |
| 25 // The number of cohorts we divide clients into. | |
| 26 const int kNumCohorts = 32; | |
| 27 | |
| 28 // Seconds before the initial log is generated. | 25 // Seconds before the initial log is generated. |
| 29 const int kInitialLogIntervalSeconds = 15; | 26 const int kInitialLogIntervalSeconds = 15; |
| 30 // Interval between ongoing logs. | 27 // Interval between ongoing logs. |
| 31 const int kLogIntervalSeconds = 30 * 60; | 28 const int kLogIntervalSeconds = 30 * 60; |
| 32 | 29 |
| 33 const char kMimeType[] = "application/vnd.chrome.rappor"; | 30 const char kMimeType[] = "application/vnd.chrome.rappor"; |
| 34 | 31 |
| 35 // Constants for the RAPPOR rollout field trial. | 32 // Constants for the RAPPOR rollout field trial. |
| 36 const char kRapporRolloutFieldTrialName[] = "RapporRollout"; | 33 const char kRapporRolloutFieldTrialName[] = "RapporRollout"; |
| 37 | 34 |
| 38 // Constant for the finch parameter name for the server URL | 35 // Constant for the finch parameter name for the server URL |
| 39 const char kRapporRolloutServerUrlParam[] = "ServerUrl"; | 36 const char kRapporRolloutServerUrlParam[] = "ServerUrl"; |
| 40 | 37 |
| 41 GURL GetServerUrl() { | 38 GURL GetServerUrl() { |
| 42 return GURL(chrome_variations::GetVariationParamValue( | 39 return GURL(chrome_variations::GetVariationParamValue( |
| 43 kRapporRolloutFieldTrialName, | 40 kRapporRolloutFieldTrialName, |
| 44 kRapporRolloutServerUrlParam)); | 41 kRapporRolloutServerUrlParam)); |
| 45 } | 42 } |
| 46 | 43 |
| 47 const RapporParameters kRapporParametersForType[NUM_RAPPOR_TYPES] = { | 44 const RapporParameters kRapporParametersForType[NUM_RAPPOR_TYPES] = { |
| 48 // ETLD_PLUS_ONE_RAPPOR_TYPE | 45 // ETLD_PLUS_ONE_RAPPOR_TYPE |
| 49 {16 /* Bloom filter size bytes */, | 46 {128 /* Num cohorts */, |
| 47 16 /* Bloom filter size bytes */, |
| 50 2 /* Bloom filter hash count */, | 48 2 /* Bloom filter hash count */, |
| 51 rappor::PROBABILITY_75 /* Fake data probability */, | 49 rappor::PROBABILITY_50 /* Fake data probability */, |
| 52 rappor::PROBABILITY_50 /* Fake one probability */, | 50 rappor::PROBABILITY_50 /* Fake one probability */, |
| 53 rappor::PROBABILITY_75 /* One coin probability */, | 51 rappor::PROBABILITY_75 /* One coin probability */, |
| 54 rappor::PROBABILITY_50 /* Zero coin probability */}, | 52 rappor::PROBABILITY_25 /* Zero coin probability */}, |
| 55 }; | 53 }; |
| 56 | 54 |
| 57 } // namespace | 55 } // namespace |
| 58 | 56 |
| 59 RapporService::RapporService() : cohort_(-1) {} | 57 RapporService::RapporService() : cohort_(-1) {} |
| 60 | 58 |
| 61 RapporService::~RapporService() { | 59 RapporService::~RapporService() { |
| 62 STLDeleteValues(&metrics_map_); | 60 STLDeleteValues(&metrics_map_); |
| 63 } | 61 } |
| 64 | 62 |
| (...skipping 24 matching lines...) Expand all Loading... |
| 89 } | 87 } |
| 90 log_rotation_timer_.Start(FROM_HERE, | 88 log_rotation_timer_.Start(FROM_HERE, |
| 91 base::TimeDelta::FromSeconds(kLogIntervalSeconds), | 89 base::TimeDelta::FromSeconds(kLogIntervalSeconds), |
| 92 this, | 90 this, |
| 93 &RapporService::OnLogInterval); | 91 &RapporService::OnLogInterval); |
| 94 } | 92 } |
| 95 | 93 |
| 96 // static | 94 // static |
| 97 void RapporService::RegisterPrefs(PrefRegistrySimple* registry) { | 95 void RapporService::RegisterPrefs(PrefRegistrySimple* registry) { |
| 98 registry->RegisterStringPref(prefs::kRapporSecret, std::string()); | 96 registry->RegisterStringPref(prefs::kRapporSecret, std::string()); |
| 99 registry->RegisterIntegerPref(prefs::kRapporCohort, -1); | 97 registry->RegisterIntegerPref(prefs::kRapporCohortSeed, -1); |
| 100 } | 98 } |
| 101 | 99 |
| 102 void RapporService::LoadCohort(PrefService* pref_service) { | 100 void RapporService::LoadCohort(PrefService* pref_service) { |
| 103 DCHECK(!IsInitialized()); | 101 DCHECK(!IsInitialized()); |
| 104 cohort_ = pref_service->GetInteger(prefs::kRapporCohort); | 102 cohort_ = pref_service->GetInteger(prefs::kRapporCohortSeed); |
| 105 // If the user is already assigned to a valid cohort, we're done. | 103 // If the user is already assigned to a valid cohort, we're done. |
| 106 if (cohort_ >= 0 && cohort_ < kNumCohorts) | 104 if (cohort_ >= 0 && cohort_ < RapporParameters::kMaxCohorts) |
| 107 return; | 105 return; |
| 108 | 106 |
| 109 // This is the first time the client has started the service (or their | 107 // This is the first time the client has started the service (or their |
| 110 // preferences were corrupted). Randomly assign them to a cohort. | 108 // preferences were corrupted). Randomly assign them to a cohort. |
| 111 cohort_ = base::RandGenerator(kNumCohorts); | 109 cohort_ = base::RandGenerator(RapporParameters::kMaxCohorts); |
| 112 pref_service->SetInteger(prefs::kRapporCohort, cohort_); | 110 pref_service->SetInteger(prefs::kRapporCohortSeed, cohort_); |
| 113 } | 111 } |
| 114 | 112 |
| 115 void RapporService::LoadSecret(PrefService* pref_service) { | 113 void RapporService::LoadSecret(PrefService* pref_service) { |
| 116 DCHECK(secret_.empty()); | 114 DCHECK(secret_.empty()); |
| 117 std::string secret_base64 = pref_service->GetString(prefs::kRapporSecret); | 115 std::string secret_base64 = pref_service->GetString(prefs::kRapporSecret); |
| 118 if (!secret_base64.empty()) { | 116 if (!secret_base64.empty()) { |
| 119 bool decoded = base::Base64Decode(secret_base64, &secret_); | 117 bool decoded = base::Base64Decode(secret_base64, &secret_); |
| 120 if (decoded && secret_.size() == HmacByteVectorGenerator::kEntropyInputSize) | 118 if (decoded && secret_.size() == HmacByteVectorGenerator::kEntropyInputSize) |
| 121 return; | 119 return; |
| 122 // If the preference fails to decode, or is the wrong size, it must be | 120 // If the preference fails to decode, or is the wrong size, it must be |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 182 DCHECK_EQ(parameters.ToString(), metric->parameters().ToString()); | 180 DCHECK_EQ(parameters.ToString(), metric->parameters().ToString()); |
| 183 return metric; | 181 return metric; |
| 184 } | 182 } |
| 185 | 183 |
| 186 RapporMetric* new_metric = new RapporMetric(metric_name, parameters, cohort_); | 184 RapporMetric* new_metric = new RapporMetric(metric_name, parameters, cohort_); |
| 187 metrics_map_[metric_name] = new_metric; | 185 metrics_map_[metric_name] = new_metric; |
| 188 return new_metric; | 186 return new_metric; |
| 189 } | 187 } |
| 190 | 188 |
| 191 } // namespace rappor | 189 } // namespace rappor |
| OLD | NEW |