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/base64.h"
10 #include "base/build_time.h"
11 #include "base/command_line.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/metrics/histogram.h"
14 #include "base/metrics/sparse_histogram.h"
15 #include "base/prefs/pref_registry_simple.h"
16 #include "base/prefs/pref_service.h"
17 #include "base/sha1.h"
18 #include "base/strings/string_number_conversions.h"
19 #include "base/version.h"
20 #include "chrome/browser/browser_process.h"
21 #include "chrome/browser/network_time/network_time_tracker.h"
22 #include "chrome/common/chrome_switches.h"
23 #include "chrome/common/metrics/variations/variations_util.h"
24 #include "chrome/common/pref_names.h"
25 #include "components/variations/proto/variations_seed.pb.h"
26 #include "components/variations/variations_seed_processor.h"
27 #include "content/public/browser/browser_thread.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"
40 #if defined(OS_CHROMEOS)
41 #include "chrome/browser/chromeos/settings/cros_settings.h"
44 namespace chrome_variations
{
48 // Default server of Variations seed info.
49 const char kDefaultVariationsServerURL
[] =
50 "https://clients4.google.com/chrome-variations/seed";
51 const int kMaxRetrySeedFetch
= 5;
53 // TODO(mad): To be removed when we stop updating the NetworkTimeTracker.
54 // For the HTTP date headers, the resolution of the server time is 1 second.
55 const int64 kServerTimeResolutionMs
= 1000;
57 // Wrapper around channel checking, used to enable channel mocking for
58 // testing. If the current browser channel is not UNKNOWN, this will return
59 // that channel value. Otherwise, if the fake channel flag is provided, this
60 // will return the fake channel. Failing that, this will return the UNKNOWN
62 Study_Channel
GetChannelForVariations() {
63 switch (chrome::VersionInfo::GetChannel()) {
64 case chrome::VersionInfo::CHANNEL_CANARY
:
65 return Study_Channel_CANARY
;
66 case chrome::VersionInfo::CHANNEL_DEV
:
67 return Study_Channel_DEV
;
68 case chrome::VersionInfo::CHANNEL_BETA
:
69 return Study_Channel_BETA
;
70 case chrome::VersionInfo::CHANNEL_STABLE
:
71 return Study_Channel_STABLE
;
72 case chrome::VersionInfo::CHANNEL_UNKNOWN
:
75 const std::string forced_channel
=
76 CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
77 switches::kFakeVariationsChannel
);
78 if (forced_channel
== "stable")
79 return Study_Channel_STABLE
;
80 if (forced_channel
== "beta")
81 return Study_Channel_BETA
;
82 if (forced_channel
== "dev")
83 return Study_Channel_DEV
;
84 if (forced_channel
== "canary")
85 return Study_Channel_CANARY
;
86 DVLOG(1) << "Invalid channel provided: " << forced_channel
;
87 return Study_Channel_UNKNOWN
;
90 // Returns a string that will be used for the value of the 'osname' URL param
91 // to the variations server.
92 std::string
GetPlatformString() {
97 #elif defined(OS_MACOSX)
99 #elif defined(OS_CHROMEOS)
101 #elif defined(OS_ANDROID)
103 #elif defined(OS_LINUX) || defined(OS_BSD) || defined(OS_SOLARIS)
104 // Default BSD and SOLARIS to Linux to not break those builds, although these
105 // platforms are not officially supported by Chrome.
108 #error Unknown platform
112 // Gets the restrict parameter from |local_state| or from Chrome OS settings in
113 // the case of that platform.
114 std::string
GetRestrictParameterPref(PrefService
* local_state
) {
115 std::string parameter
;
116 #if defined(OS_CHROMEOS)
117 chromeos::CrosSettings::Get()->GetString(
118 chromeos::kVariationsRestrictParameter
, ¶meter
);
121 parameter
= local_state
->GetString(prefs::kVariationsRestrictParameter
);
126 // Computes a hash of the serialized variations seed data.
127 std::string
HashSeed(const std::string
& seed_data
) {
128 const std::string sha1
= base::SHA1HashString(seed_data
);
129 return base::HexEncode(sha1
.data(), sha1
.size());
132 enum ResourceRequestsAllowedState
{
133 RESOURCE_REQUESTS_ALLOWED
,
134 RESOURCE_REQUESTS_NOT_ALLOWED
,
135 RESOURCE_REQUESTS_ALLOWED_NOTIFIED
,
136 RESOURCE_REQUESTS_NOT_ALLOWED_EULA_NOT_ACCEPTED
,
137 RESOURCE_REQUESTS_NOT_ALLOWED_NETWORK_DOWN
,
138 RESOURCE_REQUESTS_NOT_ALLOWED_COMMAND_LINE_DISABLED
,
139 RESOURCE_REQUESTS_ALLOWED_ENUM_SIZE
,
142 // Records UMA histogram with the current resource requests allowed state.
143 void RecordRequestsAllowedHistogram(ResourceRequestsAllowedState state
) {
144 UMA_HISTOGRAM_ENUMERATION("Variations.ResourceRequestsAllowed", state
,
145 RESOURCE_REQUESTS_ALLOWED_ENUM_SIZE
);
148 // Converts ResourceRequestAllowedNotifier::State to the corresponding
149 // ResourceRequestsAllowedState value.
150 ResourceRequestsAllowedState
ResourceRequestStateToHistogramValue(
151 ResourceRequestAllowedNotifier::State state
) {
153 case ResourceRequestAllowedNotifier::DISALLOWED_EULA_NOT_ACCEPTED
:
154 return RESOURCE_REQUESTS_NOT_ALLOWED_EULA_NOT_ACCEPTED
;
155 case ResourceRequestAllowedNotifier::DISALLOWED_NETWORK_DOWN
:
156 return RESOURCE_REQUESTS_NOT_ALLOWED_NETWORK_DOWN
;
157 case ResourceRequestAllowedNotifier::DISALLOWED_COMMAND_LINE_DISABLED
:
158 return RESOURCE_REQUESTS_NOT_ALLOWED_COMMAND_LINE_DISABLED
;
159 case ResourceRequestAllowedNotifier::ALLOWED
:
160 return RESOURCE_REQUESTS_ALLOWED
;
163 return RESOURCE_REQUESTS_NOT_ALLOWED
;
166 enum VariationSeedEmptyState
{
167 VARIATIONS_SEED_NOT_EMPTY
,
168 VARIATIONS_SEED_EMPTY
,
169 VARIATIONS_SEED_CORRUPT
,
170 VARIATIONS_SEED_EMPTY_ENUM_SIZE
,
173 void RecordVariationSeedEmptyHistogram(VariationSeedEmptyState state
) {
174 UMA_HISTOGRAM_ENUMERATION("Variations.SeedEmpty", state
,
175 VARIATIONS_SEED_EMPTY_ENUM_SIZE
);
178 // Get current form factor and convert it from enum DeviceFormFactor to enum
180 Study_FormFactor
GetCurrentFormFactor() {
181 switch (ui::GetDeviceFormFactor()) {
182 case ui::DEVICE_FORM_FACTOR_PHONE
:
183 return Study_FormFactor_PHONE
;
184 case ui::DEVICE_FORM_FACTOR_TABLET
:
185 return Study_FormFactor_TABLET
;
186 case ui::DEVICE_FORM_FACTOR_DESKTOP
:
187 return Study_FormFactor_DESKTOP
;
190 return Study_FormFactor_DESKTOP
;
195 VariationsService::VariationsService(PrefService
* local_state
)
196 : local_state_(local_state
),
197 variations_server_url_(GetVariationsServerURL(local_state
)),
198 create_trials_from_seed_called_(false),
199 initial_request_completed_(false),
200 resource_request_allowed_notifier_(
201 new ResourceRequestAllowedNotifier
) {
202 resource_request_allowed_notifier_
->Init(this);
205 VariationsService::VariationsService(ResourceRequestAllowedNotifier
* notifier
,
206 PrefService
* local_state
)
207 : local_state_(local_state
),
208 variations_server_url_(GetVariationsServerURL(NULL
)),
209 create_trials_from_seed_called_(false),
210 initial_request_completed_(false),
211 resource_request_allowed_notifier_(notifier
) {
212 resource_request_allowed_notifier_
->Init(this);
215 VariationsService::~VariationsService() {
218 bool VariationsService::CreateTrialsFromSeed() {
219 create_trials_from_seed_called_
= true;
222 if (!LoadVariationsSeedFromPref(&seed
))
225 const int64 date_value
= local_state_
->GetInt64(prefs::kVariationsSeedDate
);
226 const base::Time seed_date
= base::Time::FromInternalValue(date_value
);
227 const base::Time build_time
= base::GetBuildTime();
228 // Use the build time for date checks if either the seed date is invalid or
229 // the build time is newer than the seed date.
230 base::Time reference_date
= seed_date
;
231 if (seed_date
.is_null() || seed_date
< build_time
)
232 reference_date
= build_time
;
234 const chrome::VersionInfo current_version_info
;
235 if (!current_version_info
.is_valid())
238 const base::Version
current_version(current_version_info
.Version());
239 if (!current_version
.IsValid())
242 VariationsSeedProcessor().CreateTrialsFromSeed(
243 seed
, g_browser_process
->GetApplicationLocale(), reference_date
,
244 current_version
, GetChannelForVariations(), GetCurrentFormFactor());
246 // Log the "freshness" of the seed that was just used. The freshness is the
247 // time between the last successful seed download and now.
248 const int64 last_fetch_time_internal
=
249 local_state_
->GetInt64(prefs::kVariationsLastFetchTime
);
250 if (last_fetch_time_internal
) {
251 const base::Time now
= base::Time::Now();
252 const base::TimeDelta delta
=
253 now
- base::Time::FromInternalValue(last_fetch_time_internal
);
254 // Log the value in number of minutes.
255 UMA_HISTOGRAM_CUSTOM_COUNTS("Variations.SeedFreshness", delta
.InMinutes(),
256 1, base::TimeDelta::FromDays(30).InMinutes(), 50);
262 void VariationsService::StartRepeatedVariationsSeedFetch() {
263 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI
));
265 // Check that |CreateTrialsFromSeed| was called, which is necessary to
266 // retrieve the serial number that will be sent to the server.
267 DCHECK(create_trials_from_seed_called_
);
269 DCHECK(!request_scheduler_
.get());
270 // Note that the act of instantiating the scheduler will start the fetch, if
271 // the scheduler deems appropriate. Using Unretained is fine here since the
272 // lifespan of request_scheduler_ is guaranteed to be shorter than that of
274 request_scheduler_
.reset(VariationsRequestScheduler::Create(
275 base::Bind(&VariationsService::FetchVariationsSeed
,
276 base::Unretained(this)), local_state_
));
277 request_scheduler_
->Start();
281 GURL
VariationsService::GetVariationsServerURL(PrefService
* local_state
) {
282 std::string
server_url_string(CommandLine::ForCurrentProcess()->
283 GetSwitchValueASCII(switches::kVariationsServerURL
));
284 if (server_url_string
.empty())
285 server_url_string
= kDefaultVariationsServerURL
;
286 GURL server_url
= GURL(server_url_string
);
288 const std::string restrict_param
= GetRestrictParameterPref(local_state
);
289 if (!restrict_param
.empty()) {
290 server_url
= net::AppendOrReplaceQueryParameter(server_url
,
295 server_url
= net::AppendOrReplaceQueryParameter(server_url
, "osname",
296 GetPlatformString());
298 DCHECK(server_url
.is_valid());
303 void VariationsService::StartGoogleUpdateRegistrySync() {
304 registry_syncer_
.RequestRegistrySync();
308 void VariationsService::SetCreateTrialsFromSeedCalledForTesting(bool called
) {
309 create_trials_from_seed_called_
= called
;
313 std::string
VariationsService::GetDefaultVariationsServerURLForTesting() {
314 return kDefaultVariationsServerURL
;
318 void VariationsService::RegisterPrefs(PrefRegistrySimple
* registry
) {
319 registry
->RegisterStringPref(prefs::kVariationsSeed
, std::string());
320 registry
->RegisterStringPref(prefs::kVariationsSeedHash
, std::string());
321 registry
->RegisterInt64Pref(prefs::kVariationsSeedDate
,
322 base::Time().ToInternalValue());
323 registry
->RegisterInt64Pref(prefs::kVariationsLastFetchTime
, 0);
324 registry
->RegisterStringPref(prefs::kVariationsRestrictParameter
,
329 VariationsService
* VariationsService::Create(PrefService
* local_state
) {
330 #if !defined(GOOGLE_CHROME_BUILD)
331 // Unless the URL was provided, unsupported builds should return NULL to
332 // indicate that the service should not be used.
333 if (!CommandLine::ForCurrentProcess()->HasSwitch(
334 switches::kVariationsServerURL
)) {
335 DVLOG(1) << "Not creating VariationsService in unofficial build without --"
336 << switches::kVariationsServerURL
<< " specified.";
340 return new VariationsService(local_state
);
343 void VariationsService::DoActualFetch() {
344 pending_seed_request_
.reset(net::URLFetcher::Create(
345 0, variations_server_url_
, net::URLFetcher::GET
, this));
346 pending_seed_request_
->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES
|
347 net::LOAD_DO_NOT_SAVE_COOKIES
);
348 pending_seed_request_
->SetRequestContext(
349 g_browser_process
->system_request_context());
350 pending_seed_request_
->SetMaxRetriesOn5xx(kMaxRetrySeedFetch
);
351 if (!variations_serial_number_
.empty()) {
352 pending_seed_request_
->AddExtraRequestHeader("If-Match:" +
353 variations_serial_number_
);
355 pending_seed_request_
->Start();
357 const base::TimeTicks now
= base::TimeTicks::Now();
358 base::TimeDelta time_since_last_fetch
;
359 // Record a time delta of 0 (default value) if there was no previous fetch.
360 if (!last_request_started_time_
.is_null())
361 time_since_last_fetch
= now
- last_request_started_time_
;
362 UMA_HISTOGRAM_CUSTOM_COUNTS("Variations.TimeSinceLastFetchAttempt",
363 time_since_last_fetch
.InMinutes(), 0,
364 base::TimeDelta::FromDays(7).InMinutes(), 50);
365 last_request_started_time_
= now
;
368 void VariationsService::FetchVariationsSeed() {
369 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI
));
371 const ResourceRequestAllowedNotifier::State state
=
372 resource_request_allowed_notifier_
->GetResourceRequestsAllowedState();
373 RecordRequestsAllowedHistogram(ResourceRequestStateToHistogramValue(state
));
374 if (state
!= ResourceRequestAllowedNotifier::ALLOWED
) {
375 DVLOG(1) << "Resource requests were not allowed. Waiting for notification.";
382 void VariationsService::OnURLFetchComplete(const net::URLFetcher
* source
) {
383 DCHECK_EQ(pending_seed_request_
.get(), source
);
385 const bool is_first_request
= !initial_request_completed_
;
386 initial_request_completed_
= true;
388 // The fetcher will be deleted when the request is handled.
389 scoped_ptr
<const net::URLFetcher
> request(pending_seed_request_
.release());
390 const net::URLRequestStatus
& request_status
= request
->GetStatus();
391 if (request_status
.status() != net::URLRequestStatus::SUCCESS
) {
392 UMA_HISTOGRAM_SPARSE_SLOWLY("Variations.FailedRequestErrorCode",
393 -request_status
.error());
394 DVLOG(1) << "Variations server request failed with error: "
395 << request_status
.error() << ": "
396 << net::ErrorToString(request_status
.error());
397 // It's common for the very first fetch attempt to fail (e.g. the network
398 // may not yet be available). In such a case, try again soon, rather than
399 // waiting the full time interval.
400 if (is_first_request
)
401 request_scheduler_
->ScheduleFetchShortly();
405 // Log the response code.
406 const int response_code
= request
->GetResponseCode();
407 UMA_HISTOGRAM_SPARSE_SLOWLY("Variations.SeedFetchResponseCode",
410 const base::TimeDelta latency
=
411 base::TimeTicks::Now() - last_request_started_time_
;
413 base::Time response_date
;
414 if (response_code
== net::HTTP_OK
||
415 response_code
== net::HTTP_NOT_MODIFIED
) {
416 bool success
= request
->GetResponseHeaders()->GetDateValue(&response_date
);
417 DCHECK(success
|| response_date
.is_null());
419 if (!response_date
.is_null()) {
420 NetworkTimeTracker::BuildNotifierUpdateCallback().Run(
422 base::TimeDelta::FromMilliseconds(kServerTimeResolutionMs
),
427 if (response_code
!= net::HTTP_OK
) {
428 DVLOG(1) << "Variations server request returned non-HTTP_OK response code: "
430 if (response_code
== net::HTTP_NOT_MODIFIED
) {
431 UMA_HISTOGRAM_MEDIUM_TIMES("Variations.FetchNotModifiedLatency", latency
);
432 RecordLastFetchTime();
433 // Update the seed date value in local state (used for expiry check on
434 // next start up), since 304 is a successful response.
435 local_state_
->SetInt64(prefs::kVariationsSeedDate
,
436 response_date
.ToInternalValue());
438 UMA_HISTOGRAM_MEDIUM_TIMES("Variations.FetchOtherLatency", latency
);
442 UMA_HISTOGRAM_MEDIUM_TIMES("Variations.FetchSuccessLatency", latency
);
444 std::string seed_data
;
445 bool success
= request
->GetResponseAsString(&seed_data
);
448 StoreSeedData(seed_data
, response_date
);
451 void VariationsService::OnResourceRequestsAllowed() {
452 // Note that this only attempts to fetch the seed at most once per period
453 // (kSeedFetchPeriodHours). This works because
454 // |resource_request_allowed_notifier_| only calls this method if an
455 // attempt was made earlier that fails (which implies that the period had
456 // elapsed). After a successful attempt is made, the notifier will know not
457 // to call this method again until another failed attempt occurs.
458 RecordRequestsAllowedHistogram(RESOURCE_REQUESTS_ALLOWED_NOTIFIED
);
459 DVLOG(1) << "Retrying fetch.";
462 // This service must have created a scheduler in order for this to be called.
463 DCHECK(request_scheduler_
.get());
464 request_scheduler_
->Reset();
467 bool VariationsService::StoreSeedData(const std::string
& seed_data
,
468 const base::Time
& seed_date
) {
469 if (seed_data
.empty()) {
470 VLOG(1) << "Variations Seed data from server is empty, rejecting the seed.";
474 // Only store the seed data if it parses correctly.
476 if (!seed
.ParseFromString(seed_data
)) {
477 VLOG(1) << "Variations Seed data from server is not in valid proto format, "
478 << "rejecting the seed.";
482 std::string base64_seed_data
;
483 base::Base64Encode(seed_data
, &base64_seed_data
);
485 local_state_
->SetString(prefs::kVariationsSeed
, base64_seed_data
);
486 local_state_
->SetString(prefs::kVariationsSeedHash
, HashSeed(seed_data
));
487 local_state_
->SetInt64(prefs::kVariationsSeedDate
,
488 seed_date
.ToInternalValue());
489 variations_serial_number_
= seed
.serial_number();
491 RecordLastFetchTime();
496 bool VariationsService::LoadVariationsSeedFromPref(VariationsSeed
* seed
) {
497 const std::string base64_seed_data
=
498 local_state_
->GetString(prefs::kVariationsSeed
);
499 if (base64_seed_data
.empty()) {
500 RecordVariationSeedEmptyHistogram(VARIATIONS_SEED_EMPTY
);
504 const std::string hash_from_pref
=
505 local_state_
->GetString(prefs::kVariationsSeedHash
);
506 // If the decode process fails, assume the pref value is corrupt and clear it.
507 std::string seed_data
;
508 if (!base::Base64Decode(base64_seed_data
, &seed_data
) ||
509 (!hash_from_pref
.empty() && HashSeed(seed_data
) != hash_from_pref
) ||
510 !seed
->ParseFromString(seed_data
)) {
511 VLOG(1) << "Variations seed data in local pref is corrupt, clearing the "
513 local_state_
->ClearPref(prefs::kVariationsSeed
);
514 local_state_
->ClearPref(prefs::kVariationsSeedDate
);
515 local_state_
->ClearPref(prefs::kVariationsSeedHash
);
516 RecordVariationSeedEmptyHistogram(VARIATIONS_SEED_CORRUPT
);
519 variations_serial_number_
= seed
->serial_number();
520 RecordVariationSeedEmptyHistogram(VARIATIONS_SEED_NOT_EMPTY
);
524 void VariationsService::RecordLastFetchTime() {
525 // local_state_ is NULL in tests, so check it first.
527 local_state_
->SetInt64(prefs::kVariationsLastFetchTime
,
528 base::Time::Now().ToInternalValue());
532 } // namespace chrome_variations