Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(68)

Side by Side Diff: components/variations/service/variations_service.cc

Issue 1404583004: Support gzip-compressed seed data from the variations server. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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,
222 const base::StringPiece& name) {
223 std::vector<std::string> values;
224 void* iter = NULL;
225 std::string value;
226 while (headers->EnumerateHeader(&iter, name, &value)) {
227 values.push_back(value);
228 }
229 return values;
230 }
231
232 // Looks for delta and gzip compression instance manipulation flags set by the
233 // server in |headers|. Checks the order of flags and presence of unknown
234 // instance manipulations. If successful, |is_delta_compressed| and
235 // |is_gzip_compressed| contain compression flags and true is returned.
236 bool GetInstanceManipulations(const net::HttpResponseHeaders* headers,
237 bool* is_delta_compressed,
238 bool* is_gzip_compressed) {
239 std::vector<std::string> ims = GetHeaderValuesList(headers, "IM");
240 const auto delta_im = std::find(ims.begin(), ims.end(), "x-bm");
241 const auto gzip_im = std::find(ims.begin(), ims.end(), "gzip");
242 *is_delta_compressed = delta_im != ims.end();
243 *is_gzip_compressed = gzip_im != ims.end();
244
245 // The IM field should not have anything but x-bm and gzip.
246 size_t im_count = (*is_delta_compressed ? 1 : 0) +
247 (*is_gzip_compressed ? 1 : 0);
248 if (im_count != ims.size()) {
249 DVLOG(1) << "Unrecognized instance manipulations in "
250 << base::JoinString(ims, ",")
251 << "; only x-bm and gzip are supported";
252 return false;
253 }
254
255 // The IM field defines order in which instance manipulations were applied.
256 // The client requests and supports gzip-compressed delta-compressed seeds,
257 // but not vice versa.
258 if (*is_delta_compressed && *is_gzip_compressed && delta_im > gzip_im) {
259 DVLOG(1) << "Unsupported instance manipulations order: "
260 << "requested x-bm,gzip but received gzip,x-bm";
261 return false;
262 }
263
264 return true;
265 }
266
217 } // namespace 267 } // namespace
218 268
219 VariationsService::VariationsService( 269 VariationsService::VariationsService(
220 scoped_ptr<VariationsServiceClient> client, 270 scoped_ptr<VariationsServiceClient> client,
221 scoped_ptr<web_resource::ResourceRequestAllowedNotifier> notifier, 271 scoped_ptr<web_resource::ResourceRequestAllowedNotifier> notifier,
222 PrefService* local_state, 272 PrefService* local_state,
223 metrics::MetricsStateManager* state_manager, 273 metrics::MetricsStateManager* state_manager,
224 const UIStringOverrider& ui_string_overrider) 274 const UIStringOverrider& ui_string_overrider)
225 : client_(client.Pass()), 275 : client_(client.Pass()),
226 ui_string_overrider_(ui_string_overrider), 276 ui_string_overrider_(ui_string_overrider),
(...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after
467 517
468 pending_seed_request_ = net::URLFetcher::Create(0, variations_server_url_, 518 pending_seed_request_ = net::URLFetcher::Create(0, variations_server_url_,
469 net::URLFetcher::GET, this); 519 net::URLFetcher::GET, this);
470 data_use_measurement::DataUseUserData::AttachToFetcher( 520 data_use_measurement::DataUseUserData::AttachToFetcher(
471 pending_seed_request_.get(), 521 pending_seed_request_.get(),
472 data_use_measurement::DataUseUserData::VARIATIONS); 522 data_use_measurement::DataUseUserData::VARIATIONS);
473 pending_seed_request_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | 523 pending_seed_request_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES |
474 net::LOAD_DO_NOT_SAVE_COOKIES); 524 net::LOAD_DO_NOT_SAVE_COOKIES);
475 pending_seed_request_->SetRequestContext(client_->GetURLRequestContext()); 525 pending_seed_request_->SetRequestContext(client_->GetURLRequestContext());
476 pending_seed_request_->SetMaxRetriesOn5xx(kMaxRetrySeedFetch); 526 pending_seed_request_->SetMaxRetriesOn5xx(kMaxRetrySeedFetch);
527 bool enable_deltas = false;
477 if (!seed_store_.variations_serial_number().empty() && 528 if (!seed_store_.variations_serial_number().empty() &&
478 !disable_deltas_for_next_request_) { 529 !disable_deltas_for_next_request_) {
479 // If the current seed includes a country code, deltas are not supported (as 530 // 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 531 // 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 532 // will update us with a seed that doesn't include a country code which will
482 // enable deltas to work. 533 // enable deltas to work.
483 // TODO(asvitkine): Remove the check in M50+ when the percentage of clients 534 // TODO(asvitkine): Remove the check in M50+ when the percentage of clients
484 // that have an old seed with a country code becomes miniscule. 535 // that have an old seed with a country code becomes miniscule.
485 if (!seed_store_.seed_has_country_code()) { 536 if (!seed_store_.seed_has_country_code()) {
486 // Tell the server that delta-compressed seeds are supported. 537 // Tell the server that delta-compressed seeds are supported.
487 pending_seed_request_->AddExtraRequestHeader("A-IM:x-bm"); 538 enable_deltas = true;
488 } 539 }
489 // Get the seed only if its serial number doesn't match what we have. 540 // Get the seed only if its serial number doesn't match what we have.
490 pending_seed_request_->AddExtraRequestHeader( 541 pending_seed_request_->AddExtraRequestHeader(
491 "If-None-Match:" + seed_store_.variations_serial_number()); 542 "If-None-Match:" + seed_store_.variations_serial_number());
492 } 543 }
544 // Tell the server that delta-compressed and gzipped seeds are supported.
545 const char* supported_im = enable_deltas ? "A-IM:x-bm,gzip" : "A-IM:gzip";
546 pending_seed_request_->AddExtraRequestHeader(supported_im);
547
493 pending_seed_request_->Start(); 548 pending_seed_request_->Start();
494 549
495 const base::TimeTicks now = base::TimeTicks::Now(); 550 const base::TimeTicks now = base::TimeTicks::Now();
496 base::TimeDelta time_since_last_fetch; 551 base::TimeDelta time_since_last_fetch;
497 // Record a time delta of 0 (default value) if there was no previous fetch. 552 // Record a time delta of 0 (default value) if there was no previous fetch.
498 if (!last_request_started_time_.is_null()) 553 if (!last_request_started_time_.is_null())
499 time_since_last_fetch = now - last_request_started_time_; 554 time_since_last_fetch = now - last_request_started_time_;
500 UMA_HISTOGRAM_CUSTOM_COUNTS("Variations.TimeSinceLastFetchAttempt", 555 UMA_HISTOGRAM_CUSTOM_COUNTS("Variations.TimeSinceLastFetchAttempt",
501 time_since_last_fetch.InMinutes(), 0, 556 time_since_last_fetch.InMinutes(), 0,
502 base::TimeDelta::FromDays(7).InMinutes(), 50); 557 base::TimeDelta::FromDays(7).InMinutes(), 50);
503 UMA_HISTOGRAM_COUNTS_100("Variations.RequestCount", request_count_); 558 UMA_HISTOGRAM_COUNTS_100("Variations.RequestCount", request_count_);
504 ++request_count_; 559 ++request_count_;
505 last_request_started_time_ = now; 560 last_request_started_time_ = now;
506 disable_deltas_for_next_request_ = false; 561 disable_deltas_for_next_request_ = false;
507 } 562 }
508 563
509 bool VariationsService::StoreSeed(const std::string& seed_data, 564 bool VariationsService::StoreSeed(const std::string& seed_data,
510 const std::string& seed_signature, 565 const std::string& seed_signature,
511 const std::string& country_code, 566 const std::string& country_code,
512 const base::Time& date_fetched, 567 const base::Time& date_fetched,
513 bool is_delta_compressed) { 568 bool is_delta_compressed,
569 bool is_gzip_compressed) {
514 DCHECK(thread_checker_.CalledOnValidThread()); 570 DCHECK(thread_checker_.CalledOnValidThread());
515 571
516 scoped_ptr<variations::VariationsSeed> seed(new variations::VariationsSeed); 572 scoped_ptr<variations::VariationsSeed> seed(new variations::VariationsSeed);
517 if (!seed_store_.StoreSeedData(seed_data, seed_signature, country_code, 573 if (!seed_store_.StoreSeedData(seed_data, seed_signature, country_code,
518 date_fetched, is_delta_compressed, 574 date_fetched, is_delta_compressed,
519 seed.get())) { 575 is_gzip_compressed, seed.get())) {
520 return false; 576 return false;
521 } 577 }
522 RecordLastFetchTime(); 578 RecordLastFetchTime();
523 579
524 // Perform seed simulation only if |state_manager_| is not-NULL. The state 580 // Perform seed simulation only if |state_manager_| is not-NULL. The state
525 // manager may be NULL for some unit tests. 581 // manager may be NULL for some unit tests.
526 if (!state_manager_) 582 if (!state_manager_)
527 return true; 583 return true;
528 584
529 base::PostTaskAndReplyWithResult( 585 base::PostTaskAndReplyWithResult(
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
621 seed_store_.UpdateSeedDateAndLogDayChange(response_date); 677 seed_store_.UpdateSeedDateAndLogDayChange(response_date);
622 } 678 }
623 return; 679 return;
624 } 680 }
625 681
626 std::string seed_data; 682 std::string seed_data;
627 bool success = request->GetResponseAsString(&seed_data); 683 bool success = request->GetResponseAsString(&seed_data);
628 DCHECK(success); 684 DCHECK(success);
629 685
630 net::HttpResponseHeaders* headers = request->GetResponseHeaders(); 686 net::HttpResponseHeaders* headers = request->GetResponseHeaders();
687 bool is_delta_compressed;
688 bool is_gzip_compressed;
689 if (!GetInstanceManipulations(headers, &is_delta_compressed,
690 &is_gzip_compressed)) {
691 // The header does not specify supported instance manipulations, unable to
692 // process data. Details of errors were logged by GetInstanceManipulations.
693 seed_store_.ReportUnsupportedSeedFormatError();
694 return;
695 }
696
631 const std::string signature = GetHeaderValue(headers, "X-Seed-Signature"); 697 const std::string signature = GetHeaderValue(headers, "X-Seed-Signature");
632 const std::string country_code = GetHeaderValue(headers, "X-Country"); 698 const std::string country_code = GetHeaderValue(headers, "X-Country");
633 const bool is_delta_compressed = (GetHeaderValue(headers, "IM") == "x-bm");
634 const bool store_success = StoreSeed(seed_data, signature, country_code, 699 const bool store_success = StoreSeed(seed_data, signature, country_code,
635 response_date, is_delta_compressed); 700 response_date, is_delta_compressed,
701 is_gzip_compressed);
636 if (!store_success && is_delta_compressed) { 702 if (!store_success && is_delta_compressed) {
637 disable_deltas_for_next_request_ = true; 703 disable_deltas_for_next_request_ = true;
638 request_scheduler_->ScheduleFetchShortly(); 704 request_scheduler_->ScheduleFetchShortly();
639 } 705 }
640 } 706 }
641 707
642 void VariationsService::OnResourceRequestsAllowed() { 708 void VariationsService::OnResourceRequestsAllowed() {
643 DCHECK(thread_checker_.CalledOnValidThread()); 709 DCHECK(thread_checker_.CalledOnValidThread());
644 710
645 // Note that this only attempts to fetch the seed at most once per period 711 // 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
787 std::string stored_country; 853 std::string stored_country;
788 854
789 if (list_value->GetSize() == 2) { 855 if (list_value->GetSize() == 2) {
790 list_value->GetString(1, &stored_country); 856 list_value->GetString(1, &stored_country);
791 } 857 }
792 858
793 return stored_country; 859 return stored_country;
794 } 860 }
795 861
796 } // namespace variations 862 } // namespace variations
OLDNEW
« no previous file with comments | « components/variations/service/variations_service.h ('k') | components/variations/service/variations_service_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698