OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/metrics_services_manager.h" | 5 #include "chrome/browser/metrics/metrics_services_manager.h" |
6 | 6 |
| 7 #include <string> |
| 8 |
7 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/guid.h" |
| 11 #include "base/logging.h" |
8 #include "base/prefs/pref_service.h" | 12 #include "base/prefs/pref_service.h" |
9 #include "chrome/browser/metrics/chrome_metrics_service_client.h" | 13 #include "chrome/browser/metrics/chrome_metrics_service_client.h" |
10 #include "chrome/browser/metrics/variations/variations_service.h" | 14 #include "chrome/browser/metrics/variations/variations_service.h" |
11 #include "chrome/common/chrome_switches.h" | 15 #include "chrome/common/chrome_switches.h" |
12 #include "chrome/common/pref_names.h" | 16 #include "chrome/common/pref_names.h" |
| 17 #include "chrome/installer/util/google_update_settings.h" |
13 #include "components/metrics/metrics_service.h" | 18 #include "components/metrics/metrics_service.h" |
14 #include "components/metrics/metrics_state_manager.h" | 19 #include "components/metrics/metrics_state_manager.h" |
15 #include "components/rappor/rappor_service.h" | 20 #include "components/rappor/rappor_service.h" |
16 | 21 |
17 #if defined(OS_CHROMEOS) | 22 #if defined(OS_CHROMEOS) |
18 #include "chrome/browser/chromeos/settings/cros_settings.h" | 23 #include "chrome/browser/chromeos/settings/cros_settings.h" |
19 #endif | 24 #endif |
20 | 25 |
| 26 namespace { |
| 27 |
| 28 // Returns the client info returned by |
| 29 // GoogleUpdateSettings::LoadMetricsClientInfo. Also migrates old client id |
| 30 // formats to the new one. |
| 31 scoped_ptr<metrics::ClientInfo> LoadMetricsClientInfo() { |
| 32 scoped_ptr<metrics::ClientInfo> client_info = |
| 33 GoogleUpdateSettings::LoadMetricsClientInfo(); |
| 34 |
| 35 // Prior to 2014-07, the client ID was stripped of its dashes before being |
| 36 // saved. Migrate back to a proper GUID if this is the case. This migration |
| 37 // code below can be removed in M41+. |
| 38 const size_t kGUIDLengthWithoutDashes = 32U; |
| 39 if (client_info && |
| 40 client_info->client_id.length() == kGUIDLengthWithoutDashes) { |
| 41 DCHECK(client_info->client_id.find('-') == std::string::npos); |
| 42 |
| 43 std::string client_id_with_dashes; |
| 44 client_id_with_dashes.reserve(kGUIDLengthWithoutDashes + 4U); |
| 45 std::string::const_iterator client_id_it = client_info->client_id.begin(); |
| 46 for (size_t i = 0; i < kGUIDLengthWithoutDashes + 4U; ++i) { |
| 47 if (i == 8U || i == 13U || i == 18U || i == 23U) { |
| 48 client_id_with_dashes.push_back('-'); |
| 49 } else { |
| 50 client_id_with_dashes.push_back(*client_id_it); |
| 51 ++client_id_it; |
| 52 } |
| 53 } |
| 54 DCHECK(client_id_it == client_info->client_id.end()); |
| 55 client_info->client_id.assign(client_id_with_dashes); |
| 56 } |
| 57 |
| 58 // The GUID retrieved (and possibly fixed above) should be valid unless |
| 59 // retrieval failed. |
| 60 DCHECK(!client_info || base::IsValidGUID(client_info->client_id)); |
| 61 |
| 62 return client_info.Pass(); |
| 63 } |
| 64 |
| 65 } // namespace |
| 66 |
21 MetricsServicesManager::MetricsServicesManager(PrefService* local_state) | 67 MetricsServicesManager::MetricsServicesManager(PrefService* local_state) |
22 : local_state_(local_state) { | 68 : local_state_(local_state) { |
23 DCHECK(local_state); | 69 DCHECK(local_state); |
24 } | 70 } |
25 | 71 |
26 MetricsServicesManager::~MetricsServicesManager() { | 72 MetricsServicesManager::~MetricsServicesManager() { |
27 } | 73 } |
28 | 74 |
29 MetricsService* MetricsServicesManager::GetMetricsService() { | 75 MetricsService* MetricsServicesManager::GetMetricsService() { |
30 DCHECK(thread_checker_.CalledOnValidThread()); | 76 DCHECK(thread_checker_.CalledOnValidThread()); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
63 } | 109 } |
64 return metrics_service_client_.get(); | 110 return metrics_service_client_.get(); |
65 } | 111 } |
66 | 112 |
67 metrics::MetricsStateManager* MetricsServicesManager::GetMetricsStateManager() { | 113 metrics::MetricsStateManager* MetricsServicesManager::GetMetricsStateManager() { |
68 DCHECK(thread_checker_.CalledOnValidThread()); | 114 DCHECK(thread_checker_.CalledOnValidThread()); |
69 if (!metrics_state_manager_) { | 115 if (!metrics_state_manager_) { |
70 metrics_state_manager_ = metrics::MetricsStateManager::Create( | 116 metrics_state_manager_ = metrics::MetricsStateManager::Create( |
71 local_state_, | 117 local_state_, |
72 base::Bind(&MetricsServicesManager::IsMetricsReportingEnabled, | 118 base::Bind(&MetricsServicesManager::IsMetricsReportingEnabled, |
73 base::Unretained(this))); | 119 base::Unretained(this)), |
| 120 base::Bind(&GoogleUpdateSettings::StoreMetricsClientInfo), |
| 121 base::Bind(&LoadMetricsClientInfo)); |
74 } | 122 } |
75 return metrics_state_manager_.get(); | 123 return metrics_state_manager_.get(); |
76 } | 124 } |
77 | 125 |
78 // TODO(asvitkine): This function does not report the correct value on Android, | 126 // TODO(asvitkine): This function does not report the correct value on Android, |
79 // see http://crbug.com/362192. | 127 // see http://crbug.com/362192. |
80 bool MetricsServicesManager::IsMetricsReportingEnabled() const { | 128 bool MetricsServicesManager::IsMetricsReportingEnabled() const { |
81 // If the user permits metrics reporting with the checkbox in the | 129 // If the user permits metrics reporting with the checkbox in the |
82 // prefs, we turn on recording. We disable metrics completely for | 130 // prefs, we turn on recording. We disable metrics completely for |
83 // non-official builds, or when field trials are forced. | 131 // non-official builds, or when field trials are forced. |
84 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kForceFieldTrials)) | 132 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kForceFieldTrials)) |
85 return false; | 133 return false; |
86 | 134 |
87 bool enabled = false; | 135 bool enabled = false; |
88 #if defined(GOOGLE_CHROME_BUILD) | 136 #if defined(GOOGLE_CHROME_BUILD) |
89 #if defined(OS_CHROMEOS) | 137 #if defined(OS_CHROMEOS) |
90 chromeos::CrosSettings::Get()->GetBoolean(chromeos::kStatsReportingPref, | 138 chromeos::CrosSettings::Get()->GetBoolean(chromeos::kStatsReportingPref, |
91 &enabled); | 139 &enabled); |
92 #else | 140 #else |
93 enabled = local_state_->GetBoolean(prefs::kMetricsReportingEnabled); | 141 enabled = local_state_->GetBoolean(prefs::kMetricsReportingEnabled); |
94 #endif // #if defined(OS_CHROMEOS) | 142 #endif // #if defined(OS_CHROMEOS) |
95 #endif // defined(GOOGLE_CHROME_BUILD) | 143 #endif // defined(GOOGLE_CHROME_BUILD) |
96 return enabled; | 144 return enabled; |
97 } | 145 } |
OLD | NEW |