Commit c998692a authored by Robert Kaplow's avatar Robert Kaplow Committed by Commit Bot

Make UKM unaffected by the state of the MetricsReporting Feature.



Bug: 767192
Change-Id: Ibea39c5a94c36d8c7c67672ee7fc380f50fa7acb
Reviewed-on: https://chromium-review.googlesource.com/676193Reviewed-by: default avatarOlivier Robin <olivierrobin@chromium.org>
Reviewed-by: default avatarSteven Holte <holte@chromium.org>
Commit-Queue: Robert Kaplow <rkaplow@chromium.org>
Cr-Commit-Position: refs/heads/master@{#505253}
parent 776edf8b
...@@ -46,17 +46,23 @@ ...@@ -46,17 +46,23 @@
#include "chromeos/settings/cros_settings_names.h" #include "chromeos/settings/cros_settings_names.h"
#endif // defined(OS_CHROMEOS) #endif // defined(OS_CHROMEOS)
namespace { namespace metrics {
// Name of the variations param that defines the sampling rate.
const char kRateParamName[] = "sampling_rate_per_mille";
namespace internal {
// Metrics reporting feature. This feature, along with user consent, controls if // Metrics reporting feature. This feature, along with user consent, controls if
// recording and reporting are enabled. If the feature is enabled, but no // recording and reporting are enabled. If the feature is enabled, but no
// consent is given, then there will be no recording or reporting. // consent is given, then there will be no recording or reporting.
const base::Feature kMetricsReportingFeature{"MetricsReporting", const base::Feature kMetricsReportingFeature{"MetricsReporting",
base::FEATURE_ENABLED_BY_DEFAULT}; base::FEATURE_ENABLED_BY_DEFAULT};
} // namespace internal
} // namespace metrics
namespace {
// Name of the variations param that defines the sampling rate.
const char kRateParamName[] = "sampling_rate_per_mille";
// Posts |GoogleUpdateSettings::StoreMetricsClientInfo| on blocking pool thread // Posts |GoogleUpdateSettings::StoreMetricsClientInfo| on blocking pool thread
// because it needs access to IO and cannot work from UI thread. // because it needs access to IO and cannot work from UI thread.
void PostStoreMetricsClientInfo(const metrics::ClientInfo& client_info) { void PostStoreMetricsClientInfo(const metrics::ClientInfo& client_info) {
...@@ -180,7 +186,7 @@ void ChromeMetricsServicesManagerClient::CreateFallbackSamplingTrial( ...@@ -180,7 +186,7 @@ void ChromeMetricsServicesManagerClient::CreateFallbackSamplingTrial(
// Setup the feature. // Setup the feature.
const std::string& group_name = trial->GetGroupNameWithoutActivation(); const std::string& group_name = trial->GetGroupNameWithoutActivation();
feature_list->RegisterFieldTrialOverride( feature_list->RegisterFieldTrialOverride(
kMetricsReportingFeature.name, metrics::internal::kMetricsReportingFeature.name,
group_name == kSampledOutGroup group_name == kSampledOutGroup
? base::FeatureList::OVERRIDE_DISABLE_FEATURE ? base::FeatureList::OVERRIDE_DISABLE_FEATURE
: base::FeatureList::OVERRIDE_ENABLE_FEATURE, : base::FeatureList::OVERRIDE_ENABLE_FEATURE,
...@@ -196,7 +202,8 @@ bool ChromeMetricsServicesManagerClient::IsClientInSample() { ...@@ -196,7 +202,8 @@ bool ChromeMetricsServicesManagerClient::IsClientInSample() {
if (!IsClientEligibleForSampling()) if (!IsClientEligibleForSampling())
return true; return true;
return base::FeatureList::IsEnabled(kMetricsReportingFeature); return base::FeatureList::IsEnabled(
metrics::internal::kMetricsReportingFeature);
} }
// static // static
...@@ -207,7 +214,7 @@ bool ChromeMetricsServicesManagerClient::GetSamplingRatePerMille(int* rate) { ...@@ -207,7 +214,7 @@ bool ChromeMetricsServicesManagerClient::GetSamplingRatePerMille(int* rate) {
return false; return false;
std::string rate_str = variations::GetVariationParamValueByFeature( std::string rate_str = variations::GetVariationParamValueByFeature(
kMetricsReportingFeature, kRateParamName); metrics::internal::kMetricsReportingFeature, kRateParamName);
if (rate_str.empty()) if (rate_str.empty())
return false; return false;
...@@ -253,6 +260,10 @@ bool ChromeMetricsServicesManagerClient::IsMetricsReportingEnabled() { ...@@ -253,6 +260,10 @@ bool ChromeMetricsServicesManagerClient::IsMetricsReportingEnabled() {
return enabled_state_provider_->IsReportingEnabled(); return enabled_state_provider_->IsReportingEnabled();
} }
bool ChromeMetricsServicesManagerClient::IsMetricsConsentGiven() {
return enabled_state_provider_->IsConsentGiven();
}
#if defined(OS_WIN) #if defined(OS_WIN)
void ChromeMetricsServicesManagerClient::UpdateRunningServices( void ChromeMetricsServicesManagerClient::UpdateRunningServices(
bool may_record, bool may_record,
......
...@@ -22,6 +22,11 @@ class PrefService; ...@@ -22,6 +22,11 @@ class PrefService;
namespace metrics { namespace metrics {
class EnabledStateProvider; class EnabledStateProvider;
class MetricsStateManager; class MetricsStateManager;
// Used only for testing.
namespace internal {
extern const base::Feature kMetricsReportingFeature;
}
} }
namespace version_info { namespace version_info {
...@@ -72,6 +77,7 @@ class ChromeMetricsServicesManagerClient ...@@ -72,6 +77,7 @@ class ChromeMetricsServicesManagerClient
CreateEntropyProvider() override; CreateEntropyProvider() override;
net::URLRequestContextGetter* GetURLRequestContext() override; net::URLRequestContextGetter* GetURLRequestContext() override;
bool IsMetricsReportingEnabled() override; bool IsMetricsReportingEnabled() override;
bool IsMetricsConsentGiven() override;
#if defined(OS_WIN) #if defined(OS_WIN)
// On Windows, the client controls whether Crashpad can upload crash reports. // On Windows, the client controls whether Crashpad can upload crash reports.
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#include "base/run_loop.h" #include "base/run_loop.h"
#include "chrome/browser/browser_process.h" #include "chrome/browser/browser_process.h"
#include "chrome/browser/metrics/chrome_metrics_service_accessor.h" #include "chrome/browser/metrics/chrome_metrics_service_accessor.h"
#include "chrome/browser/metrics/chrome_metrics_services_manager_client.h"
#include "chrome/browser/profiles/profile_manager.h" #include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/sync/profile_sync_service_factory.h" #include "chrome/browser/sync/profile_sync_service_factory.h"
#include "chrome/browser/sync/test/integration/profile_sync_service_harness.h" #include "chrome/browser/sync/test/integration/profile_sync_service_harness.h"
...@@ -49,10 +50,12 @@ class UkmBrowserTest : public SyncTest { ...@@ -49,10 +50,12 @@ class UkmBrowserTest : public SyncTest {
public: public:
UkmBrowserTest() : SyncTest(SINGLE_CLIENT) {} UkmBrowserTest() : SyncTest(SINGLE_CLIENT) {}
void SetUpCommandLine(base::CommandLine* command_line) override { void SetUp() override {
SyncTest::SetUpCommandLine(command_line); // Explicitly enable UKM and disable the MetricsReporting (which should
command_line->AppendSwitchASCII(switches::kEnableFeatures, // not affect UKM).
ukm::kUkmFeature.name); scoped_feature_list_.InitWithFeatures({ukm::kUkmFeature},
{internal::kMetricsReportingFeature});
SyncTest::SetUp();
} }
protected: protected:
...@@ -97,6 +100,7 @@ class UkmBrowserTest : public SyncTest { ...@@ -97,6 +100,7 @@ class UkmBrowserTest : public SyncTest {
ukm::UkmService* ukm_service() { ukm::UkmService* ukm_service() {
return static_cast<ukm::UkmService*>(ukm::UkmRecorder::Get()); return static_cast<ukm::UkmService*>(ukm::UkmRecorder::Get());
} }
base::test::ScopedFeatureList scoped_feature_list_;
}; };
// Make sure that UKM is disabled while an incognito window is open. // Make sure that UKM is disabled while an incognito window is open.
...@@ -223,6 +227,36 @@ IN_PROC_BROWSER_TEST_F(UkmBrowserTest, DisableSyncCheck) { ...@@ -223,6 +227,36 @@ IN_PROC_BROWSER_TEST_F(UkmBrowserTest, DisableSyncCheck) {
ChromeMetricsServiceAccessor::SetMetricsAndCrashReportingForTesting(nullptr); ChromeMetricsServiceAccessor::SetMetricsAndCrashReportingForTesting(nullptr);
} }
// Make sure that UKM is not affected by MetricsReporting Feature (sampling).
IN_PROC_BROWSER_TEST_F(UkmBrowserTest, MetricsReportingCheck) {
// Need to set the Metrics Default to OPT_OUT to trigger MetricsReporting.
DCHECK(g_browser_process);
PrefService* local_state = g_browser_process->local_state();
metrics::ForceRecordMetricsReportingDefaultState(
local_state, metrics::EnableMetricsDefault::OPT_OUT);
// Verify that kMetricsReportingFeature is disabled (i.e. other metrics
// services will be sampled out).
EXPECT_FALSE(
base::FeatureList::IsEnabled(internal::kMetricsReportingFeature));
// Enable metrics recording and update MetricsServicesManager.
bool metrics_enabled = true;
ChromeMetricsServiceAccessor::SetMetricsAndCrashReportingForTesting(
&metrics_enabled);
g_browser_process->GetMetricsServicesManager()->UpdateUploadPermissions(true);
Profile* profile = ProfileManager::GetActiveUserProfile();
std::unique_ptr<ProfileSyncServiceHarness> harness =
EnableSyncForProfile(profile);
Browser* sync_browser = CreateBrowser(profile);
EXPECT_TRUE(ukm_enabled());
harness->service()->RequestStop(browser_sync::ProfileSyncService::CLEAR_DATA);
CloseBrowserSynchronously(sync_browser);
ChromeMetricsServiceAccessor::SetMetricsAndCrashReportingForTesting(nullptr);
}
// TODO(crbug/745939): Add a tests for disable w/ multiprofiles. // TODO(crbug/745939): Add a tests for disable w/ multiprofiles.
// TODO(crbug/745939): Add a tests for guest profile. // TODO(crbug/745939): Add a tests for guest profile.
......
...@@ -22,6 +22,12 @@ void RecordMetricsReportingDefaultState(PrefService* local_state, ...@@ -22,6 +22,12 @@ void RecordMetricsReportingDefaultState(PrefService* local_state,
local_state->SetInteger(prefs::kMetricsDefaultOptIn, default_state); local_state->SetInteger(prefs::kMetricsDefaultOptIn, default_state);
} }
void ForceRecordMetricsReportingDefaultState(
PrefService* local_state,
EnableMetricsDefault default_state) {
local_state->SetInteger(prefs::kMetricsDefaultOptIn, default_state);
}
EnableMetricsDefault GetMetricsReportingDefaultState(PrefService* local_state) { EnableMetricsDefault GetMetricsReportingDefaultState(PrefService* local_state) {
return static_cast<EnableMetricsDefault>( return static_cast<EnableMetricsDefault>(
local_state->GetInteger(prefs::kMetricsDefaultOptIn)); local_state->GetInteger(prefs::kMetricsDefaultOptIn));
......
...@@ -34,6 +34,11 @@ void RegisterMetricsReportingStatePrefs(PrefRegistrySimple* registry); ...@@ -34,6 +34,11 @@ void RegisterMetricsReportingStatePrefs(PrefRegistrySimple* registry);
void RecordMetricsReportingDefaultState(PrefService* local_state, void RecordMetricsReportingDefaultState(PrefService* local_state,
EnableMetricsDefault default_state); EnableMetricsDefault default_state);
// Same as above, but does not verify the current state is UNKNOWN.
void ForceRecordMetricsReportingDefaultState(
PrefService* local_state,
EnableMetricsDefault default_state);
// Gets information about the default value for the enable metrics reporting // Gets information about the default value for the enable metrics reporting
// checkbox shown during first-run. // checkbox shown during first-run.
EnableMetricsDefault GetMetricsReportingDefaultState(PrefService* local_state); EnableMetricsDefault GetMetricsReportingDefaultState(PrefService* local_state);
......
...@@ -21,7 +21,10 @@ namespace metrics_services_manager { ...@@ -21,7 +21,10 @@ namespace metrics_services_manager {
MetricsServicesManager::MetricsServicesManager( MetricsServicesManager::MetricsServicesManager(
std::unique_ptr<MetricsServicesManagerClient> client) std::unique_ptr<MetricsServicesManagerClient> client)
: client_(std::move(client)), may_upload_(false), may_record_(false) { : client_(std::move(client)),
may_upload_(false),
may_record_(false),
consent_given_(false) {
DCHECK(client_); DCHECK(client_);
} }
...@@ -81,10 +84,12 @@ MetricsServicesManager::GetMetricsServiceClient() { ...@@ -81,10 +84,12 @@ MetricsServicesManager::GetMetricsServiceClient() {
} }
void MetricsServicesManager::UpdatePermissions(bool current_may_record, void MetricsServicesManager::UpdatePermissions(bool current_may_record,
bool current_consent_given,
bool current_may_upload) { bool current_may_upload) {
DCHECK(thread_checker_.CalledOnValidThread()); DCHECK(thread_checker_.CalledOnValidThread());
// If the user has opted out of metrics, delete local UKM state. // If the user has opted out of metrics, delete local UKM state. We Only check
if (may_record_ && !current_may_record) { // consent for UKM.
if (consent_given_ && !current_consent_given) {
ukm::UkmService* ukm = GetUkmService(); ukm::UkmService* ukm = GetUkmService();
if (ukm) { if (ukm) {
ukm->Purge(); ukm->Purge();
...@@ -93,10 +98,9 @@ void MetricsServicesManager::UpdatePermissions(bool current_may_record, ...@@ -93,10 +98,9 @@ void MetricsServicesManager::UpdatePermissions(bool current_may_record,
} }
// Stash the current permissions so that we can update the RapporServiceImpl // Stash the current permissions so that we can update the RapporServiceImpl
// correctly when the Rappor preference changes. The metrics recording // correctly when the Rappor preference changes.
// preference partially determines the initial rappor setting, and also
// controls whether FINE metrics are sent.
may_record_ = current_may_record; may_record_ = current_may_record;
consent_given_ = current_consent_given;
may_upload_ = current_may_upload; may_upload_ = current_may_upload;
UpdateRunningServices(); UpdateRunningServices();
} }
...@@ -138,7 +142,8 @@ void MetricsServicesManager::UpdateUkmService() { ...@@ -138,7 +142,8 @@ void MetricsServicesManager::UpdateUkmService() {
client_->IsMetricsReportingForceEnabled() || client_->IsMetricsReportingForceEnabled() ||
metrics_service_client_->IsHistorySyncEnabledOnAllProfiles(); metrics_service_client_->IsHistorySyncEnabledOnAllProfiles();
bool is_incognito = client_->IsIncognitoSessionActive(); bool is_incognito = client_->IsIncognitoSessionActive();
if (may_record_ && sync_enabled & !is_incognito) {
if (consent_given_ && sync_enabled & !is_incognito) {
ukm->EnableRecording(); ukm->EnableRecording();
if (may_upload_) if (may_upload_)
ukm->EnableReporting(); ukm->EnableReporting();
...@@ -151,9 +156,13 @@ void MetricsServicesManager::UpdateUkmService() { ...@@ -151,9 +156,13 @@ void MetricsServicesManager::UpdateUkmService() {
} }
void MetricsServicesManager::UpdateUploadPermissions(bool may_upload) { void MetricsServicesManager::UpdateUploadPermissions(bool may_upload) {
UpdatePermissions((client_->IsMetricsReportingForceEnabled() || if (client_->IsMetricsReportingForceEnabled()) {
client_->IsMetricsReportingEnabled()), UpdatePermissions(true, true, true);
client_->IsMetricsReportingForceEnabled() || may_upload); return;
}
UpdatePermissions(client_->IsMetricsReportingEnabled(),
client_->IsMetricsConsentGiven(), may_upload);
} }
} // namespace metrics_services_manager } // namespace metrics_services_manager
...@@ -77,10 +77,6 @@ class MetricsServicesManager { ...@@ -77,10 +77,6 @@ class MetricsServicesManager {
// renderer process exits unexpectedly. // renderer process exits unexpectedly.
void OnRendererProcessCrash(); void OnRendererProcessCrash();
// Update the managed services when permissions for recording/uploading
// metrics change.
void UpdatePermissions(bool current_may_record, bool current_may_upload);
// Update the managed services when permissions for uploading metrics change. // Update the managed services when permissions for uploading metrics change.
void UpdateUploadPermissions(bool may_upload); void UpdateUploadPermissions(bool may_upload);
...@@ -101,6 +97,12 @@ class MetricsServicesManager { ...@@ -101,6 +97,12 @@ class MetricsServicesManager {
// Update the state of UkmService to match current permissions. // Update the state of UkmService to match current permissions.
void UpdateUkmService(); void UpdateUkmService();
// Update the managed services when permissions for recording/uploading
// metrics change.
void UpdatePermissions(bool current_may_record,
bool current_consent_given,
bool current_may_upload);
// The client passed in from the embedder. // The client passed in from the embedder.
std::unique_ptr<MetricsServicesManagerClient> client_; std::unique_ptr<MetricsServicesManagerClient> client_;
...@@ -113,6 +115,9 @@ class MetricsServicesManager { ...@@ -113,6 +115,9 @@ class MetricsServicesManager {
// The current metrics recording setting. // The current metrics recording setting.
bool may_record_; bool may_record_;
// The current metrics setting reflecting if consent was given.
bool consent_given_;
// The MetricsServiceClient. Owns the MetricsService. // The MetricsServiceClient. Owns the MetricsService.
std::unique_ptr<metrics::MetricsServiceClient> metrics_service_client_; std::unique_ptr<metrics::MetricsServiceClient> metrics_service_client_;
......
...@@ -51,6 +51,9 @@ class MetricsServicesManagerClient { ...@@ -51,6 +51,9 @@ class MetricsServicesManagerClient {
// Returns whether metrics reporting is enabled. // Returns whether metrics reporting is enabled.
virtual bool IsMetricsReportingEnabled() = 0; virtual bool IsMetricsReportingEnabled() = 0;
// Returns whether metrics consent is given.
virtual bool IsMetricsConsentGiven() = 0;
// Returns whether there are any Incognito browsers/tabs open. // Returns whether there are any Incognito browsers/tabs open.
virtual bool IsIncognitoSessionActive() = 0; virtual bool IsIncognitoSessionActive() = 0;
......
...@@ -44,6 +44,8 @@ class IOSChromeMetricsServicesManagerClient ...@@ -44,6 +44,8 @@ class IOSChromeMetricsServicesManagerClient
net::URLRequestContextGetter* GetURLRequestContext() override; net::URLRequestContextGetter* GetURLRequestContext() override;
bool IsMetricsReportingEnabled() override; bool IsMetricsReportingEnabled() override;
bool IsMetricsConsentGiven() override;
bool IsIncognitoSessionActive() override; bool IsIncognitoSessionActive() override;
// Gets the MetricsStateManager, creating it if it has not already been // Gets the MetricsStateManager, creating it if it has not already been
......
...@@ -99,6 +99,10 @@ bool IOSChromeMetricsServicesManagerClient::IsMetricsReportingEnabled() { ...@@ -99,6 +99,10 @@ bool IOSChromeMetricsServicesManagerClient::IsMetricsReportingEnabled() {
return enabled_state_provider_->IsReportingEnabled(); return enabled_state_provider_->IsReportingEnabled();
} }
bool IOSChromeMetricsServicesManagerClient::IsMetricsConsentGiven() {
return enabled_state_provider_->IsConsentGiven();
}
metrics::MetricsStateManager* metrics::MetricsStateManager*
IOSChromeMetricsServicesManagerClient::GetMetricsStateManager() { IOSChromeMetricsServicesManagerClient::GetMetricsStateManager() {
DCHECK(thread_checker_.CalledOnValidThread()); DCHECK(thread_checker_.CalledOnValidThread());
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment