1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
|
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/ntp_tiles/enterprise/ntp_shortcuts_policy_handler.h"
#include <string>
#include "base/containers/flat_set.h"
#include "base/feature_list.h"
#include "base/strings/string_number_conversions.h"
#include "base/values.h"
#include "components/ntp_tiles/enterprise/enterprise_shortcuts_store.h"
#include "components/ntp_tiles/features.h"
#include "components/ntp_tiles/pref_names.h"
#include "components/policy/core/browser/policy_error_map.h"
#include "components/policy/core/common/policy_map.h"
#include "components/policy/policy_constants.h"
#include "components/prefs/pref_value_map.h"
#include "components/strings/grit/components_strings.h"
#include "url/gurl.h"
using ntp_tiles::EnterpriseShortcut;
using ntp_tiles::EnterpriseShortcutsStore;
namespace policy {
namespace {
bool IsNTPEnterpriseShortcutsEnabled() {
// Check that FeatureList is available as a protection against early startup
// crashes. Some policy providers are initialized very early even before
// base::FeatureList is available, but when policies are finally applied, the
// feature stack is fully initialized. The instance check ensures that the
// final decision is delayed until all features are initialized, without any
// other downstream effect.
return base::FeatureList::GetInstance() &&
base::FeatureList::IsEnabled(ntp_tiles::kNtpEnterpriseShortcuts);
}
// Converts a shortcuts policy entry `policy_dict` into a dictionary to be
// saved to prefs, with fields corresponding to `EnterpriseShortcut`. `CHECK`s
// are safe since this function is only used after policy values are validated.
base::Value NTPShortcutsDictFromPolicyValue(
const base::Value::Dict& policy_dict) {
base::Value::Dict dict;
// To align with `EnterpriseShortcut`, use "title" as dictionary key instead
// of "name".
const std::string* name =
policy_dict.FindString(NTPShortcutsPolicyHandler::kName);
CHECK(name);
dict.Set(EnterpriseShortcutsStore::kDictionaryKeyTitle, *name);
const std::string* url =
policy_dict.FindString(NTPShortcutsPolicyHandler::kUrl);
CHECK(url);
dict.Set(EnterpriseShortcutsStore::kDictionaryKeyUrl, *url);
dict.Set(EnterpriseShortcutsStore::kDictionaryKeyPolicyOrigin,
static_cast<int>(EnterpriseShortcut::PolicyOrigin::kNtpShortcuts));
dict.Set(EnterpriseShortcutsStore::kDictionaryKeyIsHiddenByUser, false);
const bool allow_user_edit =
policy_dict.FindBool(NTPShortcutsPolicyHandler::kAllowUserEdit)
.value_or(false);
dict.Set(EnterpriseShortcutsStore::kDictionaryKeyAllowUserEdit,
allow_user_edit);
const bool allow_user_delete =
policy_dict.FindBool(NTPShortcutsPolicyHandler::kAllowUserDelete)
.value_or(false);
dict.Set(EnterpriseShortcutsStore::kDictionaryKeyAllowUserDelete,
allow_user_delete);
return base::Value(std::move(dict));
}
bool UrlAlreadySeen(const std::string& policy_name,
const GURL& url,
const base::flat_set<GURL>& valid_urls_already_seen,
PolicyErrorMap* errors,
base::flat_set<GURL>* duplicated_urls) {
if (valid_urls_already_seen.find(url) == valid_urls_already_seen.end()) {
return false;
}
if (duplicated_urls->find(url) == duplicated_urls->end()) {
duplicated_urls->insert(url);
// Only show an warning message once per url.
errors->AddError(policy_name, IDS_POLICY_NTP_SHORTCUTS_DUPLICATED_URL,
url.spec(), {}, PolicyMap::MessageType::kWarning);
}
return true;
}
// Used for applying fake shortcuts for testing purposes only.
void ApplyFakeDataForManualTesting(PrefValueMap* prefs) {
base::Value::List shortcuts;
{
base::Value::Dict shortcut;
shortcut.Set("name", "Google Search");
shortcut.Set("url", "https://www.google.com/");
shortcuts.Append(NTPShortcutsDictFromPolicyValue(shortcut));
}
{
base::Value::Dict shortcut;
shortcut.Set("name", "Workday");
shortcut.Set("url", "https://workday.com");
shortcuts.Append(NTPShortcutsDictFromPolicyValue(shortcut));
}
{
base::Value::Dict shortcut;
shortcut.Set("name", "Concur");
shortcut.Set("url", "https://www.concur.com");
shortcuts.Append(NTPShortcutsDictFromPolicyValue(shortcut));
}
{
base::Value::Dict shortcut;
shortcut.Set("name", "JIRA");
shortcut.Set("url", "https://www.jira.com");
shortcuts.Append(NTPShortcutsDictFromPolicyValue(shortcut));
}
{
base::Value::Dict shortcut;
shortcut.Set("name", "Google Drive (E/D)");
shortcut.Set("url", "https://drive.google.com");
shortcut.Set("allow_user_edit", true);
shortcut.Set("allow_user_delete", true);
shortcuts.Append(NTPShortcutsDictFromPolicyValue(shortcut));
}
{
base::Value::Dict shortcut;
shortcut.Set("name", "Zoom (E/D)");
shortcut.Set("url", "https://zoom.com");
shortcut.Set("allow_user_edit", true);
shortcut.Set("allow_user_delete", true);
shortcuts.Append(NTPShortcutsDictFromPolicyValue(shortcut));
}
{
base::Value::Dict shortcut;
shortcut.Set("name", "Google Calendar (E)");
shortcut.Set("url", "https://calendar.google.com");
shortcut.Set("allow_user_edit", true);
shortcut.Set("allow_user_delete", false);
shortcuts.Append(NTPShortcutsDictFromPolicyValue(shortcut));
}
{
base::Value::Dict shortcut;
shortcut.Set("name", "Gmail (D)");
shortcut.Set("url", "https://mail.google.com");
shortcut.Set("allow_user_edit", false);
shortcut.Set("allow_user_delete", true);
shortcuts.Append(NTPShortcutsDictFromPolicyValue(shortcut));
}
prefs->SetValue(ntp_tiles::prefs::kEnterpriseShortcutsPolicyList,
base::Value(std::move(shortcuts)));
}
} // namespace
const char NTPShortcutsPolicyHandler::kName[] = "name";
const char NTPShortcutsPolicyHandler::kUrl[] = "url";
const char NTPShortcutsPolicyHandler::kAllowUserEdit[] = "allow_user_edit";
const char NTPShortcutsPolicyHandler::kAllowUserDelete[] = "allow_user_delete";
const int NTPShortcutsPolicyHandler::kMaxNtpShortcuts = 10;
const int NTPShortcutsPolicyHandler::kMaxNtpShortcutTextLength = 1000;
NTPShortcutsPolicyHandler::NTPShortcutsPolicyHandler(Schema schema)
: SimpleSchemaValidatingPolicyHandler(
key::kNTPShortcuts,
ntp_tiles::prefs::kEnterpriseShortcutsPolicyList,
schema,
policy::SchemaOnErrorStrategy::
SCHEMA_ALLOW_UNKNOWN_AND_INVALID_LIST_ENTRY,
SimpleSchemaValidatingPolicyHandler::
RECOMMENDED_PROHIBITED, // Recommended policies are not supported
// since `allow_user_edit` and
// `allow_user_delete` fields allow for
// user modification.
SimpleSchemaValidatingPolicyHandler::MANDATORY_ALLOWED) {}
NTPShortcutsPolicyHandler::~NTPShortcutsPolicyHandler() = default;
bool NTPShortcutsPolicyHandler::CheckPolicySettings(const PolicyMap& policies,
PolicyErrorMap* errors) {
ignored_urls_.clear();
if (!IsNTPEnterpriseShortcutsEnabled() || !policies.Get(policy_name())) {
return true;
}
if (!SimpleSchemaValidatingPolicyHandler::CheckPolicySettings(policies,
errors)) {
return false;
}
const base::Value::List& shortcuts =
policies.GetValue(policy_name(), base::Value::Type::LIST)->GetList();
if (shortcuts.size() > kMaxNtpShortcuts) {
errors->AddError(policy_name(),
IDS_POLICY_NTP_SHORTCUTS_MAX_SHORTCUTS_LIMIT_ERROR,
base::NumberToString(kMaxNtpShortcuts));
return false;
}
base::flat_set<GURL> valid_urls_already_seen;
base::flat_set<GURL> duplicated_urls;
for (const base::Value& entry : shortcuts) {
const base::Value::Dict& dict = entry.GetDict();
const std::string* name = dict.FindString(kName);
const std::string* url_str = dict.FindString(kUrl);
// The `SCHEMA_ALLOW_UNKNOWN_AND_INVALID_LIST_ENTRY` strategy guarantees
// that `entry` is a dictionary, but its properties might be missing.
// A missing name or URL is a schema validation error, which is already
// reported as a generic warning.
if (!url_str) {
continue;
}
if (url_str->empty()) {
errors->AddError(policy_name(), IDS_SEARCH_POLICY_SETTINGS_URL_IS_EMPTY,
{}, PolicyMap::MessageType::kWarning);
continue;
}
GURL url(*url_str);
if (!url.is_valid()) {
errors->AddError(policy_name(), IDS_POLICY_INVALID_URL_ERROR, {},
PolicyMap::MessageType::kWarning);
ignored_urls_.insert(url);
continue;
}
if (url_str->length() > kMaxNtpShortcutTextLength) {
errors->AddError(policy_name(), IDS_POLICY_NTP_SHORTCUTS_URL_TOO_LONG,
base::NumberToString(kMaxNtpShortcutTextLength), {},
PolicyMap::MessageType::kWarning);
ignored_urls_.insert(url);
continue;
}
if (UrlAlreadySeen(policy_name(), url, valid_urls_already_seen, errors,
&duplicated_urls)) {
ignored_urls_.insert(url);
continue;
}
if (!name) {
ignored_urls_.insert(url);
continue;
}
if (name->empty()) {
errors->AddError(policy_name(), IDS_SEARCH_POLICY_SETTINGS_NAME_IS_EMPTY,
{}, PolicyMap::MessageType::kWarning);
ignored_urls_.insert(url);
continue;
}
if (name->length() > kMaxNtpShortcutTextLength) {
errors->AddError(policy_name(), IDS_POLICY_NTP_SHORTCUTS_NAME_TOO_LONG,
base::NumberToString(kMaxNtpShortcutTextLength), {},
PolicyMap::MessageType::kWarning);
ignored_urls_.insert(url);
continue;
}
valid_urls_already_seen.insert(url);
}
// Accept if there is at least one valid shortcut that has a unique URL
// (`valid_urls_already_seen` stores all valid urls including duplicate URLs).
if (valid_urls_already_seen.size() > duplicated_urls.size()) {
return true;
}
errors->AddError(policy_name(), IDS_POLICY_NTP_SHORTCUTS_NO_VALID_PROVIDER);
return false;
}
void NTPShortcutsPolicyHandler::ApplyPolicySettings(const PolicyMap& policies,
PrefValueMap* prefs) {
// If policy handler is disabled, the pref should be cleared to prevent old
// shortcuts from appearing.
if (!IsNTPEnterpriseShortcutsEnabled()) {
prefs->RemoveValue(ntp_tiles::prefs::kEnterpriseShortcutsPolicyList);
return;
}
if (ntp_tiles::kNtpEnterpriseShortcutsUseFakeDataParam.Get()) {
ApplyFakeDataForManualTesting(prefs);
return;
}
const base::Value* policy_value =
policies.GetValue(policy_name(), base::Value::Type::LIST);
if (!policy_value) {
prefs->RemoveValue(ntp_tiles::prefs::kEnterpriseShortcutsPolicyList);
return;
}
base::Value::List shortcuts;
for (const base::Value& item : policy_value->GetList()) {
const base::Value::Dict& policy_dict = item.GetDict();
const std::string* url_str = policy_dict.FindString(kUrl);
// An entry with a missing, empty, or invalid URL should be ignored.
if (!url_str) {
continue;
}
GURL url(*url_str);
if (!url.is_valid()) {
continue;
}
if (ignored_urls_.find(url) == ignored_urls_.end()) {
shortcuts.Append(NTPShortcutsDictFromPolicyValue(policy_dict));
}
}
prefs->SetValue(ntp_tiles::prefs::kEnterpriseShortcutsPolicyList,
base::Value(std::move(shortcuts)));
}
} // namespace policy
|