Skip to content

Commit

Permalink
Do not removed shields settings with empty primary pattern.
Browse files Browse the repository at this point in the history
  • Loading branch information
boocmp committed Feb 27, 2025
1 parent 8355412 commit ab34e7d
Show file tree
Hide file tree
Showing 9 changed files with 132 additions and 21 deletions.
1 change: 1 addition & 0 deletions browser/browsing_data/DEPS
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
include_rules = [
"+brave/components/content_settings/core/browser",
"+brave/components/content_settings/core/common",
]
48 changes: 45 additions & 3 deletions browser/browsing_data/brave_browsing_data_remover_delegate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@

#include "brave/browser/browsing_data/brave_browsing_data_remover_delegate.h"

#include <memory>
#include <utility>
#include <vector>

#include "base/containers/flat_map.h"
#include "brave/browser/ai_chat/ai_chat_service_factory.h"
#include "brave/browser/brave_news/brave_news_controller_factory.h"
#include "brave/components/ai_chat/core/browser/ai_chat_service.h"
#include "brave/components/ai_chat/core/browser/utils.h"
#include "brave/components/ai_chat/core/common/features.h"
#include "brave/components/brave_news/browser/brave_news_controller.h"
#include "brave/components/content_settings/core/browser/brave_content_settings_pref_provider.h"
#include "brave/components/content_settings/core/browser/brave_content_settings_utils.h"
Expand All @@ -25,6 +24,47 @@
#include "components/content_settings/core/browser/host_content_settings_map.h"
#include "content/public/browser/browsing_data_remover.h"

namespace {

class ContentSettingsDefaultsKeeper {
public:
explicit ContentSettingsDefaultsKeeper(Profile* profile) : profile_(profile) {
auto* map = HostContentSettingsMapFactory::GetForProfile(profile_);

const auto shields_content_types =
content_settings::GetShieldsContentSettingsTypes();
for (const auto content_type : shields_content_types) {
ContentSettingsForOneType settings =
map->GetSettingsForOneType(content_type);
for (const auto& setting : settings) {
if (setting.source !=
content_settings::ProviderType::kDefaultProvider &&
setting.primary_pattern.MatchesAllHosts()) {
defaults_[content_type].push_back(setting);
}
}
}
}

~ContentSettingsDefaultsKeeper() {
auto* map = HostContentSettingsMapFactory::GetForProfile(profile_);
for (auto&& [content_type, settings] : defaults_) {
for (auto&& setting : settings) {
map->SetWebsiteSettingCustomScope(
setting.primary_pattern, setting.secondary_pattern, content_type,
std::move(setting.setting_value));
}
}
}

private:
raw_ptr<Profile> profile_ = nullptr;
base::flat_map<ContentSettingsType, std::vector<ContentSettingPatternSource>>
defaults_;
};

} // namespace

BraveBrowsingDataRemoverDelegate::BraveBrowsingDataRemoverDelegate(
content::BrowserContext* browser_context)
: ChromeBrowsingDataRemoverDelegate(browser_context),
Expand All @@ -39,6 +79,8 @@ void BraveBrowsingDataRemoverDelegate::RemoveEmbedderData(
content::BrowsingDataFilterBuilder* filter_builder,
uint64_t origin_type_mask,
base::OnceCallback<void(/*failed_data_types=*/uint64_t)> callback) {
ContentSettingsDefaultsKeeper default_values_keeper(profile_);

ChromeBrowsingDataRemoverDelegate::RemoveEmbedderData(
delete_begin, delete_end, remove_mask, filter_builder, origin_type_mask,
std::move(callback));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,16 @@
#include "brave/browser/browsing_data/brave_browsing_data_remover_delegate.h"

#include <memory>
#include <string>
#include <utility>

#include "base/functional/bind.h"
#include "base/test/bind.h"
#include "brave/components/brave_shields/content/browser/brave_shields_util.h"
#include "brave/components/brave_shields/core/common/brave_shield_constants.h"
#include "brave/components/content_settings/core/browser/brave_content_settings_utils.h"
#include "chrome/browser/browsing_data/chrome_browsing_data_remover_constants.h"
#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
#include "chrome/test/base/testing_profile.h"
#include "components/content_settings/core/browser/host_content_settings_map.h"
#include "components/content_settings/core/common/content_settings_types.h"
#include "content/public/browser/browsing_data_filter_builder.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
Expand Down Expand Up @@ -85,3 +84,31 @@ TEST_F(BraveBrowsingDataRemoverDelegateTest, ShieldsSettingsClearTest) {
delegate()->ClearShieldsSettings(k1DaysOld, kNow);
EXPECT_EQ(start_count, GetShieldsSettingsCount());
}

TEST_F(BraveBrowsingDataRemoverDelegateTest, ShieldsSettingsKeepDefaults) {
brave_shields::SetAdControlType(map(), brave_shields::ControlType::BLOCK,
GURL());
brave_shields::SetCosmeticFilteringControlType(
map(), brave_shields::ControlType::BLOCK, GURL());

EXPECT_EQ(brave_shields::DomainBlockingType::kAggressive,
brave_shields::GetDomainBlockingType(map(), GURL()));
auto filter_builder = content::BrowsingDataFilterBuilder::Create(
content::BrowsingDataFilterBuilder::Mode::kPreserve);
base::RunLoop run_loop;
profile()->GetBrowsingDataRemoverDelegate()->RemoveEmbedderData(
/*delete_begin=*/base::Time::Min(),
/*delete_end=*/base::Time::Max(),
/*remove_mask=*/
chrome_browsing_data_remover::DATA_TYPE_CONTENT_SETTINGS,
filter_builder.get(),
/*origin_type_mask=*/1,
base::BindLambdaForTesting([&run_loop](uint64_t failed_data_types) {
EXPECT_EQ(failed_data_types, 0U);
run_loop.Quit();
}));
run_loop.Run();

EXPECT_EQ(brave_shields::DomainBlockingType::kAggressive,
brave_shields::GetDomainBlockingType(map(), GURL()));
}
1 change: 1 addition & 0 deletions browser/browsing_data/sources.gni
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ brave_browser_browsing_data_deps = [
"//base",
"//brave/browser/ai_chat",
"//brave/components/ai_chat/core/browser",
"//brave/components/content_settings/core/common",
"//chrome/browser:browser_process",
"//chrome/browser/browsing_data:constants",
"//chrome/browser/profiles:profile",
Expand Down
3 changes: 3 additions & 0 deletions chromium_src/chrome/browser/browsing_data/counters/DEPS
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
include_rules = [
"+brave/components/content_settings/core/browser",
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/* Copyright (c) 2025 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 https://mozilla.org/MPL/2.0/. */

#include "chrome/browser/browsing_data/counters/site_settings_counter.h"

#include "brave/components/content_settings/core/browser/brave_content_settings_utils.h"

namespace {

int GetBraveShieldsDefaultsCount(base::Time period_start,
base::Time period_end,
HostContentSettingsMap* map) {
int empty_host_pattern = 0;
auto iterate_content_settings_list =
[&](ContentSettingsType content_type,
const ContentSettingsForOneType& content_settings_list) {
for (const auto& content_setting : content_settings_list) {
if (content_settings::GetSettingSourceFromProviderType(
content_setting.source) ==
content_settings::SettingSource::kUser &&
content_setting.source !=
content_settings::ProviderType::kDefaultProvider) {
base::Time last_modified = content_setting.metadata.last_modified();
if (last_modified >= period_start && last_modified < period_end) {
if (content_setting.primary_pattern.GetHost().empty()) {
empty_host_pattern++;
}
}
}
}
};

for (const auto type : content_settings::GetShieldsContentSettingsTypes()) {
iterate_content_settings_list(type, map->GetSettingsForOneType(type));
}

return empty_host_pattern;
}

} // namespace

#define ReportResult(...) \
ReportResult(__VA_ARGS__ - GetBraveShieldsDefaultsCount( \
period_start, period_end, map_.get()))

#include "src/chrome/browser/browsing_data/counters/site_settings_counter.cc"

#undef ReportResult

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include <string>
#include <vector>

#include "base/time/time.h"
#include "components/content_settings/core/common/content_settings.h"
#include "components/content_settings/core/common/content_settings_constraints.h"
#include "components/content_settings/core/common/content_settings_types.h"
Expand Down
1 change: 1 addition & 0 deletions test/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ test("brave_unit_tests") {
"//chrome:resources",
"//chrome:strings",
"//chrome/app:command_ids",
"//chrome/browser/browsing_data:constants",
"//chrome/browser/content_settings:content_settings_factory",
"//chrome/browser/favicon",
"//chrome/browser/permissions",
Expand Down

0 comments on commit ab34e7d

Please sign in to comment.