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 "chrome/browser/metrics/variations/variations_service.h"
9 #include "base/build_time.h"
10 #include "base/command_line.h"
11 #include "base/metrics/histogram.h"
12 #include "base/metrics/sparse_histogram.h"
13 #include "base/prefs/pref_registry_simple.h"
14 #include "base/prefs/pref_service.h"
15 #include "base/sys_info.h"
16 #include "base/task_runner_util.h"
17 #include "base/timer/elapsed_timer.h"
18 #include "base/values.h"
19 #include "base/version.h"
20 #include "chrome/browser/browser_process.h"
21 #include "chrome/browser/metrics/variations/generated_resources_map.h"
22 #include "chrome/browser/metrics/variations/variations_url_constants.h"
23 #include "chrome/common/chrome_switches.h"
24 #include "chrome/common/pref_names.h"
25 #include "components/metrics/metrics_state_manager.h"
26 #include "components/network_time/network_time_tracker.h"
27 #include "components/pref_registry/pref_registry_syncable.h"
28 #include "components/variations/proto/variations_seed.pb.h"
29 #include "components/variations/variations_seed_processor.h"
30 #include "components/variations/variations_seed_simulator.h"
31 #include "content/public/browser/browser_thread.h"
32 #include "net/base/load_flags.h"
33 #include "net/base/net_errors.h"
34 #include "net/base/network_change_notifier.h"
35 #include "net/base/url_util.h"
36 #include "net/http/http_response_headers.h"
37 #include "net/http/http_status_code.h"
38 #include "net/http/http_util.h"
39 #include "net/url_request/url_fetcher.h"
40 #include "net/url_request/url_request_status.h"
41 #include "ui/base/device_form_factor.h"
42 #include "ui/base/resource/resource_bundle.h"
45 #if !defined(OS_ANDROID) && !defined(OS_IOS) && !defined(OS_CHROMEOS)
46 #include "chrome/browser/upgrade_detector_impl.h"
49 #if defined(OS_CHROMEOS)
50 #include "chrome/browser/chromeos/settings/cros_settings.h"
53 namespace chrome_variations
{
57 const int kMaxRetrySeedFetch
= 5;
59 // TODO(mad): To be removed when we stop updating the NetworkTimeTracker.
60 // For the HTTP date headers, the resolution of the server time is 1 second.
61 const int64 kServerTimeResolutionMs
= 1000;
63 // Wrapper around channel checking, used to enable channel mocking for
64 // testing. If the current browser channel is not UNKNOWN, this will return
65 // that channel value. Otherwise, if the fake channel flag is provided, this
66 // will return the fake channel. Failing that, this will return the UNKNOWN
68 variations::Study_Channel
GetChannelForVariations() {
69 switch (chrome::VersionInfo::GetChannel()) {
70 case chrome::VersionInfo::CHANNEL_CANARY
:
71 return variations::Study_Channel_CANARY
;
72 case chrome::VersionInfo::CHANNEL_DEV
:
73 return variations::Study_Channel_DEV
;
74 case chrome::VersionInfo::CHANNEL_BETA
:
75 return variations::Study_Channel_BETA
;
76 case chrome::VersionInfo::CHANNEL_STABLE
:
77 return variations::Study_Channel_STABLE
;
78 case chrome::VersionInfo::CHANNEL_UNKNOWN
:
81 const std::string forced_channel
=
82 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
83 switches::kFakeVariationsChannel
);
84 if (forced_channel
== "stable")
85 return variations::Study_Channel_STABLE
;
86 if (forced_channel
== "beta")
87 return variations::Study_Channel_BETA
;
88 if (forced_channel
== "dev")
89 return variations::Study_Channel_DEV
;
90 if (forced_channel
== "canary")
91 return variations::Study_Channel_CANARY
;
92 DVLOG(1) << "Invalid channel provided: " << forced_channel
;
93 return variations::Study_Channel_UNKNOWN
;
96 // Returns a string that will be used for the value of the 'osname' URL param
97 // to the variations server.
98 std::string
GetPlatformString() {
101 #elif defined(OS_IOS)
103 #elif defined(OS_MACOSX)
105 #elif defined(OS_CHROMEOS)
107 #elif defined(OS_ANDROID)
109 #elif defined(OS_LINUX) || defined(OS_BSD) || defined(OS_SOLARIS)
110 // Default BSD and SOLARIS to Linux to not break those builds, although these
111 // platforms are not officially supported by Chrome.
114 #error Unknown platform
118 // Gets the version number to use for variations seed simulation. Must be called
119 // on a thread where IO is allowed.
120 base::Version
GetVersionForSimulation() {
121 #if !defined(OS_ANDROID) && !defined(OS_IOS) && !defined(OS_CHROMEOS)
122 const base::Version installed_version
=
123 UpgradeDetectorImpl::GetCurrentlyInstalledVersion();
124 if (installed_version
.IsValid())
125 return installed_version
;
126 #endif // !defined(OS_ANDROID) && !defined(OS_IOS) && !defined(OS_CHROMEOS)
128 // TODO(asvitkine): Get the version that will be used on restart instead of
129 // the current version on Android, iOS and ChromeOS.
130 return base::Version(chrome::VersionInfo().Version());
133 // Gets the restrict parameter from |policy_pref_service| or from Chrome OS
134 // settings in the case of that platform.
135 std::string
GetRestrictParameterPref(PrefService
* policy_pref_service
) {
136 std::string parameter
;
137 #if defined(OS_CHROMEOS)
138 chromeos::CrosSettings::Get()->GetString(
139 chromeos::kVariationsRestrictParameter
, ¶meter
);
141 if (policy_pref_service
) {
143 policy_pref_service
->GetString(prefs::kVariationsRestrictParameter
);
149 enum ResourceRequestsAllowedState
{
150 RESOURCE_REQUESTS_ALLOWED
,
151 RESOURCE_REQUESTS_NOT_ALLOWED
,
152 RESOURCE_REQUESTS_ALLOWED_NOTIFIED
,
153 RESOURCE_REQUESTS_NOT_ALLOWED_EULA_NOT_ACCEPTED
,
154 RESOURCE_REQUESTS_NOT_ALLOWED_NETWORK_DOWN
,
155 RESOURCE_REQUESTS_NOT_ALLOWED_COMMAND_LINE_DISABLED
,
156 RESOURCE_REQUESTS_ALLOWED_ENUM_SIZE
,
159 // Records UMA histogram with the current resource requests allowed state.
160 void RecordRequestsAllowedHistogram(ResourceRequestsAllowedState state
) {
161 UMA_HISTOGRAM_ENUMERATION("Variations.ResourceRequestsAllowed", state
,
162 RESOURCE_REQUESTS_ALLOWED_ENUM_SIZE
);
165 // Converts ResourceRequestAllowedNotifier::State to the corresponding
166 // ResourceRequestsAllowedState value.
167 ResourceRequestsAllowedState
ResourceRequestStateToHistogramValue(
168 web_resource::ResourceRequestAllowedNotifier::State state
) {
169 using web_resource::ResourceRequestAllowedNotifier
;
171 case ResourceRequestAllowedNotifier::DISALLOWED_EULA_NOT_ACCEPTED
:
172 return RESOURCE_REQUESTS_NOT_ALLOWED_EULA_NOT_ACCEPTED
;
173 case ResourceRequestAllowedNotifier::DISALLOWED_NETWORK_DOWN
:
174 return RESOURCE_REQUESTS_NOT_ALLOWED_NETWORK_DOWN
;
175 case ResourceRequestAllowedNotifier::DISALLOWED_COMMAND_LINE_DISABLED
:
176 return RESOURCE_REQUESTS_NOT_ALLOWED_COMMAND_LINE_DISABLED
;
177 case ResourceRequestAllowedNotifier::ALLOWED
:
178 return RESOURCE_REQUESTS_ALLOWED
;
181 return RESOURCE_REQUESTS_NOT_ALLOWED
;
185 // Gets current form factor and converts it from enum DeviceFormFactor to enum
187 variations::Study_FormFactor
GetCurrentFormFactor() {
188 switch (ui::GetDeviceFormFactor()) {
189 case ui::DEVICE_FORM_FACTOR_PHONE
:
190 return variations::Study_FormFactor_PHONE
;
191 case ui::DEVICE_FORM_FACTOR_TABLET
:
192 return variations::Study_FormFactor_TABLET
;
193 case ui::DEVICE_FORM_FACTOR_DESKTOP
:
194 return variations::Study_FormFactor_DESKTOP
;
197 return variations::Study_FormFactor_DESKTOP
;
200 // Gets the hardware class and returns it as a string. This returns an empty
201 // string if the client is not ChromeOS.
202 std::string
GetHardwareClass() {
203 #if defined(OS_CHROMEOS)
204 return base::SysInfo::GetLsbReleaseBoard();
205 #endif // OS_CHROMEOS
206 return std::string();
209 // Returns the date that should be used by the VariationsSeedProcessor to do
210 // expiry and start date checks.
211 base::Time
GetReferenceDateForExpiryChecks(PrefService
* local_state
) {
212 const int64 date_value
= local_state
->GetInt64(prefs::kVariationsSeedDate
);
213 const base::Time seed_date
= base::Time::FromInternalValue(date_value
);
214 const base::Time build_time
= base::GetBuildTime();
215 // Use the build time for date checks if either the seed date is invalid or
216 // the build time is newer than the seed date.
217 base::Time reference_date
= seed_date
;
218 if (seed_date
.is_null() || seed_date
< build_time
)
219 reference_date
= build_time
;
220 return reference_date
;
223 // Overrides the string resource sepecified by |hash| with |string| in the
224 // resource bundle. Used as a callback passed to the variations seed processor.
225 void OverrideUIString(uint32_t hash
, const base::string16
& string
) {
226 int resource_id
= GetResourceIndex(hash
);
227 if (resource_id
== -1)
230 ui::ResourceBundle::GetSharedInstance().OverrideLocaleStringResource(
231 resource_id
, string
);
236 VariationsService::VariationsService(
237 web_resource::ResourceRequestAllowedNotifier
* notifier
,
238 PrefService
* local_state
,
239 metrics::MetricsStateManager
* state_manager
)
240 : local_state_(local_state
),
241 state_manager_(state_manager
),
242 policy_pref_service_(local_state
),
243 seed_store_(local_state
),
244 create_trials_from_seed_called_(false),
245 initial_request_completed_(false),
246 resource_request_allowed_notifier_(notifier
),
248 weak_ptr_factory_(this) {
249 resource_request_allowed_notifier_
->Init(this);
252 VariationsService::~VariationsService() {
255 bool VariationsService::CreateTrialsFromSeed() {
256 create_trials_from_seed_called_
= true;
258 variations::VariationsSeed seed
;
259 if (!seed_store_
.LoadSeed(&seed
))
262 const chrome::VersionInfo current_version_info
;
263 const base::Version
current_version(current_version_info
.Version());
264 if (!current_version
.IsValid())
267 variations::Study_Channel channel
= GetChannelForVariations();
268 UMA_HISTOGRAM_SPARSE_SLOWLY("Variations.UserChannel", channel
);
270 variations::VariationsSeedProcessor().CreateTrialsFromSeed(
272 g_browser_process
->GetApplicationLocale(),
273 GetReferenceDateForExpiryChecks(local_state_
),
276 GetCurrentFormFactor(),
278 LoadPermanentConsistencyCountry(current_version
, seed
),
279 base::Bind(&OverrideUIString
));
281 const base::Time now
= base::Time::Now();
283 // Log the "freshness" of the seed that was just used. The freshness is the
284 // time between the last successful seed download and now.
285 const int64 last_fetch_time_internal
=
286 local_state_
->GetInt64(prefs::kVariationsLastFetchTime
);
287 if (last_fetch_time_internal
) {
288 const base::TimeDelta delta
=
289 now
- base::Time::FromInternalValue(last_fetch_time_internal
);
290 // Log the value in number of minutes.
291 UMA_HISTOGRAM_CUSTOM_COUNTS("Variations.SeedFreshness", delta
.InMinutes(),
292 1, base::TimeDelta::FromDays(30).InMinutes(), 50);
295 // Log the skew between the seed date and the system clock/build time to
296 // analyze whether either could be used to make old variations seeds expire
298 const int64 seed_date_internal
=
299 local_state_
->GetInt64(prefs::kVariationsSeedDate
);
300 if (seed_date_internal
) {
301 const base::Time seed_date
=
302 base::Time::FromInternalValue(seed_date_internal
);
303 const int system_clock_delta_days
= (now
- seed_date
).InDays();
304 if (system_clock_delta_days
< 0) {
305 UMA_HISTOGRAM_COUNTS_100("Variations.SeedDateSkew.SystemClockBehindBy",
306 -system_clock_delta_days
);
308 UMA_HISTOGRAM_COUNTS_100("Variations.SeedDateSkew.SystemClockAheadBy",
309 system_clock_delta_days
);
312 const int build_time_delta_days
=
313 (base::GetBuildTime() - seed_date
).InDays();
314 if (build_time_delta_days
< 0) {
315 UMA_HISTOGRAM_COUNTS_100("Variations.SeedDateSkew.BuildTimeBehindBy",
316 -build_time_delta_days
);
318 UMA_HISTOGRAM_COUNTS_100("Variations.SeedDateSkew.BuildTimeAheadBy",
319 build_time_delta_days
);
326 void VariationsService::StartRepeatedVariationsSeedFetch() {
327 DCHECK_CURRENTLY_ON(content::BrowserThread::UI
);
329 // Initialize the Variations server URL.
330 variations_server_url_
=
331 GetVariationsServerURL(policy_pref_service_
, restrict_mode_
);
333 // Check that |CreateTrialsFromSeed| was called, which is necessary to
334 // retrieve the serial number that will be sent to the server.
335 DCHECK(create_trials_from_seed_called_
);
337 DCHECK(!request_scheduler_
.get());
338 // Note that the act of instantiating the scheduler will start the fetch, if
339 // the scheduler deems appropriate.
340 request_scheduler_
.reset(VariationsRequestScheduler::Create(
341 base::Bind(&VariationsService::FetchVariationsSeed
,
342 weak_ptr_factory_
.GetWeakPtr()),
344 request_scheduler_
->Start();
347 void VariationsService::AddObserver(Observer
* observer
) {
348 observer_list_
.AddObserver(observer
);
351 void VariationsService::RemoveObserver(Observer
* observer
) {
352 observer_list_
.RemoveObserver(observer
);
355 void VariationsService::OnAppEnterForeground() {
356 // On mobile platforms, initialize the fetch scheduler when we receive the
357 // first app foreground notification.
358 if (!request_scheduler_
)
359 StartRepeatedVariationsSeedFetch();
360 request_scheduler_
->OnAppEnterForeground();
364 void VariationsService::StartGoogleUpdateRegistrySync() {
365 registry_syncer_
.RequestRegistrySync();
369 void VariationsService::SetRestrictMode(const std::string
& restrict_mode
) {
370 // This should be called before the server URL has been computed.
371 DCHECK(variations_server_url_
.is_empty());
372 restrict_mode_
= restrict_mode
;
375 void VariationsService::SetCreateTrialsFromSeedCalledForTesting(bool called
) {
376 create_trials_from_seed_called_
= called
;
380 GURL
VariationsService::GetVariationsServerURL(
381 PrefService
* policy_pref_service
,
382 const std::string
& restrict_mode_override
) {
383 std::string
server_url_string(
384 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
385 switches::kVariationsServerURL
));
386 if (server_url_string
.empty())
387 server_url_string
= kDefaultServerUrl
;
388 GURL server_url
= GURL(server_url_string
);
390 const std::string restrict_param
= !restrict_mode_override
.empty() ?
391 restrict_mode_override
: GetRestrictParameterPref(policy_pref_service
);
392 if (!restrict_param
.empty()) {
393 server_url
= net::AppendOrReplaceQueryParameter(server_url
,
398 server_url
= net::AppendOrReplaceQueryParameter(server_url
, "osname",
399 GetPlatformString());
401 DCHECK(server_url
.is_valid());
406 std::string
VariationsService::GetDefaultVariationsServerURLForTesting() {
407 return kDefaultServerUrl
;
411 void VariationsService::RegisterPrefs(PrefRegistrySimple
* registry
) {
412 VariationsSeedStore::RegisterPrefs(registry
);
413 registry
->RegisterInt64Pref(prefs::kVariationsLastFetchTime
, 0);
414 // This preference will only be written by the policy service, which will fill
415 // it according to a value stored in the User Policy.
416 registry
->RegisterStringPref(prefs::kVariationsRestrictParameter
,
418 // This preference keeps track of the country code used to filter
419 // permanent-consistency studies.
420 registry
->RegisterListPref(prefs::kVariationsPermanentConsistencyCountry
);
424 void VariationsService::RegisterProfilePrefs(
425 user_prefs::PrefRegistrySyncable
* registry
) {
426 // This preference will only be written by the policy service, which will fill
427 // it according to a value stored in the User Policy.
428 registry
->RegisterStringPref(prefs::kVariationsRestrictParameter
,
433 scoped_ptr
<VariationsService
> VariationsService::Create(
434 PrefService
* local_state
,
435 metrics::MetricsStateManager
* state_manager
) {
436 scoped_ptr
<VariationsService
> result
;
437 #if !defined(GOOGLE_CHROME_BUILD)
438 // Unless the URL was provided, unsupported builds should return NULL to
439 // indicate that the service should not be used.
440 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
441 switches::kVariationsServerURL
)) {
442 DVLOG(1) << "Not creating VariationsService in unofficial build without --"
443 << switches::kVariationsServerURL
<< " specified.";
444 return result
.Pass();
447 result
.reset(new VariationsService(
448 new web_resource::ResourceRequestAllowedNotifier(
449 local_state
, switches::kDisableBackgroundNetworking
),
450 local_state
, state_manager
));
451 return result
.Pass();
454 void VariationsService::DoActualFetch() {
455 pending_seed_request_
= net::URLFetcher::Create(0, variations_server_url_
,
456 net::URLFetcher::GET
, this);
457 pending_seed_request_
->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES
|
458 net::LOAD_DO_NOT_SAVE_COOKIES
);
459 pending_seed_request_
->SetRequestContext(
460 g_browser_process
->system_request_context());
461 pending_seed_request_
->SetMaxRetriesOn5xx(kMaxRetrySeedFetch
);
462 if (!seed_store_
.variations_serial_number().empty()) {
463 pending_seed_request_
->AddExtraRequestHeader(
464 "If-Match:" + seed_store_
.variations_serial_number());
466 pending_seed_request_
->Start();
468 const base::TimeTicks now
= base::TimeTicks::Now();
469 base::TimeDelta time_since_last_fetch
;
470 // Record a time delta of 0 (default value) if there was no previous fetch.
471 if (!last_request_started_time_
.is_null())
472 time_since_last_fetch
= now
- last_request_started_time_
;
473 UMA_HISTOGRAM_CUSTOM_COUNTS("Variations.TimeSinceLastFetchAttempt",
474 time_since_last_fetch
.InMinutes(), 0,
475 base::TimeDelta::FromDays(7).InMinutes(), 50);
476 UMA_HISTOGRAM_COUNTS_100("Variations.RequestCount", request_count_
);
478 last_request_started_time_
= now
;
481 void VariationsService::StoreSeed(const std::string
& seed_data
,
482 const std::string
& seed_signature
,
483 const base::Time
& date_fetched
) {
484 scoped_ptr
<variations::VariationsSeed
> seed(new variations::VariationsSeed
);
485 if (!seed_store_
.StoreSeedData(seed_data
, seed_signature
, date_fetched
,
489 RecordLastFetchTime();
491 // Perform seed simulation only if |state_manager_| is not-NULL. The state
492 // manager may be NULL for some unit tests.
496 base::PostTaskAndReplyWithResult(
497 content::BrowserThread::GetBlockingPool(),
499 base::Bind(&GetVersionForSimulation
),
500 base::Bind(&VariationsService::PerformSimulationWithVersion
,
501 weak_ptr_factory_
.GetWeakPtr(), base::Passed(&seed
)));
504 void VariationsService::FetchVariationsSeed() {
505 DCHECK_CURRENTLY_ON(content::BrowserThread::UI
);
507 const web_resource::ResourceRequestAllowedNotifier::State state
=
508 resource_request_allowed_notifier_
->GetResourceRequestsAllowedState();
509 RecordRequestsAllowedHistogram(ResourceRequestStateToHistogramValue(state
));
510 if (state
!= web_resource::ResourceRequestAllowedNotifier::ALLOWED
) {
511 DVLOG(1) << "Resource requests were not allowed. Waiting for notification.";
518 void VariationsService::NotifyObservers(
519 const variations::VariationsSeedSimulator::Result
& result
) {
520 if (result
.kill_critical_group_change_count
> 0) {
521 FOR_EACH_OBSERVER(Observer
, observer_list_
,
522 OnExperimentChangesDetected(Observer::CRITICAL
));
523 } else if (result
.kill_best_effort_group_change_count
> 0) {
524 FOR_EACH_OBSERVER(Observer
, observer_list_
,
525 OnExperimentChangesDetected(Observer::BEST_EFFORT
));
529 void VariationsService::OnURLFetchComplete(const net::URLFetcher
* source
) {
530 DCHECK_EQ(pending_seed_request_
.get(), source
);
532 const bool is_first_request
= !initial_request_completed_
;
533 initial_request_completed_
= true;
535 // The fetcher will be deleted when the request is handled.
536 scoped_ptr
<const net::URLFetcher
> request(pending_seed_request_
.release());
537 const net::URLRequestStatus
& request_status
= request
->GetStatus();
538 if (request_status
.status() != net::URLRequestStatus::SUCCESS
) {
539 UMA_HISTOGRAM_SPARSE_SLOWLY("Variations.FailedRequestErrorCode",
540 -request_status
.error());
541 DVLOG(1) << "Variations server request failed with error: "
542 << request_status
.error() << ": "
543 << net::ErrorToString(request_status
.error());
544 // It's common for the very first fetch attempt to fail (e.g. the network
545 // may not yet be available). In such a case, try again soon, rather than
546 // waiting the full time interval.
547 if (is_first_request
)
548 request_scheduler_
->ScheduleFetchShortly();
552 // Log the response code.
553 const int response_code
= request
->GetResponseCode();
554 UMA_HISTOGRAM_SPARSE_SLOWLY("Variations.SeedFetchResponseCode",
557 const base::TimeDelta latency
=
558 base::TimeTicks::Now() - last_request_started_time_
;
560 base::Time response_date
;
561 if (response_code
== net::HTTP_OK
||
562 response_code
== net::HTTP_NOT_MODIFIED
) {
563 bool success
= request
->GetResponseHeaders()->GetDateValue(&response_date
);
564 DCHECK(success
|| response_date
.is_null());
566 if (!response_date
.is_null()) {
567 g_browser_process
->network_time_tracker()->UpdateNetworkTime(
569 base::TimeDelta::FromMilliseconds(kServerTimeResolutionMs
),
571 base::TimeTicks::Now());
575 if (response_code
!= net::HTTP_OK
) {
576 DVLOG(1) << "Variations server request returned non-HTTP_OK response code: "
578 if (response_code
== net::HTTP_NOT_MODIFIED
) {
579 RecordLastFetchTime();
580 // Update the seed date value in local state (used for expiry check on
581 // next start up), since 304 is a successful response.
582 seed_store_
.UpdateSeedDateAndLogDayChange(response_date
);
587 std::string seed_data
;
588 bool success
= request
->GetResponseAsString(&seed_data
);
591 std::string seed_signature
;
592 request
->GetResponseHeaders()->EnumerateHeader(NULL
,
595 StoreSeed(seed_data
, seed_signature
, response_date
);
598 void VariationsService::OnResourceRequestsAllowed() {
599 // Note that this only attempts to fetch the seed at most once per period
600 // (kSeedFetchPeriodHours). This works because
601 // |resource_request_allowed_notifier_| only calls this method if an
602 // attempt was made earlier that fails (which implies that the period had
603 // elapsed). After a successful attempt is made, the notifier will know not
604 // to call this method again until another failed attempt occurs.
605 RecordRequestsAllowedHistogram(RESOURCE_REQUESTS_ALLOWED_NOTIFIED
);
606 DVLOG(1) << "Retrying fetch.";
609 // This service must have created a scheduler in order for this to be called.
610 DCHECK(request_scheduler_
.get());
611 request_scheduler_
->Reset();
614 void VariationsService::PerformSimulationWithVersion(
615 scoped_ptr
<variations::VariationsSeed
> seed
,
616 const base::Version
& version
) {
617 if (!version
.IsValid())
620 const base::ElapsedTimer timer
;
622 scoped_ptr
<const base::FieldTrial::EntropyProvider
> entropy_provider
=
623 state_manager_
->CreateEntropyProvider();
624 variations::VariationsSeedSimulator
seed_simulator(*entropy_provider
);
626 const variations::VariationsSeedSimulator::Result result
=
627 seed_simulator
.SimulateSeedStudies(
628 *seed
, g_browser_process
->GetApplicationLocale(),
629 GetReferenceDateForExpiryChecks(local_state_
), version
,
630 GetChannelForVariations(), GetCurrentFormFactor(), GetHardwareClass(),
631 LoadPermanentConsistencyCountry(version
, *seed
));
633 UMA_HISTOGRAM_COUNTS_100("Variations.SimulateSeed.NormalChanges",
634 result
.normal_group_change_count
);
635 UMA_HISTOGRAM_COUNTS_100("Variations.SimulateSeed.KillBestEffortChanges",
636 result
.kill_best_effort_group_change_count
);
637 UMA_HISTOGRAM_COUNTS_100("Variations.SimulateSeed.KillCriticalChanges",
638 result
.kill_critical_group_change_count
);
640 UMA_HISTOGRAM_TIMES("Variations.SimulateSeed.Duration", timer
.Elapsed());
642 NotifyObservers(result
);
645 void VariationsService::RecordLastFetchTime() {
646 // local_state_ is NULL in tests, so check it first.
648 local_state_
->SetInt64(prefs::kVariationsLastFetchTime
,
649 base::Time::Now().ToInternalValue());
653 std::string
VariationsService::GetInvalidVariationsSeedSignature() const {
654 return seed_store_
.GetInvalidSignature();
657 std::string
VariationsService::LoadPermanentConsistencyCountry(
658 const base::Version
& version
,
659 const variations::VariationsSeed
& seed
) {
660 DCHECK(version
.IsValid());
662 const base::ListValue
* list_value
=
663 local_state_
->GetList(prefs::kVariationsPermanentConsistencyCountry
);
664 std::string stored_version_string
;
665 std::string stored_country
;
667 // Determine if the saved pref value is present and valid.
668 const bool is_pref_present
= list_value
&& !list_value
->empty();
669 const bool is_pref_valid
= is_pref_present
&& list_value
->GetSize() == 2 &&
670 list_value
->GetString(0, &stored_version_string
) &&
671 list_value
->GetString(1, &stored_country
) &&
672 base::Version(stored_version_string
).IsValid();
674 // Determine if the version from the saved pref matches |version|.
675 const bool does_version_match
=
676 is_pref_valid
&& version
.Equals(base::Version(stored_version_string
));
678 // Determine if the country in the saved pref matches the country in |seed|.
679 const bool does_country_match
= is_pref_valid
&& seed
.has_country_code() &&
680 stored_country
== seed
.country_code();
682 // Record a histogram for how the saved pref value compares to the current
683 // version and the country code in the variations seed.
684 LoadPermanentConsistencyCountryResult result
;
685 if (!is_pref_present
) {
686 result
= seed
.has_country_code() ? LOAD_COUNTRY_NO_PREF_HAS_SEED
687 : LOAD_COUNTRY_NO_PREF_NO_SEED
;
688 } else if (!is_pref_valid
) {
689 result
= seed
.has_country_code() ? LOAD_COUNTRY_INVALID_PREF_HAS_SEED
690 : LOAD_COUNTRY_INVALID_PREF_NO_SEED
;
691 } else if (!seed
.has_country_code()) {
692 result
= does_version_match
? LOAD_COUNTRY_HAS_PREF_NO_SEED_VERSION_EQ
693 : LOAD_COUNTRY_HAS_PREF_NO_SEED_VERSION_NEQ
;
694 } else if (does_version_match
) {
695 result
= does_country_match
? LOAD_COUNTRY_HAS_BOTH_VERSION_EQ_COUNTRY_EQ
696 : LOAD_COUNTRY_HAS_BOTH_VERSION_EQ_COUNTRY_NEQ
;
698 result
= does_country_match
? LOAD_COUNTRY_HAS_BOTH_VERSION_NEQ_COUNTRY_EQ
699 : LOAD_COUNTRY_HAS_BOTH_VERSION_NEQ_COUNTRY_NEQ
;
701 UMA_HISTOGRAM_ENUMERATION("Variations.LoadPermanentConsistencyCountryResult",
702 result
, LOAD_COUNTRY_MAX
);
704 // Use the stored country if one is available and was fetched since the last
705 // time Chrome was updated.
706 if (is_pref_present
&& does_version_match
)
707 return stored_country
;
709 if (!seed
.has_country_code()) {
710 // Clear the pref so that the next country code from the server will be used
711 // as the permanent consistency country code.
712 local_state_
->ClearPref(prefs::kVariationsPermanentConsistencyCountry
);
713 // Use an empty country so that it won't pass any filters that
714 // specifically include countries, but so that it will pass any filters that
715 // specifically exclude countries.
716 return std::string();
719 // Otherwise, update the pref with the current Chrome version and country.
720 base::ListValue new_list_value
;
721 new_list_value
.AppendString(version
.GetString());
722 new_list_value
.AppendString(seed
.country_code());
723 local_state_
->Set(prefs::kVariationsPermanentConsistencyCountry
,
725 return seed
.country_code();
728 } // namespace chrome_variations