Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(457)

Side by Side Diff: chrome/browser/metrics/variations/variations_service.cc

Issue 136003008: [Variations] Read the policy from profile prefs if on Android (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: addressed comments Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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, &parameter); 114 chromeos::kVariationsRestrictParameter, &parameter);
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
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),
Joao da Silva 2014/02/11 16:28:33 You need to initialize policy_pref_service_ here
Mathieu 2014/02/11 18:02:47 Done.
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
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 12 matching lines...) Expand all
291 298
292 // static 299 // static
293 std::string VariationsService::GetDefaultVariationsServerURLForTesting() { 300 std::string VariationsService::GetDefaultVariationsServerURLForTesting() {
294 return kDefaultVariationsServerURL; 301 return kDefaultVariationsServerURL;
295 } 302 }
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);
308 // This preference will only be written by the policy service, which will fill
309 // it according to a value stored in the User Policy.
301 registry->RegisterStringPref(prefs::kVariationsRestrictParameter, 310 registry->RegisterStringPref(prefs::kVariationsRestrictParameter,
302 std::string()); 311 std::string());
303 } 312 }
304 313
305 // static 314 // static
315 void VariationsService::RegisterProfilePrefs(
316 user_prefs::PrefRegistrySyncable* registry) {
317 // This preference will only be written by the policy service, which will fill
318 // it according to a value stored in the User Policy.
319 registry->RegisterStringPref(
320 prefs::kVariationsRestrictParameter,
321 std::string(),
322 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
323 }
324
325 // static
306 VariationsService* VariationsService::Create(PrefService* local_state) { 326 VariationsService* VariationsService::Create(PrefService* local_state) {
307 #if !defined(GOOGLE_CHROME_BUILD) 327 #if !defined(GOOGLE_CHROME_BUILD)
308 // Unless the URL was provided, unsupported builds should return NULL to 328 // Unless the URL was provided, unsupported builds should return NULL to
309 // indicate that the service should not be used. 329 // indicate that the service should not be used.
310 if (!CommandLine::ForCurrentProcess()->HasSwitch( 330 if (!CommandLine::ForCurrentProcess()->HasSwitch(
311 switches::kVariationsServerURL)) { 331 switches::kVariationsServerURL)) {
312 DVLOG(1) << "Not creating VariationsService in unofficial build without --" 332 DVLOG(1) << "Not creating VariationsService in unofficial build without --"
313 << switches::kVariationsServerURL << " specified."; 333 << switches::kVariationsServerURL << " specified.";
314 return NULL; 334 return NULL;
315 } 335 }
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after
448 468
449 void VariationsService::RecordLastFetchTime() { 469 void VariationsService::RecordLastFetchTime() {
450 // local_state_ is NULL in tests, so check it first. 470 // local_state_ is NULL in tests, so check it first.
451 if (local_state_) { 471 if (local_state_) {
452 local_state_->SetInt64(prefs::kVariationsLastFetchTime, 472 local_state_->SetInt64(prefs::kVariationsLastFetchTime,
453 base::Time::Now().ToInternalValue()); 473 base::Time::Now().ToInternalValue());
454 } 474 }
455 } 475 }
456 476
457 } // namespace chrome_variations 477 } // namespace chrome_variations
OLDNEW
« no previous file with comments | « chrome/browser/metrics/variations/variations_service.h ('k') | chrome/browser/prefs/browser_prefs.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698