OLD | NEW |
| (Empty) |
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 variations { | |
18 class VariationsSeed; | |
19 } | |
20 | |
21 namespace chrome_variations { | |
22 | |
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(); | |
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) 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); | |
50 | |
51 // Updates |kVariationsSeedDate| and logs when previous date was from a | |
52 // different day. | |
53 void UpdateSeedDateAndLogDayChange(const base::Time& server_date_fetched); | |
54 | |
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_; | |
58 } | |
59 | |
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_; | |
63 } | |
64 | |
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; | |
68 | |
69 // Registers Local State prefs used by this class. | |
70 static void RegisterPrefs(PrefRegistrySimple* registry); | |
71 | |
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, | |
81 }; | |
82 | |
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); | |
92 | |
93 private: | |
94 FRIEND_TEST_ALL_PREFIXES(VariationsSeedStoreTest, VerifySeedSignature); | |
95 FRIEND_TEST_ALL_PREFIXES(VariationsSeedStoreTest, ApplyDeltaPatch); | |
96 | |
97 // Clears all prefs related to variations seed storage. | |
98 void ClearPrefs(); | |
99 | |
100 // Reads the variations seed data from prefs; returns true on success. | |
101 bool ReadSeedData(std::string* seed_data); | |
102 | |
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); | |
111 | |
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); | |
117 | |
118 // The pref service used to persist the variations seed. | |
119 PrefService* local_state_; | |
120 | |
121 // Cached serial number from the most recently fetched variations seed. | |
122 std::string variations_serial_number_; | |
123 | |
124 // Whether the most recently fetched variations seed has the country code | |
125 // field set. | |
126 bool seed_has_country_code_; | |
127 | |
128 // Keeps track of an invalid signature. | |
129 std::string invalid_base64_signature_; | |
130 | |
131 DISALLOW_COPY_AND_ASSIGN(VariationsSeedStore); | |
132 }; | |
133 | |
134 } // namespace chrome_variations | |
135 | |
136 #endif // CHROME_BROWSER_METRICS_VARIATIONS_VARIATIONS_SEED_STORE_H_ | |
OLD | NEW |