Skip to content

Commit f89d852

Browse files
committed
Fixed brave shiels panel in incognito.
1 parent 740ea00 commit f89d852

File tree

8 files changed

+264
-35
lines changed

8 files changed

+264
-35
lines changed

browser/brave_shields/BUILD.gn

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Copyright (c) 2025 The Brave Authors. All rights reserved.
2+
# This Source Code Form is subject to the terms of the Mozilla Public
3+
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
4+
# You can obtain one at https://mozilla.org/MPL/2.0/.
5+
6+
import("//testing/test.gni")
7+
8+
source_set("unit_tests") {
9+
testonly = true
10+
11+
sources = []
12+
13+
deps = [
14+
"//brave/components/brave_shields/content/browser",
15+
"//brave/components/brave_shields/core/browser",
16+
"//brave/components/brave_shields/core/common",
17+
"//chrome/test:test_support",
18+
"//testing/gtest",
19+
]
20+
21+
if (!is_android) {
22+
sources += [ "brave_shields_util_profiles_unittest.cc" ]
23+
24+
deps += [
25+
"//chrome/browser",
26+
"//chrome/browser/content_settings:content_settings_factory",
27+
]
28+
}
29+
}

browser/brave_shields/ad_block_service_browsertest.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -680,7 +680,7 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, SubFrameShieldsOff) {
680680
)"));
681681
content::RunAllTasksUntilIdle();
682682
EXPECT_EQ(profile()->GetPrefs()->GetUint64(kAdsBlocked), 0ULL);
683-
brave_shields::ResetBraveShieldsEnabled(content_settings(), url);
683+
brave_shields::SetBraveShieldsEnabled(content_settings(), true, url);
684684
}
685685

686686
// Requests made by a service worker should be blocked as well.

browser/brave_shields/brave_shields_tab_helper.cc

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -178,13 +178,8 @@ bool BraveShieldsTabHelper::GetBraveShieldsEnabled() {
178178

179179
void BraveShieldsTabHelper::SetBraveShieldsEnabled(bool is_enabled) {
180180
auto* map = GetHostContentSettingsMap(web_contents());
181-
if (map->GetDefaultContentSetting(ContentSettingsType::BRAVE_SHIELDS,
182-
nullptr) == is_enabled) {
183-
brave_shields::ResetBraveShieldsEnabled(map, GetCurrentSiteURL());
184-
} else {
185-
brave_shields::SetBraveShieldsEnabled(map, is_enabled, GetCurrentSiteURL(),
186-
g_browser_process->local_state());
187-
}
181+
brave_shields::SetBraveShieldsEnabled(map, is_enabled, GetCurrentSiteURL(),
182+
g_browser_process->local_state());
188183
ReloadWebContents();
189184
}
190185

Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
// Copyright (c) 2025 The Brave Authors. All rights reserved.
2+
// This Source Code Form is subject to the terms of the Mozilla Public
3+
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
4+
// You can obtain one at https://mozilla.org/MPL/2.0/.
5+
6+
#include "brave/components/brave_shields/content/browser/brave_shields_util.h"
7+
#include "chrome/browser/content_settings/cookie_settings_factory.h"
8+
#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
9+
#include "chrome/browser/profiles/profile.h"
10+
#include "chrome/test/base/testing_browser_process.h"
11+
#include "chrome/test/base/testing_profile.h"
12+
#include "chrome/test/base/testing_profile_manager.h"
13+
#include "components/content_settings/core/browser/cookie_settings.h"
14+
#include "components/content_settings/core/browser/host_content_settings_map.h"
15+
#include "content/public/test/browser_task_environment.h"
16+
#include "testing/gtest/include/gtest/gtest.h"
17+
#include "url/gurl.h"
18+
19+
namespace {
20+
21+
using ScopedIncognitoProfile =
22+
std::unique_ptr<Profile, decltype([](Profile* p) {
23+
ASSERT_TRUE(!!p->IsOffTheRecord());
24+
p->GetOriginalProfile()->DestroyOffTheRecordProfile(p);
25+
})>;
26+
27+
testing::Message& operator<<(testing::Message& m,
28+
brave_shields::ControlType c) {
29+
return m << brave_shields::ControlTypeToString(c);
30+
}
31+
32+
} // namespace
33+
34+
namespace brave_shields {
35+
36+
class BraveShieldsUtilProfilesTest : public testing::Test {
37+
public:
38+
BraveShieldsUtilProfilesTest()
39+
: local_state_(TestingBrowserProcess::GetGlobal()) {}
40+
~BraveShieldsUtilProfilesTest() override = default;
41+
42+
TestingProfile* regular_profile() { return &profile_; }
43+
ScopedIncognitoProfile incognito_profile() {
44+
return ScopedIncognitoProfile(regular_profile()->GetOffTheRecordProfile(
45+
Profile::OTRProfileID::PrimaryID(),
46+
/*create_if_needed=*/true));
47+
}
48+
49+
HostContentSettingsMap* hcsm(Profile* profile) {
50+
return HostContentSettingsMapFactory::GetForProfile(profile);
51+
}
52+
53+
HostContentSettingsMap* hcsm(ScopedIncognitoProfile& profile) {
54+
return hcsm(profile.get());
55+
}
56+
57+
const GURL test_url{"https://example.com"};
58+
59+
template <typename ValueT, typename SetFn, typename GetFn>
60+
void RunTest(base::span<const std::pair<ValueT, ValueT>> cases,
61+
SetFn setter,
62+
GetFn getter) {
63+
testing::Message issues;
64+
for (const auto& [value, expect] : cases) {
65+
setter(hcsm(regular_profile()), value);
66+
const auto get = getter(hcsm(regular_profile()));
67+
if (expect != get) {
68+
issues << "Regular profile: Set " << value << " Get " << get
69+
<< " Expected " << expect << "\n";
70+
}
71+
72+
auto incognito = incognito_profile();
73+
// Now change value for incognito and expect that values are not depended
74+
// on the regular profile.
75+
for (const auto& [ivalue, iexpect] : cases) {
76+
setter(hcsm(incognito), ivalue);
77+
const auto iget = getter(hcsm(incognito));
78+
if (iexpect != iget) {
79+
issues << "Incognito profile: Set " << ivalue << " Get " << iget
80+
<< " Expected " << iexpect << " Regular profile value "
81+
<< value << "\n";
82+
}
83+
}
84+
}
85+
const auto message = issues.GetString();
86+
EXPECT_TRUE(message.empty()) << message;
87+
}
88+
89+
private:
90+
content::BrowserTaskEnvironment task_environment_;
91+
TestingProfile profile_;
92+
ScopedTestingLocalState local_state_;
93+
};
94+
95+
TEST_F(BraveShieldsUtilProfilesTest, SetBraveShieldsEnabled) {
96+
constexpr std::pair<bool, bool> kExpects[] = {{true, true}, {false, false}};
97+
98+
auto set = [this](HostContentSettingsMap* map, bool value) {
99+
SetBraveShieldsEnabled(map, value, test_url);
100+
};
101+
auto get = [this](HostContentSettingsMap* map) {
102+
return GetBraveShieldsEnabled(map, test_url);
103+
};
104+
105+
RunTest<bool>(kExpects, std::move(set), std::move(get));
106+
}
107+
108+
TEST_F(BraveShieldsUtilProfilesTest, SetAdControlType) {
109+
constexpr std::pair<ControlType, ControlType> kExpects[] = {{ALLOW, ALLOW},
110+
{BLOCK, BLOCK}};
111+
112+
auto set = [this](HostContentSettingsMap* map, ControlType value) {
113+
SetAdControlType(map, value, test_url);
114+
};
115+
auto get = [this](HostContentSettingsMap* map) {
116+
return GetAdControlType(map, test_url);
117+
};
118+
119+
RunTest<ControlType>(kExpects, std::move(set), std::move(get));
120+
}
121+
122+
TEST_F(BraveShieldsUtilProfilesTest, SetCosmeticFilteringControlType) {
123+
constexpr std::pair<ControlType, ControlType> kExpects[] = {
124+
{ALLOW, ALLOW}, {BLOCK, BLOCK}, {BLOCK_THIRD_PARTY, BLOCK_THIRD_PARTY}};
125+
126+
auto set = [this](HostContentSettingsMap* map, ControlType value) {
127+
SetCosmeticFilteringControlType(map, value, test_url);
128+
};
129+
auto get = [this](HostContentSettingsMap* map) {
130+
return GetCosmeticFilteringControlType(map, test_url);
131+
};
132+
133+
RunTest<ControlType>(kExpects, std::move(set), std::move(get));
134+
}
135+
136+
TEST_F(BraveShieldsUtilProfilesTest, SetCookieControlType) {
137+
constexpr std::pair<ControlType, ControlType> kExpects[] = {
138+
{ALLOW, ALLOW}, {BLOCK, BLOCK}, {BLOCK_THIRD_PARTY, BLOCK_THIRD_PARTY}};
139+
140+
auto set = [this](HostContentSettingsMap* map, ControlType value) {
141+
SetCookieControlType(map, regular_profile()->GetPrefs(), value, test_url);
142+
};
143+
144+
auto cookie_settings =
145+
CookieSettingsFactory::GetForProfile(regular_profile());
146+
147+
auto get = [this, cookie_settings](HostContentSettingsMap* map) {
148+
return GetCookieControlType(map, cookie_settings.get(), test_url);
149+
};
150+
151+
RunTest<ControlType>(kExpects, std::move(set), std::move(get));
152+
}
153+
154+
TEST_F(BraveShieldsUtilProfilesTest, SetFingerprintingControlType) {
155+
constexpr std::pair<ControlType, ControlType> kExpects[] = {
156+
{ALLOW, ALLOW},
157+
{BLOCK, DEFAULT},
158+
{BLOCK_THIRD_PARTY, DEFAULT},
159+
{DEFAULT, DEFAULT}};
160+
161+
auto set = [this](HostContentSettingsMap* map, ControlType value) {
162+
SetFingerprintingControlType(map, value, test_url);
163+
};
164+
auto get = [this](HostContentSettingsMap* map) {
165+
return GetFingerprintingControlType(map, test_url);
166+
};
167+
168+
RunTest<ControlType>(kExpects, std::move(set), std::move(get));
169+
}
170+
171+
TEST_F(BraveShieldsUtilProfilesTest, SetHttpsUpgradeControlType) {
172+
constexpr std::pair<ControlType, ControlType> kExpects[] = {
173+
{ALLOW, ALLOW}, {BLOCK, BLOCK}, {BLOCK_THIRD_PARTY, BLOCK_THIRD_PARTY}};
174+
175+
auto set = [this](HostContentSettingsMap* map, ControlType value) {
176+
SetHttpsUpgradeControlType(map, value, test_url);
177+
};
178+
auto get = [this](HostContentSettingsMap* map) {
179+
return GetHttpsUpgradeControlType(map, test_url);
180+
};
181+
182+
RunTest<ControlType>(kExpects, std::move(set), std::move(get));
183+
}
184+
185+
TEST_F(BraveShieldsUtilProfilesTest, SetNoScriptControlType) {
186+
constexpr std::pair<ControlType, ControlType> kExpects[] = {
187+
{ALLOW, ALLOW}, {BLOCK, BLOCK}, {DEFAULT, BLOCK}};
188+
189+
auto set = [this](HostContentSettingsMap* map, ControlType value) {
190+
SetNoScriptControlType(map, value, test_url);
191+
};
192+
auto get = [this](HostContentSettingsMap* map) {
193+
return GetNoScriptControlType(map, test_url);
194+
};
195+
196+
RunTest<ControlType>(kExpects, std::move(set), std::move(get));
197+
}
198+
199+
TEST_F(BraveShieldsUtilProfilesTest, SetForgetFirstPartyStorageEnabled) {
200+
constexpr std::pair<bool, bool> kExpects[] = {{true, true}, {false, false}};
201+
202+
auto set = [this](HostContentSettingsMap* map, bool value) {
203+
SetForgetFirstPartyStorageEnabled(map, value, test_url);
204+
};
205+
auto get = [this](HostContentSettingsMap* map) {
206+
return GetForgetFirstPartyStorageEnabled(map, test_url);
207+
};
208+
209+
RunTest<bool>(kExpects, std::move(set), std::move(get));
210+
}
211+
212+
} // namespace brave_shields

browser/brave_shields/domain_block_page_browsertest.cc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
#include "url/gurl.h"
2929

3030
using brave_shields::ControlType;
31-
using brave_shields::ResetBraveShieldsEnabled;
3231
using brave_shields::SetBraveShieldsEnabled;
3332
using brave_shields::SetCosmeticFilteringControlType;
3433
using brave_shields::features::kBraveDomainBlock;

components/brave_shields/content/browser/brave_shields_util.cc

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -237,29 +237,21 @@ void SetBraveShieldsEnabled(HostContentSettingsMap* map,
237237
return;
238238
}
239239

240-
map->SetContentSettingCustomScope(
241-
primary_pattern, ContentSettingsPattern::Wildcard(),
242-
ContentSettingsType::BRAVE_SHIELDS,
243-
// this is 'allow_brave_shields' so 'enable' == 'allow'
244-
enable ? CONTENT_SETTING_ALLOW : CONTENT_SETTING_BLOCK);
245-
246-
RecordShieldsToggled(local_state);
247-
}
248-
249-
void ResetBraveShieldsEnabled(HostContentSettingsMap* map, const GURL& url) {
250-
if (url.is_valid() && !url.SchemeIsHTTPOrHTTPS()) {
251-
return;
252-
}
253-
254-
auto primary_pattern = GetPatternFromURL(url);
255-
256-
if (!primary_pattern.IsValid()) {
257-
return;
240+
// this is 'allow_brave_shields' so 'enable' == 'allow'
241+
const auto setting = enable ? CONTENT_SETTING_ALLOW : CONTENT_SETTING_BLOCK;
242+
if (map->IsOffTheRecord() ||
243+
setting != map->GetDefaultContentSetting(
244+
ContentSettingsType::BRAVE_SHIELDS, nullptr)) {
245+
map->SetContentSettingCustomScope(
246+
primary_pattern, ContentSettingsPattern::Wildcard(),
247+
ContentSettingsType::BRAVE_SHIELDS, setting);
248+
249+
RecordShieldsToggled(local_state);
250+
} else {
251+
map->SetContentSettingCustomScope(
252+
primary_pattern, ContentSettingsPattern::Wildcard(),
253+
ContentSettingsType::BRAVE_SHIELDS, CONTENT_SETTING_DEFAULT);
258254
}
259-
260-
map->SetContentSettingCustomScope(
261-
primary_pattern, ContentSettingsPattern::Wildcard(),
262-
ContentSettingsType::BRAVE_SHIELDS, CONTENT_SETTING_DEFAULT);
263255
}
264256

265257
bool GetBraveShieldsEnabled(HostContentSettingsMap* map, const GURL& url) {
@@ -283,7 +275,8 @@ void SetAdControlType(HostContentSettingsMap* map,
283275
ControlType type,
284276
const GURL& url,
285277
PrefService* local_state) {
286-
DCHECK(type != ControlType::BLOCK_THIRD_PARTY);
278+
DCHECK_NE(type, ControlType::BLOCK_THIRD_PARTY);
279+
DCHECK_NE(type, ControlType::DEFAULT);
287280
auto primary_pattern = GetPatternFromURL(url);
288281

289282
if (!primary_pattern.IsValid()) {
@@ -319,6 +312,7 @@ void SetCosmeticFilteringControlType(HostContentSettingsMap* map,
319312
const GURL& url,
320313
PrefService* local_state,
321314
PrefService* profile_state) {
315+
DCHECK_NE(type, ControlType::DEFAULT);
322316
auto primary_pattern = GetPatternFromURL(url);
323317

324318
if (!primary_pattern.IsValid()) {
@@ -682,6 +676,7 @@ void SetHttpsUpgradeControlType(HostContentSettingsMap* map,
682676
ControlType type,
683677
const GURL& url,
684678
PrefService* local_state) {
679+
DCHECK_NE(type, ControlType::DEFAULT);
685680
if (!url.SchemeIsHTTPOrHTTPS() && !url.is_empty()) {
686681
return;
687682
}
@@ -786,7 +781,7 @@ void SetNoScriptControlType(HostContentSettingsMap* map,
786781
ControlType type,
787782
const GURL& url,
788783
PrefService* local_state) {
789-
DCHECK(type != ControlType::BLOCK_THIRD_PARTY);
784+
DCHECK_NE(type, ControlType::BLOCK_THIRD_PARTY);
790785
auto primary_pattern = GetPatternFromURL(url);
791786

792787
if (!primary_pattern.IsValid()) {

components/brave_shields/content/browser/brave_shields_util.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,6 @@ void SetBraveShieldsEnabled(HostContentSettingsMap* map,
5858
bool enable,
5959
const GURL& url,
6060
PrefService* local_state = nullptr);
61-
// reset to the default value
62-
void ResetBraveShieldsEnabled(HostContentSettingsMap* map, const GURL& url);
6361
bool GetBraveShieldsEnabled(HostContentSettingsMap* map, const GURL& url);
6462

6563
void SetAdControlType(HostContentSettingsMap* map,

test/BUILD.gn

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ test("brave_unit_tests") {
166166
"//brave/browser/autocomplete:unit_tests",
167167
"//brave/browser/brave_ads",
168168
"//brave/browser/brave_ads/device_id:unit_tests",
169+
"//brave/browser/brave_shields:unit_tests",
169170
"//brave/browser/brave_stats:unit_tests",
170171
"//brave/browser/brave_wallet",
171172
"//brave/browser/brave_wallet:unit_tests",

0 commit comments

Comments
 (0)