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