cygprofile: increase timeouts to allow showing web contents
[chromium-blink-merge.git] / components / variations / service / variations_service.cc
blob97d0769075fd28a73e110b1a86d8dac5863d5d74
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
3 // found in the LICENSE file.
5 #include "components/variations/service/variations_service.h"
7 #include "base/build_time.h"
8 #include "base/command_line.h"
9 #include "base/metrics/histogram.h"
10 #include "base/metrics/sparse_histogram.h"
11 #include "base/prefs/pref_registry_simple.h"
12 #include "base/prefs/pref_service.h"
13 #include "base/sys_info.h"
14 #include "base/task_runner_util.h"
15 #include "base/timer/elapsed_timer.h"
16 #include "base/values.h"
17 #include "base/version.h"
18 #include "components/metrics/metrics_state_manager.h"
19 #include "components/network_time/network_time_tracker.h"
20 #include "components/pref_registry/pref_registry_syncable.h"
21 #include "components/variations/pref_names.h"
22 #include "components/variations/proto/variations_seed.pb.h"
23 #include "components/variations/variations_seed_processor.h"
24 #include "components/variations/variations_seed_simulator.h"
25 #include "components/variations/variations_switches.h"
26 #include "components/variations/variations_url_constants.h"
27 #include "components/version_info/version_info.h"
28 #include "net/base/load_flags.h"
29 #include "net/base/net_errors.h"
30 #include "net/base/network_change_notifier.h"
31 #include "net/base/url_util.h"
32 #include "net/http/http_response_headers.h"
33 #include "net/http/http_status_code.h"
34 #include "net/http/http_util.h"
35 #include "net/url_request/url_fetcher.h"
36 #include "net/url_request/url_request_status.h"
37 #include "ui/base/device_form_factor.h"
38 #include "url/gurl.h"
40 namespace variations {
42 namespace {
44 const int kMaxRetrySeedFetch = 5;
46 // TODO(mad): To be removed when we stop updating the NetworkTimeTracker.
47 // For the HTTP date headers, the resolution of the server time is 1 second.
48 const int64 kServerTimeResolutionMs = 1000;
50 // Wrapper around channel checking, used to enable channel mocking for
51 // testing. If the current browser channel is not UNKNOWN, this will return
52 // that channel value. Otherwise, if the fake channel flag is provided, this
53 // will return the fake channel. Failing that, this will return the UNKNOWN
54 // channel.
55 variations::Study_Channel GetChannelForVariations(
56 version_info::Channel product_channel) {
57 switch (product_channel) {
58 case version_info::Channel::CANARY:
59 return variations::Study_Channel_CANARY;
60 case version_info::Channel::DEV:
61 return variations::Study_Channel_DEV;
62 case version_info::Channel::BETA:
63 return variations::Study_Channel_BETA;
64 case version_info::Channel::STABLE:
65 return variations::Study_Channel_STABLE;
66 case version_info::Channel::UNKNOWN:
67 break;
69 const std::string forced_channel =
70 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
71 switches::kFakeVariationsChannel);
72 if (forced_channel == "stable")
73 return variations::Study_Channel_STABLE;
74 if (forced_channel == "beta")
75 return variations::Study_Channel_BETA;
76 if (forced_channel == "dev")
77 return variations::Study_Channel_DEV;
78 if (forced_channel == "canary")
79 return variations::Study_Channel_CANARY;
80 DVLOG(1) << "Invalid channel provided: " << forced_channel;
81 return variations::Study_Channel_UNKNOWN;
84 // Returns a string that will be used for the value of the 'osname' URL param
85 // to the variations server.
86 std::string GetPlatformString() {
87 #if defined(OS_WIN)
88 return "win";
89 #elif defined(OS_IOS)
90 return "ios";
91 #elif defined(OS_MACOSX)
92 return "mac";
93 #elif defined(OS_CHROMEOS)
94 return "chromeos";
95 #elif defined(OS_ANDROID)
96 return "android";
97 #elif defined(OS_LINUX) || defined(OS_BSD) || defined(OS_SOLARIS)
98 // Default BSD and SOLARIS to Linux to not break those builds, although these
99 // platforms are not officially supported by Chrome.
100 return "linux";
101 #else
102 #error Unknown platform
103 #endif
106 // Gets the restrict parameter from either the client or |policy_pref_service|.
107 std::string GetRestrictParameterPref(VariationsServiceClient* client,
108 PrefService* policy_pref_service) {
109 std::string parameter;
110 if (client->OverridesRestrictParameter(&parameter) || !policy_pref_service)
111 return parameter;
113 return policy_pref_service->GetString(prefs::kVariationsRestrictParameter);
116 enum ResourceRequestsAllowedState {
117 RESOURCE_REQUESTS_ALLOWED,
118 RESOURCE_REQUESTS_NOT_ALLOWED,
119 RESOURCE_REQUESTS_ALLOWED_NOTIFIED,
120 RESOURCE_REQUESTS_NOT_ALLOWED_EULA_NOT_ACCEPTED,
121 RESOURCE_REQUESTS_NOT_ALLOWED_NETWORK_DOWN,
122 RESOURCE_REQUESTS_NOT_ALLOWED_COMMAND_LINE_DISABLED,
123 RESOURCE_REQUESTS_ALLOWED_ENUM_SIZE,
126 // Records UMA histogram with the current resource requests allowed state.
127 void RecordRequestsAllowedHistogram(ResourceRequestsAllowedState state) {
128 UMA_HISTOGRAM_ENUMERATION("Variations.ResourceRequestsAllowed", state,
129 RESOURCE_REQUESTS_ALLOWED_ENUM_SIZE);
132 // Converts ResourceRequestAllowedNotifier::State to the corresponding
133 // ResourceRequestsAllowedState value.
134 ResourceRequestsAllowedState ResourceRequestStateToHistogramValue(
135 web_resource::ResourceRequestAllowedNotifier::State state) {
136 using web_resource::ResourceRequestAllowedNotifier;
137 switch (state) {
138 case ResourceRequestAllowedNotifier::DISALLOWED_EULA_NOT_ACCEPTED:
139 return RESOURCE_REQUESTS_NOT_ALLOWED_EULA_NOT_ACCEPTED;
140 case ResourceRequestAllowedNotifier::DISALLOWED_NETWORK_DOWN:
141 return RESOURCE_REQUESTS_NOT_ALLOWED_NETWORK_DOWN;
142 case ResourceRequestAllowedNotifier::DISALLOWED_COMMAND_LINE_DISABLED:
143 return RESOURCE_REQUESTS_NOT_ALLOWED_COMMAND_LINE_DISABLED;
144 case ResourceRequestAllowedNotifier::ALLOWED:
145 return RESOURCE_REQUESTS_ALLOWED;
147 NOTREACHED();
148 return RESOURCE_REQUESTS_NOT_ALLOWED;
152 // Gets current form factor and converts it from enum DeviceFormFactor to enum
153 // Study_FormFactor.
154 variations::Study_FormFactor GetCurrentFormFactor() {
155 switch (ui::GetDeviceFormFactor()) {
156 case ui::DEVICE_FORM_FACTOR_PHONE:
157 return variations::Study_FormFactor_PHONE;
158 case ui::DEVICE_FORM_FACTOR_TABLET:
159 return variations::Study_FormFactor_TABLET;
160 case ui::DEVICE_FORM_FACTOR_DESKTOP:
161 return variations::Study_FormFactor_DESKTOP;
163 NOTREACHED();
164 return variations::Study_FormFactor_DESKTOP;
167 // Gets the hardware class and returns it as a string. This returns an empty
168 // string if the client is not ChromeOS.
169 std::string GetHardwareClass() {
170 #if defined(OS_CHROMEOS)
171 return base::SysInfo::GetLsbReleaseBoard();
172 #endif // OS_CHROMEOS
173 return std::string();
176 // Returns the date that should be used by the VariationsSeedProcessor to do
177 // expiry and start date checks.
178 base::Time GetReferenceDateForExpiryChecks(PrefService* local_state) {
179 const int64 date_value = local_state->GetInt64(prefs::kVariationsSeedDate);
180 const base::Time seed_date = base::Time::FromInternalValue(date_value);
181 const base::Time build_time = base::GetBuildTime();
182 // Use the build time for date checks if either the seed date is invalid or
183 // the build time is newer than the seed date.
184 base::Time reference_date = seed_date;
185 if (seed_date.is_null() || seed_date < build_time)
186 reference_date = build_time;
187 return reference_date;
190 // Returns the header value for |name| from |headers| or an empty string if not
191 // set.
192 std::string GetHeaderValue(const net::HttpResponseHeaders* headers,
193 const base::StringPiece& name) {
194 std::string value;
195 headers->EnumerateHeader(NULL, name, &value);
196 return value;
199 } // namespace
201 VariationsService::VariationsService(
202 scoped_ptr<VariationsServiceClient> client,
203 scoped_ptr<web_resource::ResourceRequestAllowedNotifier> notifier,
204 PrefService* local_state,
205 metrics::MetricsStateManager* state_manager)
206 : client_(client.Pass()),
207 local_state_(local_state),
208 state_manager_(state_manager),
209 policy_pref_service_(local_state),
210 seed_store_(local_state),
211 create_trials_from_seed_called_(false),
212 initial_request_completed_(false),
213 disable_deltas_for_next_request_(false),
214 resource_request_allowed_notifier_(notifier.Pass()),
215 request_count_(0),
216 weak_ptr_factory_(this) {
217 resource_request_allowed_notifier_->Init(this);
220 VariationsService::~VariationsService() {
223 bool VariationsService::CreateTrialsFromSeed() {
224 DCHECK(thread_checker_.CalledOnValidThread());
226 create_trials_from_seed_called_ = true;
228 variations::VariationsSeed seed;
229 if (!seed_store_.LoadSeed(&seed))
230 return false;
232 const base::Version current_version(version_info::GetVersionNumber());
233 if (!current_version.IsValid())
234 return false;
236 variations::Study_Channel channel =
237 GetChannelForVariations(client_->GetChannel());
238 UMA_HISTOGRAM_SPARSE_SLOWLY("Variations.UserChannel", channel);
240 const std::string latest_country =
241 local_state_->GetString(prefs::kVariationsCountry);
242 // Note that passing |client_| via base::Unretained below is safe because
243 // the callback is executed synchronously.
244 variations::VariationsSeedProcessor().CreateTrialsFromSeed(
245 seed, client_->GetApplicationLocale(),
246 GetReferenceDateForExpiryChecks(local_state_), current_version, channel,
247 GetCurrentFormFactor(), GetHardwareClass(), latest_country,
248 LoadPermanentConsistencyCountry(current_version, latest_country),
249 base::Bind(&VariationsServiceClient::OverrideUIString,
250 base::Unretained(client_.get())));
252 const base::Time now = base::Time::Now();
254 // Log the "freshness" of the seed that was just used. The freshness is the
255 // time between the last successful seed download and now.
256 const int64 last_fetch_time_internal =
257 local_state_->GetInt64(prefs::kVariationsLastFetchTime);
258 if (last_fetch_time_internal) {
259 const base::TimeDelta delta =
260 now - base::Time::FromInternalValue(last_fetch_time_internal);
261 // Log the value in number of minutes.
262 UMA_HISTOGRAM_CUSTOM_COUNTS("Variations.SeedFreshness", delta.InMinutes(),
263 1, base::TimeDelta::FromDays(30).InMinutes(), 50);
266 // Log the skew between the seed date and the system clock/build time to
267 // analyze whether either could be used to make old variations seeds expire
268 // after some time.
269 const int64 seed_date_internal =
270 local_state_->GetInt64(prefs::kVariationsSeedDate);
271 if (seed_date_internal) {
272 const base::Time seed_date =
273 base::Time::FromInternalValue(seed_date_internal);
274 const int system_clock_delta_days = (now - seed_date).InDays();
275 if (system_clock_delta_days < 0) {
276 UMA_HISTOGRAM_COUNTS_100("Variations.SeedDateSkew.SystemClockBehindBy",
277 -system_clock_delta_days);
278 } else {
279 UMA_HISTOGRAM_COUNTS_100("Variations.SeedDateSkew.SystemClockAheadBy",
280 system_clock_delta_days);
283 const int build_time_delta_days =
284 (base::GetBuildTime() - seed_date).InDays();
285 if (build_time_delta_days < 0) {
286 UMA_HISTOGRAM_COUNTS_100("Variations.SeedDateSkew.BuildTimeBehindBy",
287 -build_time_delta_days);
288 } else {
289 UMA_HISTOGRAM_COUNTS_100("Variations.SeedDateSkew.BuildTimeAheadBy",
290 build_time_delta_days);
294 return true;
297 void VariationsService::PerformPreMainMessageLoopStartup() {
298 DCHECK(thread_checker_.CalledOnValidThread());
300 StartRepeatedVariationsSeedFetch();
301 client_->OnInitialStartup();
304 void VariationsService::StartRepeatedVariationsSeedFetch() {
305 DCHECK(thread_checker_.CalledOnValidThread());
307 // Initialize the Variations server URL.
308 variations_server_url_ =
309 GetVariationsServerURL(policy_pref_service_, restrict_mode_);
311 // Check that |CreateTrialsFromSeed| was called, which is necessary to
312 // retrieve the serial number that will be sent to the server.
313 DCHECK(create_trials_from_seed_called_);
315 DCHECK(!request_scheduler_.get());
316 // Note that the act of instantiating the scheduler will start the fetch, if
317 // the scheduler deems appropriate.
318 request_scheduler_.reset(VariationsRequestScheduler::Create(
319 base::Bind(&VariationsService::FetchVariationsSeed,
320 weak_ptr_factory_.GetWeakPtr()),
321 local_state_));
322 request_scheduler_->Start();
325 void VariationsService::AddObserver(Observer* observer) {
326 DCHECK(thread_checker_.CalledOnValidThread());
327 observer_list_.AddObserver(observer);
330 void VariationsService::RemoveObserver(Observer* observer) {
331 DCHECK(thread_checker_.CalledOnValidThread());
332 observer_list_.RemoveObserver(observer);
335 void VariationsService::OnAppEnterForeground() {
336 DCHECK(thread_checker_.CalledOnValidThread());
338 // On mobile platforms, initialize the fetch scheduler when we receive the
339 // first app foreground notification.
340 if (!request_scheduler_)
341 StartRepeatedVariationsSeedFetch();
342 request_scheduler_->OnAppEnterForeground();
345 void VariationsService::SetRestrictMode(const std::string& restrict_mode) {
346 DCHECK(thread_checker_.CalledOnValidThread());
348 // This should be called before the server URL has been computed.
349 DCHECK(variations_server_url_.is_empty());
350 restrict_mode_ = restrict_mode;
353 void VariationsService::SetCreateTrialsFromSeedCalledForTesting(bool called) {
354 DCHECK(thread_checker_.CalledOnValidThread());
355 create_trials_from_seed_called_ = called;
358 GURL VariationsService::GetVariationsServerURL(
359 PrefService* policy_pref_service,
360 const std::string& restrict_mode_override) {
361 std::string server_url_string(
362 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
363 switches::kVariationsServerURL));
364 if (server_url_string.empty())
365 server_url_string = kDefaultServerUrl;
366 GURL server_url = GURL(server_url_string);
368 const std::string restrict_param =
369 !restrict_mode_override.empty()
370 ? restrict_mode_override
371 : GetRestrictParameterPref(client_.get(), policy_pref_service);
372 if (!restrict_param.empty()) {
373 server_url = net::AppendOrReplaceQueryParameter(server_url,
374 "restrict",
375 restrict_param);
378 server_url = net::AppendOrReplaceQueryParameter(server_url, "osname",
379 GetPlatformString());
381 DCHECK(server_url.is_valid());
382 return server_url;
385 // static
386 std::string VariationsService::GetDefaultVariationsServerURLForTesting() {
387 return kDefaultServerUrl;
390 // static
391 void VariationsService::RegisterPrefs(PrefRegistrySimple* registry) {
392 VariationsSeedStore::RegisterPrefs(registry);
393 registry->RegisterInt64Pref(prefs::kVariationsLastFetchTime, 0);
394 // This preference will only be written by the policy service, which will fill
395 // it according to a value stored in the User Policy.
396 registry->RegisterStringPref(prefs::kVariationsRestrictParameter,
397 std::string());
398 // This preference keeps track of the country code used to filter
399 // permanent-consistency studies.
400 registry->RegisterListPref(prefs::kVariationsPermanentConsistencyCountry);
403 // static
404 void VariationsService::RegisterProfilePrefs(
405 user_prefs::PrefRegistrySyncable* registry) {
406 // This preference will only be written by the policy service, which will fill
407 // it according to a value stored in the User Policy.
408 registry->RegisterStringPref(prefs::kVariationsRestrictParameter,
409 std::string());
412 // static
413 scoped_ptr<VariationsService> VariationsService::Create(
414 scoped_ptr<VariationsServiceClient> client,
415 PrefService* local_state,
416 metrics::MetricsStateManager* state_manager,
417 const char* disable_network_switch) {
418 scoped_ptr<VariationsService> result;
419 #if !defined(GOOGLE_CHROME_BUILD)
420 // Unless the URL was provided, unsupported builds should return NULL to
421 // indicate that the service should not be used.
422 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
423 switches::kVariationsServerURL)) {
424 DVLOG(1) << "Not creating VariationsService in unofficial build without --"
425 << switches::kVariationsServerURL << " specified.";
426 return result.Pass();
428 #endif
429 result.reset(new VariationsService(
430 client.Pass(),
431 make_scoped_ptr(new web_resource::ResourceRequestAllowedNotifier(
432 local_state, disable_network_switch)),
433 local_state, state_manager));
434 return result.Pass();
437 // static
438 scoped_ptr<VariationsService> VariationsService::CreateForTesting(
439 scoped_ptr<VariationsServiceClient> client,
440 PrefService* local_state) {
441 return make_scoped_ptr(new VariationsService(
442 client.Pass(),
443 make_scoped_ptr(new web_resource::ResourceRequestAllowedNotifier(
444 local_state, nullptr)),
445 local_state, nullptr));
448 void VariationsService::DoActualFetch() {
449 DCHECK(thread_checker_.CalledOnValidThread());
450 DCHECK(!pending_seed_request_);
452 pending_seed_request_ = net::URLFetcher::Create(0, variations_server_url_,
453 net::URLFetcher::GET, this);
454 pending_seed_request_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES |
455 net::LOAD_DO_NOT_SAVE_COOKIES);
456 pending_seed_request_->SetRequestContext(client_->GetURLRequestContext());
457 pending_seed_request_->SetMaxRetriesOn5xx(kMaxRetrySeedFetch);
458 if (!seed_store_.variations_serial_number().empty() &&
459 !disable_deltas_for_next_request_) {
460 // If the current seed includes a country code, deltas are not supported (as
461 // the serial number doesn't take into account the country code). The server
462 // will update us with a seed that doesn't include a country code which will
463 // enable deltas to work.
464 // TODO(asvitkine): Remove the check in M50+ when the percentage of clients
465 // that have an old seed with a country code becomes miniscule.
466 if (!seed_store_.seed_has_country_code()) {
467 // Tell the server that delta-compressed seeds are supported.
468 pending_seed_request_->AddExtraRequestHeader("A-IM:x-bm");
470 // Get the seed only if its serial number doesn't match what we have.
471 pending_seed_request_->AddExtraRequestHeader(
472 "If-None-Match:" + seed_store_.variations_serial_number());
474 pending_seed_request_->Start();
476 const base::TimeTicks now = base::TimeTicks::Now();
477 base::TimeDelta time_since_last_fetch;
478 // Record a time delta of 0 (default value) if there was no previous fetch.
479 if (!last_request_started_time_.is_null())
480 time_since_last_fetch = now - last_request_started_time_;
481 UMA_HISTOGRAM_CUSTOM_COUNTS("Variations.TimeSinceLastFetchAttempt",
482 time_since_last_fetch.InMinutes(), 0,
483 base::TimeDelta::FromDays(7).InMinutes(), 50);
484 UMA_HISTOGRAM_COUNTS_100("Variations.RequestCount", request_count_);
485 ++request_count_;
486 last_request_started_time_ = now;
487 disable_deltas_for_next_request_ = false;
490 bool VariationsService::StoreSeed(const std::string& seed_data,
491 const std::string& seed_signature,
492 const std::string& country_code,
493 const base::Time& date_fetched,
494 bool is_delta_compressed) {
495 DCHECK(thread_checker_.CalledOnValidThread());
497 scoped_ptr<variations::VariationsSeed> seed(new variations::VariationsSeed);
498 if (!seed_store_.StoreSeedData(seed_data, seed_signature, country_code,
499 date_fetched, is_delta_compressed,
500 seed.get())) {
501 return false;
503 RecordLastFetchTime();
505 // Perform seed simulation only if |state_manager_| is not-NULL. The state
506 // manager may be NULL for some unit tests.
507 if (!state_manager_)
508 return true;
510 base::PostTaskAndReplyWithResult(
511 client_->GetBlockingPool(), FROM_HERE,
512 client_->GetVersionForSimulationCallback(),
513 base::Bind(&VariationsService::PerformSimulationWithVersion,
514 weak_ptr_factory_.GetWeakPtr(), base::Passed(&seed)));
515 return true;
518 void VariationsService::FetchVariationsSeed() {
519 DCHECK(thread_checker_.CalledOnValidThread());
521 const web_resource::ResourceRequestAllowedNotifier::State state =
522 resource_request_allowed_notifier_->GetResourceRequestsAllowedState();
523 RecordRequestsAllowedHistogram(ResourceRequestStateToHistogramValue(state));
524 if (state != web_resource::ResourceRequestAllowedNotifier::ALLOWED) {
525 DVLOG(1) << "Resource requests were not allowed. Waiting for notification.";
526 return;
529 DoActualFetch();
532 void VariationsService::NotifyObservers(
533 const variations::VariationsSeedSimulator::Result& result) {
534 DCHECK(thread_checker_.CalledOnValidThread());
536 if (result.kill_critical_group_change_count > 0) {
537 FOR_EACH_OBSERVER(Observer, observer_list_,
538 OnExperimentChangesDetected(Observer::CRITICAL));
539 } else if (result.kill_best_effort_group_change_count > 0) {
540 FOR_EACH_OBSERVER(Observer, observer_list_,
541 OnExperimentChangesDetected(Observer::BEST_EFFORT));
545 void VariationsService::OnURLFetchComplete(const net::URLFetcher* source) {
546 DCHECK(thread_checker_.CalledOnValidThread());
547 DCHECK_EQ(pending_seed_request_.get(), source);
549 const bool is_first_request = !initial_request_completed_;
550 initial_request_completed_ = true;
552 // The fetcher will be deleted when the request is handled.
553 scoped_ptr<const net::URLFetcher> request(pending_seed_request_.release());
554 const net::URLRequestStatus& request_status = request->GetStatus();
555 if (request_status.status() != net::URLRequestStatus::SUCCESS) {
556 UMA_HISTOGRAM_SPARSE_SLOWLY("Variations.FailedRequestErrorCode",
557 -request_status.error());
558 DVLOG(1) << "Variations server request failed with error: "
559 << request_status.error() << ": "
560 << net::ErrorToString(request_status.error());
561 // It's common for the very first fetch attempt to fail (e.g. the network
562 // may not yet be available). In such a case, try again soon, rather than
563 // waiting the full time interval.
564 if (is_first_request)
565 request_scheduler_->ScheduleFetchShortly();
566 return;
569 // Log the response code.
570 const int response_code = request->GetResponseCode();
571 UMA_HISTOGRAM_SPARSE_SLOWLY("Variations.SeedFetchResponseCode",
572 response_code);
574 const base::TimeDelta latency =
575 base::TimeTicks::Now() - last_request_started_time_;
577 base::Time response_date;
578 if (response_code == net::HTTP_OK ||
579 response_code == net::HTTP_NOT_MODIFIED) {
580 bool success = request->GetResponseHeaders()->GetDateValue(&response_date);
581 DCHECK(success || response_date.is_null());
583 if (!response_date.is_null()) {
584 client_->GetNetworkTimeTracker()->UpdateNetworkTime(
585 response_date,
586 base::TimeDelta::FromMilliseconds(kServerTimeResolutionMs), latency,
587 base::TimeTicks::Now());
591 if (response_code != net::HTTP_OK) {
592 DVLOG(1) << "Variations server request returned non-HTTP_OK response code: "
593 << response_code;
594 if (response_code == net::HTTP_NOT_MODIFIED) {
595 RecordLastFetchTime();
596 // Update the seed date value in local state (used for expiry check on
597 // next start up), since 304 is a successful response.
598 seed_store_.UpdateSeedDateAndLogDayChange(response_date);
600 return;
603 std::string seed_data;
604 bool success = request->GetResponseAsString(&seed_data);
605 DCHECK(success);
607 net::HttpResponseHeaders* headers = request->GetResponseHeaders();
608 const std::string signature = GetHeaderValue(headers, "X-Seed-Signature");
609 const std::string country_code = GetHeaderValue(headers, "X-Country");
610 const bool is_delta_compressed = (GetHeaderValue(headers, "IM") == "x-bm");
611 const bool store_success = StoreSeed(seed_data, signature, country_code,
612 response_date, is_delta_compressed);
613 if (!store_success && is_delta_compressed) {
614 disable_deltas_for_next_request_ = true;
615 request_scheduler_->ScheduleFetchShortly();
619 void VariationsService::OnResourceRequestsAllowed() {
620 DCHECK(thread_checker_.CalledOnValidThread());
622 // Note that this only attempts to fetch the seed at most once per period
623 // (kSeedFetchPeriodHours). This works because
624 // |resource_request_allowed_notifier_| only calls this method if an
625 // attempt was made earlier that fails (which implies that the period had
626 // elapsed). After a successful attempt is made, the notifier will know not
627 // to call this method again until another failed attempt occurs.
628 RecordRequestsAllowedHistogram(RESOURCE_REQUESTS_ALLOWED_NOTIFIED);
629 DVLOG(1) << "Retrying fetch.";
630 DoActualFetch();
632 // This service must have created a scheduler in order for this to be called.
633 DCHECK(request_scheduler_.get());
634 request_scheduler_->Reset();
637 void VariationsService::PerformSimulationWithVersion(
638 scoped_ptr<variations::VariationsSeed> seed,
639 const base::Version& version) {
640 DCHECK(thread_checker_.CalledOnValidThread());
642 if (!version.IsValid())
643 return;
645 const base::ElapsedTimer timer;
647 scoped_ptr<const base::FieldTrial::EntropyProvider> entropy_provider =
648 state_manager_->CreateEntropyProvider();
649 variations::VariationsSeedSimulator seed_simulator(*entropy_provider);
651 const std::string latest_country =
652 local_state_->GetString(prefs::kVariationsCountry);
653 const variations::VariationsSeedSimulator::Result result =
654 seed_simulator.SimulateSeedStudies(
655 *seed, client_->GetApplicationLocale(),
656 GetReferenceDateForExpiryChecks(local_state_), version,
657 GetChannelForVariations(client_->GetChannel()),
658 GetCurrentFormFactor(), GetHardwareClass(), latest_country,
659 LoadPermanentConsistencyCountry(version, latest_country));
661 UMA_HISTOGRAM_COUNTS_100("Variations.SimulateSeed.NormalChanges",
662 result.normal_group_change_count);
663 UMA_HISTOGRAM_COUNTS_100("Variations.SimulateSeed.KillBestEffortChanges",
664 result.kill_best_effort_group_change_count);
665 UMA_HISTOGRAM_COUNTS_100("Variations.SimulateSeed.KillCriticalChanges",
666 result.kill_critical_group_change_count);
668 UMA_HISTOGRAM_TIMES("Variations.SimulateSeed.Duration", timer.Elapsed());
670 NotifyObservers(result);
673 void VariationsService::RecordLastFetchTime() {
674 DCHECK(thread_checker_.CalledOnValidThread());
676 // local_state_ is NULL in tests, so check it first.
677 if (local_state_) {
678 local_state_->SetInt64(prefs::kVariationsLastFetchTime,
679 base::Time::Now().ToInternalValue());
683 std::string VariationsService::GetInvalidVariationsSeedSignature() const {
684 DCHECK(thread_checker_.CalledOnValidThread());
685 return seed_store_.GetInvalidSignature();
688 std::string VariationsService::LoadPermanentConsistencyCountry(
689 const base::Version& version,
690 const std::string& latest_country) {
691 DCHECK(thread_checker_.CalledOnValidThread());
692 DCHECK(version.IsValid());
694 const base::ListValue* list_value =
695 local_state_->GetList(prefs::kVariationsPermanentConsistencyCountry);
696 std::string stored_version_string;
697 std::string stored_country;
699 // Determine if the saved pref value is present and valid.
700 const bool is_pref_present = list_value && !list_value->empty();
701 const bool is_pref_valid = is_pref_present && list_value->GetSize() == 2 &&
702 list_value->GetString(0, &stored_version_string) &&
703 list_value->GetString(1, &stored_country) &&
704 base::Version(stored_version_string).IsValid();
706 // Determine if the version from the saved pref matches |version|.
707 const bool does_version_match =
708 is_pref_valid && version.Equals(base::Version(stored_version_string));
710 // Determine if the country in the saved pref matches the country in |seed|.
711 const bool does_country_match = is_pref_valid && !latest_country.empty() &&
712 stored_country == latest_country;
714 // Record a histogram for how the saved pref value compares to the current
715 // version and the country code in the variations seed.
716 LoadPermanentConsistencyCountryResult result;
717 if (!is_pref_present) {
718 result = !latest_country.empty() ? LOAD_COUNTRY_NO_PREF_HAS_SEED
719 : LOAD_COUNTRY_NO_PREF_NO_SEED;
720 } else if (!is_pref_valid) {
721 result = !latest_country.empty() ? LOAD_COUNTRY_INVALID_PREF_HAS_SEED
722 : LOAD_COUNTRY_INVALID_PREF_NO_SEED;
723 } else if (latest_country.empty()) {
724 result = does_version_match ? LOAD_COUNTRY_HAS_PREF_NO_SEED_VERSION_EQ
725 : LOAD_COUNTRY_HAS_PREF_NO_SEED_VERSION_NEQ;
726 } else if (does_version_match) {
727 result = does_country_match ? LOAD_COUNTRY_HAS_BOTH_VERSION_EQ_COUNTRY_EQ
728 : LOAD_COUNTRY_HAS_BOTH_VERSION_EQ_COUNTRY_NEQ;
729 } else {
730 result = does_country_match ? LOAD_COUNTRY_HAS_BOTH_VERSION_NEQ_COUNTRY_EQ
731 : LOAD_COUNTRY_HAS_BOTH_VERSION_NEQ_COUNTRY_NEQ;
733 UMA_HISTOGRAM_ENUMERATION("Variations.LoadPermanentConsistencyCountryResult",
734 result, LOAD_COUNTRY_MAX);
736 // Use the stored country if one is available and was fetched since the last
737 // time Chrome was updated.
738 if (is_pref_present && does_version_match)
739 return stored_country;
741 if (latest_country.empty()) {
742 if (!is_pref_valid)
743 local_state_->ClearPref(prefs::kVariationsPermanentConsistencyCountry);
744 // If we've never received a country code from the server, use an empty
745 // country so that it won't pass any filters that specifically include
746 // countries, but so that it will pass any filters that specifically exclude
747 // countries.
748 return std::string();
751 // Otherwise, update the pref with the current Chrome version and country.
752 base::ListValue new_list_value;
753 new_list_value.AppendString(version.GetString());
754 new_list_value.AppendString(latest_country);
755 local_state_->Set(prefs::kVariationsPermanentConsistencyCountry,
756 new_list_value);
757 return latest_country;
760 } // namespace variations