From 58cd7cf50633dc355a48869f8aa5f91298cbc899 Mon Sep 17 00:00:00 2001 From: Aleksei Seren <86598290+aseren@users.noreply.github.com> Date: Tue, 21 Oct 2025 21:37:22 -0400 Subject: [PATCH] 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. --- browser/DEPS | 1 + browser/brave_browser_process_impl.cc | 6 + browser/sources.gni | 1 + components/BUILD.gn | 1 + components/brave_policy/BUILD.gn | 1 + .../brave_policy/ad_block_only_mode/BUILD.gn | 46 +++++ .../brave_policy/ad_block_only_mode/DEPS | 3 + .../ad_block_only_mode_policy_manager.cc | 113 +++++++++++ .../ad_block_only_mode_policy_manager.h | 63 ++++++ ...block_only_mode_policy_manager_unittest.cc | 187 ++++++++++++++++++ .../ad_block_only_mode/buildflags/BUILD.gn | 15 ++ .../buildflags/buildflags.gni | 10 + .../brave_profile_policy_provider.cc | 35 +++- .../brave_profile_policy_provider.h | 6 + 14 files changed, 487 insertions(+), 1 deletion(-) create mode 100644 components/brave_policy/ad_block_only_mode/BUILD.gn create mode 100644 components/brave_policy/ad_block_only_mode/DEPS create mode 100644 components/brave_policy/ad_block_only_mode/ad_block_only_mode_policy_manager.cc create mode 100644 components/brave_policy/ad_block_only_mode/ad_block_only_mode_policy_manager.h create mode 100644 components/brave_policy/ad_block_only_mode/ad_block_only_mode_policy_manager_unittest.cc create mode 100644 components/brave_policy/ad_block_only_mode/buildflags/BUILD.gn create mode 100644 components/brave_policy/ad_block_only_mode/buildflags/buildflags.gni diff --git a/browser/DEPS b/browser/DEPS index f44ac90e0c8..e8e92cc943e 100644 --- a/browser/DEPS +++ b/browser/DEPS @@ -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", diff --git a/browser/brave_browser_process_impl.cc b/browser/brave_browser_process_impl.cc index 5068ad45b6c..b524f18e8fa 100644 --- a/browser/brave_browser_process_impl.cc +++ b/browser/brave_browser_process_impl.cc @@ -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(); } diff --git a/browser/sources.gni b/browser/sources.gni index f454be2f08f..a828f8017fa 100644 --- a/browser/sources.gni +++ b/browser/sources.gni @@ -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", diff --git a/components/BUILD.gn b/components/BUILD.gn index 011b93feab5..8321f6405ec 100644 --- a/components/BUILD.gn +++ b/components/BUILD.gn @@ -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", diff --git a/components/brave_policy/BUILD.gn b/components/brave_policy/BUILD.gn index e38fa0934a4..4ce183e0f1b 100644 --- a/components/brave_policy/BUILD.gn +++ b/components/brave_policy/BUILD.gn @@ -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", diff --git a/components/brave_policy/ad_block_only_mode/BUILD.gn b/components/brave_policy/ad_block_only_mode/BUILD.gn new file mode 100644 index 00000000000..9cae45f489c --- /dev/null +++ b/components/brave_policy/ad_block_only_mode/BUILD.gn @@ -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", + ] + } +} diff --git a/components/brave_policy/ad_block_only_mode/DEPS b/components/brave_policy/ad_block_only_mode/DEPS new file mode 100644 index 00000000000..38460a37c12 --- /dev/null +++ b/components/brave_policy/ad_block_only_mode/DEPS @@ -0,0 +1,3 @@ +include_rules = [ + "+components/content_settings", +] diff --git a/components/brave_policy/ad_block_only_mode/ad_block_only_mode_policy_manager.cc b/components/brave_policy/ad_block_only_mode/ad_block_only_mode_policy_manager.cc new file mode 100644 index 00000000000..fcf31af7225 --- /dev/null +++ b/components/brave_policy/ad_block_only_mode/ad_block_only_mode_policy_manager.cc @@ -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 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 diff --git a/components/brave_policy/ad_block_only_mode/ad_block_only_mode_policy_manager.h b/components/brave_policy/ad_block_only_mode/ad_block_only_mode_policy_manager.h new file mode 100644 index 00000000000..7e0752675f8 --- /dev/null +++ b/components/brave_policy/ad_block_only_mode/ad_block_only_mode_policy_manager.h @@ -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 +class NoDestructor; +} + +class PrefService; + +namespace brave_policy { + +using AdBlockOnlyModePolicies = base::flat_map; + +// 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(); + + void OnAdBlockOnlyModeChanged(); + AdBlockOnlyModePolicies GetPoliciesImpl() const; + + raw_ptr local_state_; // Not owned. + PrefChangeRegistrar pref_change_registrar_; + base::ObserverList observers_; +}; + +} // namespace brave_policy + +#endif // BRAVE_COMPONENTS_BRAVE_POLICY_AD_BLOCK_ONLY_MODE_AD_BLOCK_ONLY_MODE_POLICY_MANAGER_H_ diff --git a/components/brave_policy/ad_block_only_mode/ad_block_only_mode_policy_manager_unittest.cc b/components/brave_policy/ad_block_only_mode/ad_block_only_mode_policy_manager_unittest.cc new file mode 100644 index 00000000000..22e9f6ce808 --- /dev/null +++ b/components/brave_policy/ad_block_only_mode/ad_block_only_mode_policy_manager_unittest.cc @@ -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 + +#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& LastAdBlockOnlyModePolicies() + const { + return last_ad_block_only_mode_policies_; + } + + private: + std::optional 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 diff --git a/components/brave_policy/ad_block_only_mode/buildflags/BUILD.gn b/components/brave_policy/ad_block_only_mode/buildflags/BUILD.gn new file mode 100644 index 00000000000..0bc31619436 --- /dev/null +++ b/components/brave_policy/ad_block_only_mode/buildflags/BUILD.gn @@ -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", + ] +} diff --git a/components/brave_policy/ad_block_only_mode/buildflags/buildflags.gni b/components/brave_policy/ad_block_only_mode/buildflags/buildflags.gni new file mode 100644 index 00000000000..dc169235ee8 --- /dev/null +++ b/components/brave_policy/ad_block_only_mode/buildflags/buildflags.gni @@ -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 +} diff --git a/components/brave_policy/brave_profile_policy_provider.cc b/components/brave_policy/brave_profile_policy_provider.cc index 622976b370c..bfaffcea0e2 100644 --- a/components/brave_policy/brave_profile_policy_provider.cc +++ b/components/brave_policy/brave_profile_policy_provider.cc @@ -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) { diff --git a/components/brave_policy/brave_profile_policy_provider.h b/components/brave_policy/brave_profile_policy_provider.h index 54e33cb69f4..0465eb65aff 100644 --- a/components/brave_policy/brave_profile_policy_provider.h +++ b/components/brave_policy/brave_profile_policy_provider.h @@ -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 + ad_block_only_mode_policy_observation_{this}; + base::WeakPtrFactory weak_factory_{this}; };