| 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 COMPONENTS_VARIATIONS_VARIATIONS_SEED_STORE_H_ | 5 #ifndef COMPONENTS_VARIATIONS_VARIATIONS_SEED_STORE_H_ |
| 6 #define COMPONENTS_VARIATIONS_VARIATIONS_SEED_STORE_H_ | 6 #define COMPONENTS_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" |
| 11 #include "base/gtest_prod_util.h" | 11 #include "base/gtest_prod_util.h" |
| 12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/time/time.h" | 13 #include "base/time/time.h" |
| 14 #include "build/build_config.h" | 14 #include "build/build_config.h" |
| 15 #include "components/variations/metrics.h" |
| 15 | 16 |
| 16 class PrefService; | 17 class PrefService; |
| 17 class PrefRegistrySimple; | 18 class PrefRegistrySimple; |
| 18 | 19 |
| 19 namespace variations { | 20 namespace variations { |
| 20 class VariationsSeed; | 21 class VariationsSeed; |
| 21 } | 22 } |
| 22 | 23 |
| 23 namespace variations { | 24 namespace variations { |
| 24 | 25 |
| 25 // VariationsSeedStore is a helper class for reading and writing the variations | 26 // VariationsSeedStore is a helper class for reading and writing the variations |
| 26 // seed from Local State. | 27 // seed from Local State. |
| 27 class VariationsSeedStore { | 28 class VariationsSeedStore { |
| 28 public: | 29 public: |
| 29 explicit VariationsSeedStore(PrefService* local_state); | 30 explicit VariationsSeedStore(PrefService* local_state); |
| 30 virtual ~VariationsSeedStore(); | 31 virtual ~VariationsSeedStore(); |
| 31 | 32 |
| 32 // Loads the variations seed data from local state into |seed|. If there is a | 33 // Loads the variations seed data from local state into |seed|. If there is a |
| 33 // problem with loading, the pref value is cleared and false is returned. If | 34 // problem with loading, the pref value is cleared and false is returned. If |
| 34 // successful, |seed| will contain the loaded data and true is returned. | 35 // successful, |seed| will contain the loaded data and true is returned. |
| 35 bool LoadSeed(variations::VariationsSeed* seed); | 36 bool LoadSeed(variations::VariationsSeed* seed) WARN_UNUSED_RESULT; |
| 36 | 37 |
| 37 // Stores the given seed |data| (serialized protobuf) to local state, along | 38 // Stores the given seed |data| (serialized protobuf) to local state, along |
| 38 // with a base64-encoded digital signature for seed and the date when it was | 39 // with a base64-encoded digital signature for seed and the date when it was |
| 39 // fetched. If |is_gzip_compressed| is true, treats |data| as being gzip | 40 // fetched. If |is_gzip_compressed| is true, treats |data| as being gzip |
| 40 // compressed and decompresses it before any other processing. | 41 // compressed and decompresses it before any other processing. |
| 41 // If |is_delta_compressed| is true, treats |data| as being delta | 42 // If |is_delta_compressed| is true, treats |data| as being delta |
| 42 // compressed and attempts to decode it first using the store's seed data. | 43 // compressed and attempts to decode it first using the store's seed data. |
| 43 // The actual seed data will be base64 encoded for storage. If the string | 44 // The actual seed data will be base64 encoded for storage. If the string |
| 44 // is invalid, the existing prefs are untouched and false is returned. | 45 // is invalid, the existing prefs are untouched and false is returned. |
| 45 // Additionally, stores the |country_code| that was received with the seed in | 46 // Additionally, stores the |country_code| that was received with the seed in |
| 46 // a separate pref. On success and if |parsed_seed| is not NULL, |parsed_seed| | 47 // a separate pref. On success and if |parsed_seed| is not NULL, |parsed_seed| |
| 47 // will be filled with the de-serialized decoded protobuf. | 48 // will be filled with the de-serialized decoded protobuf. |
| 48 bool StoreSeedData(const std::string& data, | 49 bool StoreSeedData(const std::string& data, |
| 49 const std::string& base64_seed_signature, | 50 const std::string& base64_seed_signature, |
| 50 const std::string& country_code, | 51 const std::string& country_code, |
| 51 const base::Time& date_fetched, | 52 const base::Time& date_fetched, |
| 52 bool is_delta_compressed, | 53 bool is_delta_compressed, |
| 53 bool is_gzip_compressed, | 54 bool is_gzip_compressed, |
| 54 variations::VariationsSeed* parsed_seed); | 55 variations::VariationsSeed* parsed_seed) |
| 56 WARN_UNUSED_RESULT; |
| 55 | 57 |
| 56 // Updates |kVariationsSeedDate| and logs when previous date was from a | 58 // Updates |kVariationsSeedDate| and logs when previous date was from a |
| 57 // different day. | 59 // different day. |
| 58 void UpdateSeedDateAndLogDayChange(const base::Time& server_date_fetched); | 60 void UpdateSeedDateAndLogDayChange(const base::Time& server_date_fetched); |
| 59 | 61 |
| 60 // Reports to UMA that the seed format specified by the server is unsupported. | 62 // Reports to UMA that the seed format specified by the server is unsupported. |
| 61 void ReportUnsupportedSeedFormatError(); | 63 void ReportUnsupportedSeedFormatError(); |
| 62 | 64 |
| 63 // Returns the serial number of the last loaded or stored seed. | 65 // Returns the serial number of the last loaded or stored seed. |
| 64 const std::string& variations_serial_number() const { | 66 const std::string& variations_serial_number() const { |
| 65 return variations_serial_number_; | 67 return variations_serial_number_; |
| 66 } | 68 } |
| 67 | 69 |
| 68 // Returns the invalid signature in base64 format, or an empty string if the | 70 // Returns the invalid signature in base64 format, or an empty string if the |
| 69 // signature was valid, missing, or if signature verification is disabled. | 71 // signature was valid, missing, or if signature verification is disabled. |
| 70 std::string GetInvalidSignature() const; | 72 std::string GetInvalidSignature() const; |
| 71 | 73 |
| 72 // Registers Local State prefs used by this class. | 74 // Registers Local State prefs used by this class. |
| 73 static void RegisterPrefs(PrefRegistrySimple* registry); | 75 static void RegisterPrefs(PrefRegistrySimple* registry); |
| 74 | 76 |
| 75 protected: | 77 protected: |
| 76 // Note: UMA histogram enum - don't re-order or remove entries. | 78 // Whether signature verification is enabled. Overridable for tests. |
| 77 enum VerifySignatureResult { | 79 virtual bool SignatureVerificationEnabled(); |
| 78 VARIATIONS_SEED_SIGNATURE_MISSING, | |
| 79 VARIATIONS_SEED_SIGNATURE_DECODE_FAILED, | |
| 80 VARIATIONS_SEED_SIGNATURE_INVALID_SIGNATURE, | |
| 81 VARIATIONS_SEED_SIGNATURE_INVALID_SEED, | |
| 82 VARIATIONS_SEED_SIGNATURE_VALID, | |
| 83 VARIATIONS_SEED_SIGNATURE_ENUM_SIZE, | |
| 84 }; | |
| 85 | |
| 86 // Verifies a variations seed (the serialized proto bytes) with the specified | |
| 87 // base-64 encoded signature that was received from the server and returns the | |
| 88 // result. The signature is assumed to be an "ECDSA with SHA-256" signature | |
| 89 // (see kECDSAWithSHA256AlgorithmID in the .cc file). Returns the result of | |
| 90 // signature verification or VARIATIONS_SEED_SIGNATURE_ENUM_SIZE if signature | |
| 91 // verification is not enabled. | |
| 92 virtual VariationsSeedStore::VerifySignatureResult VerifySeedSignature( | |
| 93 const std::string& seed_bytes, | |
| 94 const std::string& base64_seed_signature); | |
| 95 | 80 |
| 96 private: | 81 private: |
| 97 FRIEND_TEST_ALL_PREFIXES(VariationsSeedStoreTest, VerifySeedSignature); | 82 FRIEND_TEST_ALL_PREFIXES(VariationsSeedStoreTest, VerifySeedSignature); |
| 98 FRIEND_TEST_ALL_PREFIXES(VariationsSeedStoreTest, ApplyDeltaPatch); | 83 FRIEND_TEST_ALL_PREFIXES(VariationsSeedStoreTest, ApplyDeltaPatch); |
| 99 | 84 |
| 100 // Clears all prefs related to variations seed storage. | 85 // Clears all prefs related to variations seed storage. |
| 101 void ClearPrefs(); | 86 void ClearPrefs(); |
| 102 | 87 |
| 103 #if defined(OS_ANDROID) | 88 #if defined(OS_ANDROID) |
| 104 // Imports the variations seed data from Java side during the first | 89 // Imports the variations seed data from Java side during the first |
| 105 // Chrome for Android run. | 90 // Chrome for Android run. |
| 106 void ImportFirstRunJavaSeed(); | 91 void ImportFirstRunJavaSeed(); |
| 107 #endif // OS_ANDROID | 92 #endif // OS_ANDROID |
| 108 | 93 |
| 109 // Reads the variations seed data from prefs; returns true on success. | 94 // Reads the variations seed data from prefs into |seed_data|, and returns the |
| 110 bool ReadSeedData(std::string* seed_data); | 95 // result of the load. The value stored into |seed_data| should only be used |
| 96 // if the result is SUCCESS. |
| 97 // Side-effect: If the read fails, clears the prefs associated with the seed. |
| 98 LoadSeedResult ReadSeedData(std::string* seed_data) WARN_UNUSED_RESULT; |
| 111 | 99 |
| 112 // Internal version of |StoreSeedData()| that assumes |seed_data| is not delta | 100 // Internal version of |StoreSeedData()| that assumes |seed_data| is not delta |
| 113 // compressed. | 101 // compressed. |
| 114 bool StoreSeedDataNoDelta( | 102 bool StoreSeedDataNoDelta(const std::string& seed_data, |
| 115 const std::string& seed_data, | 103 const std::string& base64_seed_signature, |
| 116 const std::string& base64_seed_signature, | 104 const std::string& country_code, |
| 117 const std::string& country_code, | 105 const base::Time& date_fetched, |
| 118 const base::Time& date_fetched, | 106 variations::VariationsSeed* parsed_seed) |
| 119 variations::VariationsSeed* parsed_seed); | 107 WARN_UNUSED_RESULT; |
| 120 | 108 |
| 121 // Applies a delta-compressed |patch| to |existing_data|, producing the result | 109 // Applies a delta-compressed |patch| to |existing_data|, producing the result |
| 122 // in |output|. Returns whether the operation was successful. | 110 // in |output|. Returns whether the operation was successful. |
| 123 static bool ApplyDeltaPatch(const std::string& existing_data, | 111 static bool ApplyDeltaPatch(const std::string& existing_data, |
| 124 const std::string& patch, | 112 const std::string& patch, |
| 125 std::string* output); | 113 std::string* output) WARN_UNUSED_RESULT; |
| 126 | 114 |
| 127 // The pref service used to persist the variations seed. | 115 // The pref service used to persist the variations seed. |
| 128 PrefService* local_state_; | 116 PrefService* local_state_; |
| 129 | 117 |
| 130 // Cached serial number from the most recently fetched variations seed. | 118 // Cached serial number from the most recently fetched variations seed. |
| 131 std::string variations_serial_number_; | 119 std::string variations_serial_number_; |
| 132 | 120 |
| 133 // Keeps track of an invalid signature. | 121 // Keeps track of an invalid signature. |
| 134 std::string invalid_base64_signature_; | 122 std::string invalid_base64_signature_; |
| 135 | 123 |
| 136 DISALLOW_COPY_AND_ASSIGN(VariationsSeedStore); | 124 DISALLOW_COPY_AND_ASSIGN(VariationsSeedStore); |
| 137 }; | 125 }; |
| 138 | 126 |
| 139 } // namespace variations | 127 } // namespace variations |
| 140 | 128 |
| 141 #endif // COMPONENTS_VARIATIONS_VARIATIONS_SEED_STORE_H_ | 129 #endif // COMPONENTS_VARIATIONS_VARIATIONS_SEED_STORE_H_ |
| OLD | NEW |