Chromium Code Reviews| 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)) { | |
|
Alexei Svitkine (slow)
2015/02/06 22:31:11
Can you add a test for this?
Steven Holte
2015/02/07 02:26:21
Done.
| |
| 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 | |
| 89 void MetricsServicesManager::UpdatePermissions(bool may_record, | 121 void MetricsServicesManager::UpdatePermissions(bool may_record, |
| 90 bool may_upload) { | 122 bool may_upload) { |
| 91 metrics::MetricsService* metrics = GetMetricsService(); | 123 metrics::MetricsService* metrics = GetMetricsService(); |
| 92 | 124 |
| 93 const base::CommandLine* cmdline = base::CommandLine::ForCurrentProcess(); | 125 const base::CommandLine* cmdline = base::CommandLine::ForCurrentProcess(); |
| 94 | 126 |
| 95 const bool only_do_metrics_recording = | 127 const bool only_do_metrics_recording = |
| 96 cmdline->HasSwitch(switches::kMetricsRecordingOnly) || | 128 cmdline->HasSwitch(switches::kMetricsRecordingOnly) || |
| 97 cmdline->HasSwitch(switches::kEnableBenchmarking); | 129 cmdline->HasSwitch(switches::kEnableBenchmarking); |
| 98 | 130 |
| 99 if (only_do_metrics_recording) { | 131 if (only_do_metrics_recording) { |
| 100 metrics->StartRecordingForTests(); | 132 metrics->StartRecordingForTests(); |
| 101 GetRapporService()->Update(rappor::FINE_LEVEL, false); | 133 GetRapporService()->Update(rappor::FINE_LEVEL, false); |
| 102 return; | 134 return; |
| 103 } | 135 } |
| 104 | 136 |
| 105 if (may_record) { | 137 if (may_record) { |
| 106 if (!metrics->recording_active()) | 138 if (!metrics->recording_active()) |
| 107 metrics->Start(); | 139 metrics->Start(); |
| 108 | 140 |
| 109 if (may_upload) | 141 if (may_upload) |
| 110 metrics->EnableReporting(); | 142 metrics->EnableReporting(); |
| 111 else | 143 else |
| 112 metrics->DisableReporting(); | 144 metrics->DisableReporting(); |
| 113 } else if (metrics->recording_active() || metrics->reporting_active()) { | 145 } else if (metrics->recording_active() || metrics->reporting_active()) { |
| 114 metrics->Stop(); | 146 metrics->Stop(); |
| 115 } | 147 } |
| 116 | 148 |
| 117 rappor::RecordingLevel recording_level = may_record ? | 149 rappor::RecordingLevel recording_level = rappor::RECORDING_DISABLED; |
| 118 rappor::FINE_LEVEL : rappor::RECORDING_DISABLED; | 150 #if defined(GOOGLE_CHROME_BUILD) |
| 151 #if defined(OS_IOS) || defined(OS_ANDROID) | |
| 152 if (may_record) { | |
| 153 recording_level = rappor::FINE_LEVEL; | |
| 154 } | |
| 155 #else // defined(OS_IOS) || defined(OS_ANDROID) | |
|
Alexei Svitkine (slow)
2015/02/06 22:31:11
Why is there different logic between mobile and de
Steven Holte
2015/02/06 22:51:46
Because they have a different settings UI that I h
| |
| 156 if (IsRapporEnabled(may_record)) { | |
| 157 recording_level = may_record ? rappor::FINE_LEVEL : rappor::COARSE_LEVEL; | |
| 158 } | |
| 159 #endif // defined(OS_IOS) || defined(OS_ANDROID) | |
| 160 #endif // defined(GOOGLE_CHROME_BUILD) | |
| 119 GetRapporService()->Update(recording_level, may_upload); | 161 GetRapporService()->Update(recording_level, may_upload); |
| 120 } | 162 } |
| 121 | 163 |
| 122 // TODO(asvitkine): This function does not report the correct value on Android, | 164 // TODO(asvitkine): This function does not report the correct value on Android, |
| 123 // see http://crbug.com/362192. | 165 // see http://crbug.com/362192. |
| 124 bool MetricsServicesManager::IsMetricsReportingEnabled() const { | 166 bool MetricsServicesManager::IsMetricsReportingEnabled() const { |
| 125 // If the user permits metrics reporting with the checkbox in the | 167 // If the user permits metrics reporting with the checkbox in the |
| 126 // prefs, we turn on recording. We disable metrics completely for | 168 // prefs, we turn on recording. We disable metrics completely for |
| 127 // non-official builds, or when field trials are forced. | 169 // non-official builds, or when field trials are forced. |
| 128 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | 170 if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 129 switches::kForceFieldTrials)) | 171 switches::kForceFieldTrials)) |
| 130 return false; | 172 return false; |
| 131 | 173 |
| 132 bool enabled = false; | 174 bool enabled = false; |
| 133 #if defined(GOOGLE_CHROME_BUILD) | 175 #if defined(GOOGLE_CHROME_BUILD) |
| 134 #if defined(OS_CHROMEOS) | 176 #if defined(OS_CHROMEOS) |
| 135 chromeos::CrosSettings::Get()->GetBoolean(chromeos::kStatsReportingPref, | 177 chromeos::CrosSettings::Get()->GetBoolean(chromeos::kStatsReportingPref, |
| 136 &enabled); | 178 &enabled); |
| 137 #else | 179 #else |
| 138 enabled = local_state_->GetBoolean(prefs::kMetricsReportingEnabled); | 180 enabled = local_state_->GetBoolean(prefs::kMetricsReportingEnabled); |
| 139 #endif // #if defined(OS_CHROMEOS) | 181 #endif // #if defined(OS_CHROMEOS) |
| 140 #endif // defined(GOOGLE_CHROME_BUILD) | 182 #endif // defined(GOOGLE_CHROME_BUILD) |
| 141 return enabled; | 183 return enabled; |
| 142 } | 184 } |
| OLD | NEW |