OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/service/variations_service.h" | 5 #include "components/variations/service/variations_service.h" |
6 | 6 |
7 #include "base/build_time.h" | 7 #include "base/build_time.h" |
8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
9 #include "base/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
10 #include "base/metrics/sparse_histogram.h" | 10 #include "base/metrics/sparse_histogram.h" |
11 #include "base/prefs/pref_registry_simple.h" | 11 #include "base/prefs/pref_registry_simple.h" |
12 #include "base/prefs/pref_service.h" | 12 #include "base/prefs/pref_service.h" |
13 #include "base/strings/string_util.h" | |
13 #include "base/sys_info.h" | 14 #include "base/sys_info.h" |
14 #include "base/task_runner_util.h" | 15 #include "base/task_runner_util.h" |
15 #include "base/timer/elapsed_timer.h" | 16 #include "base/timer/elapsed_timer.h" |
16 #include "base/values.h" | 17 #include "base/values.h" |
17 #include "base/version.h" | 18 #include "base/version.h" |
18 #include "components/data_use_measurement/core/data_use_user_data.h" | 19 #include "components/data_use_measurement/core/data_use_user_data.h" |
19 #include "components/metrics/metrics_state_manager.h" | 20 #include "components/metrics/metrics_state_manager.h" |
20 #include "components/network_time/network_time_tracker.h" | 21 #include "components/network_time/network_time_tracker.h" |
21 #include "components/pref_registry/pref_registry_syncable.h" | 22 #include "components/pref_registry/pref_registry_syncable.h" |
22 #include "components/variations/pref_names.h" | 23 #include "components/variations/pref_names.h" |
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
207 | 208 |
208 // Returns the header value for |name| from |headers| or an empty string if not | 209 // Returns the header value for |name| from |headers| or an empty string if not |
209 // set. | 210 // set. |
210 std::string GetHeaderValue(const net::HttpResponseHeaders* headers, | 211 std::string GetHeaderValue(const net::HttpResponseHeaders* headers, |
211 const base::StringPiece& name) { | 212 const base::StringPiece& name) { |
212 std::string value; | 213 std::string value; |
213 headers->EnumerateHeader(NULL, name, &value); | 214 headers->EnumerateHeader(NULL, name, &value); |
214 return value; | 215 return value; |
215 } | 216 } |
216 | 217 |
218 // Returns the list of values for |name| from |headers|. If the header in not | |
219 // set, return an empty list. | |
220 std::vector<std::string> GetHeaderValuesList( | |
221 const net::HttpResponseHeaders* headers, const base::StringPiece& name) { | |
222 std::vector<std::string> values; | |
223 void* iter = NULL; | |
224 std::string value; | |
225 while (headers->EnumerateHeader(&iter, name, &value)) { | |
226 values.push_back(value); | |
227 } | |
228 return values; | |
229 } | |
230 | |
217 } // namespace | 231 } // namespace |
218 | 232 |
219 VariationsService::VariationsService( | 233 VariationsService::VariationsService( |
220 scoped_ptr<VariationsServiceClient> client, | 234 scoped_ptr<VariationsServiceClient> client, |
221 scoped_ptr<web_resource::ResourceRequestAllowedNotifier> notifier, | 235 scoped_ptr<web_resource::ResourceRequestAllowedNotifier> notifier, |
222 PrefService* local_state, | 236 PrefService* local_state, |
223 metrics::MetricsStateManager* state_manager, | 237 metrics::MetricsStateManager* state_manager, |
224 const UIStringOverrider& ui_string_overrider) | 238 const UIStringOverrider& ui_string_overrider) |
225 : client_(client.Pass()), | 239 : client_(client.Pass()), |
226 ui_string_overrider_(ui_string_overrider), | 240 ui_string_overrider_(ui_string_overrider), |
(...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
467 | 481 |
468 pending_seed_request_ = net::URLFetcher::Create(0, variations_server_url_, | 482 pending_seed_request_ = net::URLFetcher::Create(0, variations_server_url_, |
469 net::URLFetcher::GET, this); | 483 net::URLFetcher::GET, this); |
470 data_use_measurement::DataUseUserData::AttachToFetcher( | 484 data_use_measurement::DataUseUserData::AttachToFetcher( |
471 pending_seed_request_.get(), | 485 pending_seed_request_.get(), |
472 data_use_measurement::DataUseUserData::VARIATIONS); | 486 data_use_measurement::DataUseUserData::VARIATIONS); |
473 pending_seed_request_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | | 487 pending_seed_request_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | |
474 net::LOAD_DO_NOT_SAVE_COOKIES); | 488 net::LOAD_DO_NOT_SAVE_COOKIES); |
475 pending_seed_request_->SetRequestContext(client_->GetURLRequestContext()); | 489 pending_seed_request_->SetRequestContext(client_->GetURLRequestContext()); |
476 pending_seed_request_->SetMaxRetriesOn5xx(kMaxRetrySeedFetch); | 490 pending_seed_request_->SetMaxRetriesOn5xx(kMaxRetrySeedFetch); |
491 bool enable_deltas = false; | |
477 if (!seed_store_.variations_serial_number().empty() && | 492 if (!seed_store_.variations_serial_number().empty() && |
478 !disable_deltas_for_next_request_) { | 493 !disable_deltas_for_next_request_) { |
479 // If the current seed includes a country code, deltas are not supported (as | 494 // If the current seed includes a country code, deltas are not supported (as |
480 // the serial number doesn't take into account the country code). The server | 495 // the serial number doesn't take into account the country code). The server |
481 // will update us with a seed that doesn't include a country code which will | 496 // will update us with a seed that doesn't include a country code which will |
482 // enable deltas to work. | 497 // enable deltas to work. |
483 // TODO(asvitkine): Remove the check in M50+ when the percentage of clients | 498 // TODO(asvitkine): Remove the check in M50+ when the percentage of clients |
484 // that have an old seed with a country code becomes miniscule. | 499 // that have an old seed with a country code becomes miniscule. |
485 if (!seed_store_.seed_has_country_code()) { | 500 if (!seed_store_.seed_has_country_code()) { |
486 // Tell the server that delta-compressed seeds are supported. | 501 // Tell the server that delta-compressed seeds are supported. |
487 pending_seed_request_->AddExtraRequestHeader("A-IM:x-bm"); | 502 enable_deltas = true; |
488 } | 503 } |
489 // Get the seed only if its serial number doesn't match what we have. | 504 // Get the seed only if its serial number doesn't match what we have. |
490 pending_seed_request_->AddExtraRequestHeader( | 505 pending_seed_request_->AddExtraRequestHeader( |
491 "If-None-Match:" + seed_store_.variations_serial_number()); | 506 "If-None-Match:" + seed_store_.variations_serial_number()); |
492 } | 507 } |
508 // Tell the server that delta-compressed and gzipped seeds are supported. | |
509 std::string supported_im = enable_deltas ? "x-bm,gzip" : "gzip"; | |
510 pending_seed_request_->AddExtraRequestHeader("A-IM:" + supported_im); | |
511 | |
493 pending_seed_request_->Start(); | 512 pending_seed_request_->Start(); |
494 | 513 |
495 const base::TimeTicks now = base::TimeTicks::Now(); | 514 const base::TimeTicks now = base::TimeTicks::Now(); |
496 base::TimeDelta time_since_last_fetch; | 515 base::TimeDelta time_since_last_fetch; |
497 // Record a time delta of 0 (default value) if there was no previous fetch. | 516 // Record a time delta of 0 (default value) if there was no previous fetch. |
498 if (!last_request_started_time_.is_null()) | 517 if (!last_request_started_time_.is_null()) |
499 time_since_last_fetch = now - last_request_started_time_; | 518 time_since_last_fetch = now - last_request_started_time_; |
500 UMA_HISTOGRAM_CUSTOM_COUNTS("Variations.TimeSinceLastFetchAttempt", | 519 UMA_HISTOGRAM_CUSTOM_COUNTS("Variations.TimeSinceLastFetchAttempt", |
501 time_since_last_fetch.InMinutes(), 0, | 520 time_since_last_fetch.InMinutes(), 0, |
502 base::TimeDelta::FromDays(7).InMinutes(), 50); | 521 base::TimeDelta::FromDays(7).InMinutes(), 50); |
503 UMA_HISTOGRAM_COUNTS_100("Variations.RequestCount", request_count_); | 522 UMA_HISTOGRAM_COUNTS_100("Variations.RequestCount", request_count_); |
504 ++request_count_; | 523 ++request_count_; |
505 last_request_started_time_ = now; | 524 last_request_started_time_ = now; |
506 disable_deltas_for_next_request_ = false; | 525 disable_deltas_for_next_request_ = false; |
507 } | 526 } |
508 | 527 |
509 bool VariationsService::StoreSeed(const std::string& seed_data, | 528 bool VariationsService::StoreSeed(const std::string& seed_data, |
510 const std::string& seed_signature, | 529 const std::string& seed_signature, |
511 const std::string& country_code, | 530 const std::string& country_code, |
512 const base::Time& date_fetched, | 531 const base::Time& date_fetched, |
513 bool is_delta_compressed) { | 532 bool is_delta_compressed, |
533 bool is_gzip_compressed) { | |
514 DCHECK(thread_checker_.CalledOnValidThread()); | 534 DCHECK(thread_checker_.CalledOnValidThread()); |
515 | 535 |
516 scoped_ptr<variations::VariationsSeed> seed(new variations::VariationsSeed); | 536 scoped_ptr<variations::VariationsSeed> seed(new variations::VariationsSeed); |
517 if (!seed_store_.StoreSeedData(seed_data, seed_signature, country_code, | 537 if (!seed_store_.StoreSeedData(seed_data, seed_signature, country_code, |
518 date_fetched, is_delta_compressed, | 538 date_fetched, is_delta_compressed, |
519 seed.get())) { | 539 is_gzip_compressed, seed.get())) { |
520 return false; | 540 return false; |
521 } | 541 } |
522 RecordLastFetchTime(); | 542 RecordLastFetchTime(); |
523 | 543 |
524 // Perform seed simulation only if |state_manager_| is not-NULL. The state | 544 // Perform seed simulation only if |state_manager_| is not-NULL. The state |
525 // manager may be NULL for some unit tests. | 545 // manager may be NULL for some unit tests. |
526 if (!state_manager_) | 546 if (!state_manager_) |
527 return true; | 547 return true; |
528 | 548 |
529 base::PostTaskAndReplyWithResult( | 549 base::PostTaskAndReplyWithResult( |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
623 return; | 643 return; |
624 } | 644 } |
625 | 645 |
626 std::string seed_data; | 646 std::string seed_data; |
627 bool success = request->GetResponseAsString(&seed_data); | 647 bool success = request->GetResponseAsString(&seed_data); |
628 DCHECK(success); | 648 DCHECK(success); |
629 | 649 |
630 net::HttpResponseHeaders* headers = request->GetResponseHeaders(); | 650 net::HttpResponseHeaders* headers = request->GetResponseHeaders(); |
631 const std::string signature = GetHeaderValue(headers, "X-Seed-Signature"); | 651 const std::string signature = GetHeaderValue(headers, "X-Seed-Signature"); |
632 const std::string country_code = GetHeaderValue(headers, "X-Country"); | 652 const std::string country_code = GetHeaderValue(headers, "X-Country"); |
633 const bool is_delta_compressed = (GetHeaderValue(headers, "IM") == "x-bm"); | 653 |
654 // Look for delta and gzip compression flags set by the server. | |
655 std::vector<std::string> ims = GetHeaderValuesList(headers, "IM"); | |
656 const auto delta_im = std::find(ims.begin(), ims.end(), "x-bm"); | |
657 const auto gzip_im = std::find(ims.begin(), ims.end(), "gzip"); | |
658 bool is_delta_compressed = delta_im != ims.end(); | |
659 bool is_gzip_compressed = gzip_im != ims.end(); | |
660 // The IM field should not have anything but x-bm and gzip. | |
661 if (static_cast<size_t>(is_delta_compressed + is_gzip_compressed) != | |
662 ims.size()) { | |
663 DVLOG(1) << "Unrecognized instance manipulations in " | |
664 << base::JoinString(ims, ",") | |
665 << "; only x-bm and gzip are supported"; | |
666 return; | |
667 } | |
668 // The IM field defines order in which instance manipulations were applied. | |
669 // The client requests and supports gzip-compressed delta-compressed seeds, | |
670 // but not vice versa. | |
671 if (is_delta_compressed && is_gzip_compressed && delta_im > gzip_im) { | |
672 DVLOG(1) << "Unsupported instance manipulations order: " | |
673 << "requested x-bm,gzip but received gzip,x-bm"; | |
674 return; | |
675 } | |
Alexei Svitkine (slow)
2015/10/15 15:50:43
Can you make a new helper for this block of code?
veranika
2015/10/15 20:02:56
Done.
| |
676 | |
634 const bool store_success = StoreSeed(seed_data, signature, country_code, | 677 const bool store_success = StoreSeed(seed_data, signature, country_code, |
635 response_date, is_delta_compressed); | 678 response_date, is_delta_compressed, |
679 is_gzip_compressed); | |
636 if (!store_success && is_delta_compressed) { | 680 if (!store_success && is_delta_compressed) { |
637 disable_deltas_for_next_request_ = true; | 681 disable_deltas_for_next_request_ = true; |
638 request_scheduler_->ScheduleFetchShortly(); | 682 request_scheduler_->ScheduleFetchShortly(); |
639 } | 683 } |
640 } | 684 } |
641 | 685 |
642 void VariationsService::OnResourceRequestsAllowed() { | 686 void VariationsService::OnResourceRequestsAllowed() { |
643 DCHECK(thread_checker_.CalledOnValidThread()); | 687 DCHECK(thread_checker_.CalledOnValidThread()); |
644 | 688 |
645 // Note that this only attempts to fetch the seed at most once per period | 689 // Note that this only attempts to fetch the seed at most once per period |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
787 std::string stored_country; | 831 std::string stored_country; |
788 | 832 |
789 if (list_value->GetSize() == 2) { | 833 if (list_value->GetSize() == 2) { |
790 list_value->GetString(1, &stored_country); | 834 list_value->GetString(1, &stored_country); |
791 } | 835 } |
792 | 836 |
793 return stored_country; | 837 return stored_country; |
794 } | 838 } |
795 | 839 |
796 } // namespace variations | 840 } // namespace variations |
OLD | NEW |