Revert "Reland c91b178b07b0d - Delete dead signin code (SigninGlobalError)"
[chromium-blink-merge.git] / chrome / browser / metrics / variations / variations_seed_store.h
blob0598e908066a0c25751507021c1799fe7498b01c
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_
8 #include <string>
10 #include "base/compiler_specific.h"
11 #include "base/gtest_prod_util.h"
12 #include "base/time/time.h"
14 class PrefService;
15 class PrefRegistrySimple;
17 namespace variations {
18 class VariationsSeed;
21 namespace chrome_variations {
23 // VariationsSeedStore is a helper class for reading and writing the variations
24 // seed from Local State.
25 class VariationsSeedStore {
26 public:
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) to local state, along
36 // with a base64-encoded digital signature for seed and the date when it was
37 // fetched. If |is_delta_compressed| is true, treats |data| as being delta
38 // compressed and attempts to decode it first using the store's seed data.
39 // The actual seed data will be base64 encoded for storage. If the string
40 // is invalid, the existing prefs are untouched and false is returned.
41 // Additionally, stores the |country_code| that was received with the seed in
42 // a separate pref. On success and if |parsed_seed| is not NULL, |parsed_seed|
43 // will be filled with the de-serialized decoded protobuf.
44 bool StoreSeedData(const std::string& data,
45 const std::string& base64_seed_signature,
46 const std::string& country_code,
47 const base::Time& date_fetched,
48 bool is_delta_compressed,
49 variations::VariationsSeed* parsed_seed);
51 // Updates |kVariationsSeedDate| and logs when previous date was from a
52 // different day.
53 void UpdateSeedDateAndLogDayChange(const base::Time& server_date_fetched);
55 // Returns the serial number of the last loaded or stored seed.
56 const std::string& variations_serial_number() const {
57 return variations_serial_number_;
60 // Returns whether the last loaded or stored seed has the country field set.
61 bool seed_has_country_code() const {
62 return seed_has_country_code_;
65 // Returns the invalid signature in base64 format, or an empty string if the
66 // signature was valid, missing, or if signature verification is disabled.
67 std::string GetInvalidSignature() const;
69 // Registers Local State prefs used by this class.
70 static void RegisterPrefs(PrefRegistrySimple* registry);
72 protected:
73 // Note: UMA histogram enum - don't re-order or remove entries.
74 enum VerifySignatureResult {
75 VARIATIONS_SEED_SIGNATURE_MISSING,
76 VARIATIONS_SEED_SIGNATURE_DECODE_FAILED,
77 VARIATIONS_SEED_SIGNATURE_INVALID_SIGNATURE,
78 VARIATIONS_SEED_SIGNATURE_INVALID_SEED,
79 VARIATIONS_SEED_SIGNATURE_VALID,
80 VARIATIONS_SEED_SIGNATURE_ENUM_SIZE,
83 // Verifies a variations seed (the serialized proto bytes) with the specified
84 // base-64 encoded signature that was received from the server and returns the
85 // result. The signature is assumed to be an "ECDSA with SHA-256" signature
86 // (see kECDSAWithSHA256AlgorithmID in the .cc file). Returns the result of
87 // signature verification or VARIATIONS_SEED_SIGNATURE_ENUM_SIZE if signature
88 // verification is not enabled.
89 virtual VariationsSeedStore::VerifySignatureResult VerifySeedSignature(
90 const std::string& seed_bytes,
91 const std::string& base64_seed_signature);
93 private:
94 FRIEND_TEST_ALL_PREFIXES(VariationsSeedStoreTest, VerifySeedSignature);
95 FRIEND_TEST_ALL_PREFIXES(VariationsSeedStoreTest, ApplyDeltaPatch);
97 // Clears all prefs related to variations seed storage.
98 void ClearPrefs();
100 // Reads the variations seed data from prefs; returns true on success.
101 bool ReadSeedData(std::string* seed_data);
103 // Internal version of |StoreSeedData()| that assumes |seed_data| is not delta
104 // compressed.
105 bool StoreSeedDataNoDelta(
106 const std::string& seed_data,
107 const std::string& base64_seed_signature,
108 const std::string& country_code,
109 const base::Time& date_fetched,
110 variations::VariationsSeed* parsed_seed);
112 // Applies a delta-compressed |patch| to |existing_data|, producing the result
113 // in |output|. Returns whether the operation was successful.
114 static bool ApplyDeltaPatch(const std::string& existing_data,
115 const std::string& patch,
116 std::string* output);
118 // The pref service used to persist the variations seed.
119 PrefService* local_state_;
121 // Cached serial number from the most recently fetched variations seed.
122 std::string variations_serial_number_;
124 // Whether the most recently fetched variations seed has the country code
125 // field set.
126 bool seed_has_country_code_;
128 // Keeps track of an invalid signature.
129 std::string invalid_base64_signature_;
131 DISALLOW_COPY_AND_ASSIGN(VariationsSeedStore);
134 } // namespace chrome_variations
136 #endif // CHROME_BROWSER_METRICS_VARIATIONS_VARIATIONS_SEED_STORE_H_