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 | 25 |
23 #if defined(OS_CHROMEOS) | 26 #if defined(OS_CHROMEOS) |
24 #include "chrome/browser/chromeos/settings/cros_settings.h" | 27 #include "chrome/browser/chromeos/settings/cros_settings.h" |
25 #endif | 28 #endif |
26 | 29 |
| 30 namespace { |
| 31 |
| 32 // Check if the safe browsing setting is enabled for all existing profiles. |
| 33 bool CheckSafeBrowsingSettings() { |
| 34 bool all_enabled = true; |
| 35 ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 36 if (profile_manager) { |
| 37 std::vector<Profile*> profiles = profile_manager->GetLoadedProfiles(); |
| 38 for (size_t i = 0; i < profiles.size(); ++i) { |
| 39 if (profiles[i]->IsOffTheRecord()) |
| 40 continue; |
| 41 all_enabled = all_enabled && profiles[i]->GetPrefs()->GetBoolean( |
| 42 prefs::kSafeBrowsingEnabled); |
| 43 } |
| 44 } |
| 45 return all_enabled; |
| 46 } |
| 47 |
| 48 } // namespace |
| 49 |
27 MetricsServicesManager::MetricsServicesManager(PrefService* local_state) | 50 MetricsServicesManager::MetricsServicesManager(PrefService* local_state) |
28 : local_state_(local_state) { | 51 : local_state_(local_state) { |
29 DCHECK(local_state); | 52 DCHECK(local_state); |
30 } | 53 } |
31 | 54 |
32 MetricsServicesManager::~MetricsServicesManager() { | 55 MetricsServicesManager::~MetricsServicesManager() { |
33 } | 56 } |
34 | 57 |
35 metrics::MetricsService* MetricsServicesManager::GetMetricsService() { | 58 metrics::MetricsService* MetricsServicesManager::GetMetricsService() { |
36 DCHECK(thread_checker_.CalledOnValidThread()); | 59 DCHECK(thread_checker_.CalledOnValidThread()); |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
79 metrics_state_manager_ = metrics::MetricsStateManager::Create( | 102 metrics_state_manager_ = metrics::MetricsStateManager::Create( |
80 local_state_, | 103 local_state_, |
81 base::Bind(&MetricsServicesManager::IsMetricsReportingEnabled, | 104 base::Bind(&MetricsServicesManager::IsMetricsReportingEnabled, |
82 base::Unretained(this)), | 105 base::Unretained(this)), |
83 base::Bind(&GoogleUpdateSettings::StoreMetricsClientInfo), | 106 base::Bind(&GoogleUpdateSettings::StoreMetricsClientInfo), |
84 base::Bind(&GoogleUpdateSettings::LoadMetricsClientInfo)); | 107 base::Bind(&GoogleUpdateSettings::LoadMetricsClientInfo)); |
85 } | 108 } |
86 return metrics_state_manager_.get(); | 109 return metrics_state_manager_.get(); |
87 } | 110 } |
88 | 111 |
| 112 bool MetricsServicesManager::IsRapporEnabled(bool metrics_enabled) const { |
| 113 if (!local_state_->HasPrefPath(rappor::prefs::kRapporEnabled)) { |
| 114 // For upgrading users, derive an initial setting from UMA / safe browsing. |
| 115 bool should_enable = metrics_enabled || CheckSafeBrowsingSettings(); |
| 116 local_state_->SetBoolean(rappor::prefs::kRapporEnabled, should_enable); |
| 117 } |
| 118 return local_state_->GetBoolean(rappor::prefs::kRapporEnabled); |
| 119 } |
| 120 |
| 121 rappor::RecordingLevel MetricsServicesManager::GetRapporRecordingLevel( |
| 122 bool metrics_enabled) const { |
| 123 rappor::RecordingLevel recording_level = rappor::RECORDING_DISABLED; |
| 124 #if defined(GOOGLE_CHROME_BUILD) |
| 125 // TODO(holte): Remove special casing once the UIs for iOS/Android are updated |
| 126 // to allow control of the rappor.enabled pref. |
| 127 #if defined(OS_IOS) || defined(OS_ANDROID) |
| 128 if (metrics_enabled) { |
| 129 recording_level = rappor::FINE_LEVEL; |
| 130 } |
| 131 #else // defined(OS_IOS) || defined(OS_ANDROID) |
| 132 if (IsRapporEnabled(metrics_enabled)) { |
| 133 recording_level = metrics_enabled ? |
| 134 rappor::FINE_LEVEL : |
| 135 rappor::COARSE_LEVEL; |
| 136 } |
| 137 #endif // defined(OS_IOS) || defined(OS_ANDROID) |
| 138 #endif // defined(GOOGLE_CHROME_BUILD) |
| 139 return recording_level; |
| 140 } |
| 141 |
89 void MetricsServicesManager::UpdatePermissions(bool may_record, | 142 void MetricsServicesManager::UpdatePermissions(bool may_record, |
90 bool may_upload) { | 143 bool may_upload) { |
91 metrics::MetricsService* metrics = GetMetricsService(); | 144 metrics::MetricsService* metrics = GetMetricsService(); |
92 | 145 |
93 const base::CommandLine* cmdline = base::CommandLine::ForCurrentProcess(); | 146 const base::CommandLine* cmdline = base::CommandLine::ForCurrentProcess(); |
94 | 147 |
95 const bool only_do_metrics_recording = | 148 const bool only_do_metrics_recording = |
96 cmdline->HasSwitch(switches::kMetricsRecordingOnly) || | 149 cmdline->HasSwitch(switches::kMetricsRecordingOnly) || |
97 cmdline->HasSwitch(switches::kEnableBenchmarking); | 150 cmdline->HasSwitch(switches::kEnableBenchmarking); |
98 | 151 |
99 if (only_do_metrics_recording) { | 152 if (only_do_metrics_recording) { |
100 metrics->StartRecordingForTests(); | 153 metrics->StartRecordingForTests(); |
101 GetRapporService()->Update(rappor::FINE_LEVEL, false); | 154 GetRapporService()->Update(rappor::FINE_LEVEL, false); |
102 return; | 155 return; |
103 } | 156 } |
104 | 157 |
105 if (may_record) { | 158 if (may_record) { |
106 if (!metrics->recording_active()) | 159 if (!metrics->recording_active()) |
107 metrics->Start(); | 160 metrics->Start(); |
108 | 161 |
109 if (may_upload) | 162 if (may_upload) |
110 metrics->EnableReporting(); | 163 metrics->EnableReporting(); |
111 else | 164 else |
112 metrics->DisableReporting(); | 165 metrics->DisableReporting(); |
113 } else if (metrics->recording_active() || metrics->reporting_active()) { | 166 } else if (metrics->recording_active() || metrics->reporting_active()) { |
114 metrics->Stop(); | 167 metrics->Stop(); |
115 } | 168 } |
116 | 169 |
117 rappor::RecordingLevel recording_level = may_record ? | 170 GetRapporService()->Update(GetRapporRecordingLevel(may_record), may_upload); |
118 rappor::FINE_LEVEL : rappor::RECORDING_DISABLED; | |
119 GetRapporService()->Update(recording_level, may_upload); | |
120 } | 171 } |
121 | 172 |
122 // TODO(asvitkine): This function does not report the correct value on Android, | 173 // TODO(asvitkine): This function does not report the correct value on Android, |
123 // see http://crbug.com/362192. | 174 // see http://crbug.com/362192. |
124 bool MetricsServicesManager::IsMetricsReportingEnabled() const { | 175 bool MetricsServicesManager::IsMetricsReportingEnabled() const { |
125 // If the user permits metrics reporting with the checkbox in the | 176 // If the user permits metrics reporting with the checkbox in the |
126 // prefs, we turn on recording. We disable metrics completely for | 177 // prefs, we turn on recording. We disable metrics completely for |
127 // non-official builds, or when field trials are forced. | 178 // non-official builds, or when field trials are forced. |
128 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | 179 if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
129 switches::kForceFieldTrials)) | 180 switches::kForceFieldTrials)) |
130 return false; | 181 return false; |
131 | 182 |
132 bool enabled = false; | 183 bool enabled = false; |
133 #if defined(GOOGLE_CHROME_BUILD) | 184 #if defined(GOOGLE_CHROME_BUILD) |
134 #if defined(OS_CHROMEOS) | 185 #if defined(OS_CHROMEOS) |
135 chromeos::CrosSettings::Get()->GetBoolean(chromeos::kStatsReportingPref, | 186 chromeos::CrosSettings::Get()->GetBoolean(chromeos::kStatsReportingPref, |
136 &enabled); | 187 &enabled); |
137 #else | 188 #else |
138 enabled = local_state_->GetBoolean(prefs::kMetricsReportingEnabled); | 189 enabled = local_state_->GetBoolean(prefs::kMetricsReportingEnabled); |
139 #endif // #if defined(OS_CHROMEOS) | 190 #endif // #if defined(OS_CHROMEOS) |
140 #endif // defined(GOOGLE_CHROME_BUILD) | 191 #endif // defined(GOOGLE_CHROME_BUILD) |
141 return enabled; | 192 return enabled; |
142 } | 193 } |
OLD | NEW |