- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / metrics / variations / variations_service.cc
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/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 "url/gurl.h"
38
39 #if defined(OS_CHROMEOS)
40 #include "chrome/browser/chromeos/settings/cros_settings.h"
41 #endif
42
43 namespace chrome_variations {
44
45 namespace {
46
47 // Default server of Variations seed info.
48 const char kDefaultVariationsServerURL[] =
49     "https://clients4.google.com/chrome-variations/seed";
50 const int kMaxRetrySeedFetch = 5;
51
52 // TODO(mad): To be removed when we stop updating the NetworkTimeTracker.
53 // For the HTTP date headers, the resolution of the server time is 1 second.
54 const int64 kServerTimeResolutionMs = 1000;
55
56 // Wrapper around channel checking, used to enable channel mocking for
57 // testing. If the current browser channel is not UNKNOWN, this will return
58 // that channel value. Otherwise, if the fake channel flag is provided, this
59 // will return the fake channel. Failing that, this will return the UNKNOWN
60 // channel.
61 Study_Channel GetChannelForVariations() {
62   switch (chrome::VersionInfo::GetChannel()) {
63     case chrome::VersionInfo::CHANNEL_CANARY:
64       return Study_Channel_CANARY;
65     case chrome::VersionInfo::CHANNEL_DEV:
66       return Study_Channel_DEV;
67     case chrome::VersionInfo::CHANNEL_BETA:
68       return Study_Channel_BETA;
69     case chrome::VersionInfo::CHANNEL_STABLE:
70       return Study_Channel_STABLE;
71     case chrome::VersionInfo::CHANNEL_UNKNOWN:
72       break;
73   }
74   const std::string forced_channel =
75       CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
76           switches::kFakeVariationsChannel);
77   if (forced_channel == "stable")
78     return Study_Channel_STABLE;
79   if (forced_channel == "beta")
80     return Study_Channel_BETA;
81   if (forced_channel == "dev")
82     return Study_Channel_DEV;
83   if (forced_channel == "canary")
84     return Study_Channel_CANARY;
85   DVLOG(1) << "Invalid channel provided: " << forced_channel;
86   return Study_Channel_UNKNOWN;
87 }
88
89 // Returns a string that will be used for the value of the 'osname' URL param
90 // to the variations server.
91 std::string GetPlatformString() {
92 #if defined(OS_WIN)
93   return "win";
94 #elif defined(OS_IOS)
95   return "ios";
96 #elif defined(OS_MACOSX)
97   return "mac";
98 #elif defined(OS_CHROMEOS)
99   return "chromeos";
100 #elif defined(OS_ANDROID)
101   return "android";
102 #elif defined(OS_LINUX) || defined(OS_BSD) || defined(OS_SOLARIS)
103   // Default BSD and SOLARIS to Linux to not break those builds, although these
104   // platforms are not officially supported by Chrome.
105   return "linux";
106 #else
107 #error Unknown platform
108 #endif
109 }
110
111 // Converts |date_time| in Study date format to base::Time.
112 base::Time ConvertStudyDateToBaseTime(int64 date_time) {
113   return base::Time::UnixEpoch() + base::TimeDelta::FromSeconds(date_time);
114 }
115
116 // Gets the restrict parameter from |local_state| or from Chrome OS settings in
117 // the case of that platform.
118 std::string GetRestrictParameterPref(PrefService* local_state) {
119   std::string parameter;
120 #if defined(OS_CHROMEOS)
121   chromeos::CrosSettings::Get()->GetString(
122       chromeos::kVariationsRestrictParameter, &parameter);
123 #else
124   if (local_state)
125     parameter = local_state->GetString(prefs::kVariationsRestrictParameter);
126 #endif
127   return parameter;
128 }
129
130 // Computes a hash of the serialized variations seed data.
131 std::string HashSeed(const std::string& seed_data) {
132   const std::string sha1 = base::SHA1HashString(seed_data);
133   return base::HexEncode(sha1.data(), sha1.size());
134 }
135
136 enum ResourceRequestsAllowedState {
137   RESOURCE_REQUESTS_ALLOWED,
138   RESOURCE_REQUESTS_NOT_ALLOWED,
139   RESOURCE_REQUESTS_ALLOWED_NOTIFIED,
140   RESOURCE_REQUESTS_ALLOWED_ENUM_SIZE,
141 };
142
143 // Records UMA histogram with the current resource requests allowed state.
144 void RecordRequestsAllowedHistogram(ResourceRequestsAllowedState state) {
145   UMA_HISTOGRAM_ENUMERATION("Variations.ResourceRequestsAllowed", state,
146                             RESOURCE_REQUESTS_ALLOWED_ENUM_SIZE);
147 }
148
149 enum VariationSeedEmptyState {
150   VARIATIONS_SEED_NOT_EMPTY,
151   VARIATIONS_SEED_EMPTY,
152   VARIATIONS_SEED_CORRUPT,
153   VARIATIONS_SEED_EMPTY_ENUM_SIZE,
154 };
155
156 void RecordVariationSeedEmptyHistogram(VariationSeedEmptyState state) {
157   UMA_HISTOGRAM_ENUMERATION("Variations.SeedEmpty", state,
158                             VARIATIONS_SEED_EMPTY_ENUM_SIZE);
159 }
160
161 }  // namespace
162
163 VariationsService::VariationsService(PrefService* local_state)
164     : local_state_(local_state),
165       variations_server_url_(GetVariationsServerURL(local_state)),
166       create_trials_from_seed_called_(false),
167       initial_request_completed_(false),
168       resource_request_allowed_notifier_(
169           new ResourceRequestAllowedNotifier) {
170   resource_request_allowed_notifier_->Init(this);
171 }
172
173 VariationsService::VariationsService(ResourceRequestAllowedNotifier* notifier,
174                                      PrefService* local_state)
175     : local_state_(local_state),
176       variations_server_url_(GetVariationsServerURL(NULL)),
177       create_trials_from_seed_called_(false),
178       initial_request_completed_(false),
179       resource_request_allowed_notifier_(notifier) {
180   resource_request_allowed_notifier_->Init(this);
181 }
182
183 VariationsService::~VariationsService() {
184 }
185
186 bool VariationsService::CreateTrialsFromSeed() {
187   create_trials_from_seed_called_ = true;
188
189   VariationsSeed seed;
190   if (!LoadVariationsSeedFromPref(&seed))
191     return false;
192
193   const int64 date_value = local_state_->GetInt64(prefs::kVariationsSeedDate);
194   const base::Time seed_date = base::Time::FromInternalValue(date_value);
195   const base::Time build_time = base::GetBuildTime();
196   // Use the build time for date checks if either the seed date is invalid or
197   // the build time is newer than the seed date.
198   base::Time reference_date = seed_date;
199   if (seed_date.is_null() || seed_date < build_time)
200     reference_date = build_time;
201
202   const chrome::VersionInfo current_version_info;
203   if (!current_version_info.is_valid())
204     return false;
205
206   const base::Version current_version(current_version_info.Version());
207   if (!current_version.IsValid())
208     return false;
209
210   VariationsSeedProcessor().CreateTrialsFromSeed(
211       seed, g_browser_process->GetApplicationLocale(), reference_date,
212       current_version, GetChannelForVariations());
213
214   // Log the "freshness" of the seed that was just used. The freshness is the
215   // time between the last successful seed download and now.
216   const int64 last_fetch_time_internal =
217       local_state_->GetInt64(prefs::kVariationsLastFetchTime);
218   if (last_fetch_time_internal) {
219     const base::Time now = base::Time::Now();
220     const base::TimeDelta delta =
221         now - base::Time::FromInternalValue(last_fetch_time_internal);
222     // Log the value in number of minutes.
223     UMA_HISTOGRAM_CUSTOM_COUNTS("Variations.SeedFreshness", delta.InMinutes(),
224         1, base::TimeDelta::FromDays(30).InMinutes(), 50);
225   }
226
227   return true;
228 }
229
230 void VariationsService::StartRepeatedVariationsSeedFetch() {
231   DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
232
233   // Check that |CreateTrialsFromSeed| was called, which is necessary to
234   // retrieve the serial number that will be sent to the server.
235   DCHECK(create_trials_from_seed_called_);
236
237   DCHECK(!request_scheduler_.get());
238   // Note that the act of instantiating the scheduler will start the fetch, if
239   // the scheduler deems appropriate. Using Unretained is fine here since the
240   // lifespan of request_scheduler_ is guaranteed to be shorter than that of
241   // this service.
242   request_scheduler_.reset(VariationsRequestScheduler::Create(
243       base::Bind(&VariationsService::FetchVariationsSeed,
244           base::Unretained(this)), local_state_));
245   request_scheduler_->Start();
246 }
247
248 // static
249 GURL VariationsService::GetVariationsServerURL(PrefService* local_state) {
250   std::string server_url_string(CommandLine::ForCurrentProcess()->
251       GetSwitchValueASCII(switches::kVariationsServerURL));
252   if (server_url_string.empty())
253     server_url_string = kDefaultVariationsServerURL;
254   GURL server_url = GURL(server_url_string);
255
256   const std::string restrict_param = GetRestrictParameterPref(local_state);
257   if (!restrict_param.empty()) {
258     server_url = net::AppendOrReplaceQueryParameter(server_url,
259                                                     "restrict",
260                                                     restrict_param);
261   }
262
263   server_url = net::AppendOrReplaceQueryParameter(server_url, "osname",
264                                                   GetPlatformString());
265
266   DCHECK(server_url.is_valid());
267   return server_url;
268 }
269
270 #if defined(OS_WIN)
271 void VariationsService::StartGoogleUpdateRegistrySync() {
272   registry_syncer_.RequestRegistrySync();
273 }
274 #endif
275
276 void VariationsService::SetCreateTrialsFromSeedCalledForTesting(bool called) {
277   create_trials_from_seed_called_ = called;
278 }
279
280 // static
281 std::string VariationsService::GetDefaultVariationsServerURLForTesting() {
282   return kDefaultVariationsServerURL;
283 }
284
285 // static
286 void VariationsService::RegisterPrefs(PrefRegistrySimple* registry) {
287   registry->RegisterStringPref(prefs::kVariationsSeed, std::string());
288   registry->RegisterStringPref(prefs::kVariationsSeedHash, std::string());
289   registry->RegisterInt64Pref(prefs::kVariationsSeedDate,
290                               base::Time().ToInternalValue());
291   registry->RegisterInt64Pref(prefs::kVariationsLastFetchTime, 0);
292   registry->RegisterStringPref(prefs::kVariationsRestrictParameter,
293                                std::string());
294 }
295
296 // static
297 VariationsService* VariationsService::Create(PrefService* local_state) {
298 #if !defined(GOOGLE_CHROME_BUILD)
299   // Unless the URL was provided, unsupported builds should return NULL to
300   // indicate that the service should not be used.
301   if (!CommandLine::ForCurrentProcess()->HasSwitch(
302           switches::kVariationsServerURL)) {
303     DVLOG(1) << "Not creating VariationsService in unofficial build without --"
304              << switches::kVariationsServerURL << " specified.";
305     return NULL;
306   }
307 #endif
308   return new VariationsService(local_state);
309 }
310
311 void VariationsService::DoActualFetch() {
312   pending_seed_request_.reset(net::URLFetcher::Create(
313       0, variations_server_url_, net::URLFetcher::GET, this));
314   pending_seed_request_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES |
315                                       net::LOAD_DO_NOT_SAVE_COOKIES);
316   pending_seed_request_->SetRequestContext(
317       g_browser_process->system_request_context());
318   pending_seed_request_->SetMaxRetriesOn5xx(kMaxRetrySeedFetch);
319   if (!variations_serial_number_.empty()) {
320     pending_seed_request_->AddExtraRequestHeader("If-Match:" +
321                                                  variations_serial_number_);
322   }
323   pending_seed_request_->Start();
324
325   const base::TimeTicks now = base::TimeTicks::Now();
326   base::TimeDelta time_since_last_fetch;
327   // Record a time delta of 0 (default value) if there was no previous fetch.
328   if (!last_request_started_time_.is_null())
329     time_since_last_fetch = now - last_request_started_time_;
330   UMA_HISTOGRAM_CUSTOM_COUNTS("Variations.TimeSinceLastFetchAttempt",
331                               time_since_last_fetch.InMinutes(), 0,
332                               base::TimeDelta::FromDays(7).InMinutes(), 50);
333   last_request_started_time_ = now;
334 }
335
336 void VariationsService::FetchVariationsSeed() {
337   DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
338
339   if (!resource_request_allowed_notifier_->ResourceRequestsAllowed()) {
340     RecordRequestsAllowedHistogram(RESOURCE_REQUESTS_NOT_ALLOWED);
341     DVLOG(1) << "Resource requests were not allowed. Waiting for notification.";
342     return;
343   }
344
345   RecordRequestsAllowedHistogram(RESOURCE_REQUESTS_ALLOWED);
346   DoActualFetch();
347 }
348
349 void VariationsService::OnURLFetchComplete(const net::URLFetcher* source) {
350   DCHECK_EQ(pending_seed_request_.get(), source);
351
352   const bool is_first_request = !initial_request_completed_;
353   initial_request_completed_ = true;
354
355   // The fetcher will be deleted when the request is handled.
356   scoped_ptr<const net::URLFetcher> request(pending_seed_request_.release());
357   const net::URLRequestStatus& request_status = request->GetStatus();
358   if (request_status.status() != net::URLRequestStatus::SUCCESS) {
359     UMA_HISTOGRAM_SPARSE_SLOWLY("Variations.FailedRequestErrorCode",
360                                 -request_status.error());
361     DVLOG(1) << "Variations server request failed with error: "
362              << request_status.error() << ": "
363              << net::ErrorToString(request_status.error());
364     // It's common for the very first fetch attempt to fail (e.g. the network
365     // may not yet be available). In such a case, try again soon, rather than
366     // waiting the full time interval.
367     if (is_first_request)
368       request_scheduler_->ScheduleFetchShortly();
369     return;
370   }
371
372   // Log the response code.
373   const int response_code = request->GetResponseCode();
374   UMA_HISTOGRAM_SPARSE_SLOWLY("Variations.SeedFetchResponseCode",
375                               response_code);
376
377   const base::TimeDelta latency =
378       base::TimeTicks::Now() - last_request_started_time_;
379
380   base::Time response_date;
381   if (response_code == net::HTTP_OK ||
382       response_code == net::HTTP_NOT_MODIFIED) {
383     bool success = request->GetResponseHeaders()->GetDateValue(&response_date);
384     DCHECK(success || response_date.is_null());
385
386     if (!response_date.is_null()) {
387       NetworkTimeTracker::BuildNotifierUpdateCallback().Run(
388           response_date,
389           base::TimeDelta::FromMilliseconds(kServerTimeResolutionMs),
390           latency);
391     }
392   }
393
394   if (response_code != net::HTTP_OK) {
395     DVLOG(1) << "Variations server request returned non-HTTP_OK response code: "
396              << response_code;
397     if (response_code == net::HTTP_NOT_MODIFIED) {
398       UMA_HISTOGRAM_MEDIUM_TIMES("Variations.FetchNotModifiedLatency", latency);
399       RecordLastFetchTime();
400       // Update the seed date value in local state (used for expiry check on
401       // next start up), since 304 is a successful response.
402       local_state_->SetInt64(prefs::kVariationsSeedDate,
403                              response_date.ToInternalValue());
404     } else {
405       UMA_HISTOGRAM_MEDIUM_TIMES("Variations.FetchOtherLatency", latency);
406     }
407     return;
408   }
409   UMA_HISTOGRAM_MEDIUM_TIMES("Variations.FetchSuccessLatency", latency);
410
411   std::string seed_data;
412   bool success = request->GetResponseAsString(&seed_data);
413   DCHECK(success);
414
415   StoreSeedData(seed_data, response_date);
416 }
417
418 void VariationsService::OnResourceRequestsAllowed() {
419   // Note that this only attempts to fetch the seed at most once per period
420   // (kSeedFetchPeriodHours). This works because
421   // |resource_request_allowed_notifier_| only calls this method if an
422   // attempt was made earlier that fails (which implies that the period had
423   // elapsed). After a successful attempt is made, the notifier will know not
424   // to call this method again until another failed attempt occurs.
425   RecordRequestsAllowedHistogram(RESOURCE_REQUESTS_ALLOWED_NOTIFIED);
426   DVLOG(1) << "Retrying fetch.";
427   DoActualFetch();
428
429   // This service must have created a scheduler in order for this to be called.
430   DCHECK(request_scheduler_.get());
431   request_scheduler_->Reset();
432 }
433
434 bool VariationsService::StoreSeedData(const std::string& seed_data,
435                                       const base::Time& seed_date) {
436   if (seed_data.empty()) {
437     VLOG(1) << "Variations Seed data from server is empty, rejecting the seed.";
438     return false;
439   }
440
441   // Only store the seed data if it parses correctly.
442   VariationsSeed seed;
443   if (!seed.ParseFromString(seed_data)) {
444     VLOG(1) << "Variations Seed data from server is not in valid proto format, "
445             << "rejecting the seed.";
446     return false;
447   }
448
449   std::string base64_seed_data;
450   if (!base::Base64Encode(seed_data, &base64_seed_data)) {
451     VLOG(1) << "Variations Seed data from server fails Base64Encode, rejecting "
452             << "the seed.";
453     return false;
454   }
455
456   local_state_->SetString(prefs::kVariationsSeed, base64_seed_data);
457   local_state_->SetString(prefs::kVariationsSeedHash, HashSeed(seed_data));
458   local_state_->SetInt64(prefs::kVariationsSeedDate,
459                          seed_date.ToInternalValue());
460   variations_serial_number_ = seed.serial_number();
461
462   RecordLastFetchTime();
463
464   return true;
465 }
466
467 bool VariationsService::LoadVariationsSeedFromPref(VariationsSeed* seed) {
468   const std::string base64_seed_data =
469       local_state_->GetString(prefs::kVariationsSeed);
470   if (base64_seed_data.empty()) {
471     RecordVariationSeedEmptyHistogram(VARIATIONS_SEED_EMPTY);
472     return false;
473   }
474
475   const std::string hash_from_pref =
476       local_state_->GetString(prefs::kVariationsSeedHash);
477   // If the decode process fails, assume the pref value is corrupt and clear it.
478   std::string seed_data;
479   if (!base::Base64Decode(base64_seed_data, &seed_data) ||
480       (!hash_from_pref.empty() && HashSeed(seed_data) != hash_from_pref) ||
481       !seed->ParseFromString(seed_data)) {
482     VLOG(1) << "Variations seed data in local pref is corrupt, clearing the "
483             << "pref.";
484     local_state_->ClearPref(prefs::kVariationsSeed);
485     local_state_->ClearPref(prefs::kVariationsSeedDate);
486     local_state_->ClearPref(prefs::kVariationsSeedHash);
487     RecordVariationSeedEmptyHistogram(VARIATIONS_SEED_CORRUPT);
488     return false;
489   }
490   variations_serial_number_ = seed->serial_number();
491   RecordVariationSeedEmptyHistogram(VARIATIONS_SEED_NOT_EMPTY);
492   return true;
493 }
494
495 void VariationsService::RecordLastFetchTime() {
496   // local_state_ is NULL in tests, so check it first.
497   if (local_state_) {
498     local_state_->SetInt64(prefs::kVariationsLastFetchTime,
499                            base::Time::Now().ToInternalValue());
500   }
501 }
502
503 }  // namespace chrome_variations