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
);
58 // Returns the invalid signature in base64 format, or an empty string if the
59 // signature was valid, missing, or if signature verification is disabled.
60 std::string
GetInvalidSignature() const;
63 // Note: UMA histogram enum - don't re-order or remove entries.
64 enum VerifySignatureResult
{
65 VARIATIONS_SEED_SIGNATURE_MISSING
,
66 VARIATIONS_SEED_SIGNATURE_DECODE_FAILED
,
67 VARIATIONS_SEED_SIGNATURE_INVALID_SIGNATURE
,
68 VARIATIONS_SEED_SIGNATURE_INVALID_SEED
,
69 VARIATIONS_SEED_SIGNATURE_VALID
,
70 VARIATIONS_SEED_SIGNATURE_ENUM_SIZE
,
73 // Verifies a variations seed (the serialized proto bytes) with the specified
74 // base-64 encoded signature that was received from the server and returns the
75 // result. The signature is assumed to be an "ECDSA with SHA-256" signature
76 // (see kECDSAWithSHA256AlgorithmID in the .cc file). Returns the result of
77 // signature verification or VARIATIONS_SEED_SIGNATURE_ENUM_SIZE if signature
78 // verification is not enabled.
79 virtual VariationsSeedStore::VerifySignatureResult
VerifySeedSignature(
80 const std::string
& seed_bytes
,
81 const std::string
& base64_seed_signature
);
84 FRIEND_TEST_ALL_PREFIXES(VariationsSeedStoreTest
, VerifySeedSignature
);
86 // Clears all prefs related to variations seed storage.
89 // Reads the variations seed data from prefs; returns true on success.
90 bool ReadSeedData(std::string
* seed_data
);
92 // The pref service used to persist the variations seed.
93 PrefService
* local_state_
;
95 // Cached serial number from the most recently fetched variations seed.
96 std::string variations_serial_number_
;
98 // Keeps track of an invalid signature.
99 std::string invalid_base64_signature_
;
101 DISALLOW_COPY_AND_ASSIGN(VariationsSeedStore
);
104 } // namespace chrome_variations
106 #endif // CHROME_BROWSER_METRICS_VARIATIONS_VARIATIONS_SEED_STORE_H_