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