OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_METRICS_VARIATIONS_VARIATIONS_SEED_STORE_H_ | 5 #ifndef CHROME_BROWSER_METRICS_VARIATIONS_VARIATIONS_SEED_STORE_H_ |
6 #define CHROME_BROWSER_METRICS_VARIATIONS_VARIATIONS_SEED_STORE_H_ | 6 #define CHROME_BROWSER_METRICS_VARIATIONS_VARIATIONS_SEED_STORE_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
(...skipping 14 matching lines...) Expand all Loading... |
25 class VariationsSeedStore { | 25 class VariationsSeedStore { |
26 public: | 26 public: |
27 explicit VariationsSeedStore(PrefService* local_state); | 27 explicit VariationsSeedStore(PrefService* local_state); |
28 virtual ~VariationsSeedStore(); | 28 virtual ~VariationsSeedStore(); |
29 | 29 |
30 // Loads the variations seed data from local state into |seed|. If there is a | 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 | 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. | 32 // successful, |seed| will contain the loaded data and true is returned. |
33 bool LoadSeed(variations::VariationsSeed* seed); | 33 bool LoadSeed(variations::VariationsSeed* seed); |
34 | 34 |
35 // Stores the given seed data (serialized protobuf data) to local state, along | 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 | 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 | 37 // fetched. If |is_delta_compressed| is true, treats |data| as being delta |
38 // is invalid, the existing prefs are left as is and false is returned. On | 38 // compressed and attempts to decode it first using the store's seed data. |
39 // success and if |parsed_seed| is not NULL, |parsed_seed| will be filled | 39 // The actual seed data will be base64 encoded for storage. If the string |
40 // with the de-serialized protobuf decoded from |seed_data|. | 40 // is invalid, the existing prefs are untouched and false is returned. |
41 bool StoreSeedData(const std::string& seed_data, | 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, |
42 const std::string& base64_seed_signature, | 45 const std::string& base64_seed_signature, |
| 46 const std::string& country_code, |
43 const base::Time& date_fetched, | 47 const base::Time& date_fetched, |
| 48 bool is_delta_compressed, |
44 variations::VariationsSeed* parsed_seed); | 49 variations::VariationsSeed* parsed_seed); |
45 | 50 |
46 // Updates |kVariationsSeedDate| and logs when previous date was from a | 51 // Updates |kVariationsSeedDate| and logs when previous date was from a |
47 // different day. | 52 // different day. |
48 void UpdateSeedDateAndLogDayChange(const base::Time& server_date_fetched); | 53 void UpdateSeedDateAndLogDayChange(const base::Time& server_date_fetched); |
49 | 54 |
50 // Returns the serial number of the last loaded or stored seed. | 55 // Returns the serial number of the last loaded or stored seed. |
51 const std::string& variations_serial_number() const { | 56 const std::string& variations_serial_number() const { |
52 return variations_serial_number_; | 57 return variations_serial_number_; |
53 } | 58 } |
54 | 59 |
55 // Registers Local State prefs used by this class. | 60 // Returns whether the last loaded or stored seed has the country field set. |
56 static void RegisterPrefs(PrefRegistrySimple* registry); | 61 bool seed_has_country_code() const { |
| 62 return seed_has_country_code_; |
| 63 } |
57 | 64 |
58 // Returns the invalid signature in base64 format, or an empty string if the | 65 // Returns the invalid signature in base64 format, or an empty string if the |
59 // signature was valid, missing, or if signature verification is disabled. | 66 // signature was valid, missing, or if signature verification is disabled. |
60 std::string GetInvalidSignature() const; | 67 std::string GetInvalidSignature() const; |
61 | 68 |
| 69 // Registers Local State prefs used by this class. |
| 70 static void RegisterPrefs(PrefRegistrySimple* registry); |
| 71 |
62 protected: | 72 protected: |
63 // Note: UMA histogram enum - don't re-order or remove entries. | 73 // Note: UMA histogram enum - don't re-order or remove entries. |
64 enum VerifySignatureResult { | 74 enum VerifySignatureResult { |
65 VARIATIONS_SEED_SIGNATURE_MISSING, | 75 VARIATIONS_SEED_SIGNATURE_MISSING, |
66 VARIATIONS_SEED_SIGNATURE_DECODE_FAILED, | 76 VARIATIONS_SEED_SIGNATURE_DECODE_FAILED, |
67 VARIATIONS_SEED_SIGNATURE_INVALID_SIGNATURE, | 77 VARIATIONS_SEED_SIGNATURE_INVALID_SIGNATURE, |
68 VARIATIONS_SEED_SIGNATURE_INVALID_SEED, | 78 VARIATIONS_SEED_SIGNATURE_INVALID_SEED, |
69 VARIATIONS_SEED_SIGNATURE_VALID, | 79 VARIATIONS_SEED_SIGNATURE_VALID, |
70 VARIATIONS_SEED_SIGNATURE_ENUM_SIZE, | 80 VARIATIONS_SEED_SIGNATURE_ENUM_SIZE, |
71 }; | 81 }; |
72 | 82 |
73 // Verifies a variations seed (the serialized proto bytes) with the specified | 83 // 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 | 84 // 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 | 85 // result. The signature is assumed to be an "ECDSA with SHA-256" signature |
76 // (see kECDSAWithSHA256AlgorithmID in the .cc file). Returns the result of | 86 // (see kECDSAWithSHA256AlgorithmID in the .cc file). Returns the result of |
77 // signature verification or VARIATIONS_SEED_SIGNATURE_ENUM_SIZE if signature | 87 // signature verification or VARIATIONS_SEED_SIGNATURE_ENUM_SIZE if signature |
78 // verification is not enabled. | 88 // verification is not enabled. |
79 virtual VariationsSeedStore::VerifySignatureResult VerifySeedSignature( | 89 virtual VariationsSeedStore::VerifySignatureResult VerifySeedSignature( |
80 const std::string& seed_bytes, | 90 const std::string& seed_bytes, |
81 const std::string& base64_seed_signature); | 91 const std::string& base64_seed_signature); |
82 | 92 |
83 private: | 93 private: |
84 FRIEND_TEST_ALL_PREFIXES(VariationsSeedStoreTest, VerifySeedSignature); | 94 FRIEND_TEST_ALL_PREFIXES(VariationsSeedStoreTest, VerifySeedSignature); |
| 95 FRIEND_TEST_ALL_PREFIXES(VariationsSeedStoreTest, ApplyDeltaPatch); |
85 | 96 |
86 // Clears all prefs related to variations seed storage. | 97 // Clears all prefs related to variations seed storage. |
87 void ClearPrefs(); | 98 void ClearPrefs(); |
88 | 99 |
89 // Reads the variations seed data from prefs; returns true on success. | 100 // Reads the variations seed data from prefs; returns true on success. |
90 bool ReadSeedData(std::string* seed_data); | 101 bool ReadSeedData(std::string* seed_data); |
91 | 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 |
92 // The pref service used to persist the variations seed. | 118 // The pref service used to persist the variations seed. |
93 PrefService* local_state_; | 119 PrefService* local_state_; |
94 | 120 |
95 // Cached serial number from the most recently fetched variations seed. | 121 // Cached serial number from the most recently fetched variations seed. |
96 std::string variations_serial_number_; | 122 std::string variations_serial_number_; |
97 | 123 |
| 124 // Whether the most recently fetched variations seed has the country code |
| 125 // field set. |
| 126 bool seed_has_country_code_; |
| 127 |
98 // Keeps track of an invalid signature. | 128 // Keeps track of an invalid signature. |
99 std::string invalid_base64_signature_; | 129 std::string invalid_base64_signature_; |
100 | 130 |
101 DISALLOW_COPY_AND_ASSIGN(VariationsSeedStore); | 131 DISALLOW_COPY_AND_ASSIGN(VariationsSeedStore); |
102 }; | 132 }; |
103 | 133 |
104 } // namespace chrome_variations | 134 } // namespace chrome_variations |
105 | 135 |
106 #endif // CHROME_BROWSER_METRICS_VARIATIONS_VARIATIONS_SEED_STORE_H_ | 136 #endif // CHROME_BROWSER_METRICS_VARIATIONS_VARIATIONS_SEED_STORE_H_ |
OLD | NEW |