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.
5 #ifndef CHROME_BROWSER_METRICS_VARIATIONS_VARIATIONS_SEED_STORE_H_
6 #define CHROME_BROWSER_METRICS_VARIATIONS_VARIATIONS_SEED_STORE_H_
10 #include "base/compiler_specific.h"
11 #include "base/gtest_prod_util.h"
12 #include "base/time/time.h"
15 class PrefRegistrySimple
;
17 namespace variations
{
21 namespace chrome_variations
{
23 // VariationsSeedStore is a helper class for reading and writing the variations
24 // seed from Local State.
25 class VariationsSeedStore
{
27 explicit VariationsSeedStore(PrefService
* local_state
);
28 virtual ~VariationsSeedStore();
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
);
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
);
46 // Updates |kVariationsSeedDate| and logs when previous date was from a
48 void UpdateSeedDateAndLogDayChange(const base::Time
& server_date_fetched
);
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_
;
55 // Registers Local State prefs used by this class.
56 static void RegisterPrefs(PrefRegistrySimple
* registry
);
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
,
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
);
80 FRIEND_TEST_ALL_PREFIXES(VariationsSeedStoreTest
, VerifySeedSignature
);
82 // Clears all prefs related to variations seed storage.
85 // The pref service used to persist the variations seed.
86 PrefService
* local_state_
;
88 // Cached serial number from the most recently fetched variations seed.
89 std::string variations_serial_number_
;
91 DISALLOW_COPY_AND_ASSIGN(VariationsSeedStore
);
94 } // namespace chrome_variations
96 #endif // CHROME_BROWSER_METRICS_VARIATIONS_VARIATIONS_SEED_STORE_H_