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

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: Response to review 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
« no previous file with comments | « chrome/browser/metrics/variations/variations_service.h ('k') | chrome/common/chrome_switches.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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"
37 #include "net/url_request/url_request_status.h" 36 #include "net/url_request/url_request_status.h"
38 #include "ui/base/device_form_factor.h" 37 #include "ui/base/device_form_factor.h"
39 #include "ui/base/resource/resource_bundle.h"
40 #include "url/gurl.h" 38 #include "url/gurl.h"
41 39
42 #if defined(OS_CHROMEOS) 40 #if defined(OS_CHROMEOS)
43 #include "chrome/browser/chromeos/settings/cros_settings.h" 41 #include "chrome/browser/chromeos/settings/cros_settings.h"
44 #endif 42 #endif
45 43
46 namespace chrome_variations { 44 namespace chrome_variations {
47 45
48 namespace { 46 namespace {
49 47
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after
201 199
202 // Returns the header value for |name| from |headers| or an empty string if not 200 // Returns the header value for |name| from |headers| or an empty string if not
203 // set. 201 // set.
204 std::string GetHeaderValue(const net::HttpResponseHeaders* headers, 202 std::string GetHeaderValue(const net::HttpResponseHeaders* headers,
205 const base::StringPiece& name) { 203 const base::StringPiece& name) {
206 std::string value; 204 std::string value;
207 headers->EnumerateHeader(NULL, name, &value); 205 headers->EnumerateHeader(NULL, name, &value);
208 return value; 206 return value;
209 } 207 }
210 208
211 // Overrides the string resource sepecified by |hash| with |string| in the
212 // resource bundle. Used as a callback passed to the variations seed processor.
213 void OverrideUIString(uint32_t hash, const base::string16& string) {
214 int resource_id = GetResourceIndex(hash);
215 if (resource_id == -1)
216 return;
217
218 ui::ResourceBundle::GetSharedInstance().OverrideLocaleStringResource(
219 resource_id, string);
220 }
221
222 } // namespace 209 } // namespace
223 210
224 VariationsService::VariationsService( 211 VariationsService::VariationsService(
225 scoped_ptr<VariationsServiceClient> client, 212 scoped_ptr<VariationsServiceClient> client,
226 web_resource::ResourceRequestAllowedNotifier* notifier, 213 web_resource::ResourceRequestAllowedNotifier* notifier,
227 PrefService* local_state, 214 PrefService* local_state,
228 metrics::MetricsStateManager* state_manager) 215 metrics::MetricsStateManager* state_manager)
229 : client_(client.Pass()), 216 : client_(client.Pass()),
230 local_state_(local_state), 217 local_state_(local_state),
231 state_manager_(state_manager), 218 state_manager_(state_manager),
(...skipping 23 matching lines...) Expand all
255 const base::Version current_version(version_info::GetVersionNumber()); 242 const base::Version current_version(version_info::GetVersionNumber());
256 if (!current_version.IsValid()) 243 if (!current_version.IsValid())
257 return false; 244 return false;
258 245
259 variations::Study_Channel channel = 246 variations::Study_Channel channel =
260 GetChannelForVariations(client_->GetChannel()); 247 GetChannelForVariations(client_->GetChannel());
261 UMA_HISTOGRAM_SPARSE_SLOWLY("Variations.UserChannel", channel); 248 UMA_HISTOGRAM_SPARSE_SLOWLY("Variations.UserChannel", channel);
262 249
263 const std::string latest_country = 250 const std::string latest_country =
264 local_state_->GetString(prefs::kVariationsCountry); 251 local_state_->GetString(prefs::kVariationsCountry);
252 // Note that passing |client_| via base::Unretained below is safe because
253 // the callback is executed synchronously.
265 variations::VariationsSeedProcessor().CreateTrialsFromSeed( 254 variations::VariationsSeedProcessor().CreateTrialsFromSeed(
266 seed, client_->GetApplicationLocale(), 255 seed, client_->GetApplicationLocale(),
267 GetReferenceDateForExpiryChecks(local_state_), current_version, channel, 256 GetReferenceDateForExpiryChecks(local_state_), current_version, channel,
268 GetCurrentFormFactor(), GetHardwareClass(), latest_country, 257 GetCurrentFormFactor(), GetHardwareClass(), latest_country,
269 LoadPermanentConsistencyCountry(current_version, latest_country), 258 LoadPermanentConsistencyCountry(current_version, latest_country),
270 base::Bind(&OverrideUIString)); 259 base::Bind(&VariationsServiceClient::OverrideUIString,
260 base::Unretained(client_.get())));
271 261
272 const base::Time now = base::Time::Now(); 262 const base::Time now = base::Time::Now();
273 263
274 // Log the "freshness" of the seed that was just used. The freshness is the 264 // Log the "freshness" of the seed that was just used. The freshness is the
275 // time between the last successful seed download and now. 265 // time between the last successful seed download and now.
276 const int64 last_fetch_time_internal = 266 const int64 last_fetch_time_internal =
277 local_state_->GetInt64(prefs::kVariationsLastFetchTime); 267 local_state_->GetInt64(prefs::kVariationsLastFetchTime);
278 if (last_fetch_time_internal) { 268 if (last_fetch_time_internal) {
279 const base::TimeDelta delta = 269 const base::TimeDelta delta =
280 now - base::Time::FromInternalValue(last_fetch_time_internal); 270 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 415 // This preference will only be written by the policy service, which will fill
426 // it according to a value stored in the User Policy. 416 // it according to a value stored in the User Policy.
427 registry->RegisterStringPref(prefs::kVariationsRestrictParameter, 417 registry->RegisterStringPref(prefs::kVariationsRestrictParameter,
428 std::string()); 418 std::string());
429 } 419 }
430 420
431 // static 421 // static
432 scoped_ptr<VariationsService> VariationsService::Create( 422 scoped_ptr<VariationsService> VariationsService::Create(
433 scoped_ptr<VariationsServiceClient> client, 423 scoped_ptr<VariationsServiceClient> client,
434 PrefService* local_state, 424 PrefService* local_state,
435 metrics::MetricsStateManager* state_manager) { 425 metrics::MetricsStateManager* state_manager,
426 const char* disable_network_switch) {
436 scoped_ptr<VariationsService> result; 427 scoped_ptr<VariationsService> result;
437 #if !defined(GOOGLE_CHROME_BUILD) 428 #if !defined(GOOGLE_CHROME_BUILD)
438 // Unless the URL was provided, unsupported builds should return NULL to 429 // Unless the URL was provided, unsupported builds should return NULL to
439 // indicate that the service should not be used. 430 // indicate that the service should not be used.
440 if (!base::CommandLine::ForCurrentProcess()->HasSwitch( 431 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
441 switches::kVariationsServerURL)) { 432 switches::kVariationsServerURL)) {
442 DVLOG(1) << "Not creating VariationsService in unofficial build without --" 433 DVLOG(1) << "Not creating VariationsService in unofficial build without --"
443 << switches::kVariationsServerURL << " specified."; 434 << switches::kVariationsServerURL << " specified.";
444 return result.Pass(); 435 return result.Pass();
445 } 436 }
446 #endif 437 #endif
447 result.reset(new VariationsService( 438 result.reset(new VariationsService(
448 client.Pass(), new web_resource::ResourceRequestAllowedNotifier( 439 client.Pass(), new web_resource::ResourceRequestAllowedNotifier(
449 local_state, switches::kDisableBackgroundNetworking), 440 local_state, disable_network_switch),
450 local_state, state_manager)); 441 local_state, state_manager));
451 return result.Pass(); 442 return result.Pass();
452 } 443 }
453 444
454 void VariationsService::DoActualFetch() { 445 void VariationsService::DoActualFetch() {
455 DCHECK(thread_checker_.CalledOnValidThread()); 446 DCHECK(thread_checker_.CalledOnValidThread());
456 DCHECK(!pending_seed_request_); 447 DCHECK(!pending_seed_request_);
457 448
458 pending_seed_request_ = net::URLFetcher::Create(0, variations_server_url_, 449 pending_seed_request_ = net::URLFetcher::Create(0, variations_server_url_,
459 net::URLFetcher::GET, this); 450 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. 748 // Otherwise, update the pref with the current Chrome version and country.
758 base::ListValue new_list_value; 749 base::ListValue new_list_value;
759 new_list_value.AppendString(version.GetString()); 750 new_list_value.AppendString(version.GetString());
760 new_list_value.AppendString(latest_country); 751 new_list_value.AppendString(latest_country);
761 local_state_->Set(prefs::kVariationsPermanentConsistencyCountry, 752 local_state_->Set(prefs::kVariationsPermanentConsistencyCountry,
762 new_list_value); 753 new_list_value);
763 return latest_country; 754 return latest_country;
764 } 755 }
765 756
766 } // namespace chrome_variations 757 } // namespace chrome_variations
OLDNEW
« no previous file with comments | « chrome/browser/metrics/variations/variations_service.h ('k') | chrome/common/chrome_switches.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698