Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/metrics/variations/variations_service.h" | 5 #include "chrome/browser/metrics/variations/variations_service.h" |
| 6 | 6 |
| 7 #include <set> | 7 #include <set> |
| 8 | 8 |
| 9 #include "base/build_time.h" | 9 #include "base/build_time.h" |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| 11 #include "base/metrics/histogram.h" | 11 #include "base/metrics/histogram.h" |
| 12 #include "base/metrics/sparse_histogram.h" | 12 #include "base/metrics/sparse_histogram.h" |
| 13 #include "base/prefs/pref_registry_simple.h" | 13 #include "base/prefs/pref_registry_simple.h" |
| 14 #include "base/prefs/pref_service.h" | 14 #include "base/prefs/pref_service.h" |
| 15 #include "base/version.h" | 15 #include "base/version.h" |
| 16 #include "chrome/browser/browser_process.h" | 16 #include "chrome/browser/browser_process.h" |
| 17 #include "chrome/browser/network_time/network_time_tracker.h" | 17 #include "chrome/browser/network_time/network_time_tracker.h" |
| 18 #include "chrome/common/chrome_switches.h" | 18 #include "chrome/common/chrome_switches.h" |
| 19 #include "chrome/common/pref_names.h" | 19 #include "chrome/common/pref_names.h" |
| 20 #include "components/user_prefs/pref_registry_syncable.h" | |
| 20 #include "components/variations/proto/variations_seed.pb.h" | 21 #include "components/variations/proto/variations_seed.pb.h" |
| 21 #include "components/variations/variations_seed_processor.h" | 22 #include "components/variations/variations_seed_processor.h" |
| 22 #include "content/public/browser/browser_thread.h" | 23 #include "content/public/browser/browser_thread.h" |
| 23 #include "net/base/load_flags.h" | 24 #include "net/base/load_flags.h" |
| 24 #include "net/base/net_errors.h" | 25 #include "net/base/net_errors.h" |
| 25 #include "net/base/network_change_notifier.h" | 26 #include "net/base/network_change_notifier.h" |
| 26 #include "net/base/url_util.h" | 27 #include "net/base/url_util.h" |
| 27 #include "net/http/http_response_headers.h" | 28 #include "net/http/http_response_headers.h" |
| 28 #include "net/http/http_status_code.h" | 29 #include "net/http/http_status_code.h" |
| 29 #include "net/http/http_util.h" | 30 #include "net/http/http_util.h" |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 97 return "android"; | 98 return "android"; |
| 98 #elif defined(OS_LINUX) || defined(OS_BSD) || defined(OS_SOLARIS) | 99 #elif defined(OS_LINUX) || defined(OS_BSD) || defined(OS_SOLARIS) |
| 99 // Default BSD and SOLARIS to Linux to not break those builds, although these | 100 // Default BSD and SOLARIS to Linux to not break those builds, although these |
| 100 // platforms are not officially supported by Chrome. | 101 // platforms are not officially supported by Chrome. |
| 101 return "linux"; | 102 return "linux"; |
| 102 #else | 103 #else |
| 103 #error Unknown platform | 104 #error Unknown platform |
| 104 #endif | 105 #endif |
| 105 } | 106 } |
| 106 | 107 |
| 107 // Gets the restrict parameter from |local_state| or from Chrome OS settings in | 108 // Gets the restrict parameter from |policy_pref_service_| or from Chrome OS |
|
Alexei Svitkine (slow)
2014/02/10 22:12:48
Nit: remove underscore after variable name, since
Mathieu
2014/02/10 22:41:34
Done.
| |
| 108 // the case of that platform. | 109 // settings in the case of that platform. |
| 109 std::string GetRestrictParameterPref(PrefService* local_state) { | 110 std::string GetRestrictParameterPref(PrefService* policy_pref_service) { |
| 110 std::string parameter; | 111 std::string parameter; |
| 111 #if defined(OS_CHROMEOS) | 112 #if defined(OS_CHROMEOS) |
| 112 chromeos::CrosSettings::Get()->GetString( | 113 chromeos::CrosSettings::Get()->GetString( |
| 113 chromeos::kVariationsRestrictParameter, ¶meter); | 114 chromeos::kVariationsRestrictParameter, ¶meter); |
| 114 #else | 115 #else |
| 115 if (local_state) | 116 if (policy_pref_service) { |
| 116 parameter = local_state->GetString(prefs::kVariationsRestrictParameter); | 117 parameter = policy_pref_service-> |
|
Alexei Svitkine (slow)
2014/02/10 22:12:48
Nit: If this needs to wrap, wrap after the =
Mathieu
2014/02/10 22:41:34
Done.
| |
| 118 GetString(prefs::kVariationsRestrictParameter); | |
| 119 } | |
| 117 #endif | 120 #endif |
| 118 return parameter; | 121 return parameter; |
| 119 } | 122 } |
| 120 | 123 |
| 121 enum ResourceRequestsAllowedState { | 124 enum ResourceRequestsAllowedState { |
| 122 RESOURCE_REQUESTS_ALLOWED, | 125 RESOURCE_REQUESTS_ALLOWED, |
| 123 RESOURCE_REQUESTS_NOT_ALLOWED, | 126 RESOURCE_REQUESTS_NOT_ALLOWED, |
| 124 RESOURCE_REQUESTS_ALLOWED_NOTIFIED, | 127 RESOURCE_REQUESTS_ALLOWED_NOTIFIED, |
| 125 RESOURCE_REQUESTS_NOT_ALLOWED_EULA_NOT_ACCEPTED, | 128 RESOURCE_REQUESTS_NOT_ALLOWED_EULA_NOT_ACCEPTED, |
| 126 RESOURCE_REQUESTS_NOT_ALLOWED_NETWORK_DOWN, | 129 RESOURCE_REQUESTS_NOT_ALLOWED_NETWORK_DOWN, |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 165 return Study_FormFactor_DESKTOP; | 168 return Study_FormFactor_DESKTOP; |
| 166 } | 169 } |
| 167 NOTREACHED(); | 170 NOTREACHED(); |
| 168 return Study_FormFactor_DESKTOP; | 171 return Study_FormFactor_DESKTOP; |
| 169 } | 172 } |
| 170 | 173 |
| 171 } // namespace | 174 } // namespace |
| 172 | 175 |
| 173 VariationsService::VariationsService(PrefService* local_state) | 176 VariationsService::VariationsService(PrefService* local_state) |
| 174 : local_state_(local_state), | 177 : local_state_(local_state), |
| 178 policy_pref_service_(local_state), | |
| 175 seed_store_(local_state), | 179 seed_store_(local_state), |
| 176 variations_server_url_(GetVariationsServerURL(local_state)), | |
| 177 create_trials_from_seed_called_(false), | 180 create_trials_from_seed_called_(false), |
| 178 initial_request_completed_(false), | 181 initial_request_completed_(false), |
| 179 resource_request_allowed_notifier_( | 182 resource_request_allowed_notifier_( |
| 180 new ResourceRequestAllowedNotifier) { | 183 new ResourceRequestAllowedNotifier) { |
| 181 resource_request_allowed_notifier_->Init(this); | 184 resource_request_allowed_notifier_->Init(this); |
| 182 } | 185 } |
| 183 | 186 |
| 184 VariationsService::VariationsService(ResourceRequestAllowedNotifier* notifier, | 187 VariationsService::VariationsService(ResourceRequestAllowedNotifier* notifier, |
| 185 PrefService* local_state) | 188 PrefService* local_state) |
| 186 : local_state_(local_state), | 189 : local_state_(local_state), |
| 187 seed_store_(local_state), | 190 seed_store_(local_state), |
| 188 variations_server_url_(GetVariationsServerURL(NULL)), | |
| 189 create_trials_from_seed_called_(false), | 191 create_trials_from_seed_called_(false), |
| 190 initial_request_completed_(false), | 192 initial_request_completed_(false), |
| 191 resource_request_allowed_notifier_(notifier) { | 193 resource_request_allowed_notifier_(notifier) { |
| 192 resource_request_allowed_notifier_->Init(this); | 194 resource_request_allowed_notifier_->Init(this); |
| 193 } | 195 } |
| 194 | 196 |
| 195 VariationsService::~VariationsService() { | 197 VariationsService::~VariationsService() { |
| 196 } | 198 } |
| 197 | 199 |
| 198 bool VariationsService::CreateTrialsFromSeed() { | 200 bool VariationsService::CreateTrialsFromSeed() { |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 235 UMA_HISTOGRAM_CUSTOM_COUNTS("Variations.SeedFreshness", delta.InMinutes(), | 237 UMA_HISTOGRAM_CUSTOM_COUNTS("Variations.SeedFreshness", delta.InMinutes(), |
| 236 1, base::TimeDelta::FromDays(30).InMinutes(), 50); | 238 1, base::TimeDelta::FromDays(30).InMinutes(), 50); |
| 237 } | 239 } |
| 238 | 240 |
| 239 return true; | 241 return true; |
| 240 } | 242 } |
| 241 | 243 |
| 242 void VariationsService::StartRepeatedVariationsSeedFetch() { | 244 void VariationsService::StartRepeatedVariationsSeedFetch() { |
| 243 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 245 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 244 | 246 |
| 247 // Initialize the Variations server URL. | |
| 248 variations_server_url_ = GetVariationsServerURL(policy_pref_service_); | |
| 249 | |
| 245 // Check that |CreateTrialsFromSeed| was called, which is necessary to | 250 // Check that |CreateTrialsFromSeed| was called, which is necessary to |
| 246 // retrieve the serial number that will be sent to the server. | 251 // retrieve the serial number that will be sent to the server. |
| 247 DCHECK(create_trials_from_seed_called_); | 252 DCHECK(create_trials_from_seed_called_); |
| 248 | 253 |
| 249 DCHECK(!request_scheduler_.get()); | 254 DCHECK(!request_scheduler_.get()); |
| 250 // Note that the act of instantiating the scheduler will start the fetch, if | 255 // Note that the act of instantiating the scheduler will start the fetch, if |
| 251 // the scheduler deems appropriate. Using Unretained is fine here since the | 256 // the scheduler deems appropriate. Using Unretained is fine here since the |
| 252 // lifespan of request_scheduler_ is guaranteed to be shorter than that of | 257 // lifespan of request_scheduler_ is guaranteed to be shorter than that of |
| 253 // this service. | 258 // this service. |
| 254 request_scheduler_.reset(VariationsRequestScheduler::Create( | 259 request_scheduler_.reset(VariationsRequestScheduler::Create( |
| 255 base::Bind(&VariationsService::FetchVariationsSeed, | 260 base::Bind(&VariationsService::FetchVariationsSeed, |
| 256 base::Unretained(this)), local_state_)); | 261 base::Unretained(this)), local_state_)); |
| 257 request_scheduler_->Start(); | 262 request_scheduler_->Start(); |
| 258 } | 263 } |
| 259 | 264 |
| 260 // static | 265 // static |
| 261 GURL VariationsService::GetVariationsServerURL(PrefService* local_state) { | 266 GURL VariationsService::GetVariationsServerURL( |
| 267 PrefService* policy_pref_service) { | |
| 262 std::string server_url_string(CommandLine::ForCurrentProcess()-> | 268 std::string server_url_string(CommandLine::ForCurrentProcess()-> |
| 263 GetSwitchValueASCII(switches::kVariationsServerURL)); | 269 GetSwitchValueASCII(switches::kVariationsServerURL)); |
| 264 if (server_url_string.empty()) | 270 if (server_url_string.empty()) |
| 265 server_url_string = kDefaultVariationsServerURL; | 271 server_url_string = kDefaultVariationsServerURL; |
| 266 GURL server_url = GURL(server_url_string); | 272 GURL server_url = GURL(server_url_string); |
| 267 | 273 |
| 268 const std::string restrict_param = GetRestrictParameterPref(local_state); | 274 const std::string restrict_param = |
| 275 GetRestrictParameterPref(policy_pref_service); | |
| 269 if (!restrict_param.empty()) { | 276 if (!restrict_param.empty()) { |
| 270 server_url = net::AppendOrReplaceQueryParameter(server_url, | 277 server_url = net::AppendOrReplaceQueryParameter(server_url, |
| 271 "restrict", | 278 "restrict", |
| 272 restrict_param); | 279 restrict_param); |
| 273 } | 280 } |
| 274 | 281 |
| 275 server_url = net::AppendOrReplaceQueryParameter(server_url, "osname", | 282 server_url = net::AppendOrReplaceQueryParameter(server_url, "osname", |
| 276 GetPlatformString()); | 283 GetPlatformString()); |
| 277 | 284 |
| 278 DCHECK(server_url.is_valid()); | 285 DCHECK(server_url.is_valid()); |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 296 | 303 |
| 297 // static | 304 // static |
| 298 void VariationsService::RegisterPrefs(PrefRegistrySimple* registry) { | 305 void VariationsService::RegisterPrefs(PrefRegistrySimple* registry) { |
| 299 VariationsSeedStore::RegisterPrefs(registry); | 306 VariationsSeedStore::RegisterPrefs(registry); |
| 300 registry->RegisterInt64Pref(prefs::kVariationsLastFetchTime, 0); | 307 registry->RegisterInt64Pref(prefs::kVariationsLastFetchTime, 0); |
| 301 registry->RegisterStringPref(prefs::kVariationsRestrictParameter, | 308 registry->RegisterStringPref(prefs::kVariationsRestrictParameter, |
| 302 std::string()); | 309 std::string()); |
| 303 } | 310 } |
| 304 | 311 |
| 305 // static | 312 // static |
| 313 void VariationsService::RegisterProfilePrefs( | |
| 314 user_prefs::PrefRegistrySyncable* registry) { | |
| 315 registry->RegisterStringPref( | |
|
Alexei Svitkine (slow)
2014/02/10 22:12:48
Can you add a comment about this? (that it's not a
Mathieu
2014/02/10 22:41:34
Done.
| |
| 316 prefs::kVariationsRestrictParameter, | |
| 317 std::string(), | |
| 318 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); | |
| 319 } | |
| 320 | |
| 321 // static | |
| 306 VariationsService* VariationsService::Create(PrefService* local_state) { | 322 VariationsService* VariationsService::Create(PrefService* local_state) { |
| 307 #if !defined(GOOGLE_CHROME_BUILD) | 323 #if !defined(GOOGLE_CHROME_BUILD) |
| 308 // Unless the URL was provided, unsupported builds should return NULL to | 324 // Unless the URL was provided, unsupported builds should return NULL to |
| 309 // indicate that the service should not be used. | 325 // indicate that the service should not be used. |
| 310 if (!CommandLine::ForCurrentProcess()->HasSwitch( | 326 if (!CommandLine::ForCurrentProcess()->HasSwitch( |
| 311 switches::kVariationsServerURL)) { | 327 switches::kVariationsServerURL)) { |
| 312 DVLOG(1) << "Not creating VariationsService in unofficial build without --" | 328 DVLOG(1) << "Not creating VariationsService in unofficial build without --" |
| 313 << switches::kVariationsServerURL << " specified."; | 329 << switches::kVariationsServerURL << " specified."; |
| 314 return NULL; | 330 return NULL; |
| 315 } | 331 } |
| (...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 448 | 464 |
| 449 void VariationsService::RecordLastFetchTime() { | 465 void VariationsService::RecordLastFetchTime() { |
| 450 // local_state_ is NULL in tests, so check it first. | 466 // local_state_ is NULL in tests, so check it first. |
| 451 if (local_state_) { | 467 if (local_state_) { |
| 452 local_state_->SetInt64(prefs::kVariationsLastFetchTime, | 468 local_state_->SetInt64(prefs::kVariationsLastFetchTime, |
| 453 base::Time::Now().ToInternalValue()); | 469 base::Time::Now().ToInternalValue()); |
| 454 } | 470 } |
| 455 } | 471 } |
| 456 | 472 |
| 457 } // namespace chrome_variations | 473 } // namespace chrome_variations |
| OLD | NEW |