Commit af73aede authored by Huanzhong Huang's avatar Huanzhong Huang Committed by Commit Bot

Check 3rd-party cookie blocking in HaTS survey launching process

Bug: 1055860
Change-Id: Iff0c883b02c9edf7c553db97ecf33692eed8ca24
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2074697
Commit-Queue: Huanzhong Huang <huanzhong@chromium.org>
Reviewed-by: default avatarMihai Sardarescu <msarda@chromium.org>
Reviewed-by: default avatarWei Li <weili@chromium.org>
Reviewed-by: default avatarMartin Šrámek <msramek@chromium.org>
Cr-Commit-Position: refs/heads/master@{#745607}
parent 1ac3d8ba
...@@ -21,6 +21,11 @@ ...@@ -21,6 +21,11 @@
#include "chrome/browser/ui/browser_window.h" #include "chrome/browser/ui/browser_window.h"
#include "chrome/common/chrome_features.h" #include "chrome/common/chrome_features.h"
#include "chrome/common/pref_names.h" #include "chrome/common/pref_names.h"
#include "components/content_settings/core/browser/cookie_settings.h"
#include "components/content_settings/core/browser/website_settings_registry.h"
#include "components/content_settings/core/common/content_settings.h"
#include "components/content_settings/core/common/features.h"
#include "components/content_settings/core/common/pref_names.h"
#include "components/metrics_services_manager/metrics_services_manager.h" #include "components/metrics_services_manager/metrics_services_manager.h"
#include "components/prefs/pref_registry_simple.h" #include "components/prefs/pref_registry_simple.h"
#include "components/prefs/scoped_user_pref_update.h" #include "components/prefs/scoped_user_pref_update.h"
...@@ -84,7 +89,9 @@ enum class ShouldShowSurveyReasons { ...@@ -84,7 +89,9 @@ enum class ShouldShowSurveyReasons {
kNoTriggerStringMismatch = 7, kNoTriggerStringMismatch = 7,
kNoNotRegularBrowser = 8, kNoNotRegularBrowser = 8,
kNoIncognitoDisabled = 9, kNoIncognitoDisabled = 9,
kMaxValue = kNoIncognitoDisabled, kNoCookiesBlocked = 10,
kNoThirdPartyCookiesBlocked = 11,
kMaxValue = kNoThirdPartyCookiesBlocked,
}; };
} // namespace } // namespace
...@@ -140,6 +147,31 @@ void HatsService::LaunchSurvey(const std::string& trigger) { ...@@ -140,6 +147,31 @@ void HatsService::LaunchSurvey(const std::string& trigger) {
return; return;
} }
// HaTS cannot be accessed when cookies are blocked.
if (profile_->GetPrefs()->GetInteger(
content_settings::WebsiteSettingsRegistry::GetInstance()
->Get(ContentSettingsType::COOKIES)
->default_value_pref_name()) == CONTENT_SETTING_BLOCK) {
UMA_HISTOGRAM_ENUMERATION(kHatsShouldShowSurveyReasonHistogram,
ShouldShowSurveyReasons::kNoCookiesBlocked);
return;
}
// HaTS cannot be accessed when third-party cookies are blocked.
// TODO(crbug/1056654): Confirm whether third-party cookie blocking in
// incognito mode should affect HaTS.
if (profile_->GetPrefs()->GetBoolean(prefs::kBlockThirdPartyCookies) ||
(base::FeatureList::IsEnabled(
content_settings::kImprovedCookieControls) &&
static_cast<content_settings::CookieControlsMode>(
profile_->GetPrefs()->GetInteger(prefs::kCookieControlsMode)) !=
content_settings::CookieControlsMode::kOff)) {
UMA_HISTOGRAM_ENUMERATION(
kHatsShouldShowSurveyReasonHistogram,
ShouldShowSurveyReasons::kNoThirdPartyCookiesBlocked);
return;
}
UMA_HISTOGRAM_ENUMERATION(kHatsShouldShowSurveyReasonHistogram, UMA_HISTOGRAM_ENUMERATION(kHatsShouldShowSurveyReasonHistogram,
ShouldShowSurveyReasons::kYes); ShouldShowSurveyReasons::kYes);
browser->window()->ShowHatsBubble( browser->window()->ShowHatsBubble(
......
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
#include "base/test/scoped_feature_list.h" #include "base/test/scoped_feature_list.h"
#include "base/version.h" #include "base/version.h"
#include "chrome/browser/browser_process.h" #include "chrome/browser/browser_process.h"
#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
#include "chrome/browser/metrics/chrome_metrics_service_accessor.h" #include "chrome/browser/metrics/chrome_metrics_service_accessor.h"
#include "chrome/browser/prefs/incognito_mode_prefs.h" #include "chrome/browser/prefs/incognito_mode_prefs.h"
#include "chrome/browser/profiles/profile_impl.h" #include "chrome/browser/profiles/profile_impl.h"
...@@ -19,12 +20,32 @@ ...@@ -19,12 +20,32 @@
#include "chrome/common/chrome_features.h" #include "chrome/common/chrome_features.h"
#include "chrome/common/pref_names.h" #include "chrome/common/pref_names.h"
#include "chrome/test/base/in_process_browser_test.h" #include "chrome/test/base/in_process_browser_test.h"
#include "components/content_settings/core/browser/cookie_settings.h"
#include "components/content_settings/core/common/features.h"
#include "components/content_settings/core/common/pref_names.h"
#include "components/metrics_services_manager/metrics_services_manager.h" #include "components/metrics_services_manager/metrics_services_manager.h"
#include "components/version_info/version_info.h" #include "components/version_info/version_info.h"
#include "content/public/test/browser_test.h" #include "content/public/test/browser_test.h"
namespace { namespace {
base::test::ScopedFeatureList::FeatureAndParams probability_zero{
features::kHappinessTrackingSurveysForDesktop,
{{"probability", "0.000"}}};
base::test::ScopedFeatureList::FeatureAndParams probability_one{
features::kHappinessTrackingSurveysForDesktop,
{{"probability", "1.000"},
{"survey", kHatsSurveyTriggerSatisfaction},
{"en_site_id", "test_site_id"}}};
base::test::ScopedFeatureList::FeatureAndParams settings_probability_one{
features::kHappinessTrackingSurveysForDesktopSettings,
{{"probability", "1.000"},
{"survey", kHatsSurveyTriggerSettings},
{"en_site_id", "test_site_id"}}};
base::test::ScopedFeatureList::FeatureAndParams improved_cookie_controls{
content_settings::kImprovedCookieControls,
{{"DefaultInIncognito", "true"}}};
class ScopedSetMetricsConsent { class ScopedSetMetricsConsent {
public: public:
// Enables or disables metrics consent based off of |consent|. // Enables or disables metrics consent based off of |consent|.
...@@ -46,7 +67,15 @@ class ScopedSetMetricsConsent { ...@@ -46,7 +67,15 @@ class ScopedSetMetricsConsent {
class HatsServiceBrowserTestBase : public InProcessBrowserTest { class HatsServiceBrowserTestBase : public InProcessBrowserTest {
protected: protected:
explicit HatsServiceBrowserTestBase(
std::vector<base::test::ScopedFeatureList::FeatureAndParams>
enabled_features)
: enabled_features_(enabled_features) {
scoped_feature_list_.InitWithFeaturesAndParameters(enabled_features_, {});
}
HatsServiceBrowserTestBase() = default; HatsServiceBrowserTestBase() = default;
~HatsServiceBrowserTestBase() override = default; ~HatsServiceBrowserTestBase() override = default;
HatsService* GetHatsService() { HatsService* GetHatsService() {
...@@ -69,59 +98,29 @@ class HatsServiceBrowserTestBase : public InProcessBrowserTest { ...@@ -69,59 +98,29 @@ class HatsServiceBrowserTestBase : public InProcessBrowserTest {
private: private:
base::Optional<ScopedSetMetricsConsent> scoped_metrics_consent_; base::Optional<ScopedSetMetricsConsent> scoped_metrics_consent_;
DISALLOW_COPY_AND_ASSIGN(HatsServiceBrowserTestBase); base::test::ScopedFeatureList scoped_feature_list_;
};
} // namespace std::vector<base::test::ScopedFeatureList::FeatureAndParams>
enabled_features_;
IN_PROC_BROWSER_TEST_F(HatsServiceBrowserTestBase, BubbleNotShownOnDefault) { DISALLOW_COPY_AND_ASSIGN(HatsServiceBrowserTestBase);
GetHatsService()->LaunchSurvey(kHatsSurveyTriggerSatisfaction); };
EXPECT_FALSE(HatsBubbleShown());
}
namespace {
class HatsServiceProbabilityZero : public HatsServiceBrowserTestBase { class HatsServiceProbabilityZero : public HatsServiceBrowserTestBase {
protected: protected:
HatsServiceProbabilityZero() { HatsServiceProbabilityZero()
scoped_feature_list_.InitAndEnableFeatureWithParameters( : HatsServiceBrowserTestBase({probability_zero}) {}
features::kHappinessTrackingSurveysForDesktop,
{{"probability", "0.000"}});
}
~HatsServiceProbabilityZero() override = default; ~HatsServiceProbabilityZero() override = default;
private:
base::test::ScopedFeatureList scoped_feature_list_;
DISALLOW_COPY_AND_ASSIGN(HatsServiceProbabilityZero); DISALLOW_COPY_AND_ASSIGN(HatsServiceProbabilityZero);
}; };
} // namespace
IN_PROC_BROWSER_TEST_F(HatsServiceProbabilityZero, NoShow) {
GetHatsService()->LaunchSurvey(kHatsSurveyTriggerSatisfaction);
EXPECT_FALSE(HatsBubbleShown());
}
namespace {
class HatsServiceProbabilityOne : public HatsServiceBrowserTestBase { class HatsServiceProbabilityOne : public HatsServiceBrowserTestBase {
protected: protected:
HatsServiceProbabilityOne() { HatsServiceProbabilityOne()
// TODO(weili): refactor to use constants from hats_service.cc for these : HatsServiceBrowserTestBase(
// parameters. {probability_one, settings_probability_one}) {}
scoped_feature_list_.InitWithFeaturesAndParameters(
{{features::kHappinessTrackingSurveysForDesktop,
{{"probability", "1.000"},
{"survey", kHatsSurveyTriggerSatisfaction},
{"en_site_id", "test_site_id"}}},
{features::kHappinessTrackingSurveysForDesktopSettings,
{{"probability", "1.000"},
{"survey", kHatsSurveyTriggerSettings},
{"en_site_id", "test_site_id"}}}},
{});
}
~HatsServiceProbabilityOne() override = default; ~HatsServiceProbabilityOne() override = default;
...@@ -138,13 +137,46 @@ class HatsServiceProbabilityOne : public HatsServiceBrowserTestBase { ...@@ -138,13 +137,46 @@ class HatsServiceProbabilityOne : public HatsServiceBrowserTestBase {
GetHatsService()->SetSurveyMetadataForTesting({}); GetHatsService()->SetSurveyMetadataForTesting({});
} }
base::test::ScopedFeatureList scoped_feature_list_;
DISALLOW_COPY_AND_ASSIGN(HatsServiceProbabilityOne); DISALLOW_COPY_AND_ASSIGN(HatsServiceProbabilityOne);
}; };
class HatsServiceImprovedCookieControlsEnabled
: public HatsServiceBrowserTestBase {
protected:
HatsServiceImprovedCookieControlsEnabled()
: HatsServiceBrowserTestBase(
{probability_one, improved_cookie_controls}) {}
~HatsServiceImprovedCookieControlsEnabled() override = default;
private:
void SetUpOnMainThread() override {
HatsServiceBrowserTestBase::SetUpOnMainThread();
// Set the profile creation time to be old enough to ensure triggering.
browser()->profile()->SetCreationTimeForTesting(
base::Time::Now() - base::TimeDelta::FromDays(45));
}
void TearDownOnMainThread() override {
GetHatsService()->SetSurveyMetadataForTesting({});
}
DISALLOW_COPY_AND_ASSIGN(HatsServiceImprovedCookieControlsEnabled);
};
} // namespace } // namespace
IN_PROC_BROWSER_TEST_F(HatsServiceBrowserTestBase, BubbleNotShownOnDefault) {
GetHatsService()->LaunchSurvey(kHatsSurveyTriggerSatisfaction);
EXPECT_FALSE(HatsBubbleShown());
}
IN_PROC_BROWSER_TEST_F(HatsServiceProbabilityZero, NoShow) {
GetHatsService()->LaunchSurvey(kHatsSurveyTriggerSatisfaction);
EXPECT_FALSE(HatsBubbleShown());
}
IN_PROC_BROWSER_TEST_F(HatsServiceProbabilityOne, NoShowConsentNotGiven) { IN_PROC_BROWSER_TEST_F(HatsServiceProbabilityOne, NoShowConsentNotGiven) {
SetMetricsConsent(false); SetMetricsConsent(false);
ASSERT_FALSE( ASSERT_FALSE(
...@@ -253,3 +285,41 @@ IN_PROC_BROWSER_TEST_F(HatsServiceProbabilityOne, IncognitoModeDisabledNoShow) { ...@@ -253,3 +285,41 @@ IN_PROC_BROWSER_TEST_F(HatsServiceProbabilityOne, IncognitoModeDisabledNoShow) {
GetHatsService()->LaunchSurvey(kHatsSurveyTriggerSatisfaction); GetHatsService()->LaunchSurvey(kHatsSurveyTriggerSatisfaction);
EXPECT_FALSE(HatsBubbleShown()); EXPECT_FALSE(HatsBubbleShown());
} }
IN_PROC_BROWSER_TEST_F(HatsServiceProbabilityOne, CookiesBlockedNoShow) {
SetMetricsConsent(true);
auto* settings_map =
HostContentSettingsMapFactory::GetInstance()->GetForProfile(
browser()->profile());
settings_map->SetDefaultContentSetting(ContentSettingsType::COOKIES,
CONTENT_SETTING_BLOCK);
GetHatsService()->LaunchSurvey(kHatsSurveyTriggerSatisfaction);
EXPECT_FALSE(HatsBubbleShown());
}
IN_PROC_BROWSER_TEST_F(HatsServiceProbabilityOne,
ThirdPartyCookiesBlockedNoShow) {
SetMetricsConsent(true);
PrefService* pref_service = browser()->profile()->GetPrefs();
pref_service->SetBoolean(prefs::kBlockThirdPartyCookies, true);
GetHatsService()->LaunchSurvey(kHatsSurveyTriggerSatisfaction);
EXPECT_FALSE(HatsBubbleShown());
}
IN_PROC_BROWSER_TEST_F(HatsServiceImprovedCookieControlsEnabled,
ThirdPartyCookiesBlockedInIncognitoNoShow) {
SetMetricsConsent(true);
GetHatsService()->LaunchSurvey(kHatsSurveyTriggerSatisfaction);
EXPECT_FALSE(HatsBubbleShown());
}
IN_PROC_BROWSER_TEST_F(HatsServiceImprovedCookieControlsEnabled,
ThirdPartyCookiesAllowedInIncognitoShow) {
SetMetricsConsent(true);
PrefService* pref_service = browser()->profile()->GetPrefs();
pref_service->SetInteger(
prefs::kCookieControlsMode,
static_cast<int>(content_settings::CookieControlsMode::kOff));
GetHatsService()->LaunchSurvey(kHatsSurveyTriggerSatisfaction);
EXPECT_TRUE(HatsBubbleShown());
}
...@@ -31841,6 +31841,8 @@ Called by update_gpu_driver_bug_workaround_entries.py.--> ...@@ -31841,6 +31841,8 @@ Called by update_gpu_driver_bug_workaround_entries.py.-->
<int value="7" label="Trigger string mismatch"/> <int value="7" label="Trigger string mismatch"/>
<int value="8" label="Not regular browser"/> <int value="8" label="Not regular browser"/>
<int value="9" label="Incognito mode disabled"/> <int value="9" label="Incognito mode disabled"/>
<int value="10" label="Cookies are blocked"/>
<int value="11" label="Third-party cookies are blocked"/>
</enum> </enum>
<enum name="HasDiscreteGpu"> <enum name="HasDiscreteGpu">
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