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 | |
231 // Looks for delta and gzip compression instance manipulation flags set by the | |
232 // server in |headers|. Checks the order of flags and presence of unknown | |
233 // instance manipulations. If successful, |is_delta_compressed| and | |
234 // |is_gzip_compressed| contain compression flags and true is returned. | |
235 bool GetInstanceManipulations(const net::HttpResponseHeaders* headers, | |
236 bool* is_delta_compressed, | |
237 bool* is_gzip_compressed) { | |
238 std::vector<std::string> ims = GetHeaderValuesList(headers, "IM"); | |
239 const auto delta_im = std::find(ims.begin(), ims.end(), "x-bm"); | |
240 const auto gzip_im = std::find(ims.begin(), ims.end(), "gzip"); | |
241 *is_delta_compressed = delta_im != ims.end(); | |
242 *is_gzip_compressed = gzip_im != ims.end(); | |
243 | |
244 // The IM field should not have anything but x-bm and gzip. | |
245 if (static_cast<size_t>(*is_delta_compressed + *is_gzip_compressed) != | |
Alexei Svitkine (slow)
2015/10/15 20:30:26
Not a fan of adding booleans and casting to size_t
veranika
2015/10/15 22:10:37
Done.
| |
246 ims.size()) { | |
247 DVLOG(1) << "Unrecognized instance manipulations in " | |
248 << base::JoinString(ims, ",") | |
249 << "; only x-bm and gzip are supported"; | |
250 return false; | |
251 } | |
252 | |
253 // The IM field defines order in which instance manipulations were applied. | |
254 // The client requests and supports gzip-compressed delta-compressed seeds, | |
255 // but not vice versa. | |
256 if (*is_delta_compressed && *is_gzip_compressed && delta_im > gzip_im) { | |
257 DVLOG(1) << "Unsupported instance manipulations order: " | |
258 << "requested x-bm,gzip but received gzip,x-bm"; | |
259 return false; | |
260 } | |
261 | |
262 return true; | |
263 } | |
264 | |
217 } // namespace | 265 } // namespace |
218 | 266 |
219 VariationsService::VariationsService( | 267 VariationsService::VariationsService( |
220 scoped_ptr<VariationsServiceClient> client, | 268 scoped_ptr<VariationsServiceClient> client, |
221 scoped_ptr<web_resource::ResourceRequestAllowedNotifier> notifier, | 269 scoped_ptr<web_resource::ResourceRequestAllowedNotifier> notifier, |
222 PrefService* local_state, | 270 PrefService* local_state, |
223 metrics::MetricsStateManager* state_manager, | 271 metrics::MetricsStateManager* state_manager, |
224 const UIStringOverrider& ui_string_overrider) | 272 const UIStringOverrider& ui_string_overrider) |
225 : client_(client.Pass()), | 273 : client_(client.Pass()), |
226 ui_string_overrider_(ui_string_overrider), | 274 ui_string_overrider_(ui_string_overrider), |
(...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
467 | 515 |
468 pending_seed_request_ = net::URLFetcher::Create(0, variations_server_url_, | 516 pending_seed_request_ = net::URLFetcher::Create(0, variations_server_url_, |
469 net::URLFetcher::GET, this); | 517 net::URLFetcher::GET, this); |
470 data_use_measurement::DataUseUserData::AttachToFetcher( | 518 data_use_measurement::DataUseUserData::AttachToFetcher( |
471 pending_seed_request_.get(), | 519 pending_seed_request_.get(), |
472 data_use_measurement::DataUseUserData::VARIATIONS); | 520 data_use_measurement::DataUseUserData::VARIATIONS); |
473 pending_seed_request_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | | 521 pending_seed_request_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | |
474 net::LOAD_DO_NOT_SAVE_COOKIES); | 522 net::LOAD_DO_NOT_SAVE_COOKIES); |
475 pending_seed_request_->SetRequestContext(client_->GetURLRequestContext()); | 523 pending_seed_request_->SetRequestContext(client_->GetURLRequestContext()); |
476 pending_seed_request_->SetMaxRetriesOn5xx(kMaxRetrySeedFetch); | 524 pending_seed_request_->SetMaxRetriesOn5xx(kMaxRetrySeedFetch); |
525 bool enable_deltas = false; | |
477 if (!seed_store_.variations_serial_number().empty() && | 526 if (!seed_store_.variations_serial_number().empty() && |
478 !disable_deltas_for_next_request_) { | 527 !disable_deltas_for_next_request_) { |
479 // If the current seed includes a country code, deltas are not supported (as | 528 // 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 | 529 // 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 | 530 // will update us with a seed that doesn't include a country code which will |
482 // enable deltas to work. | 531 // enable deltas to work. |
483 // TODO(asvitkine): Remove the check in M50+ when the percentage of clients | 532 // TODO(asvitkine): Remove the check in M50+ when the percentage of clients |
484 // that have an old seed with a country code becomes miniscule. | 533 // that have an old seed with a country code becomes miniscule. |
485 if (!seed_store_.seed_has_country_code()) { | 534 if (!seed_store_.seed_has_country_code()) { |
486 // Tell the server that delta-compressed seeds are supported. | 535 // Tell the server that delta-compressed seeds are supported. |
487 pending_seed_request_->AddExtraRequestHeader("A-IM:x-bm"); | 536 enable_deltas = true; |
488 } | 537 } |
489 // Get the seed only if its serial number doesn't match what we have. | 538 // Get the seed only if its serial number doesn't match what we have. |
490 pending_seed_request_->AddExtraRequestHeader( | 539 pending_seed_request_->AddExtraRequestHeader( |
491 "If-None-Match:" + seed_store_.variations_serial_number()); | 540 "If-None-Match:" + seed_store_.variations_serial_number()); |
492 } | 541 } |
542 // Tell the server that delta-compressed and gzipped seeds are supported. | |
543 std::string supported_im = enable_deltas ? "x-bm,gzip" : "gzip"; | |
Alexei Svitkine (slow)
2015/10/15 20:30:26
Nit: I'd make this var a |const char*| and make it
veranika
2015/10/15 22:10:38
Done.
| |
544 pending_seed_request_->AddExtraRequestHeader("A-IM:" + supported_im); | |
545 | |
493 pending_seed_request_->Start(); | 546 pending_seed_request_->Start(); |
494 | 547 |
495 const base::TimeTicks now = base::TimeTicks::Now(); | 548 const base::TimeTicks now = base::TimeTicks::Now(); |
496 base::TimeDelta time_since_last_fetch; | 549 base::TimeDelta time_since_last_fetch; |
497 // Record a time delta of 0 (default value) if there was no previous fetch. | 550 // Record a time delta of 0 (default value) if there was no previous fetch. |
498 if (!last_request_started_time_.is_null()) | 551 if (!last_request_started_time_.is_null()) |
499 time_since_last_fetch = now - last_request_started_time_; | 552 time_since_last_fetch = now - last_request_started_time_; |
500 UMA_HISTOGRAM_CUSTOM_COUNTS("Variations.TimeSinceLastFetchAttempt", | 553 UMA_HISTOGRAM_CUSTOM_COUNTS("Variations.TimeSinceLastFetchAttempt", |
501 time_since_last_fetch.InMinutes(), 0, | 554 time_since_last_fetch.InMinutes(), 0, |
502 base::TimeDelta::FromDays(7).InMinutes(), 50); | 555 base::TimeDelta::FromDays(7).InMinutes(), 50); |
503 UMA_HISTOGRAM_COUNTS_100("Variations.RequestCount", request_count_); | 556 UMA_HISTOGRAM_COUNTS_100("Variations.RequestCount", request_count_); |
504 ++request_count_; | 557 ++request_count_; |
505 last_request_started_time_ = now; | 558 last_request_started_time_ = now; |
506 disable_deltas_for_next_request_ = false; | 559 disable_deltas_for_next_request_ = false; |
507 } | 560 } |
508 | 561 |
509 bool VariationsService::StoreSeed(const std::string& seed_data, | 562 bool VariationsService::StoreSeed(const std::string& seed_data, |
510 const std::string& seed_signature, | 563 const std::string& seed_signature, |
511 const std::string& country_code, | 564 const std::string& country_code, |
512 const base::Time& date_fetched, | 565 const base::Time& date_fetched, |
513 bool is_delta_compressed) { | 566 bool is_delta_compressed, |
567 bool is_gzip_compressed) { | |
514 DCHECK(thread_checker_.CalledOnValidThread()); | 568 DCHECK(thread_checker_.CalledOnValidThread()); |
515 | 569 |
516 scoped_ptr<variations::VariationsSeed> seed(new variations::VariationsSeed); | 570 scoped_ptr<variations::VariationsSeed> seed(new variations::VariationsSeed); |
517 if (!seed_store_.StoreSeedData(seed_data, seed_signature, country_code, | 571 if (!seed_store_.StoreSeedData(seed_data, seed_signature, country_code, |
518 date_fetched, is_delta_compressed, | 572 date_fetched, is_delta_compressed, |
519 seed.get())) { | 573 is_gzip_compressed, seed.get())) { |
520 return false; | 574 return false; |
521 } | 575 } |
522 RecordLastFetchTime(); | 576 RecordLastFetchTime(); |
523 | 577 |
524 // Perform seed simulation only if |state_manager_| is not-NULL. The state | 578 // Perform seed simulation only if |state_manager_| is not-NULL. The state |
525 // manager may be NULL for some unit tests. | 579 // manager may be NULL for some unit tests. |
526 if (!state_manager_) | 580 if (!state_manager_) |
527 return true; | 581 return true; |
528 | 582 |
529 base::PostTaskAndReplyWithResult( | 583 base::PostTaskAndReplyWithResult( |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
623 return; | 677 return; |
624 } | 678 } |
625 | 679 |
626 std::string seed_data; | 680 std::string seed_data; |
627 bool success = request->GetResponseAsString(&seed_data); | 681 bool success = request->GetResponseAsString(&seed_data); |
628 DCHECK(success); | 682 DCHECK(success); |
629 | 683 |
630 net::HttpResponseHeaders* headers = request->GetResponseHeaders(); | 684 net::HttpResponseHeaders* headers = request->GetResponseHeaders(); |
631 const std::string signature = GetHeaderValue(headers, "X-Seed-Signature"); | 685 const std::string signature = GetHeaderValue(headers, "X-Seed-Signature"); |
632 const std::string country_code = GetHeaderValue(headers, "X-Country"); | 686 const std::string country_code = GetHeaderValue(headers, "X-Country"); |
633 const bool is_delta_compressed = (GetHeaderValue(headers, "IM") == "x-bm"); | 687 |
688 bool is_delta_compressed; | |
689 bool is_gzip_compressed; | |
690 if (!GetInstanceManipulations(headers, &is_delta_compressed, | |
691 &is_gzip_compressed)) { | |
692 return; | |
Alexei Svitkine (slow)
2015/10/15 20:30:26
Add a comment about why it's OK to return here.
veranika
2015/10/15 22:10:37
Done.
| |
693 } | |
694 | |
634 const bool store_success = StoreSeed(seed_data, signature, country_code, | 695 const bool store_success = StoreSeed(seed_data, signature, country_code, |
635 response_date, is_delta_compressed); | 696 response_date, is_delta_compressed, |
697 is_gzip_compressed); | |
636 if (!store_success && is_delta_compressed) { | 698 if (!store_success && is_delta_compressed) { |
637 disable_deltas_for_next_request_ = true; | 699 disable_deltas_for_next_request_ = true; |
638 request_scheduler_->ScheduleFetchShortly(); | 700 request_scheduler_->ScheduleFetchShortly(); |
639 } | 701 } |
640 } | 702 } |
641 | 703 |
642 void VariationsService::OnResourceRequestsAllowed() { | 704 void VariationsService::OnResourceRequestsAllowed() { |
643 DCHECK(thread_checker_.CalledOnValidThread()); | 705 DCHECK(thread_checker_.CalledOnValidThread()); |
644 | 706 |
645 // Note that this only attempts to fetch the seed at most once per period | 707 // 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; | 849 std::string stored_country; |
788 | 850 |
789 if (list_value->GetSize() == 2) { | 851 if (list_value->GetSize() == 2) { |
790 list_value->GetString(1, &stored_country); | 852 list_value->GetString(1, &stored_country); |
791 } | 853 } |
792 | 854 |
793 return stored_country; | 855 return stored_country; |
794 } | 856 } |
795 | 857 |
796 } // namespace variations | 858 } // namespace variations |
OLD | NEW |