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 // Attempts to retrieve |client_id| and |installation_date| backed up in | |
29 // GoogleUpdateSettings. Returns true on success retrieving at least the | |
30 // |client_id|. | |
31 bool RetrieveClientInfo(std::string* client_id, int64* installation_date) { | |
32 DCHECK(client_id); | |
33 DCHECK(installation_date); | |
34 bool result = | |
35 GoogleUpdateSettings::RetrieveMetricsInfo(client_id, installation_date); | |
36 | |
37 // Prior to 2014-07, the client ID was stripped of its dashes before being | |
38 // saved, migrate back to a proper GUID if this is the case. This migration | |
Ilya Sherman
2014/07/08 01:10:21
nit: "saved, migrate" -> "saved. Migrate"
gab
2014/07/09 19:16:59
Done.
| |
39 // code below can be removed in M41+. | |
40 const size_t kGUIDLengthWithoutDashes = 32U; | |
41 if (result && client_id->length() == kGUIDLengthWithoutDashes) { | |
42 DCHECK(client_id->find('-') == std::string::npos); | |
43 | |
44 std::string client_id_with_dashes; | |
45 client_id_with_dashes.reserve(kGUIDLengthWithoutDashes + 4U); | |
46 std::string::const_iterator client_id_it = client_id->begin(); | |
47 for (size_t i = 0; i < kGUIDLengthWithoutDashes + 4U; ++i) { | |
48 if (i == 8U || i == 13U || i == 18U || i == 23U) { | |
49 client_id_with_dashes.push_back('-'); | |
50 } else { | |
51 client_id_with_dashes.push_back(*(client_id_it++)); | |
Ilya Sherman
2014/07/08 01:10:21
nit: I'd prefer that you separate out reading from
gab
2014/07/09 19:16:59
Done.
| |
52 } | |
53 } | |
54 DCHECK(client_id_it == client_id->end()); | |
55 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(base::IsValidGUID(*client_id) || !result); | |
61 | |
62 return result; | |
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::SaveMetricsInfo), | |
121 base::Bind(&RetrieveClientInfo)); | |
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 |