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

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

Issue 1309843005: Remove more //chrome dependencies from VariationsService (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@variations_service_get_channel
Patch Set: Created 5 years, 3 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
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 "base/build_time.h" 7 #include "base/build_time.h"
8 #include "base/command_line.h" 8 #include "base/command_line.h"
9 #include "base/metrics/histogram.h" 9 #include "base/metrics/histogram.h"
10 #include "base/metrics/sparse_histogram.h" 10 #include "base/metrics/sparse_histogram.h"
11 #include "base/prefs/pref_registry_simple.h" 11 #include "base/prefs/pref_registry_simple.h"
12 #include "base/prefs/pref_service.h" 12 #include "base/prefs/pref_service.h"
13 #include "base/sys_info.h" 13 #include "base/sys_info.h"
14 #include "base/task_runner_util.h" 14 #include "base/task_runner_util.h"
15 #include "base/timer/elapsed_timer.h" 15 #include "base/timer/elapsed_timer.h"
16 #include "base/values.h" 16 #include "base/values.h"
17 #include "base/version.h" 17 #include "base/version.h"
18 #include "chrome/browser/metrics/variations/generated_resources_map.h"
19 #include "chrome/common/chrome_switches.h"
20 #include "components/metrics/metrics_state_manager.h" 18 #include "components/metrics/metrics_state_manager.h"
21 #include "components/network_time/network_time_tracker.h" 19 #include "components/network_time/network_time_tracker.h"
22 #include "components/pref_registry/pref_registry_syncable.h" 20 #include "components/pref_registry/pref_registry_syncable.h"
23 #include "components/variations/pref_names.h" 21 #include "components/variations/pref_names.h"
24 #include "components/variations/proto/variations_seed.pb.h" 22 #include "components/variations/proto/variations_seed.pb.h"
25 #include "components/variations/variations_seed_processor.h" 23 #include "components/variations/variations_seed_processor.h"
26 #include "components/variations/variations_seed_simulator.h" 24 #include "components/variations/variations_seed_simulator.h"
25 #include "components/variations/variations_switches.h"
27 #include "components/variations/variations_url_constants.h" 26 #include "components/variations/variations_url_constants.h"
28 #include "components/version_info/version_info.h" 27 #include "components/version_info/version_info.h"
29 #include "net/base/load_flags.h" 28 #include "net/base/load_flags.h"
30 #include "net/base/net_errors.h" 29 #include "net/base/net_errors.h"
31 #include "net/base/network_change_notifier.h" 30 #include "net/base/network_change_notifier.h"
32 #include "net/base/url_util.h" 31 #include "net/base/url_util.h"
33 #include "net/http/http_response_headers.h" 32 #include "net/http/http_response_headers.h"
34 #include "net/http/http_status_code.h" 33 #include "net/http/http_status_code.h"
35 #include "net/http/http_util.h" 34 #include "net/http/http_util.h"
36 #include "net/url_request/url_fetcher.h" 35 #include "net/url_request/url_fetcher.h"
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after
203 // set. 202 // set.
204 std::string GetHeaderValue(const net::HttpResponseHeaders* headers, 203 std::string GetHeaderValue(const net::HttpResponseHeaders* headers,
205 const base::StringPiece& name) { 204 const base::StringPiece& name) {
206 std::string value; 205 std::string value;
207 headers->EnumerateHeader(NULL, name, &value); 206 headers->EnumerateHeader(NULL, name, &value);
208 return value; 207 return value;
209 } 208 }
210 209
211 // Overrides the string resource sepecified by |hash| with |string| in the 210 // Overrides the string resource sepecified by |hash| with |string| in the
212 // resource bundle. Used as a callback passed to the variations seed processor. 211 // resource bundle. Used as a callback passed to the variations seed processor.
213 void OverrideUIString(uint32_t hash, const base::string16& string) { 212 void OverrideUIString(VariationsServiceClient* client,
Alexei Svitkine (slow) 2015/08/27 18:27:10 Why not have the whole OverrideUIString() function
blundell 2015/08/27 18:33:01 In the interest of sharing as much code as possibl
Alexei Svitkine (slow) 2015/08/27 18:44:00 I see. I'm thinking that if variations/service mig
blundell 2015/08/27 19:05:58 Done. Ideally this wouldn't have to be componentiz
214 int resource_id = GetResourceIndex(hash); 213 uint32_t hash,
214 const base::string16& string) {
215 int resource_id = client->GetIndexForResource(hash);
215 if (resource_id == -1) 216 if (resource_id == -1)
216 return; 217 return;
217 218
218 ui::ResourceBundle::GetSharedInstance().OverrideLocaleStringResource( 219 ui::ResourceBundle::GetSharedInstance().OverrideLocaleStringResource(
219 resource_id, string); 220 resource_id, string);
220 } 221 }
221 222
222 } // namespace 223 } // namespace
223 224
224 VariationsService::VariationsService( 225 VariationsService::VariationsService(
(...skipping 30 matching lines...) Expand all
255 const base::Version current_version(version_info::GetVersionNumber()); 256 const base::Version current_version(version_info::GetVersionNumber());
256 if (!current_version.IsValid()) 257 if (!current_version.IsValid())
257 return false; 258 return false;
258 259
259 variations::Study_Channel channel = 260 variations::Study_Channel channel =
260 GetChannelForVariations(client_->GetChannel()); 261 GetChannelForVariations(client_->GetChannel());
261 UMA_HISTOGRAM_SPARSE_SLOWLY("Variations.UserChannel", channel); 262 UMA_HISTOGRAM_SPARSE_SLOWLY("Variations.UserChannel", channel);
262 263
263 const std::string latest_country = 264 const std::string latest_country =
264 local_state_->GetString(prefs::kVariationsCountry); 265 local_state_->GetString(prefs::kVariationsCountry);
266 // Note that passing |client_| via base::Unretained below is safe because
267 // the callback is executed synchronously.
265 variations::VariationsSeedProcessor().CreateTrialsFromSeed( 268 variations::VariationsSeedProcessor().CreateTrialsFromSeed(
266 seed, client_->GetApplicationLocale(), 269 seed, client_->GetApplicationLocale(),
267 GetReferenceDateForExpiryChecks(local_state_), current_version, channel, 270 GetReferenceDateForExpiryChecks(local_state_), current_version, channel,
268 GetCurrentFormFactor(), GetHardwareClass(), latest_country, 271 GetCurrentFormFactor(), GetHardwareClass(), latest_country,
269 LoadPermanentConsistencyCountry(current_version, latest_country), 272 LoadPermanentConsistencyCountry(current_version, latest_country),
270 base::Bind(&OverrideUIString)); 273 base::Bind(&OverrideUIString, base::Unretained(client_.get())));
271 274
272 const base::Time now = base::Time::Now(); 275 const base::Time now = base::Time::Now();
273 276
274 // Log the "freshness" of the seed that was just used. The freshness is the 277 // Log the "freshness" of the seed that was just used. The freshness is the
275 // time between the last successful seed download and now. 278 // time between the last successful seed download and now.
276 const int64 last_fetch_time_internal = 279 const int64 last_fetch_time_internal =
277 local_state_->GetInt64(prefs::kVariationsLastFetchTime); 280 local_state_->GetInt64(prefs::kVariationsLastFetchTime);
278 if (last_fetch_time_internal) { 281 if (last_fetch_time_internal) {
279 const base::TimeDelta delta = 282 const base::TimeDelta delta =
280 now - base::Time::FromInternalValue(last_fetch_time_internal); 283 now - base::Time::FromInternalValue(last_fetch_time_internal);
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after
425 // This preference will only be written by the policy service, which will fill 428 // This preference will only be written by the policy service, which will fill
426 // it according to a value stored in the User Policy. 429 // it according to a value stored in the User Policy.
427 registry->RegisterStringPref(prefs::kVariationsRestrictParameter, 430 registry->RegisterStringPref(prefs::kVariationsRestrictParameter,
428 std::string()); 431 std::string());
429 } 432 }
430 433
431 // static 434 // static
432 scoped_ptr<VariationsService> VariationsService::Create( 435 scoped_ptr<VariationsService> VariationsService::Create(
433 scoped_ptr<VariationsServiceClient> client, 436 scoped_ptr<VariationsServiceClient> client,
434 PrefService* local_state, 437 PrefService* local_state,
435 metrics::MetricsStateManager* state_manager) { 438 metrics::MetricsStateManager* state_manager,
439 const char* disable_network_switch) {
436 scoped_ptr<VariationsService> result; 440 scoped_ptr<VariationsService> result;
437 #if !defined(GOOGLE_CHROME_BUILD) 441 #if !defined(GOOGLE_CHROME_BUILD)
438 // Unless the URL was provided, unsupported builds should return NULL to 442 // Unless the URL was provided, unsupported builds should return NULL to
439 // indicate that the service should not be used. 443 // indicate that the service should not be used.
440 if (!base::CommandLine::ForCurrentProcess()->HasSwitch( 444 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
441 switches::kVariationsServerURL)) { 445 switches::kVariationsServerURL)) {
442 DVLOG(1) << "Not creating VariationsService in unofficial build without --" 446 DVLOG(1) << "Not creating VariationsService in unofficial build without --"
443 << switches::kVariationsServerURL << " specified."; 447 << switches::kVariationsServerURL << " specified.";
444 return result.Pass(); 448 return result.Pass();
445 } 449 }
446 #endif 450 #endif
447 result.reset(new VariationsService( 451 result.reset(new VariationsService(
448 client.Pass(), new web_resource::ResourceRequestAllowedNotifier( 452 client.Pass(), new web_resource::ResourceRequestAllowedNotifier(
449 local_state, switches::kDisableBackgroundNetworking), 453 local_state, disable_network_switch),
450 local_state, state_manager)); 454 local_state, state_manager));
451 return result.Pass(); 455 return result.Pass();
452 } 456 }
453 457
454 void VariationsService::DoActualFetch() { 458 void VariationsService::DoActualFetch() {
455 DCHECK(thread_checker_.CalledOnValidThread()); 459 DCHECK(thread_checker_.CalledOnValidThread());
456 DCHECK(!pending_seed_request_); 460 DCHECK(!pending_seed_request_);
457 461
458 pending_seed_request_ = net::URLFetcher::Create(0, variations_server_url_, 462 pending_seed_request_ = net::URLFetcher::Create(0, variations_server_url_,
459 net::URLFetcher::GET, this); 463 net::URLFetcher::GET, this);
(...skipping 297 matching lines...) Expand 10 before | Expand all | Expand 10 after
757 // Otherwise, update the pref with the current Chrome version and country. 761 // Otherwise, update the pref with the current Chrome version and country.
758 base::ListValue new_list_value; 762 base::ListValue new_list_value;
759 new_list_value.AppendString(version.GetString()); 763 new_list_value.AppendString(version.GetString());
760 new_list_value.AppendString(latest_country); 764 new_list_value.AppendString(latest_country);
761 local_state_->Set(prefs::kVariationsPermanentConsistencyCountry, 765 local_state_->Set(prefs::kVariationsPermanentConsistencyCountry,
762 new_list_value); 766 new_list_value);
763 return latest_country; 767 return latest_country;
764 } 768 }
765 769
766 } // namespace chrome_variations 770 } // namespace chrome_variations
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698