diff --git a/components/brave_shields/browser/cookie_pref_service.cc b/components/brave_shields/browser/cookie_pref_service.cc index 143bda02e3e4..272a1198435f 100644 --- a/components/brave_shields/browser/cookie_pref_service.cc +++ b/components/brave_shields/browser/cookie_pref_service.cc @@ -46,8 +46,12 @@ void SetCookiePrefDefaults(HostContentSettingsMap* map, PrefService* prefs) { prefs->SetInteger("profile.default_content_setting_values.cookies", CONTENT_SETTING_BLOCK); } else { - prefs->SetInteger("profile.default_content_setting_values.cookies", - CONTENT_SETTING_ALLOW); + int value = + prefs->GetInteger("profile.default_content_setting_values.cookies"); + if (IntToContentSetting(value) != CONTENT_SETTING_SESSION_ONLY) { + value = CONTENT_SETTING_ALLOW; + } + prefs->SetInteger("profile.default_content_setting_values.cookies", value); } } diff --git a/components/brave_shields/browser/cookie_pref_service_browsertest.cc b/components/brave_shields/browser/cookie_pref_service_browsertest.cc new file mode 100644 index 000000000000..a0b56ec51314 --- /dev/null +++ b/components/brave_shields/browser/cookie_pref_service_browsertest.cc @@ -0,0 +1,78 @@ +/* Copyright (c) 2019 The Brave Authors. All rights reserved. + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "brave/components/brave_shields/browser/brave_shields_util.h" +#include "chrome/browser/profiles/profile.h" +#include "chrome/browser/ui/browser.h" +#include "chrome/test/base/in_process_browser_test.h" +#include "chrome/test/base/ui_test_utils.h" +#include "components/content_settings/core/common/content_settings.h" +#include "components/prefs/pref_service.h" +#include "url/gurl.h" + +using brave_shields::ControlType; + +class CookiePrefServiceTest : public InProcessBrowserTest { + public: + CookiePrefServiceTest() = default; + ~CookiePrefServiceTest() override = default; + + Profile* profile() { return browser()->profile(); } + + ContentSetting GetCookiePref() { + return IntToContentSetting(profile()->GetPrefs()->GetInteger( + "profile.default_content_setting_values.cookies")); + } + + void SetCookiePref(ContentSetting setting) { + profile()->GetPrefs()->SetInteger( + "profile.default_content_setting_values.cookies", setting); + } +}; + +IN_PROC_BROWSER_TEST_F(CookiePrefServiceTest, CookieControlType_Preference) { + // Initial state + auto setting = brave_shields::GetCookieControlType(profile(), GURL()); + EXPECT_EQ(ControlType::BLOCK_THIRD_PARTY, setting); + EXPECT_EQ(CONTENT_SETTING_ALLOW, GetCookiePref()); + + // Control -> preference + /* BLOCK */ + brave_shields::SetCookieControlType(profile(), ControlType::BLOCK, GURL()); + EXPECT_EQ(CONTENT_SETTING_BLOCK, GetCookiePref()); + + /* ALLOW */ + brave_shields::SetCookieControlType(profile(), ControlType::ALLOW, GURL()); + EXPECT_EQ(CONTENT_SETTING_ALLOW, GetCookiePref()); + + /* BLOCK_THIRD_PARTY */ + brave_shields::SetCookieControlType(profile(), ControlType::BLOCK, GURL()); + EXPECT_EQ(CONTENT_SETTING_BLOCK, GetCookiePref()); + brave_shields::SetCookieControlType(profile(), ControlType::BLOCK_THIRD_PARTY, + GURL()); + EXPECT_EQ(CONTENT_SETTING_ALLOW, GetCookiePref()); + + // Preference -> control + /* BLOCK */ + SetCookiePref(CONTENT_SETTING_BLOCK); + EXPECT_EQ(ControlType::BLOCK, + brave_shields::GetCookieControlType(profile(), GURL())); + + /* ALLOW */ + SetCookiePref(CONTENT_SETTING_ALLOW); + EXPECT_EQ(ControlType::ALLOW, + brave_shields::GetCookieControlType(profile(), GURL())); + + // Preserve CONTENT_SETTING_SESSION_ONLY + SetCookiePref(CONTENT_SETTING_BLOCK); + EXPECT_EQ(ControlType::BLOCK, + brave_shields::GetCookieControlType(profile(), GURL())); + SetCookiePref(CONTENT_SETTING_SESSION_ONLY); + EXPECT_EQ(ControlType::ALLOW, + brave_shields::GetCookieControlType(profile(), GURL())); + SetCookiePref(CONTENT_SETTING_ALLOW); + EXPECT_EQ(ControlType::ALLOW, + brave_shields::GetCookieControlType(profile(), GURL())); +} diff --git a/test/BUILD.gn b/test/BUILD.gn index e817d75dec26..c4b29791ee37 100644 --- a/test/BUILD.gn +++ b/test/BUILD.gn @@ -510,6 +510,7 @@ test("brave_browser_tests") { "//brave/chromium_src/third_party/blink/renderer/modules/bluetooth/navigator_bluetoothtest.cc", "//brave/common/brave_channel_info_browsertest.cc", "//brave/components/brave_shields/browser/ad_block_service_browsertest.cc", + "//brave/components/brave_shields/browser/cookie_pref_service_browsertest.cc", "//brave/components/brave_shields/browser/https_everywhere_service_browsertest.cc", "//brave/components/brave_shields/browser/referrer_whitelist_service_browsertest.cc", "//brave/components/brave_shields/browser/tracking_protection_service_browsertest.cc",