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/variations/variations_seed_store.h" | 5 #include "components/variations/variations_seed_store.h" |
6 | 6 |
7 #include "base/base64.h" | 7 #include "base/base64.h" |
8 #include "base/metrics/histogram_macros.h" | 8 #include "base/metrics/histogram_macros.h" |
9 #include "base/numerics/safe_math.h" | 9 #include "base/numerics/safe_math.h" |
10 #include "base/prefs/pref_registry_simple.h" | 10 #include "base/prefs/pref_registry_simple.h" |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
145 VariationsSeedStore::VariationsSeedStore(PrefService* local_state) | 145 VariationsSeedStore::VariationsSeedStore(PrefService* local_state) |
146 : local_state_(local_state), seed_has_country_code_(false) { | 146 : local_state_(local_state), seed_has_country_code_(false) { |
147 } | 147 } |
148 | 148 |
149 VariationsSeedStore::~VariationsSeedStore() { | 149 VariationsSeedStore::~VariationsSeedStore() { |
150 } | 150 } |
151 | 151 |
152 bool VariationsSeedStore::LoadSeed(variations::VariationsSeed* seed) { | 152 bool VariationsSeedStore::LoadSeed(variations::VariationsSeed* seed) { |
153 invalid_base64_signature_.clear(); | 153 invalid_base64_signature_.clear(); |
154 | 154 |
| 155 #if defined(OS_ANDROID) |
| 156 if (!local_state_->HasPrefPath(prefs::kVariationsSeedSignature)) |
| 157 ImportFirstRunJavaSeed(); |
| 158 #endif // OS_ANDROID |
| 159 |
155 std::string seed_data; | 160 std::string seed_data; |
156 if (!ReadSeedData(&seed_data)) | 161 if (!ReadSeedData(&seed_data)) |
157 return false; | 162 return false; |
158 | 163 |
159 const std::string base64_seed_signature = | 164 const std::string base64_seed_signature = |
160 local_state_->GetString(prefs::kVariationsSeedSignature); | 165 local_state_->GetString(prefs::kVariationsSeedSignature); |
161 const VerifySignatureResult result = | 166 const VerifySignatureResult result = |
162 VerifySeedSignature(seed_data, base64_seed_signature); | 167 VerifySeedSignature(seed_data, base64_seed_signature); |
163 if (result != VARIATIONS_SEED_SIGNATURE_ENUM_SIZE) { | 168 if (result != VARIATIONS_SEED_SIGNATURE_ENUM_SIZE) { |
164 UMA_HISTOGRAM_ENUMERATION("Variations.LoadSeedSignature", result, | 169 UMA_HISTOGRAM_ENUMERATION("Variations.LoadSeedSignature", result, |
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
329 return VARIATIONS_SEED_SIGNATURE_INVALID_SEED; | 334 return VARIATIONS_SEED_SIGNATURE_INVALID_SEED; |
330 } | 335 } |
331 | 336 |
332 void VariationsSeedStore::ClearPrefs() { | 337 void VariationsSeedStore::ClearPrefs() { |
333 local_state_->ClearPref(prefs::kVariationsCompressedSeed); | 338 local_state_->ClearPref(prefs::kVariationsCompressedSeed); |
334 local_state_->ClearPref(prefs::kVariationsSeed); | 339 local_state_->ClearPref(prefs::kVariationsSeed); |
335 local_state_->ClearPref(prefs::kVariationsSeedDate); | 340 local_state_->ClearPref(prefs::kVariationsSeedDate); |
336 local_state_->ClearPref(prefs::kVariationsSeedSignature); | 341 local_state_->ClearPref(prefs::kVariationsSeedSignature); |
337 } | 342 } |
338 | 343 |
| 344 #if defined(OS_ANDROID) |
| 345 void VariationsSeedStore::ImportFirstRunJavaSeed() { |
| 346 DVLOG(1) << "Importing first run seed from Java preferences."; |
| 347 std::string seed_data; |
| 348 std::string seed_signature; |
| 349 std::string seed_country; |
| 350 if (!get_variations_first_run_seed_.is_null()) { |
| 351 get_variations_first_run_seed_.Run(&seed_data, &seed_signature, |
| 352 &seed_country); |
| 353 } |
| 354 |
| 355 if (seed_data.empty()) { |
| 356 // TODO(agulenko): add a new UMA histogram for the state of failing |
| 357 // to import seed from Java preferences during Chrome first run. |
| 358 return; |
| 359 } |
| 360 |
| 361 // TODO(agulenko): Pull actual time from the response. |
| 362 base::Time current_time = base::Time::Now(); |
| 363 |
| 364 // TODO(agulenko): Support gzip compressed seed. |
| 365 if (!StoreSeedData(seed_data, seed_signature, seed_country, current_time, |
| 366 false, false, nullptr)) { |
| 367 LOG(WARNING) << "First run variations seed is invalid."; |
| 368 return; |
| 369 } |
| 370 // TODO(agulenko): Clear Java prefs. |
| 371 } |
| 372 #endif // OS_ANDROID |
| 373 |
339 bool VariationsSeedStore::ReadSeedData(std::string* seed_data) { | 374 bool VariationsSeedStore::ReadSeedData(std::string* seed_data) { |
340 std::string base64_seed_data = | 375 std::string base64_seed_data = |
341 local_state_->GetString(prefs::kVariationsCompressedSeed); | 376 local_state_->GetString(prefs::kVariationsCompressedSeed); |
342 const bool is_compressed = !base64_seed_data.empty(); | 377 const bool is_compressed = !base64_seed_data.empty(); |
343 // If there's no compressed seed, fall back to the uncompressed one. | 378 // If there's no compressed seed, fall back to the uncompressed one. |
344 if (!is_compressed) | 379 if (!is_compressed) |
345 base64_seed_data = local_state_->GetString(prefs::kVariationsSeed); | 380 base64_seed_data = local_state_->GetString(prefs::kVariationsSeed); |
346 | 381 |
347 if (base64_seed_data.empty()) { | 382 if (base64_seed_data.empty()) { |
348 RecordVariationSeedEmptyHistogram(VARIATIONS_SEED_EMPTY); | 383 RecordVariationSeedEmptyHistogram(VARIATIONS_SEED_EMPTY); |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
477 } | 512 } |
478 return true; | 513 return true; |
479 } | 514 } |
480 | 515 |
481 void VariationsSeedStore::ReportUnsupportedSeedFormatError() { | 516 void VariationsSeedStore::ReportUnsupportedSeedFormatError() { |
482 RecordSeedStoreHistogram( | 517 RecordSeedStoreHistogram( |
483 VARIATIONS_SEED_STORE_FAILED_UNSUPPORTED_SEED_FORMAT); | 518 VARIATIONS_SEED_STORE_FAILED_UNSUPPORTED_SEED_FORMAT); |
484 } | 519 } |
485 | 520 |
486 } // namespace variations | 521 } // namespace variations |
OLD | NEW |