File: default_search_policy_handler.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (315 lines) | stat: -rw-r--r-- 13,081 bytes parent folder | download | duplicates (6)
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
// Copyright 2014 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/default_search_policy_handler.h"

#include <stddef.h>

#include <utility>

#include "base/strings/string_number_conversions.h"
#include "base/values.h"
#include "build/build_config.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/search_engines/default_search_manager.h"
#include "components/search_engines/search_engines_pref_names.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"

namespace policy {

namespace {
// Extracts a list from a policy value and adds it to a pref dictionary.
void SetListInPref(const PolicyMap& policies,
                   const char* policy_name,
                   const char* key,
                   base::Value::Dict& dict) {
  const base::Value* policy_value =
      policies.GetValue(policy_name, base::Value::Type::LIST);
  dict.Set(key, policy_value ? policy_value->Clone()
                             : base::Value(base::Value::Type::LIST));
}

// Extracts a string from a policy value and adds it to a pref dictionary.
void SetStringInPref(const PolicyMap& policies,
                     const char* policy_name,
                     const char* key,
                     base::Value::Dict& dict) {
  const base::Value* policy_value =
      policies.GetValue(policy_name, base::Value::Type::STRING);
  dict.Set(key, policy_value ? policy_value->GetString() : std::string());
}

void SetBooleanInPref(const PolicyMap& policies,
                      const char* policy_name,
                      const char* key,
                      base::Value::Dict& dict) {
  const base::Value* policy_value =
      policies.GetValue(policy_name, base::Value::Type::BOOLEAN);
  dict.SetByDottedPath(key, policy_value && policy_value->GetBool());
}

}  // namespace

// List of policy types to preference names, for policies affecting the default
// search provider. Please update ApplyPolicySettings() when add or remove
// items.
const PolicyToPreferenceMapEntry kDefaultSearchPolicyDataMap[] = {
    {key::kDefaultSearchProviderEnabled, prefs::kDefaultSearchProviderEnabled,
     base::Value::Type::BOOLEAN},
    {key::kDefaultSearchProviderName, DefaultSearchManager::kShortName,
     base::Value::Type::STRING},
    {key::kDefaultSearchProviderSearchURL, DefaultSearchManager::kURL,
     base::Value::Type::STRING},
    {key::kDefaultSearchProviderSuggestURL,
     DefaultSearchManager::kSuggestionsURL, base::Value::Type::STRING},
    {key::kDefaultSearchProviderEncodings,
     DefaultSearchManager::kInputEncodings, base::Value::Type::LIST},
    {key::kDefaultSearchProviderAlternateURLs,
     DefaultSearchManager::kAlternateURLs, base::Value::Type::LIST},
    {key::kDefaultSearchProviderImageURL, DefaultSearchManager::kImageURL,
     base::Value::Type::STRING},
    {key::kDefaultSearchProviderSearchURLPostParams,
     DefaultSearchManager::kSearchURLPostParams, base::Value::Type::STRING},
    {key::kDefaultSearchProviderSuggestURLPostParams,
     DefaultSearchManager::kSuggestionsURLPostParams,
     base::Value::Type::STRING},
    {key::kDefaultSearchProviderImageURLPostParams,
     DefaultSearchManager::kImageURLPostParams, base::Value::Type::STRING},
#if !BUILDFLAG(IS_ANDROID) && !BUILDFLAG(IS_IOS)
    {key::kDefaultSearchProviderContextMenuAccessAllowed,
     prefs::kDefaultSearchProviderContextMenuAccessAllowed,
     base::Value::Type::BOOLEAN},
    {key::kDefaultSearchProviderKeyword, DefaultSearchManager::kKeyword,
     base::Value::Type::STRING},
    {key::kDefaultSearchProviderNewTabURL, DefaultSearchManager::kNewTabURL,
     base::Value::Type::STRING},
#endif
};

// DefaultSearchPolicyHandler implementation -----------------------------------

DefaultSearchPolicyHandler::DefaultSearchPolicyHandler() = default;

DefaultSearchPolicyHandler::~DefaultSearchPolicyHandler() = default;

bool DefaultSearchPolicyHandler::CheckPolicySettings(const PolicyMap& policies,
                                                     PolicyErrorMap* errors) {
  if (!CheckIndividualPolicies(policies, errors))
    return false;

  if (!DefaultSearchProviderPolicyIsSet(policies) ||
      DefaultSearchProviderIsDisabled(policies)) {
    // Add an error for all specified default search policies except
    // DefaultSearchProviderEnabled and
    // DefaultSearchProviderContextMenuAccessAllowed.

    for (const auto& policy_map_entry : kDefaultSearchPolicyDataMap) {
      const char* policy_name = policy_map_entry.policy_name;
      if (policy_name != key::kDefaultSearchProviderEnabled &&
#if !BUILDFLAG(IS_ANDROID) && !BUILDFLAG(IS_IOS)
          policy_name != key::kDefaultSearchProviderContextMenuAccessAllowed &&
#endif
          HasDefaultSearchPolicy(policies, policy_name)) {
        errors->AddError(policy_name, IDS_POLICY_DEFAULT_SEARCH_DISABLED);
      }
    }
    return true;
  }

  const base::Value* url;
  std::string dummy;
  if (DefaultSearchURLIsValid(policies, &url, &dummy) ||
      !AnyDefaultSearchPoliciesSpecified(policies))
    return true;
  errors->AddError(key::kDefaultSearchProviderSearchURL, url ?
      IDS_POLICY_INVALID_SEARCH_URL_ERROR : IDS_POLICY_NOT_SPECIFIED_ERROR);
  return false;
}

void DefaultSearchPolicyHandler::ApplyPolicySettings(const PolicyMap& policies,
                                                     PrefValueMap* prefs) {
  // If the main switch is not set don't set anything.
  if (!DefaultSearchProviderPolicyIsSet(policies))
    return;

  if (DefaultSearchProviderIsDisabled(policies)) {
    base::Value::Dict dict;
    dict.Set(DefaultSearchManager::kDisabledByPolicy, true);
    DefaultSearchManager::AddPrefValueToMap(std::move(dict), prefs);
    return;
  }

  // The search URL is required.  The other entries are optional.  Just make
  // sure that they are all specified via policy, so that the regular prefs
  // aren't used.
  const base::Value* dummy;
  std::string url;
  if (!DefaultSearchURLIsValid(policies, &dummy, &url))
    return;

  base::Value::Dict dict;

  // Set pref values for policies affecting the default
  // search provider, which are listed in kDefaultSearchPolicyDataMap.
  // Set or remove pref accordingly when kDefaultSearchPolicyDataMap has a
  // change, then revise the number in the check below to be correct.
  SetBooleanInPref(policies, key::kDefaultSearchProviderEnabled,
                   prefs::kDefaultSearchProviderEnabled, dict);
  SetStringInPref(policies, key::kDefaultSearchProviderName,
                  DefaultSearchManager::kShortName, dict);
  SetStringInPref(policies, key::kDefaultSearchProviderSearchURL,
                  DefaultSearchManager::kURL, dict);
  SetStringInPref(policies, key::kDefaultSearchProviderSuggestURL,
                  DefaultSearchManager::kSuggestionsURL, dict);
  SetListInPref(policies, key::kDefaultSearchProviderEncodings,
                DefaultSearchManager::kInputEncodings, dict);
  SetListInPref(policies, key::kDefaultSearchProviderAlternateURLs,
                DefaultSearchManager::kAlternateURLs, dict);
  SetStringInPref(policies, key::kDefaultSearchProviderImageURL,
                  DefaultSearchManager::kImageURL, dict);
  SetStringInPref(policies, key::kDefaultSearchProviderSearchURLPostParams,
                  DefaultSearchManager::kSearchURLPostParams, dict);
  SetStringInPref(policies, key::kDefaultSearchProviderSuggestURLPostParams,
                  DefaultSearchManager::kSuggestionsURLPostParams, dict);
  SetStringInPref(policies, key::kDefaultSearchProviderImageURLPostParams,
                  DefaultSearchManager::kImageURLPostParams, dict);
#if !BUILDFLAG(IS_ANDROID) && !BUILDFLAG(IS_IOS)
  SetBooleanInPref(policies,
                   key::kDefaultSearchProviderContextMenuAccessAllowed,
                   prefs::kDefaultSearchProviderContextMenuAccessAllowed, dict);
  SetStringInPref(policies, key::kDefaultSearchProviderKeyword,
                  DefaultSearchManager::kKeyword, dict);
  SetStringInPref(policies, key::kDefaultSearchProviderNewTabURL,
                  DefaultSearchManager::kNewTabURL, dict);
  size_t policyCount = 13;
#else
  size_t policyCount = 10;
#endif

  CHECK_EQ(policyCount, std::size(kDefaultSearchPolicyDataMap));

  // Set the fields which are not specified by the policy to default values.
  dict.Set(DefaultSearchManager::kID,
           base::NumberToString(kInvalidTemplateURLID));
  dict.Set(DefaultSearchManager::kPrepopulateID, 0);
  dict.Set(DefaultSearchManager::kStarterPackId, 0);
  dict.Set(DefaultSearchManager::kSyncGUID, std::string());
  dict.Set(DefaultSearchManager::kOriginatingURL, std::string());
  dict.Set(DefaultSearchManager::kSafeForAutoReplace, true);
  dict.Set(DefaultSearchManager::kDateCreated,
           static_cast<double>(base::Time::Now().ToInternalValue()));
  dict.Set(DefaultSearchManager::kLastModified,
           static_cast<double>(base::Time::Now().ToInternalValue()));
  dict.Set(DefaultSearchManager::kUsageCount, 0);
  dict.Set(
      DefaultSearchManager::kPolicyOrigin,
      static_cast<int>(TemplateURLData::PolicyOrigin::kDefaultSearchProvider));

  // For the name and keyword, default to the host if not specified.  If
  // there is no host (as is the case with file URLs of the form:
  // "file:///c:/..."), use "_" to guarantee that the keyword is non-empty.
  std::string* keyword = dict.FindString(DefaultSearchManager::kKeyword);
  std::string* name = dict.FindString(DefaultSearchManager::kShortName);
  std::string* url_str = dict.FindString(DefaultSearchManager::kURL);
  if (url_str)
    url = *url_str;

  std::string host(GURL(url).host());
  if (host.empty())
    host = "_";
  if (!name || name->empty())
    dict.Set(DefaultSearchManager::kShortName, host);
  if (!keyword || keyword->empty())
    dict.Set(DefaultSearchManager::kKeyword, host);

  DefaultSearchManager::AddPrefValueToMap(std::move(dict), prefs);
}

bool DefaultSearchPolicyHandler::CheckIndividualPolicies(
    const PolicyMap& policies,
    PolicyErrorMap* errors) {
  bool all_ok = true;
  for (const auto& policy_map_entry : kDefaultSearchPolicyDataMap) {
    // It's safe to use `GetValueUnsafe()` as multiple policy types are handled.
    // It's important to check policy type for all policies and not just exit on
    // the first error, so we report all policy errors.
    const base::Value* value =
        policies.GetValueUnsafe(policy_map_entry.policy_name);
    if (value && value->type() != policy_map_entry.value_type) {
      errors->AddError(policy_map_entry.policy_name, IDS_POLICY_TYPE_ERROR,
                       base::Value::GetTypeName(policy_map_entry.value_type));
      all_ok = false;
    }
  }
  return all_ok;
}

bool DefaultSearchPolicyHandler::HasDefaultSearchPolicy(
    const PolicyMap& policies,
    const char* policy_name) {
  return policies.Get(policy_name) != nullptr;
}

bool DefaultSearchPolicyHandler::AnyDefaultSearchPoliciesSpecified(
    const PolicyMap& policies) {
  for (const auto& policy_map_entry : kDefaultSearchPolicyDataMap) {
    if (policies.Get(policy_map_entry.policy_name))
      return true;
  }
  return false;
}

bool DefaultSearchPolicyHandler::DefaultSearchProviderIsDisabled(
    const PolicyMap& policies) {
  const base::Value* provider_enabled = policies.GetValue(
      key::kDefaultSearchProviderEnabled, base::Value::Type::BOOLEAN);
  return provider_enabled && !provider_enabled->GetBool();
}

bool DefaultSearchPolicyHandler::DefaultSearchProviderPolicyIsSet(
    const PolicyMap& policies) {
  return HasDefaultSearchPolicy(policies, key::kDefaultSearchProviderEnabled);
}

bool DefaultSearchPolicyHandler::DefaultSearchURLIsValid(
    const PolicyMap& policies,
    const base::Value** url_value,
    std::string* url_string) {
  *url_value = policies.GetValue(key::kDefaultSearchProviderSearchURL,
                                 base::Value::Type::STRING);
  if (!*url_value)
    return false;

  *url_string = (*url_value)->GetString();
  if (url_string->empty())
    return false;
  TemplateURLData data;
  data.SetURL(*url_string);
  SearchTermsData search_terms_data;
  return TemplateURL(data).SupportsReplacement(search_terms_data);
}

void DefaultSearchPolicyHandler::EnsureStringPrefExists(
    PrefValueMap* prefs,
    const std::string& path) {
  std::string value;
  if (!prefs->GetString(path, &value))
    prefs->SetString(path, value);
}

void DefaultSearchPolicyHandler::EnsureListPrefExists(
    PrefValueMap* prefs,
    const std::string& path) {
  base::Value* value;
  if (!prefs->GetValue(path, &value) || !value->is_list())
    prefs->SetValue(path, base::Value(base::Value::Type::LIST));
}

}  // namespace policy