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/data_use_measurement/core/data_use_user_data.h"
19 #include "components/metrics/metrics_state_manager.h"
20 #include "components/network_time/network_time_tracker.h"
21 #include "components/pref_registry/pref_registry_syncable.h"
22 #include "components/variations/pref_names.h"
23 #include "components/variations/proto/variations_seed.pb.h"
24 #include "components/variations/variations_seed_processor.h"
25 #include "components/variations/variations_seed_simulator.h"
26 #include "components/variations/variations_switches.h"
27 #include "components/variations/variations_url_constants.h"
28 #include "components/version_info/version_info.h"
29 #include "net/base/load_flags.h"
30 #include "net/base/net_errors.h"
31 #include "net/base/network_change_notifier.h"
32 #include "net/base/url_util.h"
33 #include "net/http/http_response_headers.h"
34 #include "net/http/http_status_code.h"
35 #include "net/http/http_util.h"
36 #include "net/url_request/url_fetcher.h"
37 #include "net/url_request/url_request_status.h"
38 #include "ui/base/device_form_factor.h"
41 namespace variations
{
45 const int kMaxRetrySeedFetch
= 5;
47 // TODO(mad): To be removed when we stop updating the NetworkTimeTracker.
48 // For the HTTP date headers, the resolution of the server time is 1 second.
49 const int64 kServerTimeResolutionMs
= 1000;
51 // Wrapper around channel checking, used to enable channel mocking for
52 // testing. If the current browser channel is not UNKNOWN, this will return
53 // that channel value. Otherwise, if the fake channel flag is provided, this
54 // will return the fake channel. Failing that, this will return the UNKNOWN
56 variations::Study_Channel
GetChannelForVariations(
57 version_info::Channel product_channel
) {
58 switch (product_channel
) {
59 case version_info::Channel::CANARY
:
60 return variations::Study_Channel_CANARY
;
61 case version_info::Channel::DEV
:
62 return variations::Study_Channel_DEV
;
63 case version_info::Channel::BETA
:
64 return variations::Study_Channel_BETA
;
65 case version_info::Channel::STABLE
:
66 return variations::Study_Channel_STABLE
;
67 case version_info::Channel::UNKNOWN
:
70 const std::string forced_channel
=
71 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
72 switches::kFakeVariationsChannel
);
73 if (forced_channel
== "stable")
74 return variations::Study_Channel_STABLE
;
75 if (forced_channel
== "beta")
76 return variations::Study_Channel_BETA
;
77 if (forced_channel
== "dev")
78 return variations::Study_Channel_DEV
;
79 if (forced_channel
== "canary")
80 return variations::Study_Channel_CANARY
;
81 DVLOG(1) << "Invalid channel provided: " << forced_channel
;
82 return variations::Study_Channel_UNKNOWN
;
85 // Returns a string that will be used for the value of the 'osname' URL param
86 // to the variations server.
87 std::string
GetPlatformString() {
92 #elif defined(OS_MACOSX)
94 #elif defined(OS_CHROMEOS)
96 #elif defined(OS_ANDROID)
98 #elif defined(OS_LINUX) || defined(OS_BSD) || defined(OS_SOLARIS)
99 // Default BSD and SOLARIS to Linux to not break those builds, although these
100 // platforms are not officially supported by Chrome.
103 #error Unknown platform
107 // Gets the restrict parameter from either the client or |policy_pref_service|.
108 std::string
GetRestrictParameterPref(VariationsServiceClient
* client
,
109 PrefService
* policy_pref_service
) {
110 std::string parameter
;
111 if (client
->OverridesRestrictParameter(¶meter
) || !policy_pref_service
)
114 return policy_pref_service
->GetString(prefs::kVariationsRestrictParameter
);
117 enum ResourceRequestsAllowedState
{
118 RESOURCE_REQUESTS_ALLOWED
,
119 RESOURCE_REQUESTS_NOT_ALLOWED
,
120 RESOURCE_REQUESTS_ALLOWED_NOTIFIED
,
121 RESOURCE_REQUESTS_NOT_ALLOWED_EULA_NOT_ACCEPTED
,
122 RESOURCE_REQUESTS_NOT_ALLOWED_NETWORK_DOWN
,
123 RESOURCE_REQUESTS_NOT_ALLOWED_COMMAND_LINE_DISABLED
,
124 RESOURCE_REQUESTS_ALLOWED_ENUM_SIZE
,
127 // Records UMA histogram with the current resource requests allowed state.
128 void RecordRequestsAllowedHistogram(ResourceRequestsAllowedState state
) {
129 UMA_HISTOGRAM_ENUMERATION("Variations.ResourceRequestsAllowed", state
,
130 RESOURCE_REQUESTS_ALLOWED_ENUM_SIZE
);
133 // Converts ResourceRequestAllowedNotifier::State to the corresponding
134 // ResourceRequestsAllowedState value.
135 ResourceRequestsAllowedState
ResourceRequestStateToHistogramValue(
136 web_resource::ResourceRequestAllowedNotifier::State state
) {
137 using web_resource::ResourceRequestAllowedNotifier
;
139 case ResourceRequestAllowedNotifier::DISALLOWED_EULA_NOT_ACCEPTED
:
140 return RESOURCE_REQUESTS_NOT_ALLOWED_EULA_NOT_ACCEPTED
;
141 case ResourceRequestAllowedNotifier::DISALLOWED_NETWORK_DOWN
:
142 return RESOURCE_REQUESTS_NOT_ALLOWED_NETWORK_DOWN
;
143 case ResourceRequestAllowedNotifier::DISALLOWED_COMMAND_LINE_DISABLED
:
144 return RESOURCE_REQUESTS_NOT_ALLOWED_COMMAND_LINE_DISABLED
;
145 case ResourceRequestAllowedNotifier::ALLOWED
:
146 return RESOURCE_REQUESTS_ALLOWED
;
149 return RESOURCE_REQUESTS_NOT_ALLOWED
;
153 // Gets current form factor and converts it from enum DeviceFormFactor to enum
155 variations::Study_FormFactor
GetCurrentFormFactor() {
156 switch (ui::GetDeviceFormFactor()) {
157 case ui::DEVICE_FORM_FACTOR_PHONE
:
158 return variations::Study_FormFactor_PHONE
;
159 case ui::DEVICE_FORM_FACTOR_TABLET
:
160 return variations::Study_FormFactor_TABLET
;
161 case ui::DEVICE_FORM_FACTOR_DESKTOP
:
162 return variations::Study_FormFactor_DESKTOP
;
165 return variations::Study_FormFactor_DESKTOP
;
168 // Gets the hardware class and returns it as a string. This returns an empty
169 // string if the client is not ChromeOS.
170 std::string
GetHardwareClass() {
171 #if defined(OS_CHROMEOS)
172 return base::SysInfo::GetLsbReleaseBoard();
173 #endif // OS_CHROMEOS
174 return std::string();
177 // Returns the date that should be used by the VariationsSeedProcessor to do
178 // expiry and start date checks.
179 base::Time
GetReferenceDateForExpiryChecks(PrefService
* local_state
) {
180 const int64 date_value
= local_state
->GetInt64(prefs::kVariationsSeedDate
);
181 const base::Time seed_date
= base::Time::FromInternalValue(date_value
);
182 const base::Time build_time
= base::GetBuildTime();
183 // Use the build time for date checks if either the seed date is invalid or
184 // the build time is newer than the seed date.
185 base::Time reference_date
= seed_date
;
186 if (seed_date
.is_null() || seed_date
< build_time
)
187 reference_date
= build_time
;
188 return reference_date
;
191 // Returns the header value for |name| from |headers| or an empty string if not
193 std::string
GetHeaderValue(const net::HttpResponseHeaders
* headers
,
194 const base::StringPiece
& name
) {
196 headers
->EnumerateHeader(NULL
, name
, &value
);
202 VariationsService::VariationsService(
203 scoped_ptr
<VariationsServiceClient
> client
,
204 scoped_ptr
<web_resource::ResourceRequestAllowedNotifier
> notifier
,
205 PrefService
* local_state
,
206 metrics::MetricsStateManager
* state_manager
)
207 : client_(client
.Pass()),
208 local_state_(local_state
),
209 state_manager_(state_manager
),
210 policy_pref_service_(local_state
),
211 seed_store_(local_state
),
212 create_trials_from_seed_called_(false),
213 initial_request_completed_(false),
214 disable_deltas_for_next_request_(false),
215 resource_request_allowed_notifier_(notifier
.Pass()),
217 weak_ptr_factory_(this) {
218 resource_request_allowed_notifier_
->Init(this);
221 VariationsService::~VariationsService() {
224 bool VariationsService::CreateTrialsFromSeed() {
225 DCHECK(thread_checker_
.CalledOnValidThread());
227 create_trials_from_seed_called_
= true;
229 variations::VariationsSeed seed
;
230 if (!seed_store_
.LoadSeed(&seed
))
233 const base::Version
current_version(version_info::GetVersionNumber());
234 if (!current_version
.IsValid())
237 variations::Study_Channel channel
=
238 GetChannelForVariations(client_
->GetChannel());
239 UMA_HISTOGRAM_SPARSE_SLOWLY("Variations.UserChannel", channel
);
241 const std::string latest_country
=
242 local_state_
->GetString(prefs::kVariationsCountry
);
243 // Note that passing |client_| via base::Unretained below is safe because
244 // the callback is executed synchronously.
245 variations::VariationsSeedProcessor().CreateTrialsFromSeed(
246 seed
, client_
->GetApplicationLocale(),
247 GetReferenceDateForExpiryChecks(local_state_
), current_version
, channel
,
248 GetCurrentFormFactor(), GetHardwareClass(), latest_country
,
249 LoadPermanentConsistencyCountry(current_version
, latest_country
),
250 base::Bind(&VariationsServiceClient::OverrideUIString
,
251 base::Unretained(client_
.get())));
253 const base::Time now
= base::Time::Now();
255 // Log the "freshness" of the seed that was just used. The freshness is the
256 // time between the last successful seed download and now.
257 const int64 last_fetch_time_internal
=
258 local_state_
->GetInt64(prefs::kVariationsLastFetchTime
);
259 if (last_fetch_time_internal
) {
260 const base::TimeDelta delta
=
261 now
- base::Time::FromInternalValue(last_fetch_time_internal
);
262 // Log the value in number of minutes.
263 UMA_HISTOGRAM_CUSTOM_COUNTS("Variations.SeedFreshness", delta
.InMinutes(),
264 1, base::TimeDelta::FromDays(30).InMinutes(), 50);
267 // Log the skew between the seed date and the system clock/build time to
268 // analyze whether either could be used to make old variations seeds expire
270 const int64 seed_date_internal
=
271 local_state_
->GetInt64(prefs::kVariationsSeedDate
);
272 if (seed_date_internal
) {
273 const base::Time seed_date
=
274 base::Time::FromInternalValue(seed_date_internal
);
275 const int system_clock_delta_days
= (now
- seed_date
).InDays();
276 if (system_clock_delta_days
< 0) {
277 UMA_HISTOGRAM_COUNTS_100("Variations.SeedDateSkew.SystemClockBehindBy",
278 -system_clock_delta_days
);
280 UMA_HISTOGRAM_COUNTS_100("Variations.SeedDateSkew.SystemClockAheadBy",
281 system_clock_delta_days
);
284 const int build_time_delta_days
=
285 (base::GetBuildTime() - seed_date
).InDays();
286 if (build_time_delta_days
< 0) {
287 UMA_HISTOGRAM_COUNTS_100("Variations.SeedDateSkew.BuildTimeBehindBy",
288 -build_time_delta_days
);
290 UMA_HISTOGRAM_COUNTS_100("Variations.SeedDateSkew.BuildTimeAheadBy",
291 build_time_delta_days
);
298 void VariationsService::PerformPreMainMessageLoopStartup() {
299 DCHECK(thread_checker_
.CalledOnValidThread());
301 StartRepeatedVariationsSeedFetch();
302 client_
->OnInitialStartup();
305 void VariationsService::StartRepeatedVariationsSeedFetch() {
306 DCHECK(thread_checker_
.CalledOnValidThread());
308 // Initialize the Variations server URL.
309 variations_server_url_
=
310 GetVariationsServerURL(policy_pref_service_
, restrict_mode_
);
312 // Check that |CreateTrialsFromSeed| was called, which is necessary to
313 // retrieve the serial number that will be sent to the server.
314 DCHECK(create_trials_from_seed_called_
);
316 DCHECK(!request_scheduler_
.get());
317 // Note that the act of instantiating the scheduler will start the fetch, if
318 // the scheduler deems appropriate.
319 request_scheduler_
.reset(VariationsRequestScheduler::Create(
320 base::Bind(&VariationsService::FetchVariationsSeed
,
321 weak_ptr_factory_
.GetWeakPtr()),
323 request_scheduler_
->Start();
326 void VariationsService::AddObserver(Observer
* observer
) {
327 DCHECK(thread_checker_
.CalledOnValidThread());
328 observer_list_
.AddObserver(observer
);
331 void VariationsService::RemoveObserver(Observer
* observer
) {
332 DCHECK(thread_checker_
.CalledOnValidThread());
333 observer_list_
.RemoveObserver(observer
);
336 void VariationsService::OnAppEnterForeground() {
337 DCHECK(thread_checker_
.CalledOnValidThread());
339 // On mobile platforms, initialize the fetch scheduler when we receive the
340 // first app foreground notification.
341 if (!request_scheduler_
)
342 StartRepeatedVariationsSeedFetch();
343 request_scheduler_
->OnAppEnterForeground();
346 void VariationsService::SetRestrictMode(const std::string
& restrict_mode
) {
347 DCHECK(thread_checker_
.CalledOnValidThread());
349 // This should be called before the server URL has been computed.
350 DCHECK(variations_server_url_
.is_empty());
351 restrict_mode_
= restrict_mode
;
354 void VariationsService::SetCreateTrialsFromSeedCalledForTesting(bool called
) {
355 DCHECK(thread_checker_
.CalledOnValidThread());
356 create_trials_from_seed_called_
= called
;
359 GURL
VariationsService::GetVariationsServerURL(
360 PrefService
* policy_pref_service
,
361 const std::string
& restrict_mode_override
) {
362 std::string
server_url_string(
363 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
364 switches::kVariationsServerURL
));
365 if (server_url_string
.empty())
366 server_url_string
= kDefaultServerUrl
;
367 GURL server_url
= GURL(server_url_string
);
369 const std::string restrict_param
=
370 !restrict_mode_override
.empty()
371 ? restrict_mode_override
372 : GetRestrictParameterPref(client_
.get(), policy_pref_service
);
373 if (!restrict_param
.empty()) {
374 server_url
= net::AppendOrReplaceQueryParameter(server_url
,
379 server_url
= net::AppendOrReplaceQueryParameter(server_url
, "osname",
380 GetPlatformString());
382 DCHECK(server_url
.is_valid());
387 std::string
VariationsService::GetDefaultVariationsServerURLForTesting() {
388 return kDefaultServerUrl
;
392 void VariationsService::RegisterPrefs(PrefRegistrySimple
* registry
) {
393 VariationsSeedStore::RegisterPrefs(registry
);
394 registry
->RegisterInt64Pref(prefs::kVariationsLastFetchTime
, 0);
395 // This preference will only be written by the policy service, which will fill
396 // it according to a value stored in the User Policy.
397 registry
->RegisterStringPref(prefs::kVariationsRestrictParameter
,
399 // This preference keeps track of the country code used to filter
400 // permanent-consistency studies.
401 registry
->RegisterListPref(prefs::kVariationsPermanentConsistencyCountry
);
405 void VariationsService::RegisterProfilePrefs(
406 user_prefs::PrefRegistrySyncable
* registry
) {
407 // This preference will only be written by the policy service, which will fill
408 // it according to a value stored in the User Policy.
409 registry
->RegisterStringPref(prefs::kVariationsRestrictParameter
,
414 scoped_ptr
<VariationsService
> VariationsService::Create(
415 scoped_ptr
<VariationsServiceClient
> client
,
416 PrefService
* local_state
,
417 metrics::MetricsStateManager
* state_manager
,
418 const char* disable_network_switch
) {
419 scoped_ptr
<VariationsService
> result
;
420 #if !defined(GOOGLE_CHROME_BUILD)
421 // Unless the URL was provided, unsupported builds should return NULL to
422 // indicate that the service should not be used.
423 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
424 switches::kVariationsServerURL
)) {
425 DVLOG(1) << "Not creating VariationsService in unofficial build without --"
426 << switches::kVariationsServerURL
<< " specified.";
427 return result
.Pass();
430 result
.reset(new VariationsService(
432 make_scoped_ptr(new web_resource::ResourceRequestAllowedNotifier(
433 local_state
, disable_network_switch
)),
434 local_state
, state_manager
));
435 return result
.Pass();
439 scoped_ptr
<VariationsService
> VariationsService::CreateForTesting(
440 scoped_ptr
<VariationsServiceClient
> client
,
441 PrefService
* local_state
) {
442 return make_scoped_ptr(new VariationsService(
444 make_scoped_ptr(new web_resource::ResourceRequestAllowedNotifier(
445 local_state
, nullptr)),
446 local_state
, nullptr));
449 void VariationsService::DoActualFetch() {
450 DCHECK(thread_checker_
.CalledOnValidThread());
451 DCHECK(!pending_seed_request_
);
453 pending_seed_request_
= net::URLFetcher::Create(0, variations_server_url_
,
454 net::URLFetcher::GET
, this);
455 data_use_measurement::DataUseUserData::AttachToFetcher(
456 pending_seed_request_
.get(),
457 data_use_measurement::DataUseUserData::VARIATIONS
);
458 pending_seed_request_
->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES
|
459 net::LOAD_DO_NOT_SAVE_COOKIES
);
460 pending_seed_request_
->SetRequestContext(client_
->GetURLRequestContext());
461 pending_seed_request_
->SetMaxRetriesOn5xx(kMaxRetrySeedFetch
);
462 if (!seed_store_
.variations_serial_number().empty() &&
463 !disable_deltas_for_next_request_
) {
464 // If the current seed includes a country code, deltas are not supported (as
465 // the serial number doesn't take into account the country code). The server
466 // will update us with a seed that doesn't include a country code which will
467 // enable deltas to work.
468 // TODO(asvitkine): Remove the check in M50+ when the percentage of clients
469 // that have an old seed with a country code becomes miniscule.
470 if (!seed_store_
.seed_has_country_code()) {
471 // Tell the server that delta-compressed seeds are supported.
472 pending_seed_request_
->AddExtraRequestHeader("A-IM:x-bm");
474 // Get the seed only if its serial number doesn't match what we have.
475 pending_seed_request_
->AddExtraRequestHeader(
476 "If-None-Match:" + seed_store_
.variations_serial_number());
478 pending_seed_request_
->Start();
480 const base::TimeTicks now
= base::TimeTicks::Now();
481 base::TimeDelta time_since_last_fetch
;
482 // Record a time delta of 0 (default value) if there was no previous fetch.
483 if (!last_request_started_time_
.is_null())
484 time_since_last_fetch
= now
- last_request_started_time_
;
485 UMA_HISTOGRAM_CUSTOM_COUNTS("Variations.TimeSinceLastFetchAttempt",
486 time_since_last_fetch
.InMinutes(), 0,
487 base::TimeDelta::FromDays(7).InMinutes(), 50);
488 UMA_HISTOGRAM_COUNTS_100("Variations.RequestCount", request_count_
);
490 last_request_started_time_
= now
;
491 disable_deltas_for_next_request_
= false;
494 bool VariationsService::StoreSeed(const std::string
& seed_data
,
495 const std::string
& seed_signature
,
496 const std::string
& country_code
,
497 const base::Time
& date_fetched
,
498 bool is_delta_compressed
) {
499 DCHECK(thread_checker_
.CalledOnValidThread());
501 scoped_ptr
<variations::VariationsSeed
> seed(new variations::VariationsSeed
);
502 if (!seed_store_
.StoreSeedData(seed_data
, seed_signature
, country_code
,
503 date_fetched
, is_delta_compressed
,
507 RecordLastFetchTime();
509 // Perform seed simulation only if |state_manager_| is not-NULL. The state
510 // manager may be NULL for some unit tests.
514 base::PostTaskAndReplyWithResult(
515 client_
->GetBlockingPool(), FROM_HERE
,
516 client_
->GetVersionForSimulationCallback(),
517 base::Bind(&VariationsService::PerformSimulationWithVersion
,
518 weak_ptr_factory_
.GetWeakPtr(), base::Passed(&seed
)));
522 void VariationsService::FetchVariationsSeed() {
523 DCHECK(thread_checker_
.CalledOnValidThread());
525 const web_resource::ResourceRequestAllowedNotifier::State state
=
526 resource_request_allowed_notifier_
->GetResourceRequestsAllowedState();
527 RecordRequestsAllowedHistogram(ResourceRequestStateToHistogramValue(state
));
528 if (state
!= web_resource::ResourceRequestAllowedNotifier::ALLOWED
) {
529 DVLOG(1) << "Resource requests were not allowed. Waiting for notification.";
536 void VariationsService::NotifyObservers(
537 const variations::VariationsSeedSimulator::Result
& result
) {
538 DCHECK(thread_checker_
.CalledOnValidThread());
540 if (result
.kill_critical_group_change_count
> 0) {
541 FOR_EACH_OBSERVER(Observer
, observer_list_
,
542 OnExperimentChangesDetected(Observer::CRITICAL
));
543 } else if (result
.kill_best_effort_group_change_count
> 0) {
544 FOR_EACH_OBSERVER(Observer
, observer_list_
,
545 OnExperimentChangesDetected(Observer::BEST_EFFORT
));
549 void VariationsService::OnURLFetchComplete(const net::URLFetcher
* source
) {
550 DCHECK(thread_checker_
.CalledOnValidThread());
551 DCHECK_EQ(pending_seed_request_
.get(), source
);
553 const bool is_first_request
= !initial_request_completed_
;
554 initial_request_completed_
= true;
556 // The fetcher will be deleted when the request is handled.
557 scoped_ptr
<const net::URLFetcher
> request(pending_seed_request_
.release());
558 const net::URLRequestStatus
& request_status
= request
->GetStatus();
559 if (request_status
.status() != net::URLRequestStatus::SUCCESS
) {
560 UMA_HISTOGRAM_SPARSE_SLOWLY("Variations.FailedRequestErrorCode",
561 -request_status
.error());
562 DVLOG(1) << "Variations server request failed with error: "
563 << request_status
.error() << ": "
564 << net::ErrorToString(request_status
.error());
565 // It's common for the very first fetch attempt to fail (e.g. the network
566 // may not yet be available). In such a case, try again soon, rather than
567 // waiting the full time interval.
568 if (is_first_request
)
569 request_scheduler_
->ScheduleFetchShortly();
573 // Log the response code.
574 const int response_code
= request
->GetResponseCode();
575 UMA_HISTOGRAM_SPARSE_SLOWLY("Variations.SeedFetchResponseCode",
578 const base::TimeDelta latency
=
579 base::TimeTicks::Now() - last_request_started_time_
;
581 base::Time response_date
;
582 if (response_code
== net::HTTP_OK
||
583 response_code
== net::HTTP_NOT_MODIFIED
) {
584 bool success
= request
->GetResponseHeaders()->GetDateValue(&response_date
);
585 DCHECK(success
|| response_date
.is_null());
587 if (!response_date
.is_null()) {
588 client_
->GetNetworkTimeTracker()->UpdateNetworkTime(
590 base::TimeDelta::FromMilliseconds(kServerTimeResolutionMs
), latency
,
591 base::TimeTicks::Now());
595 if (response_code
!= net::HTTP_OK
) {
596 DVLOG(1) << "Variations server request returned non-HTTP_OK response code: "
598 if (response_code
== net::HTTP_NOT_MODIFIED
) {
599 RecordLastFetchTime();
600 // Update the seed date value in local state (used for expiry check on
601 // next start up), since 304 is a successful response.
602 seed_store_
.UpdateSeedDateAndLogDayChange(response_date
);
607 std::string seed_data
;
608 bool success
= request
->GetResponseAsString(&seed_data
);
611 net::HttpResponseHeaders
* headers
= request
->GetResponseHeaders();
612 const std::string signature
= GetHeaderValue(headers
, "X-Seed-Signature");
613 const std::string country_code
= GetHeaderValue(headers
, "X-Country");
614 const bool is_delta_compressed
= (GetHeaderValue(headers
, "IM") == "x-bm");
615 const bool store_success
= StoreSeed(seed_data
, signature
, country_code
,
616 response_date
, is_delta_compressed
);
617 if (!store_success
&& is_delta_compressed
) {
618 disable_deltas_for_next_request_
= true;
619 request_scheduler_
->ScheduleFetchShortly();
623 void VariationsService::OnResourceRequestsAllowed() {
624 DCHECK(thread_checker_
.CalledOnValidThread());
626 // Note that this only attempts to fetch the seed at most once per period
627 // (kSeedFetchPeriodHours). This works because
628 // |resource_request_allowed_notifier_| only calls this method if an
629 // attempt was made earlier that fails (which implies that the period had
630 // elapsed). After a successful attempt is made, the notifier will know not
631 // to call this method again until another failed attempt occurs.
632 RecordRequestsAllowedHistogram(RESOURCE_REQUESTS_ALLOWED_NOTIFIED
);
633 DVLOG(1) << "Retrying fetch.";
636 // This service must have created a scheduler in order for this to be called.
637 DCHECK(request_scheduler_
.get());
638 request_scheduler_
->Reset();
641 void VariationsService::PerformSimulationWithVersion(
642 scoped_ptr
<variations::VariationsSeed
> seed
,
643 const base::Version
& version
) {
644 DCHECK(thread_checker_
.CalledOnValidThread());
646 if (!version
.IsValid())
649 const base::ElapsedTimer timer
;
651 scoped_ptr
<const base::FieldTrial::EntropyProvider
> entropy_provider
=
652 state_manager_
->CreateEntropyProvider();
653 variations::VariationsSeedSimulator
seed_simulator(*entropy_provider
);
655 const std::string latest_country
=
656 local_state_
->GetString(prefs::kVariationsCountry
);
657 const variations::VariationsSeedSimulator::Result result
=
658 seed_simulator
.SimulateSeedStudies(
659 *seed
, client_
->GetApplicationLocale(),
660 GetReferenceDateForExpiryChecks(local_state_
), version
,
661 GetChannelForVariations(client_
->GetChannel()),
662 GetCurrentFormFactor(), GetHardwareClass(), latest_country
,
663 LoadPermanentConsistencyCountry(version
, latest_country
));
665 UMA_HISTOGRAM_COUNTS_100("Variations.SimulateSeed.NormalChanges",
666 result
.normal_group_change_count
);
667 UMA_HISTOGRAM_COUNTS_100("Variations.SimulateSeed.KillBestEffortChanges",
668 result
.kill_best_effort_group_change_count
);
669 UMA_HISTOGRAM_COUNTS_100("Variations.SimulateSeed.KillCriticalChanges",
670 result
.kill_critical_group_change_count
);
672 UMA_HISTOGRAM_TIMES("Variations.SimulateSeed.Duration", timer
.Elapsed());
674 NotifyObservers(result
);
677 void VariationsService::RecordLastFetchTime() {
678 DCHECK(thread_checker_
.CalledOnValidThread());
680 // local_state_ is NULL in tests, so check it first.
682 local_state_
->SetInt64(prefs::kVariationsLastFetchTime
,
683 base::Time::Now().ToInternalValue());
687 std::string
VariationsService::GetInvalidVariationsSeedSignature() const {
688 DCHECK(thread_checker_
.CalledOnValidThread());
689 return seed_store_
.GetInvalidSignature();
692 std::string
VariationsService::LoadPermanentConsistencyCountry(
693 const base::Version
& version
,
694 const std::string
& latest_country
) {
695 DCHECK(thread_checker_
.CalledOnValidThread());
696 DCHECK(version
.IsValid());
698 const base::ListValue
* list_value
=
699 local_state_
->GetList(prefs::kVariationsPermanentConsistencyCountry
);
700 std::string stored_version_string
;
701 std::string stored_country
;
703 // Determine if the saved pref value is present and valid.
704 const bool is_pref_present
= list_value
&& !list_value
->empty();
705 const bool is_pref_valid
= is_pref_present
&& list_value
->GetSize() == 2 &&
706 list_value
->GetString(0, &stored_version_string
) &&
707 list_value
->GetString(1, &stored_country
) &&
708 base::Version(stored_version_string
).IsValid();
710 // Determine if the version from the saved pref matches |version|.
711 const bool does_version_match
=
712 is_pref_valid
&& version
.Equals(base::Version(stored_version_string
));
714 // Determine if the country in the saved pref matches the country in |seed|.
715 const bool does_country_match
= is_pref_valid
&& !latest_country
.empty() &&
716 stored_country
== latest_country
;
718 // Record a histogram for how the saved pref value compares to the current
719 // version and the country code in the variations seed.
720 LoadPermanentConsistencyCountryResult result
;
721 if (!is_pref_present
) {
722 result
= !latest_country
.empty() ? LOAD_COUNTRY_NO_PREF_HAS_SEED
723 : LOAD_COUNTRY_NO_PREF_NO_SEED
;
724 } else if (!is_pref_valid
) {
725 result
= !latest_country
.empty() ? LOAD_COUNTRY_INVALID_PREF_HAS_SEED
726 : LOAD_COUNTRY_INVALID_PREF_NO_SEED
;
727 } else if (latest_country
.empty()) {
728 result
= does_version_match
? LOAD_COUNTRY_HAS_PREF_NO_SEED_VERSION_EQ
729 : LOAD_COUNTRY_HAS_PREF_NO_SEED_VERSION_NEQ
;
730 } else if (does_version_match
) {
731 result
= does_country_match
? LOAD_COUNTRY_HAS_BOTH_VERSION_EQ_COUNTRY_EQ
732 : LOAD_COUNTRY_HAS_BOTH_VERSION_EQ_COUNTRY_NEQ
;
734 result
= does_country_match
? LOAD_COUNTRY_HAS_BOTH_VERSION_NEQ_COUNTRY_EQ
735 : LOAD_COUNTRY_HAS_BOTH_VERSION_NEQ_COUNTRY_NEQ
;
737 UMA_HISTOGRAM_ENUMERATION("Variations.LoadPermanentConsistencyCountryResult",
738 result
, LOAD_COUNTRY_MAX
);
740 // Use the stored country if one is available and was fetched since the last
741 // time Chrome was updated.
742 if (is_pref_present
&& does_version_match
)
743 return stored_country
;
745 if (latest_country
.empty()) {
747 local_state_
->ClearPref(prefs::kVariationsPermanentConsistencyCountry
);
748 // If we've never received a country code from the server, use an empty
749 // country so that it won't pass any filters that specifically include
750 // countries, but so that it will pass any filters that specifically exclude
752 return std::string();
755 // Otherwise, update the pref with the current Chrome version and country.
756 base::ListValue new_list_value
;
757 new_list_value
.AppendString(version
.GetString());
758 new_list_value
.AppendString(latest_country
);
759 local_state_
->Set(prefs::kVariationsPermanentConsistencyCountry
,
761 return latest_country
;
764 } // namespace variations