Chromium Code Reviews| 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 #include "components/variations/variations_seed_store.h" | 5 #include "components/variations/variations_seed_store.h" |
| 6 | 6 |
| 7 #include "base/base64.h" | 7 #include "base/base64.h" |
| 8 #include "base/metrics/histogram_macros.h" | 8 #include "base/metrics/histogram_macros.h" |
| 9 #include "base/numerics/safe_math.h" | 9 #include "base/numerics/safe_math.h" |
| 10 #include "base/prefs/pref_registry_simple.h" | 10 #include "base/prefs/pref_registry_simple.h" |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 84 VARIATIONS_SEED_STORE_FAILED_PARSE, | 84 VARIATIONS_SEED_STORE_FAILED_PARSE, |
| 85 VARIATIONS_SEED_STORE_FAILED_SIGNATURE, | 85 VARIATIONS_SEED_STORE_FAILED_SIGNATURE, |
| 86 VARIATIONS_SEED_STORE_FAILED_GZIP, | 86 VARIATIONS_SEED_STORE_FAILED_GZIP, |
| 87 // DELTA_COUNT is not so much a result of the seed store, but rather counting | 87 // DELTA_COUNT is not so much a result of the seed store, but rather counting |
| 88 // the number of delta-compressed seeds the SeedStore() function saw. Kept in | 88 // the number of delta-compressed seeds the SeedStore() function saw. Kept in |
| 89 // the same histogram for convenience of comparing against the other values. | 89 // the same histogram for convenience of comparing against the other values. |
| 90 VARIATIONS_SEED_STORE_DELTA_COUNT, | 90 VARIATIONS_SEED_STORE_DELTA_COUNT, |
| 91 VARIATIONS_SEED_STORE_FAILED_DELTA_READ_SEED, | 91 VARIATIONS_SEED_STORE_FAILED_DELTA_READ_SEED, |
| 92 VARIATIONS_SEED_STORE_FAILED_DELTA_APPLY, | 92 VARIATIONS_SEED_STORE_FAILED_DELTA_APPLY, |
| 93 VARIATIONS_SEED_STORE_FAILED_DELTA_STORE, | 93 VARIATIONS_SEED_STORE_FAILED_DELTA_STORE, |
| 94 VARIATIONS_SEED_STORE_FAILED_UNGZIP, | |
| 95 VARIATIONS_SEED_STORE_FAILED_EMPTY_GZIP_CONTENTS, | |
| 96 VARIATIONS_SEED_STORE_FAILED_UNSUPPORTED_SEED_FORMAT, | |
| 94 VARIATIONS_SEED_STORE_RESULT_ENUM_SIZE, | 97 VARIATIONS_SEED_STORE_RESULT_ENUM_SIZE, |
| 95 }; | 98 }; |
| 96 | 99 |
| 97 void RecordSeedStoreHistogram(VariationsSeedStoreResult result) { | 100 void RecordSeedStoreHistogram(VariationsSeedStoreResult result) { |
| 98 UMA_HISTOGRAM_ENUMERATION("Variations.SeedStoreResult", result, | 101 UMA_HISTOGRAM_ENUMERATION("Variations.SeedStoreResult", result, |
| 99 VARIATIONS_SEED_STORE_RESULT_ENUM_SIZE); | 102 VARIATIONS_SEED_STORE_RESULT_ENUM_SIZE); |
| 100 } | 103 } |
| 101 | 104 |
| 102 // Note: UMA histogram enum - don't re-order or remove entries. | 105 // Note: UMA histogram enum - don't re-order or remove entries. |
| 103 enum VariationsSeedDateChangeState { | 106 enum VariationsSeedDateChangeState { |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 187 RecordVariationSeedEmptyHistogram(VARIATIONS_SEED_NOT_EMPTY); | 190 RecordVariationSeedEmptyHistogram(VARIATIONS_SEED_NOT_EMPTY); |
| 188 return true; | 191 return true; |
| 189 } | 192 } |
| 190 | 193 |
| 191 bool VariationsSeedStore::StoreSeedData( | 194 bool VariationsSeedStore::StoreSeedData( |
| 192 const std::string& data, | 195 const std::string& data, |
| 193 const std::string& base64_seed_signature, | 196 const std::string& base64_seed_signature, |
| 194 const std::string& country_code, | 197 const std::string& country_code, |
| 195 const base::Time& date_fetched, | 198 const base::Time& date_fetched, |
| 196 bool is_delta_compressed, | 199 bool is_delta_compressed, |
| 200 bool is_gzip_compressed, | |
| 197 variations::VariationsSeed* parsed_seed) { | 201 variations::VariationsSeed* parsed_seed) { |
| 202 // If the data is gzip compressed, first uncompress it. | |
| 203 std::string ungzipped_data; | |
| 204 if (is_gzip_compressed) { | |
| 205 if (compression::GzipUncompress(data, &ungzipped_data)) { | |
| 206 if (ungzipped_data.empty()) { | |
| 207 RecordSeedStoreHistogram(VARIATIONS_SEED_STORE_FAILED_EMPTY); | |
| 208 return false; | |
| 209 } | |
| 210 | |
| 211 int size_reduction = ungzipped_data.length() - data.length(); | |
| 212 UMA_HISTOGRAM_PERCENTAGE("Variations.StoreSeed.GzipSize.ReductionPercent", | |
| 213 100 * size_reduction / ungzipped_data.length()); | |
| 214 UMA_HISTOGRAM_COUNTS_1000("Variations.StoreSeed.GzipSize", | |
| 215 data.length() / 1024); | |
| 216 } else { | |
| 217 RecordSeedStoreHistogram(VARIATIONS_SEED_STORE_FAILED_UNGZIP); | |
| 218 return false; | |
| 219 } | |
| 220 } else { | |
| 221 ungzipped_data = data; | |
| 222 } | |
| 223 | |
| 198 if (!is_delta_compressed) { | 224 if (!is_delta_compressed) { |
| 199 const bool result = | 225 const bool result = |
| 200 StoreSeedDataNoDelta(data, base64_seed_signature, country_code, | 226 StoreSeedDataNoDelta(ungzipped_data, base64_seed_signature, |
| 201 date_fetched, parsed_seed); | 227 country_code, date_fetched, parsed_seed); |
| 202 if (result) { | 228 if (result) { |
| 203 UMA_HISTOGRAM_COUNTS_1000("Variations.StoreSeed.Size", | 229 UMA_HISTOGRAM_COUNTS_1000("Variations.StoreSeed.Size", |
| 204 data.length() / 1024); | 230 ungzipped_data.length() / 1024); |
| 205 } | 231 } |
| 206 return result; | 232 return result; |
| 207 } | 233 } |
| 208 | 234 |
| 209 // If the data is delta compressed, first decode it. | 235 // If the data is delta compressed, first decode it. |
| 210 DCHECK(invalid_base64_signature_.empty()); | 236 DCHECK(invalid_base64_signature_.empty()); |
| 211 RecordSeedStoreHistogram(VARIATIONS_SEED_STORE_DELTA_COUNT); | 237 RecordSeedStoreHistogram(VARIATIONS_SEED_STORE_DELTA_COUNT); |
| 212 | 238 |
| 213 std::string existing_seed_data; | 239 std::string existing_seed_data; |
| 214 std::string updated_seed_data; | 240 std::string updated_seed_data; |
| 215 if (!ReadSeedData(&existing_seed_data)) { | 241 if (!ReadSeedData(&existing_seed_data)) { |
| 216 RecordSeedStoreHistogram(VARIATIONS_SEED_STORE_FAILED_DELTA_READ_SEED); | 242 RecordSeedStoreHistogram(VARIATIONS_SEED_STORE_FAILED_DELTA_READ_SEED); |
| 217 return false; | 243 return false; |
| 218 } | 244 } |
| 219 if (!ApplyDeltaPatch(existing_seed_data, data, &updated_seed_data)) { | 245 if (!ApplyDeltaPatch(existing_seed_data, ungzipped_data, |
| 246 &updated_seed_data)) { | |
| 220 RecordSeedStoreHistogram(VARIATIONS_SEED_STORE_FAILED_DELTA_APPLY); | 247 RecordSeedStoreHistogram(VARIATIONS_SEED_STORE_FAILED_DELTA_APPLY); |
| 221 return false; | 248 return false; |
| 222 } | 249 } |
| 223 | 250 |
| 224 const bool result = | 251 const bool result = |
| 225 StoreSeedDataNoDelta(updated_seed_data, base64_seed_signature, | 252 StoreSeedDataNoDelta(updated_seed_data, base64_seed_signature, |
| 226 country_code, date_fetched, parsed_seed); | 253 country_code, date_fetched, parsed_seed); |
| 227 if (result) { | 254 if (result) { |
| 228 // Note: |updated_seed_data.length()| is guaranteed to be non-zero, else | 255 // Note: |updated_seed_data.length()| is guaranteed to be non-zero, else |
| 229 // result would be false. | 256 // result would be false. |
| 230 int size_reduction = updated_seed_data.length() - data.length(); | 257 int size_reduction = updated_seed_data.length() - ungzipped_data.length(); |
| 231 UMA_HISTOGRAM_PERCENTAGE("Variations.StoreSeed.DeltaSize.ReductionPercent", | 258 UMA_HISTOGRAM_PERCENTAGE("Variations.StoreSeed.DeltaSize.ReductionPercent", |
| 232 100 * size_reduction / updated_seed_data.length()); | 259 100 * size_reduction / updated_seed_data.length()); |
| 233 UMA_HISTOGRAM_COUNTS_1000("Variations.StoreSeed.DeltaSize", | 260 UMA_HISTOGRAM_COUNTS_1000("Variations.StoreSeed.DeltaSize", |
| 234 data.length() / 1024); | 261 ungzipped_data.length() / 1024); |
| 235 } else { | 262 } else { |
| 236 RecordSeedStoreHistogram(VARIATIONS_SEED_STORE_FAILED_DELTA_STORE); | 263 RecordSeedStoreHistogram(VARIATIONS_SEED_STORE_FAILED_DELTA_STORE); |
| 237 } | 264 } |
| 238 return result; | 265 return result; |
| 239 } | 266 } |
| 240 | 267 |
| 241 void VariationsSeedStore::UpdateSeedDateAndLogDayChange( | 268 void VariationsSeedStore::UpdateSeedDateAndLogDayChange( |
| 242 const base::Time& server_date_fetched) { | 269 const base::Time& server_date_fetched) { |
| 243 VariationsSeedDateChangeState date_change = SEED_DATE_NO_OLD_DATE; | 270 VariationsSeedDateChangeState date_change = SEED_DATE_NO_OLD_DATE; |
| 244 | 271 |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 340 return true; | 367 return true; |
| 341 } | 368 } |
| 342 | 369 |
| 343 bool VariationsSeedStore::StoreSeedDataNoDelta( | 370 bool VariationsSeedStore::StoreSeedDataNoDelta( |
| 344 const std::string& seed_data, | 371 const std::string& seed_data, |
| 345 const std::string& base64_seed_signature, | 372 const std::string& base64_seed_signature, |
| 346 const std::string& country_code, | 373 const std::string& country_code, |
| 347 const base::Time& date_fetched, | 374 const base::Time& date_fetched, |
| 348 variations::VariationsSeed* parsed_seed) { | 375 variations::VariationsSeed* parsed_seed) { |
| 349 if (seed_data.empty()) { | 376 if (seed_data.empty()) { |
| 350 RecordSeedStoreHistogram(VARIATIONS_SEED_STORE_FAILED_EMPTY); | 377 RecordSeedStoreHistogram(VARIATIONS_SEED_STORE_FAILED_EMPTY_GZIP_CONTENTS); |
| 351 return false; | 378 return false; |
| 352 } | 379 } |
| 353 | 380 |
| 354 // Only store the seed data if it parses correctly. | 381 // Only store the seed data if it parses correctly. |
| 355 variations::VariationsSeed seed; | 382 variations::VariationsSeed seed; |
| 356 if (!seed.ParseFromString(seed_data)) { | 383 if (!seed.ParseFromString(seed_data)) { |
| 357 RecordSeedStoreHistogram(VARIATIONS_SEED_STORE_FAILED_PARSE); | 384 RecordSeedStoreHistogram(VARIATIONS_SEED_STORE_FAILED_PARSE); |
| 358 return false; | 385 return false; |
| 359 } | 386 } |
| 360 | 387 |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 443 base::CheckedNumeric<uint32> end_offset(offset); | 470 base::CheckedNumeric<uint32> end_offset(offset); |
| 444 end_offset += length; | 471 end_offset += length; |
| 445 if (!end_offset.IsValid() || end_offset.ValueOrDie() > existing_data_size) | 472 if (!end_offset.IsValid() || end_offset.ValueOrDie() > existing_data_size) |
| 446 return false; | 473 return false; |
| 447 output->append(existing_data, offset, length); | 474 output->append(existing_data, offset, length); |
| 448 } | 475 } |
| 449 } | 476 } |
| 450 return true; | 477 return true; |
| 451 } | 478 } |
| 452 | 479 |
| 480 void VariationsSeedStore::ReportUnsupportedSeedFormatError() { | |
| 481 RecordSeedStoreHistogram( | |
| 482 VARIATIONS_SEED_STORE_FAILED_UNSUPPORTED_SEED_FORMAT); | |
| 483 } | |
| 484 | |
|
Alexei Svitkine (slow)
2015/10/16 21:10:36
Nit: Remove extra blank line.
veranika
2015/10/16 21:42:59
Done.
| |
| 485 | |
| 453 } // namespace variations | 486 } // namespace variations |
| OLD | NEW |