| 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" |
| 11 #include "base/prefs/pref_service.h" | 11 #include "base/prefs/pref_service.h" |
| 12 #include "base/sha1.h" | 12 #include "base/sha1.h" |
| 13 #include "base/strings/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.h" |
| 14 #include "components/compression/compression_utils.h" | 14 #include "components/compression/compression_utils.h" |
| 15 #include "components/variations/pref_names.h" | 15 #include "components/variations/pref_names.h" |
| 16 #include "components/variations/proto/variations_seed.pb.h" | 16 #include "components/variations/proto/variations_seed.pb.h" |
| 17 #include "crypto/signature_verifier.h" | 17 #include "crypto/signature_verifier.h" |
| 18 #include "third_party/protobuf/src/google/protobuf/io/coded_stream.h" | 18 #include "third_party/protobuf/src/google/protobuf/io/coded_stream.h" |
| 19 | 19 |
| 20 #if defined(OS_ANDROID) |
| 21 #include "components/variations/android/variations_seed_bridge.h" |
| 22 #endif // OS_ANDROID |
| 23 |
| 20 namespace variations { | 24 namespace variations { |
| 21 | 25 |
| 22 namespace { | 26 namespace { |
| 23 | 27 |
| 24 // Signature verification is disabled on mobile platforms for now, since it | 28 // Signature verification is disabled on mobile platforms for now, since it |
| 25 // adds about ~15ms to the startup time on mobile (vs. a couple ms on desktop). | 29 // adds about ~15ms to the startup time on mobile (vs. a couple ms on desktop). |
| 26 bool SignatureVerificationEnabled() { | 30 bool SignatureVerificationEnabled() { |
| 27 #if defined(OS_IOS) || defined(OS_ANDROID) | 31 #if defined(OS_IOS) || defined(OS_ANDROID) |
| 28 return false; | 32 return false; |
| 29 #else | 33 #else |
| (...skipping 347 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 377 // TODO(agulenko): Pull actual time from the response. | 381 // TODO(agulenko): Pull actual time from the response. |
| 378 base::Time current_time = base::Time::Now(); | 382 base::Time current_time = base::Time::Now(); |
| 379 | 383 |
| 380 // TODO(agulenko): Support gzip compressed seed. | 384 // TODO(agulenko): Support gzip compressed seed. |
| 381 if (!StoreSeedData(seed_data, seed_signature, seed_country, current_time, | 385 if (!StoreSeedData(seed_data, seed_signature, seed_country, current_time, |
| 382 false, false, nullptr)) { | 386 false, false, nullptr)) { |
| 383 RecordFirstRunResult(FIRST_RUN_SEED_IMPORT_FAIL_STORE_FAILED); | 387 RecordFirstRunResult(FIRST_RUN_SEED_IMPORT_FAIL_STORE_FAILED); |
| 384 LOG(WARNING) << "First run variations seed is invalid."; | 388 LOG(WARNING) << "First run variations seed is invalid."; |
| 385 return; | 389 return; |
| 386 } | 390 } |
| 387 // TODO(agulenko): Clear Java prefs. | |
| 388 RecordFirstRunResult(FIRST_RUN_SEED_IMPORT_SUCCESS); | 391 RecordFirstRunResult(FIRST_RUN_SEED_IMPORT_SUCCESS); |
| 389 } | 392 } |
| 390 #endif // OS_ANDROID | 393 #endif // OS_ANDROID |
| 391 | 394 |
| 392 bool VariationsSeedStore::ReadSeedData(std::string* seed_data) { | 395 bool VariationsSeedStore::ReadSeedData(std::string* seed_data) { |
| 393 std::string base64_seed_data = | 396 std::string base64_seed_data = |
| 394 local_state_->GetString(prefs::kVariationsCompressedSeed); | 397 local_state_->GetString(prefs::kVariationsCompressedSeed); |
| 395 const bool is_compressed = !base64_seed_data.empty(); | 398 const bool is_compressed = !base64_seed_data.empty(); |
| 396 // If there's no compressed seed, fall back to the uncompressed one. | 399 // If there's no compressed seed, fall back to the uncompressed one. |
| 397 if (!is_compressed) | 400 if (!is_compressed) |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 457 return false; | 460 return false; |
| 458 } | 461 } |
| 459 | 462 |
| 460 std::string base64_seed_data; | 463 std::string base64_seed_data; |
| 461 base::Base64Encode(compressed_seed_data, &base64_seed_data); | 464 base::Base64Encode(compressed_seed_data, &base64_seed_data); |
| 462 | 465 |
| 463 // TODO(asvitkine): This pref is no longer being used. Remove it completely | 466 // TODO(asvitkine): This pref is no longer being used. Remove it completely |
| 464 // in M45+. | 467 // in M45+. |
| 465 local_state_->ClearPref(prefs::kVariationsSeed); | 468 local_state_->ClearPref(prefs::kVariationsSeed); |
| 466 | 469 |
| 470 #if defined(OS_ANDROID) |
| 471 // If currently we do not have any stored pref then we mark seed storing as |
| 472 // successful on the Java side of Chrome for Android to avoid repeated seed |
| 473 // fetches and clear preferences on the Java side. |
| 474 if (local_state_->GetString(prefs::kVariationsCompressedSeed).empty()) { |
| 475 android::MarkVariationsSeedAsStored(); |
| 476 android::ClearJavaFirstRunPrefs(); |
| 477 } |
| 478 #endif |
| 479 |
| 467 // Update the saved country code only if one was returned from the server. | 480 // Update the saved country code only if one was returned from the server. |
| 468 // Prefer the country code that was transmitted in the header over the one in | 481 // Prefer the country code that was transmitted in the header over the one in |
| 469 // the seed (which is deprecated). | 482 // the seed (which is deprecated). |
| 470 if (!country_code.empty()) | 483 if (!country_code.empty()) |
| 471 local_state_->SetString(prefs::kVariationsCountry, country_code); | 484 local_state_->SetString(prefs::kVariationsCountry, country_code); |
| 472 else if (seed.has_country_code()) | 485 else if (seed.has_country_code()) |
| 473 local_state_->SetString(prefs::kVariationsCountry, seed.country_code()); | 486 local_state_->SetString(prefs::kVariationsCountry, seed.country_code()); |
| 474 | 487 |
| 475 local_state_->SetString(prefs::kVariationsCompressedSeed, base64_seed_data); | 488 local_state_->SetString(prefs::kVariationsCompressedSeed, base64_seed_data); |
| 476 UpdateSeedDateAndLogDayChange(date_fetched); | 489 UpdateSeedDateAndLogDayChange(date_fetched); |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 530 } | 543 } |
| 531 return true; | 544 return true; |
| 532 } | 545 } |
| 533 | 546 |
| 534 void VariationsSeedStore::ReportUnsupportedSeedFormatError() { | 547 void VariationsSeedStore::ReportUnsupportedSeedFormatError() { |
| 535 RecordSeedStoreHistogram( | 548 RecordSeedStoreHistogram( |
| 536 VARIATIONS_SEED_STORE_FAILED_UNSUPPORTED_SEED_FORMAT); | 549 VARIATIONS_SEED_STORE_FAILED_UNSUPPORTED_SEED_FORMAT); |
| 537 } | 550 } |
| 538 | 551 |
| 539 } // namespace variations | 552 } // namespace variations |
| OLD | NEW |