-
Notifications
You must be signed in to change notification settings - Fork 889
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Subclass ChromeBrowsingDataRemoverDelegate
We don't need to subclass HostContentsSettingsMap to clear shields settings. BraveBrowsingDataRemoverDelegateTest is added.
- Loading branch information
Showing
22 changed files
with
252 additions
and
174 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
browser/browsing_data/brave_browsing_data_remover_delegate.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
47
browser/browsing_data/brave_browsing_data_remover_delegate.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_ |
92 changes: 92 additions & 0 deletions
92
browser/browsing_data/brave_browsing_data_remover_delegate_unittest.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 0 additions & 14 deletions
14
chromium_src/chrome/browser/browsing_data/chrome_browsing_data_remover_delegate.cc
This file was deleted.
Oops, something went wrong.
8 changes: 8 additions & 0 deletions
8
chromium_src/chrome/browser/browsing_data/chrome_browsing_data_remover_delegate_factory.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 0 additions & 17 deletions
17
chromium_src/components/content_settings/core/browser/host_content_settings_map.h
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.