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