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> | 7 #include <string> |
8 | 8 |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/prefs/pref_service.h" | 11 #include "base/prefs/pref_service.h" |
12 #include "chrome/browser/browser_process.h" | 12 #include "chrome/browser/browser_process.h" |
13 #include "chrome/browser/metrics/chrome_metrics_service_client.h" | 13 #include "chrome/browser/metrics/chrome_metrics_service_client.h" |
14 #include "chrome/browser/metrics/variations/variations_service.h" | 14 #include "chrome/browser/metrics/variations/variations_service.h" |
15 #include "chrome/browser/profiles/profile.h" | |
16 #include "chrome/browser/profiles/profile_manager.h" | |
15 #include "chrome/browser/ui/browser_otr_state.h" | 17 #include "chrome/browser/ui/browser_otr_state.h" |
16 #include "chrome/common/chrome_switches.h" | 18 #include "chrome/common/chrome_switches.h" |
17 #include "chrome/common/pref_names.h" | 19 #include "chrome/common/pref_names.h" |
18 #include "chrome/installer/util/google_update_settings.h" | 20 #include "chrome/installer/util/google_update_settings.h" |
19 #include "components/metrics/metrics_service.h" | 21 #include "components/metrics/metrics_service.h" |
20 #include "components/metrics/metrics_state_manager.h" | 22 #include "components/metrics/metrics_state_manager.h" |
23 #include "components/rappor/rappor_pref_names.h" | |
21 #include "components/rappor/rappor_service.h" | 24 #include "components/rappor/rappor_service.h" |
22 #include "content/public/browser/browser_thread.h" | 25 #include "content/public/browser/browser_thread.h" |
23 | 26 |
24 #if defined(OS_CHROMEOS) | 27 #if defined(OS_CHROMEOS) |
25 #include "chrome/browser/chromeos/settings/cros_settings.h" | 28 #include "chrome/browser/chromeos/settings/cros_settings.h" |
26 #endif | 29 #endif |
27 | 30 |
31 namespace { | |
32 | |
33 // Check if the safe browsing setting is enabled for all existing profiles. | |
34 bool CheckSafeBrowsingSettings() { | |
35 bool all_enabled = true; | |
36 ProfileManager* profile_manager = g_browser_process->profile_manager(); | |
37 if (profile_manager) { | |
38 std::vector<Profile*> profiles = profile_manager->GetLoadedProfiles(); | |
39 for (size_t i = 0; i < profiles.size(); ++i) { | |
Thiemo Nagel
2015/02/11 10:50:51
Nit: Suggest to use C++11 range-based loop.
Steven Holte
2015/02/11 20:03:32
Done.
| |
40 if (profiles[i]->IsOffTheRecord()) | |
41 continue; | |
42 all_enabled = all_enabled && profiles[i]->GetPrefs()->GetBoolean( | |
43 prefs::kSafeBrowsingEnabled); | |
44 } | |
45 } | |
46 return all_enabled; | |
47 } | |
48 | |
28 // Posts |GoogleUpdateSettings::StoreMetricsClientInfo| on blocking pool thread | 49 // Posts |GoogleUpdateSettings::StoreMetricsClientInfo| on blocking pool thread |
29 // because it needs access to IO and cannot work from UI thread. | 50 // because it needs access to IO and cannot work from UI thread. |
30 void PostStoreMetricsClientInfo(const metrics::ClientInfo& client_info) { | 51 void PostStoreMetricsClientInfo(const metrics::ClientInfo& client_info) { |
31 content::BrowserThread::GetBlockingPool()->PostTask(FROM_HERE, | 52 content::BrowserThread::GetBlockingPool()->PostTask(FROM_HERE, |
32 base::Bind(&GoogleUpdateSettings::StoreMetricsClientInfo, client_info)); | 53 base::Bind(&GoogleUpdateSettings::StoreMetricsClientInfo, client_info)); |
33 } | 54 } |
34 | 55 |
56 } // namespace | |
57 | |
35 MetricsServicesManager::MetricsServicesManager(PrefService* local_state) | 58 MetricsServicesManager::MetricsServicesManager(PrefService* local_state) |
36 : local_state_(local_state) { | 59 : local_state_(local_state) { |
37 DCHECK(local_state); | 60 DCHECK(local_state); |
38 } | 61 } |
39 | 62 |
40 MetricsServicesManager::~MetricsServicesManager() { | 63 MetricsServicesManager::~MetricsServicesManager() { |
41 } | 64 } |
42 | 65 |
43 metrics::MetricsService* MetricsServicesManager::GetMetricsService() { | 66 metrics::MetricsService* MetricsServicesManager::GetMetricsService() { |
44 DCHECK(thread_checker_.CalledOnValidThread()); | 67 DCHECK(thread_checker_.CalledOnValidThread()); |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
87 metrics_state_manager_ = metrics::MetricsStateManager::Create( | 110 metrics_state_manager_ = metrics::MetricsStateManager::Create( |
88 local_state_, | 111 local_state_, |
89 base::Bind(&MetricsServicesManager::IsMetricsReportingEnabled, | 112 base::Bind(&MetricsServicesManager::IsMetricsReportingEnabled, |
90 base::Unretained(this)), | 113 base::Unretained(this)), |
91 base::Bind(&PostStoreMetricsClientInfo), | 114 base::Bind(&PostStoreMetricsClientInfo), |
92 base::Bind(&GoogleUpdateSettings::LoadMetricsClientInfo)); | 115 base::Bind(&GoogleUpdateSettings::LoadMetricsClientInfo)); |
93 } | 116 } |
94 return metrics_state_manager_.get(); | 117 return metrics_state_manager_.get(); |
95 } | 118 } |
96 | 119 |
120 bool MetricsServicesManager::IsRapporEnabled(bool metrics_enabled) const { | |
121 if (!local_state_->HasPrefPath(rappor::prefs::kRapporEnabled)) { | |
122 // For upgrading users, derive an initial setting from UMA / safe browsing. | |
123 bool should_enable = metrics_enabled || CheckSafeBrowsingSettings(); | |
124 local_state_->SetBoolean(rappor::prefs::kRapporEnabled, should_enable); | |
125 } | |
126 return local_state_->GetBoolean(rappor::prefs::kRapporEnabled); | |
127 } | |
128 | |
129 rappor::RecordingLevel MetricsServicesManager::GetRapporRecordingLevel( | |
130 bool metrics_enabled) const { | |
131 rappor::RecordingLevel recording_level = rappor::RECORDING_DISABLED; | |
132 #if defined(GOOGLE_CHROME_BUILD) | |
133 // TODO(holte): Remove special casing once the UIs for iOS/Android are updated | |
134 // to allow control of the rappor.enabled pref. | |
135 #if defined(OS_IOS) || defined(OS_ANDROID) | |
136 if (metrics_enabled) { | |
137 recording_level = rappor::FINE_LEVEL; | |
138 } | |
139 #else // defined(OS_IOS) || defined(OS_ANDROID) | |
140 if (IsRapporEnabled(metrics_enabled)) { | |
141 recording_level = metrics_enabled ? | |
142 rappor::FINE_LEVEL : | |
143 rappor::COARSE_LEVEL; | |
144 } | |
145 #endif // defined(OS_IOS) || defined(OS_ANDROID) | |
146 #endif // defined(GOOGLE_CHROME_BUILD) | |
147 return recording_level; | |
148 } | |
149 | |
97 void MetricsServicesManager::UpdatePermissions(bool may_record, | 150 void MetricsServicesManager::UpdatePermissions(bool may_record, |
98 bool may_upload) { | 151 bool may_upload) { |
99 metrics::MetricsService* metrics = GetMetricsService(); | 152 metrics::MetricsService* metrics = GetMetricsService(); |
100 | 153 |
101 const base::CommandLine* cmdline = base::CommandLine::ForCurrentProcess(); | 154 const base::CommandLine* cmdline = base::CommandLine::ForCurrentProcess(); |
102 | 155 |
103 const bool only_do_metrics_recording = | 156 const bool only_do_metrics_recording = |
104 cmdline->HasSwitch(switches::kMetricsRecordingOnly) || | 157 cmdline->HasSwitch(switches::kMetricsRecordingOnly) || |
105 cmdline->HasSwitch(switches::kEnableBenchmarking); | 158 cmdline->HasSwitch(switches::kEnableBenchmarking); |
106 | 159 |
107 if (only_do_metrics_recording) { | 160 if (only_do_metrics_recording) { |
108 metrics->StartRecordingForTests(); | 161 metrics->StartRecordingForTests(); |
109 GetRapporService()->Update(rappor::FINE_LEVEL, false); | 162 GetRapporService()->Update(rappor::FINE_LEVEL, false); |
110 return; | 163 return; |
111 } | 164 } |
112 | 165 |
113 if (may_record) { | 166 if (may_record) { |
114 if (!metrics->recording_active()) | 167 if (!metrics->recording_active()) |
115 metrics->Start(); | 168 metrics->Start(); |
116 | 169 |
117 if (may_upload) | 170 if (may_upload) |
118 metrics->EnableReporting(); | 171 metrics->EnableReporting(); |
119 else | 172 else |
120 metrics->DisableReporting(); | 173 metrics->DisableReporting(); |
121 } else if (metrics->recording_active() || metrics->reporting_active()) { | 174 } else if (metrics->recording_active() || metrics->reporting_active()) { |
122 metrics->Stop(); | 175 metrics->Stop(); |
123 } | 176 } |
124 | 177 |
125 rappor::RecordingLevel recording_level = may_record ? | 178 GetRapporService()->Update(GetRapporRecordingLevel(may_record), may_upload); |
126 rappor::FINE_LEVEL : rappor::RECORDING_DISABLED; | |
127 GetRapporService()->Update(recording_level, may_upload); | |
128 } | 179 } |
129 | 180 |
130 // TODO(asvitkine): This function does not report the correct value on Android, | 181 // TODO(asvitkine): This function does not report the correct value on Android, |
131 // see http://crbug.com/362192. | 182 // see http://crbug.com/362192. |
132 bool MetricsServicesManager::IsMetricsReportingEnabled() const { | 183 bool MetricsServicesManager::IsMetricsReportingEnabled() const { |
133 // If the user permits metrics reporting with the checkbox in the | 184 // If the user permits metrics reporting with the checkbox in the |
134 // prefs, we turn on recording. We disable metrics completely for | 185 // prefs, we turn on recording. We disable metrics completely for |
135 // non-official builds, or when field trials are forced. | 186 // non-official builds, or when field trials are forced. |
136 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | 187 if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
137 switches::kForceFieldTrials)) | 188 switches::kForceFieldTrials)) |
138 return false; | 189 return false; |
139 | 190 |
140 bool enabled = false; | 191 bool enabled = false; |
141 #if defined(GOOGLE_CHROME_BUILD) | 192 #if defined(GOOGLE_CHROME_BUILD) |
142 #if defined(OS_CHROMEOS) | 193 #if defined(OS_CHROMEOS) |
143 chromeos::CrosSettings::Get()->GetBoolean(chromeos::kStatsReportingPref, | 194 chromeos::CrosSettings::Get()->GetBoolean(chromeos::kStatsReportingPref, |
144 &enabled); | 195 &enabled); |
145 #else | 196 #else |
146 enabled = local_state_->GetBoolean(prefs::kMetricsReportingEnabled); | 197 enabled = local_state_->GetBoolean(prefs::kMetricsReportingEnabled); |
147 #endif // #if defined(OS_CHROMEOS) | 198 #endif // #if defined(OS_CHROMEOS) |
148 #endif // defined(GOOGLE_CHROME_BUILD) | 199 #endif // defined(GOOGLE_CHROME_BUILD) |
149 return enabled; | 200 return enabled; |
150 } | 201 } |
OLD | NEW |