Skip to content

Commit

Permalink
Clear shields settings via clear browsing data dialog
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
simonhong committed Feb 18, 2020
1 parent 4385622 commit 4b07326
Show file tree
Hide file tree
Showing 20 changed files with 381 additions and 27 deletions.
3 changes: 1 addition & 2 deletions browser/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,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",
Expand Down Expand Up @@ -167,6 +165,7 @@ source_set("browser_process") {
"//content/public/common",
"//extensions/buildflags",
"//brave/chromium_src:browser",
"browsing_data",
"themes",
"ntp_sponsored_images",
"//services/network/public/cpp",
Expand Down
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
Expand Up @@ -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
Expand Up @@ -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 = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Up @@ -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);
Expand Down
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);
}
}
}
}
Loading

0 comments on commit 4b07326

Please sign in to comment.