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 |
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 = |
| 118 policy_pref_service->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), |
| 190 policy_pref_service_(local_state), |
187 seed_store_(local_state), | 191 seed_store_(local_state), |
188 variations_server_url_(GetVariationsServerURL(NULL)), | |
189 create_trials_from_seed_called_(false), | 192 create_trials_from_seed_called_(false), |
190 initial_request_completed_(false), | 193 initial_request_completed_(false), |
191 resource_request_allowed_notifier_(notifier) { | 194 resource_request_allowed_notifier_(notifier) { |
192 resource_request_allowed_notifier_->Init(this); | 195 resource_request_allowed_notifier_->Init(this); |
193 } | 196 } |
194 | 197 |
195 VariationsService::~VariationsService() { | 198 VariationsService::~VariationsService() { |
196 } | 199 } |
197 | 200 |
198 bool VariationsService::CreateTrialsFromSeed() { | 201 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(), | 238 UMA_HISTOGRAM_CUSTOM_COUNTS("Variations.SeedFreshness", delta.InMinutes(), |
236 1, base::TimeDelta::FromDays(30).InMinutes(), 50); | 239 1, base::TimeDelta::FromDays(30).InMinutes(), 50); |
237 } | 240 } |
238 | 241 |
239 return true; | 242 return true; |
240 } | 243 } |
241 | 244 |
242 void VariationsService::StartRepeatedVariationsSeedFetch() { | 245 void VariationsService::StartRepeatedVariationsSeedFetch() { |
243 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 246 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
244 | 247 |
| 248 // Initialize the Variations server URL. |
| 249 variations_server_url_ = GetVariationsServerURL(policy_pref_service_); |
| 250 |
245 // Check that |CreateTrialsFromSeed| was called, which is necessary to | 251 // Check that |CreateTrialsFromSeed| was called, which is necessary to |
246 // retrieve the serial number that will be sent to the server. | 252 // retrieve the serial number that will be sent to the server. |
247 DCHECK(create_trials_from_seed_called_); | 253 DCHECK(create_trials_from_seed_called_); |
248 | 254 |
249 DCHECK(!request_scheduler_.get()); | 255 DCHECK(!request_scheduler_.get()); |
250 // Note that the act of instantiating the scheduler will start the fetch, if | 256 // 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 | 257 // the scheduler deems appropriate. Using Unretained is fine here since the |
252 // lifespan of request_scheduler_ is guaranteed to be shorter than that of | 258 // lifespan of request_scheduler_ is guaranteed to be shorter than that of |
253 // this service. | 259 // this service. |
254 request_scheduler_.reset(VariationsRequestScheduler::Create( | 260 request_scheduler_.reset(VariationsRequestScheduler::Create( |
255 base::Bind(&VariationsService::FetchVariationsSeed, | 261 base::Bind(&VariationsService::FetchVariationsSeed, |
256 base::Unretained(this)), local_state_)); | 262 base::Unretained(this)), local_state_)); |
257 request_scheduler_->Start(); | 263 request_scheduler_->Start(); |
258 } | 264 } |
259 | 265 |
260 // static | 266 // static |
261 GURL VariationsService::GetVariationsServerURL(PrefService* local_state) { | 267 GURL VariationsService::GetVariationsServerURL( |
| 268 PrefService* policy_pref_service) { |
262 std::string server_url_string(CommandLine::ForCurrentProcess()-> | 269 std::string server_url_string(CommandLine::ForCurrentProcess()-> |
263 GetSwitchValueASCII(switches::kVariationsServerURL)); | 270 GetSwitchValueASCII(switches::kVariationsServerURL)); |
264 if (server_url_string.empty()) | 271 if (server_url_string.empty()) |
265 server_url_string = kDefaultVariationsServerURL; | 272 server_url_string = kDefaultVariationsServerURL; |
266 GURL server_url = GURL(server_url_string); | 273 GURL server_url = GURL(server_url_string); |
267 | 274 |
268 const std::string restrict_param = GetRestrictParameterPref(local_state); | 275 const std::string restrict_param = |
| 276 GetRestrictParameterPref(policy_pref_service); |
269 if (!restrict_param.empty()) { | 277 if (!restrict_param.empty()) { |
270 server_url = net::AppendOrReplaceQueryParameter(server_url, | 278 server_url = net::AppendOrReplaceQueryParameter(server_url, |
271 "restrict", | 279 "restrict", |
272 restrict_param); | 280 restrict_param); |
273 } | 281 } |
274 | 282 |
275 server_url = net::AppendOrReplaceQueryParameter(server_url, "osname", | 283 server_url = net::AppendOrReplaceQueryParameter(server_url, "osname", |
276 GetPlatformString()); | 284 GetPlatformString()); |
277 | 285 |
278 DCHECK(server_url.is_valid()); | 286 DCHECK(server_url.is_valid()); |
(...skipping 12 matching lines...) Expand all Loading... |
291 | 299 |
292 // static | 300 // static |
293 std::string VariationsService::GetDefaultVariationsServerURLForTesting() { | 301 std::string VariationsService::GetDefaultVariationsServerURLForTesting() { |
294 return kDefaultVariationsServerURL; | 302 return kDefaultVariationsServerURL; |
295 } | 303 } |
296 | 304 |
297 // static | 305 // static |
298 void VariationsService::RegisterPrefs(PrefRegistrySimple* registry) { | 306 void VariationsService::RegisterPrefs(PrefRegistrySimple* registry) { |
299 VariationsSeedStore::RegisterPrefs(registry); | 307 VariationsSeedStore::RegisterPrefs(registry); |
300 registry->RegisterInt64Pref(prefs::kVariationsLastFetchTime, 0); | 308 registry->RegisterInt64Pref(prefs::kVariationsLastFetchTime, 0); |
| 309 // This preference will only be written by the policy service, which will fill |
| 310 // it according to a value stored in the User Policy. |
301 registry->RegisterStringPref(prefs::kVariationsRestrictParameter, | 311 registry->RegisterStringPref(prefs::kVariationsRestrictParameter, |
302 std::string()); | 312 std::string()); |
303 } | 313 } |
304 | 314 |
305 // static | 315 // static |
| 316 void VariationsService::RegisterProfilePrefs( |
| 317 user_prefs::PrefRegistrySyncable* registry) { |
| 318 // This preference will only be written by the policy service, which will fill |
| 319 // it according to a value stored in the User Policy. |
| 320 registry->RegisterStringPref( |
| 321 prefs::kVariationsRestrictParameter, |
| 322 std::string(), |
| 323 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); |
| 324 } |
| 325 |
| 326 // static |
306 VariationsService* VariationsService::Create(PrefService* local_state) { | 327 VariationsService* VariationsService::Create(PrefService* local_state) { |
307 #if !defined(GOOGLE_CHROME_BUILD) | 328 #if !defined(GOOGLE_CHROME_BUILD) |
308 // Unless the URL was provided, unsupported builds should return NULL to | 329 // Unless the URL was provided, unsupported builds should return NULL to |
309 // indicate that the service should not be used. | 330 // indicate that the service should not be used. |
310 if (!CommandLine::ForCurrentProcess()->HasSwitch( | 331 if (!CommandLine::ForCurrentProcess()->HasSwitch( |
311 switches::kVariationsServerURL)) { | 332 switches::kVariationsServerURL)) { |
312 DVLOG(1) << "Not creating VariationsService in unofficial build without --" | 333 DVLOG(1) << "Not creating VariationsService in unofficial build without --" |
313 << switches::kVariationsServerURL << " specified."; | 334 << switches::kVariationsServerURL << " specified."; |
314 return NULL; | 335 return NULL; |
315 } | 336 } |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
448 | 469 |
449 void VariationsService::RecordLastFetchTime() { | 470 void VariationsService::RecordLastFetchTime() { |
450 // local_state_ is NULL in tests, so check it first. | 471 // local_state_ is NULL in tests, so check it first. |
451 if (local_state_) { | 472 if (local_state_) { |
452 local_state_->SetInt64(prefs::kVariationsLastFetchTime, | 473 local_state_->SetInt64(prefs::kVariationsLastFetchTime, |
453 base::Time::Now().ToInternalValue()); | 474 base::Time::Now().ToInternalValue()); |
454 } | 475 } |
455 } | 476 } |
456 | 477 |
457 } // namespace chrome_variations | 478 } // namespace chrome_variations |
OLD | NEW |