• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include "chrome/browser/metrics/variations/variations_service.h"
6 
7 #include <set>
8 
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/version.h"
19 #include "chrome/browser/browser_process.h"
20 #include "chrome/common/chrome_switches.h"
21 #include "chrome/common/pref_names.h"
22 #include "components/metrics/metrics_state_manager.h"
23 #include "components/network_time/network_time_tracker.h"
24 #include "components/pref_registry/pref_registry_syncable.h"
25 #include "components/variations/proto/variations_seed.pb.h"
26 #include "components/variations/variations_seed_processor.h"
27 #include "components/variations/variations_seed_simulator.h"
28 #include "content/public/browser/browser_thread.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"
39 #include "url/gurl.h"
40 
41 #if !defined(OS_ANDROID) && !defined(OS_IOS) && !defined(OS_CHROMEOS)
42 #include "chrome/browser/upgrade_detector_impl.h"
43 #endif
44 
45 #if defined(OS_CHROMEOS)
46 #include "chrome/browser/chromeos/settings/cros_settings.h"
47 #endif
48 
49 namespace chrome_variations {
50 
51 namespace {
52 
53 // Default server of Variations seed info.
54 const char kDefaultVariationsServerURL[] =
55     "https://clients4.google.com/chrome-variations/seed";
56 const int kMaxRetrySeedFetch = 5;
57 
58 // TODO(mad): To be removed when we stop updating the NetworkTimeTracker.
59 // For the HTTP date headers, the resolution of the server time is 1 second.
60 const int64 kServerTimeResolutionMs = 1000;
61 
62 // Wrapper around channel checking, used to enable channel mocking for
63 // testing. If the current browser channel is not UNKNOWN, this will return
64 // that channel value. Otherwise, if the fake channel flag is provided, this
65 // will return the fake channel. Failing that, this will return the UNKNOWN
66 // channel.
GetChannelForVariations()67 Study_Channel GetChannelForVariations() {
68   switch (chrome::VersionInfo::GetChannel()) {
69     case chrome::VersionInfo::CHANNEL_CANARY:
70       return Study_Channel_CANARY;
71     case chrome::VersionInfo::CHANNEL_DEV:
72       return Study_Channel_DEV;
73     case chrome::VersionInfo::CHANNEL_BETA:
74       return Study_Channel_BETA;
75     case chrome::VersionInfo::CHANNEL_STABLE:
76       return Study_Channel_STABLE;
77     case chrome::VersionInfo::CHANNEL_UNKNOWN:
78       break;
79   }
80   const std::string forced_channel =
81       CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
82           switches::kFakeVariationsChannel);
83   if (forced_channel == "stable")
84     return Study_Channel_STABLE;
85   if (forced_channel == "beta")
86     return Study_Channel_BETA;
87   if (forced_channel == "dev")
88     return Study_Channel_DEV;
89   if (forced_channel == "canary")
90     return Study_Channel_CANARY;
91   DVLOG(1) << "Invalid channel provided: " << forced_channel;
92   return Study_Channel_UNKNOWN;
93 }
94 
95 // Returns a string that will be used for the value of the 'osname' URL param
96 // to the variations server.
GetPlatformString()97 std::string GetPlatformString() {
98 #if defined(OS_WIN)
99   return "win";
100 #elif defined(OS_IOS)
101   return "ios";
102 #elif defined(OS_MACOSX)
103   return "mac";
104 #elif defined(OS_CHROMEOS)
105   return "chromeos";
106 #elif defined(OS_ANDROID)
107   return "android";
108 #elif defined(OS_LINUX) || defined(OS_BSD) || defined(OS_SOLARIS)
109   // Default BSD and SOLARIS to Linux to not break those builds, although these
110   // platforms are not officially supported by Chrome.
111   return "linux";
112 #else
113 #error Unknown platform
114 #endif
115 }
116 
117 // Gets the version number to use for variations seed simulation. Must be called
118 // on a thread where IO is allowed.
GetVersionForSimulation()119 base::Version GetVersionForSimulation() {
120 #if !defined(OS_ANDROID) && !defined(OS_IOS) && !defined(OS_CHROMEOS)
121   const base::Version installed_version =
122       UpgradeDetectorImpl::GetCurrentlyInstalledVersion();
123   if (installed_version.IsValid())
124     return installed_version;
125 #endif  // !defined(OS_ANDROID) && !defined(OS_IOS) && !defined(OS_CHROMEOS)
126 
127   // TODO(asvitkine): Get the version that will be used on restart instead of
128   // the current version on Android, iOS and ChromeOS.
129   return base::Version(chrome::VersionInfo().Version());
130 }
131 
132 // Gets the restrict parameter from |policy_pref_service| or from Chrome OS
133 // settings in the case of that platform.
GetRestrictParameterPref(PrefService * policy_pref_service)134 std::string GetRestrictParameterPref(PrefService* policy_pref_service) {
135   std::string parameter;
136 #if defined(OS_CHROMEOS)
137   chromeos::CrosSettings::Get()->GetString(
138       chromeos::kVariationsRestrictParameter, &parameter);
139 #else
140   if (policy_pref_service) {
141     parameter =
142         policy_pref_service->GetString(prefs::kVariationsRestrictParameter);
143   }
144 #endif
145   return parameter;
146 }
147 
148 enum ResourceRequestsAllowedState {
149   RESOURCE_REQUESTS_ALLOWED,
150   RESOURCE_REQUESTS_NOT_ALLOWED,
151   RESOURCE_REQUESTS_ALLOWED_NOTIFIED,
152   RESOURCE_REQUESTS_NOT_ALLOWED_EULA_NOT_ACCEPTED,
153   RESOURCE_REQUESTS_NOT_ALLOWED_NETWORK_DOWN,
154   RESOURCE_REQUESTS_NOT_ALLOWED_COMMAND_LINE_DISABLED,
155   RESOURCE_REQUESTS_ALLOWED_ENUM_SIZE,
156 };
157 
158 // Records UMA histogram with the current resource requests allowed state.
RecordRequestsAllowedHistogram(ResourceRequestsAllowedState state)159 void RecordRequestsAllowedHistogram(ResourceRequestsAllowedState state) {
160   UMA_HISTOGRAM_ENUMERATION("Variations.ResourceRequestsAllowed", state,
161                             RESOURCE_REQUESTS_ALLOWED_ENUM_SIZE);
162 }
163 
164 // Converts ResourceRequestAllowedNotifier::State to the corresponding
165 // ResourceRequestsAllowedState value.
ResourceRequestStateToHistogramValue(ResourceRequestAllowedNotifier::State state)166 ResourceRequestsAllowedState ResourceRequestStateToHistogramValue(
167     ResourceRequestAllowedNotifier::State state) {
168   switch (state) {
169     case ResourceRequestAllowedNotifier::DISALLOWED_EULA_NOT_ACCEPTED:
170       return RESOURCE_REQUESTS_NOT_ALLOWED_EULA_NOT_ACCEPTED;
171     case ResourceRequestAllowedNotifier::DISALLOWED_NETWORK_DOWN:
172       return RESOURCE_REQUESTS_NOT_ALLOWED_NETWORK_DOWN;
173     case ResourceRequestAllowedNotifier::DISALLOWED_COMMAND_LINE_DISABLED:
174       return RESOURCE_REQUESTS_NOT_ALLOWED_COMMAND_LINE_DISABLED;
175     case ResourceRequestAllowedNotifier::ALLOWED:
176       return RESOURCE_REQUESTS_ALLOWED;
177   }
178   NOTREACHED();
179   return RESOURCE_REQUESTS_NOT_ALLOWED;
180 }
181 
182 
183 // Gets current form factor and converts it from enum DeviceFormFactor to enum
184 // Study_FormFactor.
GetCurrentFormFactor()185 Study_FormFactor GetCurrentFormFactor() {
186   switch (ui::GetDeviceFormFactor()) {
187     case ui::DEVICE_FORM_FACTOR_PHONE:
188       return Study_FormFactor_PHONE;
189     case ui::DEVICE_FORM_FACTOR_TABLET:
190       return Study_FormFactor_TABLET;
191     case ui::DEVICE_FORM_FACTOR_DESKTOP:
192       return Study_FormFactor_DESKTOP;
193   }
194   NOTREACHED();
195   return Study_FormFactor_DESKTOP;
196 }
197 
198 // Gets the hardware class and returns it as a string. This returns an empty
199 // string if the client is not ChromeOS.
GetHardwareClass()200 std::string GetHardwareClass() {
201 #if defined(OS_CHROMEOS)
202   return base::SysInfo::GetLsbReleaseBoard();
203 #endif  // OS_CHROMEOS
204   return std::string();
205 }
206 
207 // Returns the date that should be used by the VariationsSeedProcessor to do
208 // expiry and start date checks.
GetReferenceDateForExpiryChecks(PrefService * local_state)209 base::Time GetReferenceDateForExpiryChecks(PrefService* local_state) {
210   const int64 date_value = local_state->GetInt64(prefs::kVariationsSeedDate);
211   const base::Time seed_date = base::Time::FromInternalValue(date_value);
212   const base::Time build_time = base::GetBuildTime();
213   // Use the build time for date checks if either the seed date is invalid or
214   // the build time is newer than the seed date.
215   base::Time reference_date = seed_date;
216   if (seed_date.is_null() || seed_date < build_time)
217     reference_date = build_time;
218   return reference_date;
219 }
220 
221 }  // namespace
222 
VariationsService(PrefService * local_state,metrics::MetricsStateManager * state_manager)223 VariationsService::VariationsService(
224     PrefService* local_state,
225     metrics::MetricsStateManager* state_manager)
226     : local_state_(local_state),
227       state_manager_(state_manager),
228       policy_pref_service_(local_state),
229       seed_store_(local_state),
230       create_trials_from_seed_called_(false),
231       initial_request_completed_(false),
232       resource_request_allowed_notifier_(new ResourceRequestAllowedNotifier),
233       weak_ptr_factory_(this) {
234   resource_request_allowed_notifier_->Init(this);
235 }
236 
VariationsService(ResourceRequestAllowedNotifier * notifier,PrefService * local_state,metrics::MetricsStateManager * state_manager)237 VariationsService::VariationsService(
238     ResourceRequestAllowedNotifier* notifier,
239     PrefService* local_state,
240     metrics::MetricsStateManager* state_manager)
241     : local_state_(local_state),
242       state_manager_(state_manager),
243       policy_pref_service_(local_state),
244       seed_store_(local_state),
245       create_trials_from_seed_called_(false),
246       initial_request_completed_(false),
247       resource_request_allowed_notifier_(notifier),
248       weak_ptr_factory_(this) {
249   resource_request_allowed_notifier_->Init(this);
250 }
251 
~VariationsService()252 VariationsService::~VariationsService() {
253 }
254 
CreateTrialsFromSeed()255 bool VariationsService::CreateTrialsFromSeed() {
256   create_trials_from_seed_called_ = true;
257 
258   VariationsSeed seed;
259   if (!seed_store_.LoadSeed(&seed))
260     return false;
261 
262   const chrome::VersionInfo current_version_info;
263   if (!current_version_info.is_valid())
264     return false;
265 
266   const base::Version current_version(current_version_info.Version());
267   if (!current_version.IsValid())
268     return false;
269 
270   VariationsSeedProcessor().CreateTrialsFromSeed(
271       seed, g_browser_process->GetApplicationLocale(),
272       GetReferenceDateForExpiryChecks(local_state_), current_version,
273       GetChannelForVariations(), GetCurrentFormFactor(), GetHardwareClass());
274 
275   // Log the "freshness" of the seed that was just used. The freshness is the
276   // time between the last successful seed download and now.
277   const int64 last_fetch_time_internal =
278       local_state_->GetInt64(prefs::kVariationsLastFetchTime);
279   if (last_fetch_time_internal) {
280     const base::Time now = base::Time::Now();
281     const base::TimeDelta delta =
282         now - base::Time::FromInternalValue(last_fetch_time_internal);
283     // Log the value in number of minutes.
284     UMA_HISTOGRAM_CUSTOM_COUNTS("Variations.SeedFreshness", delta.InMinutes(),
285         1, base::TimeDelta::FromDays(30).InMinutes(), 50);
286   }
287 
288   return true;
289 }
290 
StartRepeatedVariationsSeedFetch()291 void VariationsService::StartRepeatedVariationsSeedFetch() {
292   DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
293 
294   // Initialize the Variations server URL.
295   variations_server_url_ = GetVariationsServerURL(policy_pref_service_);
296 
297   // Check that |CreateTrialsFromSeed| was called, which is necessary to
298   // retrieve the serial number that will be sent to the server.
299   DCHECK(create_trials_from_seed_called_);
300 
301   DCHECK(!request_scheduler_.get());
302   // Note that the act of instantiating the scheduler will start the fetch, if
303   // the scheduler deems appropriate. Using Unretained is fine here since the
304   // lifespan of request_scheduler_ is guaranteed to be shorter than that of
305   // this service.
306   request_scheduler_.reset(VariationsRequestScheduler::Create(
307       base::Bind(&VariationsService::FetchVariationsSeed,
308           base::Unretained(this)), local_state_));
309   request_scheduler_->Start();
310 }
311 
312 // TODO(rkaplow): Handle this and the similar event in metrics_service by
313 // observing an 'OnAppEnterForeground' event in RequestScheduler instead of
314 // requiring the frontend code to notify each service individually. Since the
315 // scheduler will handle it directly the VariationService shouldn't need to
316 // know details of this anymore.
OnAppEnterForeground()317 void VariationsService::OnAppEnterForeground() {
318   request_scheduler_->OnAppEnterForeground();
319 }
320 
321 // static
GetVariationsServerURL(PrefService * policy_pref_service)322 GURL VariationsService::GetVariationsServerURL(
323     PrefService* policy_pref_service) {
324   std::string server_url_string(CommandLine::ForCurrentProcess()->
325       GetSwitchValueASCII(switches::kVariationsServerURL));
326   if (server_url_string.empty())
327     server_url_string = kDefaultVariationsServerURL;
328   GURL server_url = GURL(server_url_string);
329 
330   const std::string restrict_param =
331       GetRestrictParameterPref(policy_pref_service);
332   if (!restrict_param.empty()) {
333     server_url = net::AppendOrReplaceQueryParameter(server_url,
334                                                     "restrict",
335                                                     restrict_param);
336   }
337 
338   server_url = net::AppendOrReplaceQueryParameter(server_url, "osname",
339                                                   GetPlatformString());
340 
341   DCHECK(server_url.is_valid());
342   return server_url;
343 }
344 
345 #if defined(OS_WIN)
StartGoogleUpdateRegistrySync()346 void VariationsService::StartGoogleUpdateRegistrySync() {
347   registry_syncer_.RequestRegistrySync();
348 }
349 #endif
350 
SetCreateTrialsFromSeedCalledForTesting(bool called)351 void VariationsService::SetCreateTrialsFromSeedCalledForTesting(bool called) {
352   create_trials_from_seed_called_ = called;
353 }
354 
355 // static
GetDefaultVariationsServerURLForTesting()356 std::string VariationsService::GetDefaultVariationsServerURLForTesting() {
357   return kDefaultVariationsServerURL;
358 }
359 
360 // static
RegisterPrefs(PrefRegistrySimple * registry)361 void VariationsService::RegisterPrefs(PrefRegistrySimple* registry) {
362   VariationsSeedStore::RegisterPrefs(registry);
363   registry->RegisterInt64Pref(prefs::kVariationsLastFetchTime, 0);
364   // This preference will only be written by the policy service, which will fill
365   // it according to a value stored in the User Policy.
366   registry->RegisterStringPref(prefs::kVariationsRestrictParameter,
367                                std::string());
368 }
369 
370 // static
RegisterProfilePrefs(user_prefs::PrefRegistrySyncable * registry)371 void VariationsService::RegisterProfilePrefs(
372     user_prefs::PrefRegistrySyncable* registry) {
373   // This preference will only be written by the policy service, which will fill
374   // it according to a value stored in the User Policy.
375   registry->RegisterStringPref(
376       prefs::kVariationsRestrictParameter,
377       std::string(),
378       user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
379 }
380 
381 // static
Create(PrefService * local_state,metrics::MetricsStateManager * state_manager)382 scoped_ptr<VariationsService> VariationsService::Create(
383     PrefService* local_state,
384     metrics::MetricsStateManager* state_manager) {
385   scoped_ptr<VariationsService> result;
386 #if !defined(GOOGLE_CHROME_BUILD)
387   // Unless the URL was provided, unsupported builds should return NULL to
388   // indicate that the service should not be used.
389   if (!CommandLine::ForCurrentProcess()->HasSwitch(
390           switches::kVariationsServerURL)) {
391     DVLOG(1) << "Not creating VariationsService in unofficial build without --"
392              << switches::kVariationsServerURL << " specified.";
393     return result.Pass();
394   }
395 #endif
396   result.reset(new VariationsService(local_state, state_manager));
397   return result.Pass();
398 }
399 
DoActualFetch()400 void VariationsService::DoActualFetch() {
401   pending_seed_request_.reset(net::URLFetcher::Create(
402       0, variations_server_url_, net::URLFetcher::GET, this));
403   pending_seed_request_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES |
404                                       net::LOAD_DO_NOT_SAVE_COOKIES);
405   pending_seed_request_->SetRequestContext(
406       g_browser_process->system_request_context());
407   pending_seed_request_->SetMaxRetriesOn5xx(kMaxRetrySeedFetch);
408   if (!seed_store_.variations_serial_number().empty()) {
409     pending_seed_request_->AddExtraRequestHeader(
410         "If-Match:" + seed_store_.variations_serial_number());
411   }
412   pending_seed_request_->Start();
413 
414   const base::TimeTicks now = base::TimeTicks::Now();
415   base::TimeDelta time_since_last_fetch;
416   // Record a time delta of 0 (default value) if there was no previous fetch.
417   if (!last_request_started_time_.is_null())
418     time_since_last_fetch = now - last_request_started_time_;
419   UMA_HISTOGRAM_CUSTOM_COUNTS("Variations.TimeSinceLastFetchAttempt",
420                               time_since_last_fetch.InMinutes(), 0,
421                               base::TimeDelta::FromDays(7).InMinutes(), 50);
422   last_request_started_time_ = now;
423 }
424 
StoreSeed(const std::string & seed_data,const std::string & seed_signature,const base::Time & date_fetched)425 void VariationsService::StoreSeed(const std::string& seed_data,
426                                   const std::string& seed_signature,
427                                   const base::Time& date_fetched) {
428   scoped_ptr<VariationsSeed> seed(new VariationsSeed);
429   if (!seed_store_.StoreSeedData(seed_data, seed_signature, date_fetched,
430                                  seed.get())) {
431     return;
432   }
433   RecordLastFetchTime();
434 
435   // Perform seed simulation only if |state_manager_| is not-NULL. The state
436   // manager may be NULL for some unit tests.
437   if (!state_manager_)
438     return;
439 
440   base::PostTaskAndReplyWithResult(
441       content::BrowserThread::GetBlockingPool(),
442       FROM_HERE,
443       base::Bind(&GetVersionForSimulation),
444       base::Bind(&VariationsService::PerformSimulationWithVersion,
445                  weak_ptr_factory_.GetWeakPtr(), base::Passed(&seed)));
446 }
447 
FetchVariationsSeed()448 void VariationsService::FetchVariationsSeed() {
449   DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
450 
451   const ResourceRequestAllowedNotifier::State state =
452       resource_request_allowed_notifier_->GetResourceRequestsAllowedState();
453   RecordRequestsAllowedHistogram(ResourceRequestStateToHistogramValue(state));
454   if (state != ResourceRequestAllowedNotifier::ALLOWED) {
455     DVLOG(1) << "Resource requests were not allowed. Waiting for notification.";
456     return;
457   }
458 
459   DoActualFetch();
460 }
461 
OnURLFetchComplete(const net::URLFetcher * source)462 void VariationsService::OnURLFetchComplete(const net::URLFetcher* source) {
463   DCHECK_EQ(pending_seed_request_.get(), source);
464 
465   const bool is_first_request = !initial_request_completed_;
466   initial_request_completed_ = true;
467 
468   // The fetcher will be deleted when the request is handled.
469   scoped_ptr<const net::URLFetcher> request(pending_seed_request_.release());
470   const net::URLRequestStatus& request_status = request->GetStatus();
471   if (request_status.status() != net::URLRequestStatus::SUCCESS) {
472     UMA_HISTOGRAM_SPARSE_SLOWLY("Variations.FailedRequestErrorCode",
473                                 -request_status.error());
474     DVLOG(1) << "Variations server request failed with error: "
475              << request_status.error() << ": "
476              << net::ErrorToString(request_status.error());
477     // It's common for the very first fetch attempt to fail (e.g. the network
478     // may not yet be available). In such a case, try again soon, rather than
479     // waiting the full time interval.
480     if (is_first_request)
481       request_scheduler_->ScheduleFetchShortly();
482     return;
483   }
484 
485   // Log the response code.
486   const int response_code = request->GetResponseCode();
487   UMA_HISTOGRAM_SPARSE_SLOWLY("Variations.SeedFetchResponseCode",
488                               response_code);
489 
490   const base::TimeDelta latency =
491       base::TimeTicks::Now() - last_request_started_time_;
492 
493   base::Time response_date;
494   if (response_code == net::HTTP_OK ||
495       response_code == net::HTTP_NOT_MODIFIED) {
496     bool success = request->GetResponseHeaders()->GetDateValue(&response_date);
497     DCHECK(success || response_date.is_null());
498 
499     if (!response_date.is_null()) {
500       g_browser_process->network_time_tracker()->UpdateNetworkTime(
501           response_date,
502           base::TimeDelta::FromMilliseconds(kServerTimeResolutionMs),
503           latency,
504           base::TimeTicks::Now());
505     }
506   }
507 
508   if (response_code != net::HTTP_OK) {
509     DVLOG(1) << "Variations server request returned non-HTTP_OK response code: "
510              << response_code;
511     if (response_code == net::HTTP_NOT_MODIFIED) {
512       RecordLastFetchTime();
513       // Update the seed date value in local state (used for expiry check on
514       // next start up), since 304 is a successful response.
515       seed_store_.UpdateSeedDateAndLogDayChange(response_date);
516     }
517     return;
518   }
519 
520   std::string seed_data;
521   bool success = request->GetResponseAsString(&seed_data);
522   DCHECK(success);
523 
524   std::string seed_signature;
525   request->GetResponseHeaders()->EnumerateHeader(NULL,
526                                                  "X-Seed-Signature",
527                                                  &seed_signature);
528   StoreSeed(seed_data, seed_signature, response_date);
529 }
530 
OnResourceRequestsAllowed()531 void VariationsService::OnResourceRequestsAllowed() {
532   // Note that this only attempts to fetch the seed at most once per period
533   // (kSeedFetchPeriodHours). This works because
534   // |resource_request_allowed_notifier_| only calls this method if an
535   // attempt was made earlier that fails (which implies that the period had
536   // elapsed). After a successful attempt is made, the notifier will know not
537   // to call this method again until another failed attempt occurs.
538   RecordRequestsAllowedHistogram(RESOURCE_REQUESTS_ALLOWED_NOTIFIED);
539   DVLOG(1) << "Retrying fetch.";
540   DoActualFetch();
541 
542   // This service must have created a scheduler in order for this to be called.
543   DCHECK(request_scheduler_.get());
544   request_scheduler_->Reset();
545 }
546 
PerformSimulationWithVersion(scoped_ptr<VariationsSeed> seed,const base::Version & version)547 void VariationsService::PerformSimulationWithVersion(
548     scoped_ptr<VariationsSeed> seed,
549     const base::Version& version) {
550   if (version.IsValid())
551     return;
552 
553   const base::ElapsedTimer timer;
554 
555   scoped_ptr<const base::FieldTrial::EntropyProvider> entropy_provider =
556       state_manager_->CreateEntropyProvider();
557   VariationsSeedSimulator seed_simulator(*entropy_provider);
558 
559   VariationsSeedSimulator::Result result = seed_simulator.SimulateSeedStudies(
560       *seed, g_browser_process->GetApplicationLocale(),
561       GetReferenceDateForExpiryChecks(local_state_), version,
562       GetChannelForVariations(), GetCurrentFormFactor(), GetHardwareClass());
563 
564   UMA_HISTOGRAM_COUNTS_100("Variations.SimulateSeed.NormalChanges",
565                            result.normal_group_change_count);
566   UMA_HISTOGRAM_COUNTS_100("Variations.SimulateSeed.KillBestEffortChanges",
567                            result.kill_best_effort_group_change_count);
568   UMA_HISTOGRAM_COUNTS_100("Variations.SimulateSeed.KillCriticalChanges",
569                            result.kill_critical_group_change_count);
570 
571   UMA_HISTOGRAM_TIMES("Variations.SimulateSeed.Duration", timer.Elapsed());
572 }
573 
RecordLastFetchTime()574 void VariationsService::RecordLastFetchTime() {
575   // local_state_ is NULL in tests, so check it first.
576   if (local_state_) {
577     local_state_->SetInt64(prefs::kVariationsLastFetchTime,
578                            base::Time::Now().ToInternalValue());
579   }
580 }
581 
582 }  // namespace chrome_variations
583