Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle shields settings properly #4624

Merged
merged 4 commits into from
Feb 21, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Clear shields settings via clear browsing data dialog
So far, shields settings are also cleard with "All time" time range.
With "All time" range option, browser nuke whole plugins type data.
With non "All time" range option, browser only clears plugins type for
empty resource ids which is flash type.
This commit makes browser clear shields data also with non "All time" range.
simonhong committed Feb 20, 2020
commit a85a399a16df59b99b18382e2a4106d63e1a32c1
3 changes: 1 addition & 2 deletions browser/BUILD.gn
Original file line number Diff line number Diff line change
@@ -56,8 +56,6 @@ source_set("browser_process") {
"brave_tab_helpers.h",
"browser_context_keyed_service_factories.cc",
"browser_context_keyed_service_factories.h",
"browsing_data/brave_clear_browsing_data.cc",
"browsing_data/brave_clear_browsing_data.h",
"component_updater/brave_component_installer.cc",
"component_updater/brave_component_installer.h",
"component_updater/brave_component_updater_configurator.cc",
@@ -165,6 +163,7 @@ source_set("browser_process") {
"//content/public/common",
"//extensions/buildflags",
"//brave/chromium_src:browser",
"browsing_data",
"themes",
"ntp_sponsored_images",
"//services/network/public/cpp",
18 changes: 18 additions & 0 deletions browser/browsing_data/BUILD.gn
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
source_set("browsing_data") {
sources = [
"brave_clear_browsing_data.cc",
"brave_clear_browsing_data.h",
"counters/brave_site_settings_counter.cc",
"counters/brave_site_settings_counter.h",
]

deps = [
"//base",
"//chrome/common",
"//content/public/browser",
"//components/browsing_data/core",
"//components/content_settings/core/browser",
"//components/content_settings/core/common",
"//components/prefs",
]
}
77 changes: 77 additions & 0 deletions browser/browsing_data/counters/brave_site_settings_counter.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/* 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/counters/brave_site_settings_counter.h"

#include <set>
#include <string>

#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"
#include "components/content_settings/core/common/content_settings_pattern.h"

BraveSiteSettingsCounter::BraveSiteSettingsCounter(
HostContentSettingsMap* map,
content::HostZoomMap* zoom_map,
ProtocolHandlerRegistry* handler_registry,
PrefService* pref_service)
: SiteSettingsCounter(map, zoom_map, handler_registry, pref_service) {
map_ = static_cast<BraveHostContentSettingsMap*>(map);
}

BraveSiteSettingsCounter::~BraveSiteSettingsCounter() = default;

int BraveSiteSettingsCounter::CountShieldsSettings() {
std::set<std::string> hosts;
int empty_host_pattern = 0;
base::Time period_start = GetPeriodStart();
base::Time period_end = GetPeriodEnd();

auto iterate_content_settings_list =
[&](ContentSettingsType content_type,
const ContentSettingsForOneType& content_settings_list,
const std::string& resource_identifier) {
for (const auto& content_setting : content_settings_list) {
// We don't care other source except preference because all shields
// settings are stored in pref storage.
if (content_setting.source != "preference")
continue;

base::Time last_modified;
DCHECK_EQ(ContentSettingsType::PLUGINS, content_type);
// Fetching last time for specific resource ids.
last_modified = map_->GetShieldsSettingLastModifiedDate(
content_setting.primary_pattern,
content_setting.secondary_pattern,
resource_identifier);
if (last_modified >= period_start && last_modified < period_end) {
if (content_setting.primary_pattern.GetHost().empty())
empty_host_pattern++;
else
hosts.insert(content_setting.primary_pattern.GetHost());
}
}
};

auto* registry = content_settings::ContentSettingsRegistry::GetInstance();
for (const content_settings::ContentSettingsInfo* info : *registry) {
ContentSettingsType type = info->website_settings_info()->type();
ContentSettingsForOneType content_settings_list;
if (type == ContentSettingsType::PLUGINS) {
for (const auto& id : content_settings::GetShieldsResourceIDs()) {
map_->GetSettingsForOneType(type, id, &content_settings_list);
iterate_content_settings_list(type, content_settings_list, id);
}
continue;
}
}

return hosts.size() + empty_host_pattern;
}

void BraveSiteSettingsCounter::ReportResult(ResultInt value) {
SiteSettingsCounter::ReportResult(value + CountShieldsSettings());
}
32 changes: 32 additions & 0 deletions browser/browsing_data/counters/brave_site_settings_counter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/* 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_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
class BraveSiteSettingsCounter : public SiteSettingsCounter {
public:
BraveSiteSettingsCounter(HostContentSettingsMap* map,
content::HostZoomMap* zoom_map,
ProtocolHandlerRegistry* handler_registry,
PrefService* pref_service);
~BraveSiteSettingsCounter() override;
BraveSiteSettingsCounter(const BraveSiteSettingsCounter&) = delete;
BraveSiteSettingsCounter& operator=(const BraveSiteSettingsCounter&) = delete;

private:
// SiteSettingsCounter overrides:
void ReportResult(ResultInt value) override;

int CountShieldsSettings();

scoped_refptr<BraveHostContentSettingsMap> map_;
};

#endif // BRAVE_BROWSER_BROWSING_DATA_COUNTERS_BRAVE_SITE_SETTINGS_COUNTER_H_
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/* 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/components/content_settings/core/browser/brave_host_content_settings_map.h"
#include "components/content_settings/core/browser/content_settings_info.h"
#include "components/content_settings/core/browser/website_settings_info.h"

#define CLEAR_SHIELDS_SETTINGS \
static_cast<BraveHostContentSettingsMap*>(host_content_settings_map_)-> \
ClearSettingsForPluginsType(delete_begin_, delete_end_);
#include "../../../../../chrome/browser/browsing_data/chrome_browsing_data_remover_delegate.cc"
#undef CLEAR_SHIELDS_SETTINGS
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/* 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/counters/brave_site_settings_counter.h"

#define SiteSettingsCounter BraveSiteSettingsCounter
#include "../../../../../../chrome/browser/browsing_data/counters/browsing_data_counter_factory.cc" // NOLINT
#undef SiteSettingsCounter
Original file line number Diff line number Diff line change
@@ -4,6 +4,8 @@
* 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"
#include "../../../../../chrome/browser/content_settings/host_content_settings_map_factory.cc" // NOLINT
#undef BRAVE_BUILD_SERVICE_INSTANCE_FOR
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/* 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_CHROMIUM_SRC_COMPONENTS_BROWSING_DATA_CORE_COUNTERS_BROWSING_DATA_COUNTER_H_
#define BRAVE_CHROMIUM_SRC_COMPONENTS_BROWSING_DATA_CORE_COUNTERS_BROWSING_DATA_COUNTER_H_

#define ReportResult virtual ReportResult
#include "../../../../../../components/browsing_data/core/counters/browsing_data_counter.h"
#undef ReportResult

#endif // BRAVE_CHROMIUM_SRC_COMPONENTS_BROWSING_DATA_CORE_COUNTERS_BROWSING_DATA_COUNTER_H_
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/* 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_CHROMIUM_SRC_COMPONENTS_CONTENT_SETTINGS_CORE_BROWSER_HOST_CONTENT_SETTINGS_MAP_H_
#define BRAVE_CHROMIUM_SRC_COMPONENTS_CONTENT_SETTINGS_CORE_BROWSER_HOST_CONTENT_SETTINGS_MAP_H_

#define BRAVE_HOST_CONTENT_SETTINGS_MAP_H \
private: \
friend class BraveHostContentSettingsMap;

#include "../../../../../../components/content_settings/core/browser/host_content_settings_map.h"

#undef BRAVE_HOST_CONTENT_SETTINGS_MAP_H

#endif // BRAVE_CHROMIUM_SRC_COMPONENTS_CONTENT_SETTINGS_CORE_BROWSER_HOST_CONTENT_SETTINGS_MAP_H_
4 changes: 4 additions & 0 deletions components/content_settings/core/browser/BUILD.gn
Original file line number Diff line number Diff line change
@@ -5,6 +5,10 @@ source_set("browser") {
"brave_content_settings_ephemeral_provider.h",
"brave_content_settings_pref_provider.cc",
"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 = [
Original file line number Diff line number Diff line change
@@ -8,27 +8,7 @@
#include <memory>
#include <utility>

#include "brave/components/brave_shields/common/brave_shield_constants.h"

namespace {

bool IsShieldsResourceID(
const content_settings::ResourceIdentifier& resource_identifier) {
if (resource_identifier == brave_shields::kAds ||
resource_identifier == brave_shields::kTrackers ||
resource_identifier == brave_shields::kHTTPUpgradableResources ||
resource_identifier == brave_shields::kJavaScript ||
resource_identifier == brave_shields::kFingerprinting ||
resource_identifier == brave_shields::kBraveShields ||
resource_identifier == brave_shields::kReferrers ||
resource_identifier == brave_shields::kCookies) {
return true;
} else {
return false;
}
}

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

namespace content_settings {

Original file line number Diff line number Diff line change
@@ -192,6 +192,11 @@ std::unique_ptr<RuleIterator> BravePrefProvider::GetRuleIterator(
cookie_rules_.at(incognito).end());
}

// Earyl return. We don't store flash plugin setting in preference.
if (content_type == ContentSettingsType::PLUGINS &&
resource_identifier == "")
return nullptr;

return PrefProvider::GetRuleIterator(content_type,
resource_identifier,
incognito);
@@ -359,4 +364,9 @@ void BravePrefProvider::OnContentSettingChanged(
}
}

void BravePrefProvider::ClearAllShieldsContentSettings() {
DCHECK(CalledOnValidThread());
GetPref(ContentSettingsType::PLUGINS)->ClearAllContentSettingsRules();
}

} // namespace content_settings
Original file line number Diff line number Diff line change
@@ -46,6 +46,8 @@ class BravePrefProvider : public PrefProvider,
const ResourceIdentifier& resource_identifier,
bool incognito) const override;

void ClearAllShieldsContentSettings();

private:
void UpdateCookieRules(ContentSettingsType content_type, bool incognito);
void OnCookieSettingsChanged(ContentSettingsType content_type);
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/* 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/components/content_settings/core/browser/brave_content_settings_utils.h"

#include <algorithm>

#include "brave/components/brave_shields/common/brave_shield_constants.h"

namespace {

const std::vector<std::string> kShieldsResourceIDs {
brave_shields::kAds,
brave_shields::kTrackers,
brave_shields::kHTTPUpgradableResources,
brave_shields::kJavaScript,
brave_shields::kFingerprinting,
brave_shields::kBraveShields,
brave_shields::kReferrers,
brave_shields::kCookies };

} // namespace

namespace content_settings {

const std::vector<std::string>& GetShieldsResourceIDs() {
return kShieldsResourceIDs;
}

bool IsShieldsResourceID(
const content_settings::ResourceIdentifier& resource_identifier) {
return std::find(kShieldsResourceIDs.begin(),
kShieldsResourceIDs.end(),
resource_identifier) != kShieldsResourceIDs.end();
}

} // namespace content_settings
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/* 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_COMPONENTS_CONTENT_SETTINGS_CORE_BROWSER_BRAVE_CONTENT_SETTINGS_UTILS_H_
#define BRAVE_COMPONENTS_CONTENT_SETTINGS_CORE_BROWSER_BRAVE_CONTENT_SETTINGS_UTILS_H_

#include <string>
#include <vector>

#include "components/content_settings/core/common/content_settings.h"

namespace content_settings {

const std::vector<std::string>& GetShieldsResourceIDs();

bool IsShieldsResourceID(const ResourceIdentifier& resource_identifier);

} // namespace content_settings

#endif // BRAVE_COMPONENTS_CONTENT_SETTINGS_CORE_BROWSER_BRAVE_CONTENT_SETTINGS_UTILS_H_
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/* 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/components/content_settings/core/browser/brave_host_content_settings_map.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"

base::Time BraveHostContentSettingsMap::GetShieldsSettingLastModifiedDate(
const ContentSettingsPattern& primary_pattern,
const ContentSettingsPattern& secondary_pattern,
const std::string& resource_identifier) const {
return GetPrefProvider()->GetWebsiteSettingLastModified(
primary_pattern, secondary_pattern,
ContentSettingsType::PLUGINS, resource_identifier);
}

void BraveHostContentSettingsMap::ClearSettingsForPluginsType() {
static_cast<content_settings::BravePrefProvider*>(GetPrefProvider())->
ClearAllShieldsContentSettings();
FlushLossyWebsiteSettings();
}

void BraveHostContentSettingsMap::ClearSettingsForPluginsType(
base::Time begin_time,
base::Time end_time) {
if (begin_time.is_null() && (end_time.is_null() || end_time.is_max())) {
ClearSettingsForPluginsType();
return;
}

auto* provider = GetPrefProvider();
for (const auto& resource_id : content_settings::GetShieldsResourceIDs()) {
ContentSettingsForOneType settings;
ContentSettingsType content_type = ContentSettingsType::PLUGINS;
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);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/* 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_COMPONENTS_CONTENT_SETTINGS_CORE_BROWSER_BRAVE_HOST_CONTENT_SETTINGS_MAP_H_
#define BRAVE_COMPONENTS_CONTENT_SETTINGS_CORE_BROWSER_BRAVE_HOST_CONTENT_SETTINGS_MAP_H_

#include <string>

#include "base/time/time.h"
#include "components/content_settings/core/browser/host_content_settings_map.h"

class BraveHostContentSettingsMap : public HostContentSettingsMap {
public:
using HostContentSettingsMap::HostContentSettingsMap;

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

base::Time GetShieldsSettingLastModifiedDate(
const ContentSettingsPattern& primary_pattern,
const ContentSettingsPattern& secondary_pattern,
const std::string& resource_identifier) const;

// Clear shields content settings with time constraint.
void ClearSettingsForPluginsType(base::Time begin_time,
base::Time end_time);

private:
~BraveHostContentSettingsMap() override = default;

// Clear all shields settings.
void ClearSettingsForPluginsType();
};

#endif // BRAVE_COMPONENTS_CONTENT_SETTINGS_CORE_BROWSER_BRAVE_HOST_CONTENT_SETTINGS_MAP_H_
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
diff --git a/chrome/browser/browsing_data/chrome_browsing_data_remover_delegate.cc b/chrome/browser/browsing_data/chrome_browsing_data_remover_delegate.cc
index 873f56bffd068bc0fbcba6db500392e32d736b5b..f9a35337e227e252ef8034e0ccea22b8d5f98306 100644
--- a/chrome/browser/browsing_data/chrome_browsing_data_remover_delegate.cc
+++ b/chrome/browser/browsing_data/chrome_browsing_data_remover_delegate.cc
@@ -709,6 +709,7 @@ void ChromeBrowsingDataRemoverDelegate::RemoveEmbedderData(
info->website_settings_info()->type(), delete_begin_, delete_end_,
HostContentSettingsMap::PatternSourcePredicate());
}
+ CLEAR_SHIELDS_SETTINGS

host_content_settings_map_->ClearSettingsForOneTypeWithPredicate(
ContentSettingsType::USB_CHOOSER_DATA, delete_begin_, delete_end_,
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
diff --git a/chrome/browser/content_settings/host_content_settings_map_factory.cc b/chrome/browser/content_settings/host_content_settings_map_factory.cc
index 47b1443298ec6ab9aed0f6525245487b02d6cba2..8c50e4cb89c03dc706c847907657cc469d8e83e2 100644
index 47b1443298ec6ab9aed0f6525245487b02d6cba2..563f6f70021763652eaed097dbcaa67dc9cd4b41 100644
--- a/chrome/browser/content_settings/host_content_settings_map_factory.cc
+++ b/chrome/browser/content_settings/host_content_settings_map_factory.cc
@@ -81,6 +81,7 @@ scoped_refptr<RefcountedKeyedService>
@@ -79,8 +79,9 @@ scoped_refptr<RefcountedKeyedService>
if (profile->IsIncognitoProfile())
GetForProfile(profile->GetOriginalProfile());

scoped_refptr<HostContentSettingsMap> settings_map(new HostContentSettingsMap(
- scoped_refptr<HostContentSettingsMap> settings_map(new HostContentSettingsMap(
+ scoped_refptr<HostContentSettingsMap> settings_map(new BraveHostContentSettingsMap(
profile->GetPrefs(),
+ BRAVE_BUILD_SERVICE_INSTANCE_FOR
profile->IsIncognitoProfile() || profile->IsGuestSession(),
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
diff --git a/components/content_settings/core/browser/host_content_settings_map.h b/components/content_settings/core/browser/host_content_settings_map.h
index 68dd1557dc70ea36d531136f359e10d619c10750..50ba9d037c3e8adf7ee4e0c506a764bf5cb1b821 100644
--- a/components/content_settings/core/browser/host_content_settings_map.h
+++ b/components/content_settings/core/browser/host_content_settings_map.h
@@ -309,6 +309,7 @@ class HostContentSettingsMap : public content_settings::Observer,
return pref_provider_;
}

+ BRAVE_HOST_CONTENT_SETTINGS_MAP_H
private:
friend class base::RefCountedThreadSafe<HostContentSettingsMap>;
friend class content_settings::TestUtils;