From ef6977b7754636e8111cdd042fda3fc7374dadc8 Mon Sep 17 00:00:00 2001 From: Anthony Tseng Date: Thu, 18 Jan 2024 16:13:25 -0800 Subject: [PATCH] Add an enabled policy for ai chat --- browser/policy/brave_simple_policy_map.h | 9 +++++++++ components/ai_chat/core/common/BUILD.gn | 2 ++ components/ai_chat/core/common/pref_names.cc | 1 + components/ai_chat/core/common/pref_names.h | 2 ++ components/ai_chat/core/common/utils.cc | 17 ++++++++++++++++ components/ai_chat/core/common/utils.h | 17 ++++++++++++++++ script/policy_source_helper.py | 21 ++++++++++++++++++++ 7 files changed, 69 insertions(+) create mode 100644 components/ai_chat/core/common/utils.cc create mode 100644 components/ai_chat/core/common/utils.h diff --git a/browser/policy/brave_simple_policy_map.h b/browser/policy/brave_simple_policy_map.h index ccd67b99bf5..aed4f9f837d 100644 --- a/browser/policy/brave_simple_policy_map.h +++ b/browser/policy/brave_simple_policy_map.h @@ -6,6 +6,7 @@ #ifndef BRAVE_BROWSER_POLICY_BRAVE_SIMPLE_POLICY_MAP_H_ #define BRAVE_BROWSER_POLICY_BRAVE_SIMPLE_POLICY_MAP_H_ +#include "brave/components/ai_chat/core/common/buildflags/buildflags.h" #include "brave/components/brave_vpn/common/buildflags/buildflags.h" #include "brave/components/constants/pref_names.h" #include "brave/components/ipfs/buildflags/buildflags.h" @@ -31,6 +32,10 @@ #include "brave/components/brave_vpn/common/pref_names.h" #endif +#if BUILDFLAG(ENABLE_AI_CHAT) +#include "brave/components/ai_chat/core/common/pref_names.h" +#endif + namespace policy { inline constexpr PolicyToPreferenceMapEntry kBraveSimplePolicyMap[] = { @@ -55,6 +60,10 @@ inline constexpr PolicyToPreferenceMapEntry kBraveSimplePolicyMap[] = { {policy::key::kBraveVPNDisabled, brave_vpn::prefs::kManagedBraveVPNDisabled, base::Value::Type::BOOLEAN}, #endif +#if BUILDFLAG(ENABLE_AI_CHAT) + {policy::key::kBraveAIChatEnabled, ai_chat::prefs::kEnabledByPolicy, + base::Value::Type::BOOLEAN}, +#endif }; } // namespace policy diff --git a/components/ai_chat/core/common/BUILD.gn b/components/ai_chat/core/common/BUILD.gn index 1a3d36a5cb2..4e27dab73ad 100644 --- a/components/ai_chat/core/common/BUILD.gn +++ b/components/ai_chat/core/common/BUILD.gn @@ -13,6 +13,8 @@ static_library("common") { "features.h", "pref_names.cc", "pref_names.h", + "utils.cc", + "utils.h", ] deps = [ diff --git a/components/ai_chat/core/common/pref_names.cc b/components/ai_chat/core/common/pref_names.cc index 3eb818884cf..2541305b0f3 100644 --- a/components/ai_chat/core/common/pref_names.cc +++ b/components/ai_chat/core/common/pref_names.cc @@ -34,6 +34,7 @@ void RegisterProfilePrefs(PrefRegistrySimple* registry) { registry->RegisterStringPref(kBraveChatProductIdAndroid, ""); #endif registry->RegisterBooleanPref(kBraveAIChatContextMenuEnabled, true); + registry->RegisterBooleanPref(kEnabledByPolicy, true); } void RegisterProfilePrefsForMigration(PrefRegistrySimple* registry) { diff --git a/components/ai_chat/core/common/pref_names.h b/components/ai_chat/core/common/pref_names.h index 3bdd839d283..aae1e850fb0 100644 --- a/components/ai_chat/core/common/pref_names.h +++ b/components/ai_chat/core/common/pref_names.h @@ -54,6 +54,8 @@ inline constexpr char kBraveChatProductIdAndroid[] = #endif inline constexpr char kBraveAIChatContextMenuEnabled[] = "brave.ai_chat.context_menu_enabled"; +// Used to indicate whether the feature is enabled by group policy. +inline constexpr char kEnabledByPolicy[] = "brave.ai_chat.enabled_by_policy"; void RegisterProfilePrefs(PrefRegistrySimple* registry); diff --git a/components/ai_chat/core/common/utils.cc b/components/ai_chat/core/common/utils.cc new file mode 100644 index 00000000000..22b252625cf --- /dev/null +++ b/components/ai_chat/core/common/utils.cc @@ -0,0 +1,17 @@ +/* Copyright (c) 2024 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/ai_chat/core/common/pref_names.h" +#include "components/prefs/pref_service.h" + +namespace ai_chat { + +bool IsDisabledByPolicy(PrefService* prefs) { + DCHECK(prefs); + return prefs->IsManagedPreference(prefs::kEnabledByPolicy) && + !prefs->GetBoolean(prefs::kEnabledByPolicy); +} + +} // namespace ai_chat diff --git a/components/ai_chat/core/common/utils.h b/components/ai_chat/core/common/utils.h new file mode 100644 index 00000000000..5430a36fb65 --- /dev/null +++ b/components/ai_chat/core/common/utils.h @@ -0,0 +1,17 @@ +/* Copyright (c) 2024 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_AI_CHAT_CORE_COMMON_UTILS_H_ +#define BRAVE_COMPONENTS_AI_CHAT_CORE_COMMON_UTILS_H_ + +class PrefService; + +namespace ai_chat { + +bool IsDisabledByPolicy(PrefService* prefs); + +} // namespace ai_chat + +#endif // BRAVE_COMPONENTS_AI_CHAT_CORE_COMMON_UTILS_H_ diff --git a/script/policy_source_helper.py b/script/policy_source_helper.py index 9cecc309bff..2e3d97affa4 100755 --- a/script/policy_source_helper.py +++ b/script/policy_source_helper.py @@ -162,6 +162,27 @@ def AddBravePolicies(template_file_contents): 'desc': ('''This policy allows an admin to specify that Brave ''' '''VPN feature will be disabled.'''), }, + { + 'name': 'BraveAIChatEnabled', + 'type': 'main', + 'schema': { + 'type': 'boolean' + }, + 'supported_on': ['chrome.*:121-'], + 'future_on': ['android'], + 'features': { + 'dynamic_refresh': False, + 'per_profile': True, + 'can_be_recommended': False, + 'can_be_mandatory': True + }, + 'example_value': True, + 'id': 7, + 'caption': '''Enable Brave AI Chat feature.''', + 'tags': [], + 'desc': ('''This policy allows an admin to specify that Brave ''' + '''AI Chat feature will be enabled.'''), + }, ] # Our new polices are added with highest id