Skip to content

Commit

Permalink
Subclass ChromeBrowsingDataRemoverDelegate
Browse files Browse the repository at this point in the history
We don't need to subclass HostContentsSettingsMap to clear shields
settings. BraveBrowsingDataRemoverDelegateTest is added.
  • Loading branch information
simonhong committed Feb 18, 2020
1 parent dfa51b5 commit e551824
Show file tree
Hide file tree
Showing 22 changed files with 252 additions and 174 deletions.
4 changes: 3 additions & 1 deletion browser/browsing_data/BUILD.gn
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
source_set("browsing_data") {
sources = [
"brave_browsing_data_remover_delegate.cc",
"brave_browsing_data_remover_delegate.h",
"brave_clear_browsing_data.cc",
"brave_clear_browsing_data.h",
"counters/brave_site_settings_counter.cc",
Expand All @@ -9,10 +11,10 @@ source_set("browsing_data") {
deps = [
"//base",
"//chrome/common",
"//content/public/browser",
"//components/browsing_data/core",
"//components/content_settings/core/browser",
"//components/content_settings/core/common",
"//components/prefs",
"//content/public/browser",
]
}
73 changes: 73 additions & 0 deletions browser/browsing_data/brave_browsing_data_remover_delegate.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/* Copyright (c) 2020 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/browser/browsing_data/brave_browsing_data_remover_delegate.h"

#include <utility>

#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"
#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "components/content_settings/core/browser/host_content_settings_map.h"

BraveBrowsingDataRemoverDelegate::BraveBrowsingDataRemoverDelegate(
content::BrowserContext* browser_context)
: ChromeBrowsingDataRemoverDelegate(browser_context),
profile_(Profile::FromBrowserContext(browser_context)) {}

void BraveBrowsingDataRemoverDelegate::RemoveEmbedderData(
const base::Time& delete_begin,
const base::Time& delete_end,
int remove_mask,
content::BrowsingDataFilterBuilder* filter_builder,
int origin_type_mask,
base::OnceClosure callback) {
ChromeBrowsingDataRemoverDelegate::RemoveEmbedderData(delete_begin,
delete_end,
remove_mask,
filter_builder,
origin_type_mask,
std::move(callback));

// We do this because ChromeBrowsingDataRemoverDelegate::RemoveEmbedderData()
// doesn't clear shields settings with non all time range.
// The reason is upstream assumes that plugins type only as empty string
// resource ids with plugins type. but we use plugins type to store our
// shields settings with non-empty resource ids.
if (remove_mask & DATA_TYPE_CONTENT_SETTINGS)
ClearShieldsSettings(delete_begin, delete_end);
}

void BraveBrowsingDataRemoverDelegate::ClearShieldsSettings(
base::Time begin_time,
base::Time end_time) {
if (begin_time.is_null() && (end_time.is_null() || end_time.is_max())) {
// For all time range, we don't need to do anything here because
// ChromeBrowsingDataRemoverDelegate::RemoveEmbedderData() nukes whole
// plugins type.
return;
}

auto* map = HostContentSettingsMapFactory::GetForProfile(profile_);
auto* provider =
static_cast<content_settings::BravePrefProvider*>(map->GetPrefProvider());
for (const auto& resource_id : content_settings::GetShieldsResourceIDs()) {
ContentSettingsForOneType settings;
ContentSettingsType content_type = ContentSettingsType::PLUGINS;
map->GetSettingsForOneType(content_type, resource_id, &settings);
for (const ContentSettingPatternSource& setting : settings) {
base::Time last_modified = provider->GetWebsiteSettingLastModified(
setting.primary_pattern, setting.secondary_pattern, content_type,
resource_id);
if (last_modified >= begin_time &&
(last_modified < end_time || end_time.is_null())) {
provider->SetWebsiteSetting(setting.primary_pattern,
setting.secondary_pattern, content_type,
resource_id, nullptr);
}
}
}
}
47 changes: 47 additions & 0 deletions browser/browsing_data/brave_browsing_data_remover_delegate.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/* Copyright (c) 2020 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/. */

#ifndef BRAVE_BROWSER_BROWSING_DATA_BRAVE_BROWSING_DATA_REMOVER_DELEGATE_H_
#define BRAVE_BROWSER_BROWSING_DATA_BRAVE_BROWSING_DATA_REMOVER_DELEGATE_H_

#include "base/time/time.h"
#include "chrome/browser/browsing_data/chrome_browsing_data_remover_delegate.h"

namespace content_settings {
class BravePrefProvider;
} // namespace content_settings

class Profile;

class BraveBrowsingDataRemoverDelegate
: public ChromeBrowsingDataRemoverDelegate {
public:
explicit BraveBrowsingDataRemoverDelegate(
content::BrowserContext* browser_context);
~BraveBrowsingDataRemoverDelegate() override = default;

BraveBrowsingDataRemoverDelegate(
const BraveBrowsingDataRemoverDelegate&) = delete;
BraveBrowsingDataRemoverDelegate operator=(
const BraveBrowsingDataRemoverDelegate&) = delete;

private:
FRIEND_TEST_ALL_PREFIXES(BraveBrowsingDataRemoverDelegateTest,
ShieldsSettingsClearTest);

// ChromeBrowsingDataRemoverDelegate overrides:
void RemoveEmbedderData(const base::Time& delete_begin,
const base::Time& delete_end,
int remove_mask,
content::BrowsingDataFilterBuilder* filter_builder,
int origin_type_mask,
base::OnceClosure callback) override;

void ClearShieldsSettings(base::Time begin_time, base::Time end_time);

Profile* profile_;
};

#endif // BRAVE_BROWSER_BROWSING_DATA_BRAVE_BROWSING_DATA_REMOVER_DELEGATE_H_
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/* Copyright (c) 2020 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/browser/browsing_data/brave_browsing_data_remover_delegate.h"

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

#include "base/bind.h"
#include "brave/components/brave_shields/browser/brave_shields_util.h"
#include "brave/components/brave_shields/common/brave_shield_constants.h"
#include "brave/components/content_settings/core/browser/brave_content_settings_utils.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 "content/public/test/browser_task_environment.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"

namespace {
const GURL kBraveURL("https://www.brave.com");
const GURL kBatURL("https://basicattentiontoken.org");
const GURL kGoogleURL("https://www.google.com");
const GURL kAbcURL("https://www.abc.com");
} // namespace

class BraveBrowsingDataRemoverDelegateTest : public testing::Test {
public:
void SetUp() override {
profile_ = std::make_unique<TestingProfile>();
map_ = HostContentSettingsMapFactory::GetForProfile(profile());
}

Profile* profile() { return profile_.get(); }

HostContentSettingsMap* map() { return map_.get(); }

BraveBrowsingDataRemoverDelegate* delegate() {
return static_cast<BraveBrowsingDataRemoverDelegate*>(
profile()->GetBrowsingDataRemoverDelegate());
}

int GetShieldsSettingsCount() {
int shields_settings_count = 0;
for (const auto& resource_id : content_settings::GetShieldsResourceIDs()) {
ContentSettingsForOneType settings;
ContentSettingsType content_type = ContentSettingsType::PLUGINS;
map()->GetSettingsForOneType(content_type, resource_id, &settings);
shields_settings_count += settings.size();
}
return shields_settings_count;
}

private:
content::BrowserTaskEnvironment task_environment_;

std::unique_ptr<TestingProfile> profile_;
scoped_refptr<HostContentSettingsMap> map_;
};

TEST_F(BraveBrowsingDataRemoverDelegateTest, ShieldsSettingsClearTest) {
// Four settings are added.
// First two settings are shields settings in PLUGINS type.
// Javascript is not counted as shields type because it's stored to
// JAVASCRIPT type instead of PLUGINS type.
// Last one is PLUGINS type but it's flash resource not shields resource.
map()->SetContentSettingDefaultScope(
kBraveURL, GURL(), ContentSettingsType::PLUGINS,
brave_shields::kHTTPUpgradableResources, CONTENT_SETTING_ALLOW);
map()->SetContentSettingDefaultScope(
kBatURL, GURL(), ContentSettingsType::PLUGINS,
brave_shields::kFingerprinting, CONTENT_SETTING_ALLOW);
map()->SetContentSettingCustomScope(
brave_shields::GetPatternFromURL(kGoogleURL, true),
ContentSettingsPattern::Wildcard(),
ContentSettingsType::JAVASCRIPT, "", CONTENT_SETTING_BLOCK);
map()->SetContentSettingDefaultScope(
kAbcURL, GURL(), ContentSettingsType::PLUGINS,
"", CONTENT_SETTING_ALLOW);

const base::Time kNow = base::Time::Now();
const base::Time k1DaysOld = kNow - base::TimeDelta::FromDays(1);

// Check current shields settings count is 2 and zero after clearing 1 day
// time range.
EXPECT_EQ(2, GetShieldsSettingsCount());
delegate()->ClearShieldsSettings(k1DaysOld, kNow);
EXPECT_EQ(0, GetShieldsSettingsCount());
}
10 changes: 6 additions & 4 deletions browser/browsing_data/counters/brave_site_settings_counter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <set>
#include <string>

#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"
#include "components/browsing_data/core/pref_names.h"
#include "components/content_settings/core/browser/content_settings_registry.h"
Expand All @@ -18,9 +19,8 @@ BraveSiteSettingsCounter::BraveSiteSettingsCounter(
content::HostZoomMap* zoom_map,
ProtocolHandlerRegistry* handler_registry,
PrefService* pref_service)
: SiteSettingsCounter(map, zoom_map, handler_registry, pref_service) {
map_ = static_cast<BraveHostContentSettingsMap*>(map);
}
: SiteSettingsCounter(map, zoom_map, handler_registry, pref_service),
map_(map) {}

BraveSiteSettingsCounter::~BraveSiteSettingsCounter() = default;

Expand All @@ -43,9 +43,11 @@ int BraveSiteSettingsCounter::CountShieldsSettings() {
base::Time last_modified;
DCHECK_EQ(ContentSettingsType::PLUGINS, content_type);
// Fetching last time for specific resource ids.
last_modified = map_->GetShieldsSettingLastModifiedDate(
last_modified =
map_->GetPrefProvider()->GetWebsiteSettingLastModified(
content_setting.primary_pattern,
content_setting.secondary_pattern,
ContentSettingsType::PLUGINS,
resource_identifier);
if (last_modified >= period_start && last_modified < period_end) {
if (content_setting.primary_pattern.GetHost().empty())
Expand Down
3 changes: 1 addition & 2 deletions browser/browsing_data/counters/brave_site_settings_counter.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#ifndef BRAVE_BROWSER_BROWSING_DATA_COUNTERS_BRAVE_SITE_SETTINGS_COUNTER_H_
#define BRAVE_BROWSER_BROWSING_DATA_COUNTERS_BRAVE_SITE_SETTINGS_COUNTER_H_

#include "brave/components/content_settings/core/browser/brave_host_content_settings_map.h"
#include "chrome/browser/browsing_data/counters/site_settings_counter.h"

// This class adds shieldss settings count
Expand All @@ -26,7 +25,7 @@ class BraveSiteSettingsCounter : public SiteSettingsCounter {

int CountShieldsSettings();

scoped_refptr<BraveHostContentSettingsMap> map_;
scoped_refptr<HostContentSettingsMap> map_;
};

#endif // BRAVE_BROWSER_BROWSING_DATA_COUNTERS_BRAVE_SITE_SETTINGS_COUNTER_H_
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
#include "base/test/simple_test_clock.h"
#include "brave/components/brave_shields/browser/brave_shields_util.h"
#include "brave/components/brave_shields/common/brave_shield_constants.h"
#include "brave/components/content_settings/core/browser/brave_host_content_settings_map.h"
#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
#include "chrome/browser/custom_handlers/protocol_handler_registry.h"
#include "chrome/browser/custom_handlers/protocol_handler_registry_factory.h"
#include "chrome/browser/custom_handlers/test_protocol_handler_registry_delegate.h"
#include "chrome/test/base/testing_profile.h"
#include "components/browsing_data/core/browsing_data_utils.h"
#include "components/browsing_data/core/pref_names.h"
#include "components/content_settings/core/browser/host_content_settings_map.h"
#include "components/prefs/pref_service.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gtest/include/gtest/gtest.h"
Expand All @@ -41,8 +41,7 @@ class BraveSiteSettingsCounterTest : public testing::Test {
public:
void SetUp() override {
profile_ = std::make_unique<TestingProfile>();
map_ = static_cast<BraveHostContentSettingsMap*>(
HostContentSettingsMapFactory::GetForProfile(profile()));
map_ = HostContentSettingsMapFactory::GetForProfile(profile());
#if !defined(OS_ANDROID)
auto* zoom_map =
content::HostZoomMap::GetDefaultForBrowserContext(profile());
Expand All @@ -61,7 +60,7 @@ class BraveSiteSettingsCounterTest : public testing::Test {

Profile* profile() { return profile_.get(); }

BraveHostContentSettingsMap* map() { return map_.get(); }
HostContentSettingsMap* map() { return map_.get(); }

BraveSiteSettingsCounter* counter() { return counter_.get(); }

Expand All @@ -84,7 +83,7 @@ class BraveSiteSettingsCounterTest : public testing::Test {
content::BrowserTaskEnvironment task_environment_;
std::unique_ptr<TestingProfile> profile_;

scoped_refptr<BraveHostContentSettingsMap> map_;
scoped_refptr<HostContentSettingsMap> map_;
std::unique_ptr<ProtocolHandlerRegistry> handler_registry_;
std::unique_ptr<BraveSiteSettingsCounter> counter_;
bool finished_;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/* Copyright (c) 2020 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/browser/browsing_data/brave_browsing_data_remover_delegate.h"

#include "../../../../../chrome/browser/browsing_data/chrome_browsing_data_remover_delegate_factory.cc"
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
#include "brave/browser/browsing_data/counters/brave_site_settings_counter.h"

#define SiteSettingsCounter BraveSiteSettingsCounter
#include "../../../../../../chrome/browser/browsing_data/counters/browsing_data_counter_factory.cc" // NOLINT
#include "../../../../../../chrome/browser/browsing_data/counters/browsing_data_counter_factory.cc"
#undef SiteSettingsCounter
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "brave/browser/profiles/profile_util.h"
#include "brave/components/content_settings/core/browser/brave_host_content_settings_map.h"

#define BRAVE_BUILD_SERVICE_INSTANCE_FOR brave::IsSessionProfile(profile) ||
#include "../../../../../chrome/browser/content_settings/host_content_settings_map_factory.cc" // NOLINT
#include "../../../../../chrome/browser/content_settings/host_content_settings_map_factory.cc"
#undef BRAVE_BUILD_SERVICE_INSTANCE_FOR

This file was deleted.

2 changes: 0 additions & 2 deletions components/content_settings/core/browser/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ source_set("browser") {
"brave_content_settings_pref_provider.h",
"brave_content_settings_utils.cc",
"brave_content_settings_utils.h",
"brave_host_content_settings_map.cc",
"brave_host_content_settings_map.h",
]

deps = [
Expand Down
Loading

0 comments on commit e551824

Please sign in to comment.