summaryrefslogtreecommitdiffstats
path: root/chromium/chrome/browser/spellchecker/spellcheck_language_policy_handler.cc
blob: c451930d32d2a10f9100e9865f60e1059a146196 (plain)
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
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/spellchecker/spellcheck_language_policy_handler.h"

#include <utility>
#include <vector>

#include "base/strings/string_util.h"
#include "base/syslog_logging.h"
#include "base/values.h"
#include "build/build_config.h"
#include "chrome/common/pref_names.h"
#include "components/policy/core/browser/policy_error_map.h"
#include "components/policy/policy_constants.h"
#include "components/prefs/pref_value_map.h"
#include "components/spellcheck/browser/pref_names.h"
#include "components/spellcheck/common/spellcheck_common.h"
#include "components/strings/grit/components_strings.h"

SpellcheckLanguagePolicyHandler::SpellcheckLanguagePolicyHandler()
    : TypeCheckingPolicyHandler(policy::key::kSpellcheckLanguage,
                                base::Value::Type::LIST) {}

SpellcheckLanguagePolicyHandler::~SpellcheckLanguagePolicyHandler() = default;

bool SpellcheckLanguagePolicyHandler::CheckPolicySettings(
    const policy::PolicyMap& policies,
    policy::PolicyErrorMap* errors) {
  const base::Value* value = nullptr;
  bool ok = CheckAndGetValue(policies, errors, &value);

  std::vector<base::Value> forced;
  std::vector<std::string> unknown;
  SortForcedLanguages(policies, &forced, &unknown);

#if !defined(OS_MACOSX)
  for (const auto& language : unknown) {
    errors->AddError(policy_name(), IDS_POLICY_SPELLCHECK_UNKNOWN_LANGUAGE,
                     language);
  }
#endif

  return ok;
}

void SpellcheckLanguagePolicyHandler::ApplyPolicySettings(
    const policy::PolicyMap& policies,
    PrefValueMap* prefs) {
  // Ignore this policy if the SpellcheckEnabled policy disables spellcheck.
  const base::Value* spellcheck_enabled_value =
      policies.GetValue(policy::key::kSpellcheckEnabled);
  if (spellcheck_enabled_value && spellcheck_enabled_value->GetBool() == false)
    return;

  // If this policy isn't set, don't modify spellcheck languages.
  const base::Value* value = policies.GetValue(policy_name());
  if (!value)
    return;

  // Set the forced dictionaries preference based on this policy's values,
  // and emit warnings for unknown languages.
  std::vector<base::Value> forced;
  std::vector<std::string> unknown;
  SortForcedLanguages(policies, &forced, &unknown);

  for (const auto& language : unknown) {
    SYSLOG(WARNING)
        << "SpellcheckLanguage policy: Unknown or unsupported language \""
        << language << "\"";
  }

  prefs->SetValue(spellcheck::prefs::kSpellCheckEnable, base::Value(true));
  prefs->SetValue(spellcheck::prefs::kSpellCheckForcedDictionaries,
                  base::Value(std::move(forced)));
}

void SpellcheckLanguagePolicyHandler::SortForcedLanguages(
    const policy::PolicyMap& policies,
    std::vector<base::Value>* const forced,
    std::vector<std::string>* const unknown) {
  const base::Value* value = policies.GetValue(policy_name());
  if (!value)
    return;

  // Separate the valid languages from the unknown / unsupported languages.
  const base::Value::ListStorage& forced_languages = value->GetList();
  for (const base::Value& language : forced_languages) {
    std::string current_language =
        spellcheck::GetCorrespondingSpellCheckLanguage(
            base::TrimWhitespaceASCII(language.GetString(), base::TRIM_ALL));

    if (current_language.empty()) {
      unknown->emplace_back(language.GetString());
    } else {
      forced->emplace_back(std::move(current_language));
    }
  }
}