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
|
// Copyright 2024 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/search_engines/enterprise/search_engine_fields_validators.h"
#include "base/feature_list.h"
#include "base/strings/string_util.h"
#include "components/omnibox/common/omnibox_features.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/search_engines/default_search_manager.h"
#include "components/search_engines/enterprise/search_aggregator_policy_handler.h"
#include "components/search_engines/search_terms_data.h"
#include "components/search_engines/template_url.h"
#include "components/search_engines/template_url_data.h"
#include "components/strings/grit/components_strings.h"
#include "url/gurl.h"
namespace policy::search_engine_fields_validators {
bool ShortcutIsEmpty(const std::string& policy_name,
const std::string& shortcut,
PolicyErrorMap* errors) {
if (!shortcut.empty()) {
return false;
}
errors->AddError(policy_name,
IDS_SEARCH_POLICY_SETTINGS_SHORTCUT_IS_EMPTY);
return true;
}
bool NameIsEmpty(const std::string& policy_name,
const std::string& name,
PolicyErrorMap* errors) {
if (!name.empty()) {
return false;
}
errors->AddError(policy_name, IDS_SEARCH_POLICY_SETTINGS_NAME_IS_EMPTY);
return true;
}
bool UrlIsEmpty(const std::string& policy_name,
const std::string& url,
PolicyErrorMap* errors) {
if (!url.empty()) {
return false;
}
errors->AddError(policy_name, IDS_SEARCH_POLICY_SETTINGS_URL_IS_EMPTY);
return true;
}
bool ShortcutHasWhitespace(const std::string& policy_name,
const std::string& shortcut,
PolicyErrorMap* errors) {
if (shortcut.find_first_of(base::kWhitespaceASCII) == std::u16string::npos) {
return false;
}
errors->AddError(policy_name,
IDS_SEARCH_POLICY_SETTINGS_SHORTCUT_CONTAINS_SPACE,
shortcut);
return true;
}
bool ShortcutStartsWithAtSymbol(const std::string& policy_name,
const std::string& shortcut,
PolicyErrorMap* errors) {
if (shortcut[0] != '@') {
return false;
}
errors->AddError(policy_name,
IDS_SEARCH_POLICY_SETTINGS_SHORTCUT_STARTS_WITH_AT,
shortcut);
return true;
}
bool ShortcutEqualsDefaultSearchProviderKeyword(const std::string& policy_name,
const std::string& shortcut,
const PolicyMap& policies,
PolicyErrorMap* errors) {
const base::Value* provider_enabled = policies.GetValue(
key::kDefaultSearchProviderEnabled, base::Value::Type::BOOLEAN);
const base::Value* provider_keyword = policies.GetValue(
key::kDefaultSearchProviderKeyword, base::Value::Type::STRING);
// Ignore if `DefaultSearchProviderEnabled` is not set, invalid, or disabled.
// Ignore if `DefaultSearchProviderKeyword` is not set, invalid, or different
// from `shortcut`.
if (!provider_enabled || !provider_enabled->GetBool() || !provider_keyword ||
shortcut != provider_keyword->GetString()) {
return false;
}
errors->AddError(policy_name,
IDS_SEARCH_POLICY_SETTINGS_SHORTCUT_EQUALS_DSP_KEYWORD,
shortcut);
return true;
}
bool ReplacementStringIsMissingFromUrl(const std::string& policy_name,
const std::string& url,
PolicyErrorMap* errors) {
TemplateURLData data;
data.SetURL(url);
SearchTermsData search_terms_data;
if (TemplateURL(data).SupportsReplacement(search_terms_data)) {
return false;
}
errors->AddError(
policy_name,
IDS_SEARCH_POLICY_SETTINGS_URL_DOESNT_SUPPORT_REPLACEMENT, url);
return true;
}
bool ShortcutEqualsSearchAggregatorProviderKeyword(const std::string& shortcut,
const PolicyMap& policies,
PolicyErrorMap* errors) {
// Early return if policy is disabled.
if (!base::FeatureList::GetInstance() ||
!base::FeatureList::IsEnabled(omnibox::kEnableSearchAggregatorPolicy)) {
return false;
}
if (!policies.IsPolicySet(key::kEnterpriseSearchAggregatorSettings) ||
!policies.GetValue(key::kEnterpriseSearchAggregatorSettings,
base::Value::Type::DICT)) {
return false;
}
const base::Value::Dict& search_aggregator =
policies
.GetValue(key::kEnterpriseSearchAggregatorSettings,
base::Value::Type::DICT)
->GetDict();
const std::string* aggregator_shortcut =
search_aggregator.FindString(SearchAggregatorPolicyHandler::kShortcut);
if (!aggregator_shortcut || shortcut != *aggregator_shortcut) {
return false;
}
errors->AddError(
key::kSiteSearchSettings,
IDS_POLICY_SITE_SEARCH_SETTINGS_SHORTCUT_EQUALS_SEARCH_AGGREGATOR_KEYWORD,
shortcut);
return true;
}
} // namespace policy::search_engine_fields_validators
|