Skip to content

Commit 7e22c2d

Browse files
authored
Merge pull request #4624 from brave/handle_shields_settings_properly
Handle shields settings properly
2 parents 873b96e + 294b7c1 commit 7e22c2d

21 files changed

+577
-24
lines changed

app/brave_generated_resources.grd

+3
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,9 @@ By installing this extension, you are agreeing to the Google Widevine Terms of U
728728
<message name="IDS_FULL_DISK_ACCESS_CONFIRM_DIALOG_OPEN_PREFS_BUTTON_TEXT" desc="The label for open privacy panel">
729729
Open System Preferences
730730
</message>
731+
<message name="IDS_SETTINGS_SITE_AND_SHIELDS_SETTINGS" desc="Label for site and shields settings in clear browsing data dialog">
732+
Site and Shields Settings
733+
</message>
731734
</messages>
732735
</release>
733736
</grit>

browser/BUILD.gn

+1-2
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,6 @@ source_set("browser_process") {
5656
"brave_tab_helpers.h",
5757
"browser_context_keyed_service_factories.cc",
5858
"browser_context_keyed_service_factories.h",
59-
"browsing_data/brave_clear_browsing_data.cc",
60-
"browsing_data/brave_clear_browsing_data.h",
6159
"component_updater/brave_component_installer.cc",
6260
"component_updater/brave_component_installer.h",
6361
"component_updater/brave_component_updater_configurator.cc",
@@ -165,6 +163,7 @@ source_set("browser_process") {
165163
"//content/public/common",
166164
"//extensions/buildflags",
167165
"//brave/chromium_src:browser",
166+
"browsing_data",
168167
"themes",
169168
"ntp_sponsored_images",
170169
"//services/network/public/cpp",

browser/browsing_data/BUILD.gn

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
source_set("browsing_data") {
2+
sources = [
3+
"brave_browsing_data_remover_delegate.cc",
4+
"brave_browsing_data_remover_delegate.h",
5+
"brave_clear_browsing_data.cc",
6+
"brave_clear_browsing_data.h",
7+
"counters/brave_site_settings_counter.cc",
8+
"counters/brave_site_settings_counter.h",
9+
]
10+
11+
deps = [
12+
"//base",
13+
"//chrome/common",
14+
"//components/browsing_data/core",
15+
"//components/content_settings/core/browser",
16+
"//components/content_settings/core/common",
17+
"//components/prefs",
18+
"//content/public/browser",
19+
]
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/* Copyright (c) 2020 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 http://mozilla.org/MPL/2.0/. */
5+
6+
#include "brave/browser/browsing_data/brave_browsing_data_remover_delegate.h"
7+
8+
#include <utility>
9+
10+
#include "brave/components/content_settings/core/browser/brave_content_settings_pref_provider.h"
11+
#include "brave/components/content_settings/core/browser/brave_content_settings_utils.h"
12+
#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
13+
#include "chrome/browser/profiles/profile.h"
14+
#include "components/content_settings/core/browser/host_content_settings_map.h"
15+
16+
BraveBrowsingDataRemoverDelegate::BraveBrowsingDataRemoverDelegate(
17+
content::BrowserContext* browser_context)
18+
: ChromeBrowsingDataRemoverDelegate(browser_context),
19+
profile_(Profile::FromBrowserContext(browser_context)) {}
20+
21+
void BraveBrowsingDataRemoverDelegate::RemoveEmbedderData(
22+
const base::Time& delete_begin,
23+
const base::Time& delete_end,
24+
int remove_mask,
25+
content::BrowsingDataFilterBuilder* filter_builder,
26+
int origin_type_mask,
27+
base::OnceClosure callback) {
28+
ChromeBrowsingDataRemoverDelegate::RemoveEmbedderData(delete_begin,
29+
delete_end,
30+
remove_mask,
31+
filter_builder,
32+
origin_type_mask,
33+
std::move(callback));
34+
35+
// We do this because ChromeBrowsingDataRemoverDelegate::RemoveEmbedderData()
36+
// doesn't clear shields settings with non all time range.
37+
// The reason is upstream assumes that plugins type only as empty string
38+
// resource ids with plugins type. but we use plugins type to store our
39+
// shields settings with non-empty resource ids.
40+
if (remove_mask & DATA_TYPE_CONTENT_SETTINGS)
41+
ClearShieldsSettings(delete_begin, delete_end);
42+
}
43+
44+
void BraveBrowsingDataRemoverDelegate::ClearShieldsSettings(
45+
base::Time begin_time,
46+
base::Time end_time) {
47+
if (begin_time.is_null() && (end_time.is_null() || end_time.is_max())) {
48+
// For all time range, we don't need to do anything here because
49+
// ChromeBrowsingDataRemoverDelegate::RemoveEmbedderData() nukes whole
50+
// plugins type.
51+
return;
52+
}
53+
54+
auto* map = HostContentSettingsMapFactory::GetForProfile(profile_);
55+
auto* provider =
56+
static_cast<content_settings::BravePrefProvider*>(map->GetPrefProvider());
57+
for (const auto& resource_id : content_settings::GetShieldsResourceIDs()) {
58+
ContentSettingsForOneType settings;
59+
ContentSettingsType content_type = ContentSettingsType::PLUGINS;
60+
map->GetSettingsForOneType(content_type, resource_id, &settings);
61+
for (const ContentSettingPatternSource& setting : settings) {
62+
base::Time last_modified = provider->GetWebsiteSettingLastModified(
63+
setting.primary_pattern, setting.secondary_pattern, content_type,
64+
resource_id);
65+
if (last_modified >= begin_time &&
66+
(last_modified < end_time || end_time.is_null())) {
67+
provider->SetWebsiteSetting(setting.primary_pattern,
68+
setting.secondary_pattern, content_type,
69+
resource_id, nullptr);
70+
}
71+
}
72+
}
73+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/* Copyright (c) 2020 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 http://mozilla.org/MPL/2.0/. */
5+
6+
#ifndef BRAVE_BROWSER_BROWSING_DATA_BRAVE_BROWSING_DATA_REMOVER_DELEGATE_H_
7+
#define BRAVE_BROWSER_BROWSING_DATA_BRAVE_BROWSING_DATA_REMOVER_DELEGATE_H_
8+
9+
#include "base/time/time.h"
10+
#include "chrome/browser/browsing_data/chrome_browsing_data_remover_delegate.h"
11+
12+
namespace content_settings {
13+
class BravePrefProvider;
14+
} // namespace content_settings
15+
16+
class Profile;
17+
18+
class BraveBrowsingDataRemoverDelegate
19+
: public ChromeBrowsingDataRemoverDelegate {
20+
public:
21+
explicit BraveBrowsingDataRemoverDelegate(
22+
content::BrowserContext* browser_context);
23+
~BraveBrowsingDataRemoverDelegate() override = default;
24+
25+
BraveBrowsingDataRemoverDelegate(
26+
const BraveBrowsingDataRemoverDelegate&) = delete;
27+
BraveBrowsingDataRemoverDelegate operator=(
28+
const BraveBrowsingDataRemoverDelegate&) = delete;
29+
30+
private:
31+
FRIEND_TEST_ALL_PREFIXES(BraveBrowsingDataRemoverDelegateTest,
32+
ShieldsSettingsClearTest);
33+
34+
// ChromeBrowsingDataRemoverDelegate overrides:
35+
void RemoveEmbedderData(const base::Time& delete_begin,
36+
const base::Time& delete_end,
37+
int remove_mask,
38+
content::BrowsingDataFilterBuilder* filter_builder,
39+
int origin_type_mask,
40+
base::OnceClosure callback) override;
41+
42+
void ClearShieldsSettings(base::Time begin_time, base::Time end_time);
43+
44+
Profile* profile_;
45+
};
46+
47+
#endif // BRAVE_BROWSER_BROWSING_DATA_BRAVE_BROWSING_DATA_REMOVER_DELEGATE_H_
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/* Copyright (c) 2020 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 http://mozilla.org/MPL/2.0/. */
5+
6+
#include "brave/browser/browsing_data/brave_browsing_data_remover_delegate.h"
7+
8+
#include <memory>
9+
#include <string>
10+
#include <utility>
11+
12+
#include "base/bind.h"
13+
#include "brave/components/brave_shields/browser/brave_shields_util.h"
14+
#include "brave/components/brave_shields/common/brave_shield_constants.h"
15+
#include "brave/components/content_settings/core/browser/brave_content_settings_utils.h"
16+
#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
17+
#include "chrome/test/base/testing_profile.h"
18+
#include "components/content_settings/core/browser/host_content_settings_map.h"
19+
#include "content/public/test/browser_task_environment.h"
20+
#include "testing/gtest/include/gtest/gtest.h"
21+
#include "url/gurl.h"
22+
23+
namespace {
24+
const GURL kBraveURL("https://www.brave.com");
25+
const GURL kBatURL("https://basicattentiontoken.org");
26+
const GURL kGoogleURL("https://www.google.com");
27+
const GURL kAbcURL("https://www.abc.com");
28+
} // namespace
29+
30+
class BraveBrowsingDataRemoverDelegateTest : public testing::Test {
31+
public:
32+
void SetUp() override {
33+
profile_ = std::make_unique<TestingProfile>();
34+
map_ = HostContentSettingsMapFactory::GetForProfile(profile());
35+
}
36+
37+
Profile* profile() { return profile_.get(); }
38+
39+
HostContentSettingsMap* map() { return map_.get(); }
40+
41+
BraveBrowsingDataRemoverDelegate* delegate() {
42+
return static_cast<BraveBrowsingDataRemoverDelegate*>(
43+
profile()->GetBrowsingDataRemoverDelegate());
44+
}
45+
46+
int GetShieldsSettingsCount() {
47+
int shields_settings_count = 0;
48+
for (const auto& resource_id : content_settings::GetShieldsResourceIDs()) {
49+
ContentSettingsForOneType settings;
50+
ContentSettingsType content_type = ContentSettingsType::PLUGINS;
51+
map()->GetSettingsForOneType(content_type, resource_id, &settings);
52+
shields_settings_count += settings.size();
53+
}
54+
return shields_settings_count;
55+
}
56+
57+
private:
58+
content::BrowserTaskEnvironment task_environment_;
59+
60+
std::unique_ptr<TestingProfile> profile_;
61+
scoped_refptr<HostContentSettingsMap> map_;
62+
};
63+
64+
TEST_F(BraveBrowsingDataRemoverDelegateTest, ShieldsSettingsClearTest) {
65+
// Four settings are added.
66+
// First two settings are shields settings in PLUGINS type.
67+
// Javascript is not counted as shields type because it's stored to
68+
// JAVASCRIPT type instead of PLUGINS type.
69+
// Last one is PLUGINS type but it's flash resource not shields resource.
70+
map()->SetContentSettingDefaultScope(
71+
kBraveURL, GURL(), ContentSettingsType::PLUGINS,
72+
brave_shields::kHTTPUpgradableResources, CONTENT_SETTING_ALLOW);
73+
map()->SetContentSettingDefaultScope(
74+
kBatURL, GURL(), ContentSettingsType::PLUGINS,
75+
brave_shields::kFingerprinting, CONTENT_SETTING_ALLOW);
76+
map()->SetContentSettingCustomScope(
77+
brave_shields::GetPatternFromURL(kGoogleURL, true),
78+
ContentSettingsPattern::Wildcard(),
79+
ContentSettingsType::JAVASCRIPT, "", CONTENT_SETTING_BLOCK);
80+
map()->SetContentSettingDefaultScope(
81+
kAbcURL, GURL(), ContentSettingsType::PLUGINS,
82+
"", CONTENT_SETTING_ALLOW);
83+
84+
const base::Time kNow = base::Time::Now();
85+
const base::Time k1DaysOld = kNow - base::TimeDelta::FromDays(1);
86+
87+
// Check current shields settings count is 2 and zero after clearing 1 day
88+
// time range.
89+
EXPECT_EQ(2, GetShieldsSettingsCount());
90+
delegate()->ClearShieldsSettings(k1DaysOld, kNow);
91+
EXPECT_EQ(0, GetShieldsSettingsCount());
92+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/* Copyright (c) 2020 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 http://mozilla.org/MPL/2.0/. */
5+
6+
#include "brave/browser/browsing_data/counters/brave_site_settings_counter.h"
7+
8+
#include <set>
9+
#include <string>
10+
11+
#include "brave/components/content_settings/core/browser/brave_content_settings_pref_provider.h"
12+
#include "brave/components/content_settings/core/browser/brave_content_settings_utils.h"
13+
#include "components/browsing_data/core/pref_names.h"
14+
#include "components/content_settings/core/browser/content_settings_registry.h"
15+
#include "components/content_settings/core/common/content_settings_pattern.h"
16+
17+
BraveSiteSettingsCounter::BraveSiteSettingsCounter(
18+
HostContentSettingsMap* map,
19+
content::HostZoomMap* zoom_map,
20+
ProtocolHandlerRegistry* handler_registry,
21+
PrefService* pref_service)
22+
: SiteSettingsCounter(map, zoom_map, handler_registry, pref_service),
23+
map_(map) {}
24+
25+
BraveSiteSettingsCounter::~BraveSiteSettingsCounter() = default;
26+
27+
int BraveSiteSettingsCounter::CountShieldsSettings() {
28+
std::set<std::string> hosts;
29+
int empty_host_pattern = 0;
30+
base::Time period_start = GetPeriodStart();
31+
base::Time period_end = GetPeriodEnd();
32+
33+
auto iterate_content_settings_list =
34+
[&](ContentSettingsType content_type,
35+
const ContentSettingsForOneType& content_settings_list,
36+
const std::string& resource_identifier) {
37+
for (const auto& content_setting : content_settings_list) {
38+
// We don't care other source except preference because all shields
39+
// settings are stored in pref storage.
40+
if (content_setting.source != "preference")
41+
continue;
42+
43+
base::Time last_modified;
44+
DCHECK_EQ(ContentSettingsType::PLUGINS, content_type);
45+
// Fetching last time for specific resource ids.
46+
last_modified =
47+
map_->GetPrefProvider()->GetWebsiteSettingLastModified(
48+
content_setting.primary_pattern,
49+
content_setting.secondary_pattern,
50+
ContentSettingsType::PLUGINS,
51+
resource_identifier);
52+
if (last_modified >= period_start && last_modified < period_end) {
53+
if (content_setting.primary_pattern.GetHost().empty())
54+
empty_host_pattern++;
55+
else
56+
hosts.insert(content_setting.primary_pattern.GetHost());
57+
}
58+
}
59+
};
60+
61+
auto* registry = content_settings::ContentSettingsRegistry::GetInstance();
62+
for (const content_settings::ContentSettingsInfo* info : *registry) {
63+
ContentSettingsType type = info->website_settings_info()->type();
64+
ContentSettingsForOneType content_settings_list;
65+
if (type == ContentSettingsType::PLUGINS) {
66+
for (const auto& id : content_settings::GetShieldsResourceIDs()) {
67+
map_->GetSettingsForOneType(type, id, &content_settings_list);
68+
iterate_content_settings_list(type, content_settings_list, id);
69+
}
70+
continue;
71+
}
72+
}
73+
74+
return hosts.size() + empty_host_pattern;
75+
}
76+
77+
void BraveSiteSettingsCounter::ReportResult(ResultInt value) {
78+
SiteSettingsCounter::ReportResult(value + CountShieldsSettings());
79+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/* Copyright (c) 2020 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 http://mozilla.org/MPL/2.0/. */
5+
6+
#ifndef BRAVE_BROWSER_BROWSING_DATA_COUNTERS_BRAVE_SITE_SETTINGS_COUNTER_H_
7+
#define BRAVE_BROWSER_BROWSING_DATA_COUNTERS_BRAVE_SITE_SETTINGS_COUNTER_H_
8+
9+
#include "chrome/browser/browsing_data/counters/site_settings_counter.h"
10+
11+
// This class adds shieldss settings count
12+
class BraveSiteSettingsCounter : public SiteSettingsCounter {
13+
public:
14+
BraveSiteSettingsCounter(HostContentSettingsMap* map,
15+
content::HostZoomMap* zoom_map,
16+
ProtocolHandlerRegistry* handler_registry,
17+
PrefService* pref_service);
18+
~BraveSiteSettingsCounter() override;
19+
BraveSiteSettingsCounter(const BraveSiteSettingsCounter&) = delete;
20+
BraveSiteSettingsCounter& operator=(const BraveSiteSettingsCounter&) = delete;
21+
22+
private:
23+
// SiteSettingsCounter overrides:
24+
void ReportResult(ResultInt value) override;
25+
26+
int CountShieldsSettings();
27+
28+
scoped_refptr<HostContentSettingsMap> map_;
29+
};
30+
31+
#endif // BRAVE_BROWSER_BROWSING_DATA_COUNTERS_BRAVE_SITE_SETTINGS_COUNTER_H_

0 commit comments

Comments
 (0)