OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/chrome_metrics_services_manager_client.h" | 5 #include "chrome/browser/metrics/chrome_metrics_services_manager_client.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/feature_list.h" | 8 #include "base/feature_list.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
11 #include "base/strings/string_number_conversions.h" | |
11 #include "chrome/browser/browser_process.h" | 12 #include "chrome/browser/browser_process.h" |
12 #include "chrome/browser/metrics/chrome_metrics_service_accessor.h" | 13 #include "chrome/browser/metrics/chrome_metrics_service_accessor.h" |
13 #include "chrome/browser/metrics/chrome_metrics_service_client.h" | 14 #include "chrome/browser/metrics/chrome_metrics_service_client.h" |
14 #include "chrome/browser/metrics/variations/chrome_variations_service_client.h" | 15 #include "chrome/browser/metrics/variations/chrome_variations_service_client.h" |
15 #include "chrome/browser/metrics/variations/ui_string_overrider_factory.h" | 16 #include "chrome/browser/metrics/variations/ui_string_overrider_factory.h" |
16 #include "chrome/browser/ui/browser_otr_state.h" | 17 #include "chrome/browser/ui/browser_otr_state.h" |
17 #include "chrome/common/chrome_switches.h" | 18 #include "chrome/common/chrome_switches.h" |
18 #include "chrome/installer/util/google_update_settings.h" | 19 #include "chrome/installer/util/google_update_settings.h" |
19 #include "components/metrics/enabled_state_provider.h" | 20 #include "components/metrics/enabled_state_provider.h" |
20 #include "components/metrics/metrics_state_manager.h" | 21 #include "components/metrics/metrics_state_manager.h" |
21 #include "components/prefs/pref_service.h" | 22 #include "components/prefs/pref_service.h" |
22 #include "components/rappor/rappor_service.h" | 23 #include "components/rappor/rappor_service.h" |
23 #include "components/variations/service/variations_service.h" | 24 #include "components/variations/service/variations_service.h" |
25 #include "components/variations/variations_associated_data.h" | |
24 #include "content/public/browser/browser_thread.h" | 26 #include "content/public/browser/browser_thread.h" |
25 | 27 |
26 namespace { | 28 namespace { |
27 | 29 |
30 // Name of the variations param that defines the sampling rate. | |
31 const char kRateParamName[] = "sampling_rate_per_mille"; | |
32 | |
33 // Metrics reporting feature. This feature, along with user consent, controls if | |
34 // recording and reporting are enabled. If the feature is enabled, but no | |
35 // consent is given, then there will be no recording or reporting. | |
36 const base::Feature kMetricsReportingFeature{"MetricsReporting", | |
37 base::FEATURE_ENABLED_BY_DEFAULT}; | |
38 | |
28 // Posts |GoogleUpdateSettings::StoreMetricsClientInfo| on blocking pool thread | 39 // Posts |GoogleUpdateSettings::StoreMetricsClientInfo| on blocking pool thread |
29 // because it needs access to IO and cannot work from UI thread. | 40 // because it needs access to IO and cannot work from UI thread. |
30 void PostStoreMetricsClientInfo(const metrics::ClientInfo& client_info) { | 41 void PostStoreMetricsClientInfo(const metrics::ClientInfo& client_info) { |
31 content::BrowserThread::GetBlockingPool()->PostTask( | 42 content::BrowserThread::GetBlockingPool()->PostTask( |
32 FROM_HERE, | 43 FROM_HERE, |
33 base::Bind(&GoogleUpdateSettings::StoreMetricsClientInfo, client_info)); | 44 base::Bind(&GoogleUpdateSettings::StoreMetricsClientInfo, client_info)); |
34 } | 45 } |
35 | 46 |
47 // Only clients that were given an opt-out metrics-reporting consent flow are | |
48 // eligible for sampling. | |
49 bool IsClientEligibleForSampling() { | |
50 return metrics::GetMetricsReportingDefaultState( | |
51 g_browser_process->local_state()) == | |
52 metrics::EnableMetricsDefault::OPT_OUT; | |
53 } | |
54 | |
36 } // namespace | 55 } // namespace |
37 | 56 |
38 const base::Feature kMetricsReportingFeature{"MetricsReporting", | |
39 base::FEATURE_ENABLED_BY_DEFAULT}; | |
40 | 57 |
41 class ChromeMetricsServicesManagerClient::ChromeEnabledStateProvider | 58 class ChromeMetricsServicesManagerClient::ChromeEnabledStateProvider |
42 : public metrics::EnabledStateProvider { | 59 : public metrics::EnabledStateProvider { |
43 public: | 60 public: |
44 ChromeEnabledStateProvider() {} | 61 ChromeEnabledStateProvider() {} |
45 ~ChromeEnabledStateProvider() override {} | 62 ~ChromeEnabledStateProvider() override {} |
46 | 63 |
47 bool IsConsentGiven() override { | 64 bool IsConsentGiven() override { |
48 return ChromeMetricsServiceAccessor::IsMetricsAndCrashReportingEnabled(); | 65 return ChromeMetricsServiceAccessor::IsMetricsAndCrashReportingEnabled(); |
49 } | 66 } |
50 | 67 |
51 bool IsReportingEnabled() override { | 68 bool IsReportingEnabled() override { |
52 return IsConsentGiven() && | 69 return IsConsentGiven() && |
53 base::FeatureList::IsEnabled(kMetricsReportingFeature); | 70 ChromeMetricsServicesManagerClient::IsClientInSample(); |
54 } | 71 } |
55 | 72 |
56 DISALLOW_COPY_AND_ASSIGN(ChromeEnabledStateProvider); | 73 DISALLOW_COPY_AND_ASSIGN(ChromeEnabledStateProvider); |
57 }; | 74 }; |
58 | 75 |
59 ChromeMetricsServicesManagerClient::ChromeMetricsServicesManagerClient( | 76 ChromeMetricsServicesManagerClient::ChromeMetricsServicesManagerClient( |
60 PrefService* local_state) | 77 PrefService* local_state) |
61 : enabled_state_provider_(new ChromeEnabledStateProvider()), | 78 : enabled_state_provider_(new ChromeEnabledStateProvider()), |
62 local_state_(local_state) { | 79 local_state_(local_state) { |
63 DCHECK(local_state); | 80 DCHECK(local_state); |
64 | 81 |
65 SetupMetricsStateForChromeOS(); | 82 SetupMetricsStateForChromeOS(); |
66 } | 83 } |
67 | 84 |
68 ChromeMetricsServicesManagerClient::~ChromeMetricsServicesManagerClient() {} | 85 ChromeMetricsServicesManagerClient::~ChromeMetricsServicesManagerClient() {} |
69 | 86 |
87 // static | |
88 bool ChromeMetricsServicesManagerClient::IsClientInSample() { | |
89 // Only some clients are eligible for sampling. Clients that aren't eligible | |
90 // will always be considered "in sample". In this case, we don't want the | |
91 // feature state queried, because we don't want the field trial that controls | |
92 // sampling to be reported as active. | |
93 if (!IsClientEligibleForSampling()) { | |
Alexei Svitkine (slow)
2016/08/01 15:49:56
Nit: No {}'s
Same below for 1-liners.
jwd
2016/08/01 16:45:29
Done.
| |
94 return true; | |
95 } | |
96 | |
97 return base::FeatureList::IsEnabled(kMetricsReportingFeature); | |
98 } | |
99 | |
100 // static | |
101 int ChromeMetricsServicesManagerClient::GetSamplingRatePerMille() { | |
102 // The population that is NOT eligible for sampling in considered "in sample", | |
103 // so that population's sampling rate is 1000/1000. | |
104 if (!IsClientEligibleForSampling()) { | |
105 return 1000; | |
106 } | |
107 | |
108 std::string rate_str = variations::GetVariationParamValueByFeature( | |
109 kMetricsReportingFeature, kRateParamName); | |
110 if (rate_str.empty()) { | |
111 // The parameter can be empty either if the feature is dissabled, or the | |
Alexei Svitkine (slow)
2016/08/01 15:49:56
disabled - also mispeled below
jwd
2016/08/01 16:45:29
Done.
| |
112 // parameter isn't defined in an associated variation. The dissabled case is | |
113 // unimportant, since no metrics are reported. If the parameter is | |
114 // undefined, assume sampling is not being done, i.e. the sample rate is | |
115 // 1000/1000. | |
116 return 1000; | |
117 } | |
118 | |
119 int rate; | |
120 if (!base::StringToInt(rate_str, &rate) || rate > 1000) { | |
121 return 1000; | |
122 } | |
123 return rate; | |
124 } | |
125 | |
70 std::unique_ptr<rappor::RapporService> | 126 std::unique_ptr<rappor::RapporService> |
71 ChromeMetricsServicesManagerClient::CreateRapporService() { | 127 ChromeMetricsServicesManagerClient::CreateRapporService() { |
72 DCHECK(thread_checker_.CalledOnValidThread()); | 128 DCHECK(thread_checker_.CalledOnValidThread()); |
73 return base::WrapUnique(new rappor::RapporService( | 129 return base::WrapUnique(new rappor::RapporService( |
74 local_state_, base::Bind(&chrome::IsIncognitoSessionActive))); | 130 local_state_, base::Bind(&chrome::IsIncognitoSessionActive))); |
75 } | 131 } |
76 | 132 |
77 std::unique_ptr<variations::VariationsService> | 133 std::unique_ptr<variations::VariationsService> |
78 ChromeMetricsServicesManagerClient::CreateVariationsService() { | 134 ChromeMetricsServicesManagerClient::CreateVariationsService() { |
79 DCHECK(thread_checker_.CalledOnValidThread()); | 135 DCHECK(thread_checker_.CalledOnValidThread()); |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
128 ChromeMetricsServicesManagerClient::GetMetricsStateManager() { | 184 ChromeMetricsServicesManagerClient::GetMetricsStateManager() { |
129 DCHECK(thread_checker_.CalledOnValidThread()); | 185 DCHECK(thread_checker_.CalledOnValidThread()); |
130 if (!metrics_state_manager_) { | 186 if (!metrics_state_manager_) { |
131 metrics_state_manager_ = metrics::MetricsStateManager::Create( | 187 metrics_state_manager_ = metrics::MetricsStateManager::Create( |
132 local_state_, enabled_state_provider_.get(), | 188 local_state_, enabled_state_provider_.get(), |
133 base::Bind(&PostStoreMetricsClientInfo), | 189 base::Bind(&PostStoreMetricsClientInfo), |
134 base::Bind(&GoogleUpdateSettings::LoadMetricsClientInfo)); | 190 base::Bind(&GoogleUpdateSettings::LoadMetricsClientInfo)); |
135 } | 191 } |
136 return metrics_state_manager_.get(); | 192 return metrics_state_manager_.get(); |
137 } | 193 } |
OLD | NEW |