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 3 // found in the LICENSE file. 4 5 #ifndef CHROME_BROWSER_METRICS_VARIATIONS_VARIATIONS_SEED_STORE_H_ 6 #define CHROME_BROWSER_METRICS_VARIATIONS_VARIATIONS_SEED_STORE_H_ 7 8 #include <string> 9 10 #include "base/compiler_specific.h" 11 #include "base/gtest_prod_util.h" 12 #include "base/time/time.h" 13 14 class PrefService; 15 class PrefRegistrySimple; 16 17 namespace chrome_variations { 18 19 class VariationsSeed; 20 21 // VariationsSeedStore is a helper class for reading and writing the variations 22 // seed from Local State. 23 class VariationsSeedStore { 24 public: 25 explicit VariationsSeedStore(PrefService* local_state); 26 virtual ~VariationsSeedStore(); 27 28 // Loads the variations seed data from local state into |seed|. If there is a 29 // problem with loading, the pref value is cleared and false is returned. If 30 // successful, |seed| will contain the loaded data and true is returned. 31 bool LoadSeed(VariationsSeed* seed); 32 33 // Stores the given seed data (serialized protobuf data) to local state, along 34 // with a base64-encoded digital signature for seed and the date when it was 35 // fetched. The |seed_data| will be base64 encoded for storage. If the string 36 // is invalid, the existing prefs are left as is and false is returned. On 37 // success and if |parsed_seed| is not NULL, |parsed_seed| will be filled 38 // with the de-serialized protobuf decoded from |seed_data|. 39 bool StoreSeedData(const std::string& seed_data, 40 const std::string& base64_seed_signature, 41 const base::Time& date_fetched, 42 VariationsSeed* parsed_seed); 43 44 // Updates |kVariationsSeedDate| and logs when previous date was from a 45 // different day. 46 void UpdateSeedDateAndLogDayChange(const base::Time& server_date_fetched); 47 48 // Returns the serial number of the last loaded or stored seed. variations_serial_number()49 const std::string& variations_serial_number() const { 50 return variations_serial_number_; 51 } 52 53 // Registers Local State prefs used by this class. 54 static void RegisterPrefs(PrefRegistrySimple* registry); 55 56 protected: 57 // Note: UMA histogram enum - don't re-order or remove entries. 58 enum VerifySignatureResult { 59 VARIATIONS_SEED_SIGNATURE_MISSING, 60 VARIATIONS_SEED_SIGNATURE_DECODE_FAILED, 61 VARIATIONS_SEED_SIGNATURE_INVALID_SIGNATURE, 62 VARIATIONS_SEED_SIGNATURE_INVALID_SEED, 63 VARIATIONS_SEED_SIGNATURE_VALID, 64 VARIATIONS_SEED_SIGNATURE_ENUM_SIZE, 65 }; 66 67 // Verifies a variations seed (the serialized proto bytes) with the specified 68 // base-64 encoded signature that was received from the server and returns the 69 // result. The signature is assumed to be an "ECDSA with SHA-256" signature 70 // (see kECDSAWithSHA256AlgorithmID in the .cc file). Returns the result of 71 // signature verification or VARIATIONS_SEED_SIGNATURE_ENUM_SIZE if signature 72 // verification is not enabled. 73 virtual VariationsSeedStore::VerifySignatureResult VerifySeedSignature( 74 const std::string& seed_bytes, 75 const std::string& base64_seed_signature); 76 77 private: 78 FRIEND_TEST_ALL_PREFIXES(VariationsSeedStoreTest, VerifySeedSignature); 79 80 // Clears all prefs related to variations seed storage. 81 void ClearPrefs(); 82 83 // The pref service used to persist the variations seed. 84 PrefService* local_state_; 85 86 // Cached serial number from the most recently fetched variations seed. 87 std::string variations_serial_number_; 88 89 DISALLOW_COPY_AND_ASSIGN(VariationsSeedStore); 90 }; 91 92 } // namespace chrome_variations 93 94 #endif // CHROME_BROWSER_METRICS_VARIATIONS_VARIATIONS_SEED_STORE_H_ 95