Commit 5f1aeb93 authored by Robert Liao's avatar Robert Liao Committed by Commit Bot

Add Metrics Reporting Check to Hats

BUG=979530

Change-Id: If6ec9cdc45eea4406c305f5f472f206a4abe8e03
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1703204Reviewed-by: default avatarMark Pearson <mpearson@chromium.org>
Commit-Queue: Robert Liao <robliao@chromium.org>
Cr-Commit-Position: refs/heads/master@{#678364}
parent 78f8bfe6
......@@ -8,10 +8,12 @@
#include "base/metrics/field_trial_params.h"
#include "base/rand_util.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/common/chrome_features.h"
#include "components/metrics_services_manager/metrics_services_manager.h"
namespace {
// Which survey we're triggering
......@@ -57,6 +59,11 @@ void HatsService::LaunchSatisfactionSurvey() {
}
bool HatsService::ShouldShowSurvey(const std::string& trigger) const {
bool consent_given =
g_browser_process->GetMetricsServicesManager()->IsMetricsConsentGiven();
if (!consent_given)
return false;
if ((trigger_ == trigger || trigger_ == kHatsSurveyTriggerDefault) &&
!launch_hats_) {
if (base::RandDouble() < probability_) {
......
......@@ -5,16 +5,38 @@
#include "base/bind.h"
#include "base/macros.h"
#include "base/metrics/user_metrics.h"
#include "base/optional.h"
#include "base/test/scoped_feature_list.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/metrics/chrome_metrics_service_accessor.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/hats/hats_service.h"
#include "chrome/browser/ui/hats/hats_service_factory.h"
#include "chrome/common/chrome_features.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "components/metrics_services_manager/metrics_services_manager.h"
#include "content/public/test/browser_test.h"
namespace {
class ScopedEnableMetricsConsent {
public:
ScopedEnableMetricsConsent() {
ChromeMetricsServiceAccessor::SetMetricsAndCrashReportingForTesting(
&enable_metrics_consent_);
}
~ScopedEnableMetricsConsent() {
ChromeMetricsServiceAccessor::SetMetricsAndCrashReportingForTesting(
nullptr);
}
private:
const bool enable_metrics_consent_ = true;
DISALLOW_COPY_AND_ASSIGN(ScopedEnableMetricsConsent);
};
class HatsServiceBrowserTestBase : public InProcessBrowserTest {
protected:
HatsServiceBrowserTestBase()
......@@ -35,6 +57,8 @@ class HatsServiceBrowserTestBase : public InProcessBrowserTest {
return HatsServiceFactory::GetForProfile(browser()->profile(), true);
}
void EnableMetricsConsent() { enable_metrics_consent_.emplace(); }
bool HatsDialogShowRequested() { return hats_dialog_show_requested_; }
private:
......@@ -47,6 +71,7 @@ class HatsServiceBrowserTestBase : public InProcessBrowserTest {
bool hats_dialog_show_requested_ = false;
base::ActionCallback on_hats_dialog_show_;
base::Optional<ScopedEnableMetricsConsent> enable_metrics_consent_;
DISALLOW_COPY_AND_ASSIGN(HatsServiceBrowserTestBase);
};
......@@ -117,7 +142,17 @@ class HatsServiceProbabilityOne : public HatsServiceBrowserTestBase {
} // namespace
IN_PROC_BROWSER_TEST_F(HatsServiceProbabilityOne, NoShowConsentNotGiven) {
ASSERT_FALSE(
g_browser_process->GetMetricsServicesManager()->IsMetricsConsentGiven());
GetHatsService()->LaunchSatisfactionSurvey();
EXPECT_FALSE(HatsDialogShowRequested());
}
IN_PROC_BROWSER_TEST_F(HatsServiceProbabilityOne, AlwaysShow) {
EnableMetricsConsent();
ASSERT_TRUE(
g_browser_process->GetMetricsServicesManager()->IsMetricsConsentGiven());
GetHatsService()->LaunchSatisfactionSurvey();
EXPECT_TRUE(HatsDialogShowRequested());
}
......@@ -177,4 +177,8 @@ bool MetricsServicesManager::IsMetricsReportingEnabled() const {
return client_->IsMetricsReportingEnabled();
}
bool MetricsServicesManager::IsMetricsConsentGiven() const {
return client_->IsMetricsConsentGiven();
}
} // namespace metrics_services_manager
......@@ -82,6 +82,9 @@ class MetricsServicesManager {
// Gets the current state of metric reporting.
bool IsMetricsReportingEnabled() const;
// Gets the current state of metrics consent.
bool IsMetricsConsentGiven() const;
private:
// Update the managed services when permissions for recording/uploading
// metrics change.
......
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