Apply policies with Ad Block Only mode enabled (#31485)

When the Ad Block Only mode is enabled, PR applies
the corresponding policies through the
Brave Profile Policy Provider.
This commit is contained in:
Aleksei Seren
2025-10-22 02:37:22 +01:00
committed by GitHub
parent 386803a0a5
commit 58cd7cf506
14 changed files with 487 additions and 1 deletions
+1
View File
@@ -35,6 +35,7 @@ include_rules += [
"+brave/components/brave_news/browser",
"+brave/components/brave_news/common",
"+brave/components/brave_origin",
"+brave/components/brave_policy/ad_block_only_mode",
"+brave/components/brave_rewards/content",
"+brave/components/brave_rewards/core",
"+brave/components/brave_shields/content/browser",
+6
View File
@@ -31,6 +31,7 @@
#include "brave/components/brave_component_updater/browser/brave_component_updater_delegate.h"
#include "brave/components/brave_component_updater/browser/local_data_files_service.h"
#include "brave/components/brave_origin/brave_origin_policy_manager.h"
#include "brave/components/brave_policy/ad_block_only_mode/ad_block_only_mode_policy_manager.h"
#include "brave/components/brave_referrals/browser/brave_referrals_service.h"
#include "brave/components/brave_shields/content/browser/ad_block_service.h"
#include "brave/components/brave_shields/content/browser/ad_block_subscription_service_manager.h"
@@ -192,6 +193,10 @@ void BraveBrowserProcessImpl::Init() {
CreateAIChatAgentProfileManager();
}
#endif
// Lazy initialization of AdBlockOnlyModePolicyManager
brave_policy::AdBlockOnlyModePolicyManager::GetInstance()->Init(
local_state());
}
void BraveBrowserProcessImpl::PreMainMessageLoopRun() {
@@ -220,6 +225,7 @@ void BraveBrowserProcessImpl::StartTearDown() {
#endif
// Reset BraveOriginPolicyManager to prevent dangling pointer to local_state_
brave_origin::BraveOriginPolicyManager::GetInstance()->Shutdown();
brave_policy::AdBlockOnlyModePolicyManager::GetInstance()->Shutdown();
brave_sync::NetworkTimeHelper::GetInstance()->Shutdown();
BrowserProcessImpl::StartTearDown();
}
+1
View File
@@ -167,6 +167,7 @@ brave_chrome_browser_deps = [
"//brave/components/brave_origin",
"//brave/components/brave_perf_predictor/browser",
"//brave/components/brave_policy",
"//brave/components/brave_policy/ad_block_only_mode",
"//brave/components/brave_private_new_tab_ui/common",
"//brave/components/brave_private_new_tab_ui/common:mojom",
"//brave/components/brave_referrals/browser",
+1
View File
@@ -28,6 +28,7 @@ test("brave_components_unittests") {
"//brave/components/brave_account/endpoints:unit_tests",
"//brave/components/brave_origin:unit_tests",
"//brave/components/brave_policy:unit_tests",
"//brave/components/brave_policy/ad_block_only_mode:unit_tests",
"//brave/components/brave_shields/core/browser:unit_tests",
"//brave/components/brave_shields/core/common:unit_tests",
"//brave/components/brave_wallet/common:unit_tests",
+1
View File
@@ -16,6 +16,7 @@ static_library("brave_policy") {
":brave_policy_observer",
"//base",
"//brave/components/brave_origin",
"//brave/components/brave_policy/ad_block_only_mode",
"//components/policy/core/browser",
"//components/policy/core/common",
"//components/prefs",
@@ -0,0 +1,46 @@
# 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/.
import(
"//brave/components/brave_policy/ad_block_only_mode/buildflags/buildflags.gni")
static_library("ad_block_only_mode") {
public_deps =
[ "//brave/components/brave_policy/ad_block_only_mode/buildflags" ]
sources = [
"ad_block_only_mode_policy_manager.cc",
"ad_block_only_mode_policy_manager.h",
]
deps = [
"//base",
"//brave/components/brave_policy:brave_policy_observer",
"//brave/components/brave_shields/core/common",
"//components/content_settings/core/common",
"//components/policy/core/browser",
"//components/policy/core/common",
"//components/prefs",
]
}
source_set("unit_tests") {
testonly = true
if (enable_ad_block_only_mode_policies) {
sources = [ "ad_block_only_mode_policy_manager_unittest.cc" ]
deps = [
":ad_block_only_mode",
"//base/test:test_support",
"//brave/components/brave_policy:brave_policy_observer",
"//brave/components/brave_shields/core/common",
"//components/prefs",
"//components/prefs:test_support",
"//testing/gmock",
"//testing/gtest",
]
}
}
@@ -0,0 +1,3 @@
include_rules = [
"+components/content_settings",
]
@@ -0,0 +1,113 @@
/* 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 "brave/components/brave_policy/ad_block_only_mode/ad_block_only_mode_policy_manager.h"
#include "base/feature_list.h"
#include "base/no_destructor.h"
#include "brave/components/brave_policy/ad_block_only_mode/buildflags/buildflags.h"
#include "brave/components/brave_shields/core/common/features.h"
#include "brave/components/brave_shields/core/common/pref_names.h"
#include "components/content_settings/core/common/content_settings.h"
#include "components/policy/policy_constants.h"
#include "components/prefs/pref_service.h"
namespace brave_policy {
// static
AdBlockOnlyModePolicyManager* AdBlockOnlyModePolicyManager::GetInstance() {
static base::NoDestructor<AdBlockOnlyModePolicyManager> instance;
return instance.get();
}
void AdBlockOnlyModePolicyManager::Init(PrefService* local_state) {
CHECK(local_state) << "AdBlockOnlyModePolicyManager local state should exist";
local_state_ = local_state;
pref_change_registrar_.Init(local_state_);
pref_change_registrar_.Add(
brave_shields::prefs::kAdBlockOnlyModeEnabled,
base::BindRepeating(
&AdBlockOnlyModePolicyManager::OnAdBlockOnlyModeChanged,
base::Unretained(this)));
OnAdBlockOnlyModeChanged();
}
void AdBlockOnlyModePolicyManager::Shutdown() {
pref_change_registrar_.RemoveAll();
observers_.Clear();
local_state_ = nullptr;
}
void AdBlockOnlyModePolicyManager::AddObserver(BravePolicyObserver* observer) {
observers_.AddObserver(observer);
if (local_state_) {
// Notify the observer to fetch Ad Block Only mode policies.
observer->OnBravePoliciesReady();
}
}
void AdBlockOnlyModePolicyManager::RemoveObserver(
BravePolicyObserver* observer) {
observers_.RemoveObserver(observer);
}
AdBlockOnlyModePolicies AdBlockOnlyModePolicyManager::GetPolicies() const {
if (!base::FeatureList::IsEnabled(
brave_shields::features::kAdblockOnlyMode)) {
return {};
}
if (!local_state_ || !local_state_->GetBoolean(
brave_shields::prefs::kAdBlockOnlyModeEnabled)) {
return {};
}
return GetPoliciesImpl();
}
AdBlockOnlyModePolicyManager::AdBlockOnlyModePolicyManager() = default;
AdBlockOnlyModePolicyManager::~AdBlockOnlyModePolicyManager() = default;
void AdBlockOnlyModePolicyManager::OnAdBlockOnlyModeChanged() {
observers_.Notify(&BravePolicyObserver::OnBravePoliciesReady);
}
AdBlockOnlyModePolicies AdBlockOnlyModePolicyManager::GetPoliciesImpl() const {
#if BUILDFLAG(ENABLE_AD_BLOCK_ONLY_MODE_POLICIES)
AdBlockOnlyModePolicies policies;
// Allow JavaScript globally.
policies.emplace(policy::key::kDefaultJavaScriptSetting,
base::Value(CONTENT_SETTING_ALLOW));
// Allow all cookies.
policies.emplace(policy::key::kDefaultCookiesSetting,
base::Value(CONTENT_SETTING_ALLOW));
// Do not block third-party cookies.
policies.emplace(policy::key::kBlockThirdPartyCookies, base::Value(false));
// Disable language fingerprinting reduction.
policies.emplace(policy::key::kBraveReduceLanguageEnabled,
base::Value(false));
// Disable De-AMP.
policies.emplace(policy::key::kBraveDeAmpEnabled, base::Value(false));
// Disable URL debouncing.
policies.emplace(policy::key::kBraveDebouncingEnabled, base::Value(false));
return policies;
#else
return {};
#endif // BUILDFLAG(ENABLE_AD_BLOCK_ONLY_MODE_POLICIES)
}
} // namespace brave_policy
@@ -0,0 +1,63 @@
/* 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/. */
#ifndef BRAVE_COMPONENTS_BRAVE_POLICY_AD_BLOCK_ONLY_MODE_AD_BLOCK_ONLY_MODE_POLICY_MANAGER_H_
#define BRAVE_COMPONENTS_BRAVE_POLICY_AD_BLOCK_ONLY_MODE_AD_BLOCK_ONLY_MODE_POLICY_MANAGER_H_
#include "base/containers/flat_map.h"
#include "base/memory/raw_ptr.h"
#include "base/observer_list.h"
#include "base/values.h"
#include "brave/components/brave_policy/brave_policy_observer.h"
#include "components/prefs/pref_change_registrar.h"
namespace base {
template <typename T>
class NoDestructor;
}
class PrefService;
namespace brave_policy {
using AdBlockOnlyModePolicies = base::flat_map<std::string, base::Value>;
// Singleton that holds Ad Block Only mode preference handling and manages
// setting policy values when the preference is changed. This
// abstracts away the local state management from policy provider.
// TODO(https://github.com/brave/brave-browser/issues/50077): Refactor this
// class when `BravePolicyManager` is introduced.
class AdBlockOnlyModePolicyManager final {
public:
static AdBlockOnlyModePolicyManager* GetInstance();
void Init(PrefService* local_state);
void Shutdown();
void AddObserver(BravePolicyObserver* observer);
void RemoveObserver(BravePolicyObserver* observer);
AdBlockOnlyModePolicies GetPolicies() const;
AdBlockOnlyModePolicyManager(const AdBlockOnlyModePolicyManager&) = delete;
AdBlockOnlyModePolicyManager& operator=(const AdBlockOnlyModePolicyManager&) =
delete;
private:
friend base::NoDestructor<AdBlockOnlyModePolicyManager>;
AdBlockOnlyModePolicyManager();
~AdBlockOnlyModePolicyManager();
void OnAdBlockOnlyModeChanged();
AdBlockOnlyModePolicies GetPoliciesImpl() const;
raw_ptr<PrefService> local_state_; // Not owned.
PrefChangeRegistrar pref_change_registrar_;
base::ObserverList<BravePolicyObserver> observers_;
};
} // namespace brave_policy
#endif // BRAVE_COMPONENTS_BRAVE_POLICY_AD_BLOCK_ONLY_MODE_AD_BLOCK_ONLY_MODE_POLICY_MANAGER_H_
@@ -0,0 +1,187 @@
/* 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 "brave/components/brave_policy/ad_block_only_mode/ad_block_only_mode_policy_manager.h"
#include <optional>
#include "base/test/scoped_feature_list.h"
#include "brave/components/brave_policy/brave_policy_observer.h"
#include "brave/components/brave_shields/core/common/features.h"
#include "brave/components/brave_shields/core/common/pref_names.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/testing_pref_service.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace brave_policy {
namespace {
class TestAdBlockOnlyModePolicyObserver : public BravePolicyObserver {
public:
~TestAdBlockOnlyModePolicyObserver() override = default;
void OnBravePoliciesReady() override {
last_ad_block_only_mode_policies_ =
AdBlockOnlyModePolicyManager::GetInstance()->GetPolicies();
}
const std::optional<AdBlockOnlyModePolicies>& LastAdBlockOnlyModePolicies()
const {
return last_ad_block_only_mode_policies_;
}
private:
std::optional<AdBlockOnlyModePolicies> last_ad_block_only_mode_policies_;
};
} // namespace
class AdBlockOnlyModePolicyManagerTest : public testing::Test {
public:
AdBlockOnlyModePolicyManagerTest() = default;
~AdBlockOnlyModePolicyManagerTest() override = default;
void SetUp() override {
local_state_.registry()->RegisterBooleanPref(
brave_shields::prefs::kAdBlockOnlyModeEnabled, false);
}
void TearDown() override { manager()->Shutdown(); }
AdBlockOnlyModePolicyManager* manager() {
return AdBlockOnlyModePolicyManager::GetInstance();
}
TestingPrefServiceSimple* local_state() { return &local_state_; }
TestAdBlockOnlyModePolicyObserver* observer() { return &observer_; }
void EnableFeature() {
feature_list_.InitAndEnableFeature(
brave_shields::features::kAdblockOnlyMode);
}
void DisableFeature() {
feature_list_.InitAndDisableFeature(
brave_shields::features::kAdblockOnlyMode);
}
private:
base::test::ScopedFeatureList feature_list_;
TestingPrefServiceSimple local_state_;
TestAdBlockOnlyModePolicyObserver observer_;
};
TEST_F(AdBlockOnlyModePolicyManagerTest, PoliciesEmptyWhenFeatureDisabled) {
DisableFeature();
manager()->Init(local_state());
local_state()->SetBoolean(brave_shields::prefs::kAdBlockOnlyModeEnabled,
true);
EXPECT_THAT(manager()->GetPolicies(), testing::IsEmpty());
}
TEST_F(AdBlockOnlyModePolicyManagerTest, PoliciesEmptyBeforeInitCalled) {
EnableFeature();
EXPECT_THAT(manager()->GetPolicies(), testing::IsEmpty());
}
TEST_F(AdBlockOnlyModePolicyManagerTest, PoliciesEmptyWhenPreferenceDisabled) {
EnableFeature();
manager()->Init(local_state());
local_state()->SetBoolean(brave_shields::prefs::kAdBlockOnlyModeEnabled,
false);
EXPECT_THAT(manager()->GetPolicies(), testing::IsEmpty());
}
TEST_F(AdBlockOnlyModePolicyManagerTest,
PoliciesNotEmptyWhenPreferenceEnabled) {
EnableFeature();
manager()->Init(local_state());
local_state()->SetBoolean(brave_shields::prefs::kAdBlockOnlyModeEnabled,
true);
EXPECT_THAT(manager()->GetPolicies(), testing::Not(testing::IsEmpty()));
}
TEST_F(AdBlockOnlyModePolicyManagerTest,
PoliciesEmptyAfterObserverAddedWhenFeatureDisabled) {
DisableFeature();
manager()->Init(local_state());
manager()->AddObserver(observer());
local_state()->SetBoolean(brave_shields::prefs::kAdBlockOnlyModeEnabled,
true);
ASSERT_TRUE(observer()->LastAdBlockOnlyModePolicies().has_value());
EXPECT_THAT(*observer()->LastAdBlockOnlyModePolicies(), testing::IsEmpty());
}
TEST_F(AdBlockOnlyModePolicyManagerTest,
PoliciesEmptyWhenPreferenceDisabledBeforeObserverAdded) {
EnableFeature();
manager()->Init(local_state());
local_state()->SetBoolean(brave_shields::prefs::kAdBlockOnlyModeEnabled,
false);
manager()->AddObserver(observer());
ASSERT_TRUE(observer()->LastAdBlockOnlyModePolicies().has_value());
EXPECT_THAT(*observer()->LastAdBlockOnlyModePolicies(), testing::IsEmpty());
}
TEST_F(AdBlockOnlyModePolicyManagerTest,
PoliciesEmptyWhenPreferenceDisabledAfterObserverAdded) {
EnableFeature();
manager()->Init(local_state());
manager()->AddObserver(observer());
local_state()->SetBoolean(brave_shields::prefs::kAdBlockOnlyModeEnabled,
false);
ASSERT_TRUE(observer()->LastAdBlockOnlyModePolicies().has_value());
EXPECT_THAT(*observer()->LastAdBlockOnlyModePolicies(), testing::IsEmpty());
}
TEST_F(AdBlockOnlyModePolicyManagerTest,
PoliciesNotEmptyWhenPreferenceEnabledAfterObserverAdded) {
EnableFeature();
manager()->Init(local_state());
manager()->AddObserver(observer());
ASSERT_TRUE(observer()->LastAdBlockOnlyModePolicies().has_value());
EXPECT_THAT(*observer()->LastAdBlockOnlyModePolicies(), testing::IsEmpty());
local_state()->SetBoolean(brave_shields::prefs::kAdBlockOnlyModeEnabled,
true);
EXPECT_THAT(*observer()->LastAdBlockOnlyModePolicies(),
testing::Not(testing::IsEmpty()));
}
TEST_F(AdBlockOnlyModePolicyManagerTest,
PoliciesNotEmptyWhenPreferenceEnabledBeforeObserverAdded) {
EnableFeature();
manager()->Init(local_state());
local_state()->SetBoolean(brave_shields::prefs::kAdBlockOnlyModeEnabled,
true);
manager()->AddObserver(observer());
ASSERT_TRUE(observer()->LastAdBlockOnlyModePolicies().has_value());
EXPECT_THAT(*observer()->LastAdBlockOnlyModePolicies(),
testing::Not(testing::IsEmpty()));
}
TEST_F(AdBlockOnlyModePolicyManagerTest, PoliciesEmptyAfterShutdown) {
EnableFeature();
manager()->Init(local_state());
local_state()->SetBoolean(brave_shields::prefs::kAdBlockOnlyModeEnabled,
true);
manager()->Shutdown();
EXPECT_THAT(manager()->GetPolicies(), testing::IsEmpty());
}
} // namespace brave_policy
@@ -0,0 +1,15 @@
# 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/.
import(
"//brave/components/brave_policy/ad_block_only_mode/buildflags/buildflags.gni")
import("//build/buildflag_header.gni")
buildflag_header("buildflags") {
header = "buildflags.h"
flags = [
"ENABLE_AD_BLOCK_ONLY_MODE_POLICIES=$enable_ad_block_only_mode_policies",
]
}
@@ -0,0 +1,10 @@
# 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/.
declare_args() {
# Controls whether Ad Block Only Mode can override some Brave browser policies
# for improved website compatibility. Disabled on iOS where not supported.
enable_ad_block_only_mode_policies = !is_ios
}
@@ -10,6 +10,7 @@
#include "base/logging.h"
#include "base/values.h"
#include "brave/components/brave_origin/brave_origin_utils.h"
#include "brave/components/brave_policy/ad_block_only_mode/ad_block_only_mode_policy_manager.h"
#include "components/policy/core/common/policy_bundle.h"
#include "components/policy/core/common/policy_map.h"
#include "components/policy/core/common/policy_namespace.h"
@@ -33,6 +34,10 @@ void BraveProfilePolicyProvider::Init(policy::SchemaRegistry* registry) {
// loading.
brave_origin_observation_.Observe(
brave_origin::BraveOriginPolicyManager::GetInstance());
// Register as AdBlockOnlyModePolicyManager observer.
ad_block_only_mode_policy_observation_.Observe(
AdBlockOnlyModePolicyManager::GetInstance());
}
void BraveProfilePolicyProvider::RefreshPolicies(
@@ -55,7 +60,12 @@ void BraveProfilePolicyProvider::OnBravePoliciesReady() {
// Once we have Brave policies and a profile ID trigger Refresh policies
if (!profile_id_.empty()) {
RefreshPolicies(policy::PolicyFetchReason::kBrowserStart);
// `OnBravePoliciesReady()` can be called multiple times, so we need to
// choose the appropriate reason for the policies refresh basing on the
// first policies load complete state.
RefreshPolicies(first_policies_loaded_
? policy::PolicyFetchReason::kUserRequest
: policy::PolicyFetchReason::kBrowserStart);
}
}
@@ -72,6 +82,8 @@ policy::PolicyBundle BraveProfilePolicyProvider::LoadPolicies() {
// Always disabled in official builds
#endif
MaybeLoadAdBlockOnlyModePolicies(bundle);
return bundle;
}
@@ -111,6 +123,27 @@ void BraveProfilePolicyProvider::LoadBraveOriginPolicy(
base::Value(enabled), nullptr);
}
void BraveProfilePolicyProvider::MaybeLoadAdBlockOnlyModePolicies(
policy::PolicyBundle& bundle) {
auto ad_block_only_mode_policies =
AdBlockOnlyModePolicyManager::GetInstance()->GetPolicies();
// Ad Block Only mode policies can be empty if the feature is disabled or
// the Ad Block Only mode is not enabled.
if (ad_block_only_mode_policies.empty()) {
return;
}
policy::PolicyMap& bundle_policy_map = bundle.Get(
policy::PolicyNamespace(policy::POLICY_DOMAIN_CHROME, std::string()));
for (auto& [policy_key, value] : ad_block_only_mode_policies) {
bundle_policy_map.Set(
std::string(policy_key), policy::POLICY_LEVEL_MANDATORY,
policy::POLICY_SCOPE_USER, policy::POLICY_SOURCE_BRAVE,
std::move(value), nullptr);
}
}
void BraveProfilePolicyProvider::OnProfilePolicyChanged(
std::string_view policy_key,
std::string_view profile_id) {
@@ -13,6 +13,7 @@
#include "base/values.h"
#include "brave/components/brave_origin/brave_origin_policy_info.h"
#include "brave/components/brave_origin/brave_origin_policy_manager.h"
#include "brave/components/brave_policy/ad_block_only_mode/ad_block_only_mode_policy_manager.h"
#include "brave/components/brave_policy/brave_policy_observer.h"
#include "components/policy/core/common/configuration_policy_provider.h"
@@ -50,6 +51,8 @@ class BraveProfilePolicyProvider : public policy::ConfigurationPolicyProvider,
std::string_view policy_key,
bool enabled);
void MaybeLoadAdBlockOnlyModePolicies(policy::PolicyBundle& bundle);
bool first_policies_loaded_ = false;
bool policies_ready_ = false;
std::string profile_id_;
@@ -58,6 +61,9 @@ class BraveProfilePolicyProvider : public policy::ConfigurationPolicyProvider,
BravePolicyObserver>
brave_origin_observation_{this};
base::ScopedObservation<AdBlockOnlyModePolicyManager, BravePolicyObserver>
ad_block_only_mode_policy_observation_{this};
base::WeakPtrFactory<BraveProfilePolicyProvider> weak_factory_{this};
};