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
|
// Copyright 2013 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/policy/core/browser/policy_error_map.h"
#include <algorithm>
#include <iterator>
#include <string>
#include <string_view>
#include <utility>
#include <vector>
#include "base/check.h"
#include "base/containers/contains.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "components/policy/core/common/schema.h"
#include "components/strings/grit/components_strings.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/resource/resource_bundle.h"
namespace policy {
namespace {
std::u16string ConvertReplacementToUTF16(const std::string& replacement) {
if (!base::IsStringUTF8(replacement))
return u"<invalid Unicode string>";
return base::UTF8ToUTF16(replacement);
}
} // namespace
bool PolicyErrorMap::Data::operator==(const PolicyErrorMap::Data& other) const {
return std::tie(message, level) == std::tie(other.message, other.level);
}
class PolicyErrorMap::PendingError {
public:
PendingError(const std::string& policy_name,
int message_id,
std::vector<std::string> replacements,
const PolicyErrorPath& error_path,
const PolicyMap::MessageType level)
: policy_name_(policy_name),
message_id_(message_id),
replacements_(std::move(replacements)),
error_path_string_(ErrorPathToString(policy_name, error_path)),
level_(level) {
DCHECK(!std::ranges::any_of(replacements_, &std::string::empty));
}
PendingError(const PendingError&) = delete;
PendingError& operator=(const PendingError&) = delete;
~PendingError() = default;
const std::string& policy_name() const { return policy_name_; }
const PolicyMap::MessageType& level() const { return level_; }
std::u16string GetMessage() const {
if (error_path_string_.empty())
return GetMessageContent();
return l10n_util::GetStringFUTF16(IDS_POLICY_ERROR_WITH_PATH,
base::ASCIIToUTF16(error_path_string_),
GetMessageContent());
}
std::u16string GetMessageContent() const {
// TODO(crbug.com/40832324): remove this together with
// AddError(policy, message, error_path) and add a DCHECK
if (message_id_ >= 0) {
std::vector<std::u16string> utf_16_replacements;
std::ranges::transform(replacements_,
std::back_inserter(utf_16_replacements),
&ConvertReplacementToUTF16);
return l10n_util::GetStringFUTF16(message_id_, utf_16_replacements,
nullptr);
}
if (!replacements_.empty()) {
return ConvertReplacementToUTF16(replacements_.front());
}
return std::u16string();
}
private:
std::string policy_name_;
int message_id_;
std::vector<std::string> replacements_;
std::string error_path_string_;
PolicyMap::MessageType level_;
};
PolicyErrorMap::PolicyErrorMap() = default;
PolicyErrorMap::~PolicyErrorMap() = default;
bool PolicyErrorMap::IsReady() const {
return ui::ResourceBundle::HasSharedInstance();
}
void PolicyErrorMap::AddError(const std::string& policy,
int message_id,
PolicyErrorPath error_path,
PolicyMap::MessageType level) {
AddError(policy, message_id, std::vector<std::string>(), error_path, level);
}
void PolicyErrorMap::AddError(const std::string& policy,
int message_id,
const std::string& replacement,
PolicyErrorPath error_path,
PolicyMap::MessageType level) {
AddError(policy, message_id, std::vector<std::string>{replacement},
error_path, level);
}
void PolicyErrorMap::AddError(const std::string& policy,
int message_id,
const std::string& replacement_a,
const std::string& replacement_b,
PolicyErrorPath error_path,
PolicyMap::MessageType level) {
AddError(policy, message_id,
std::vector<std::string>{replacement_a, replacement_b}, error_path,
level);
}
void PolicyErrorMap::AddError(const std::string& policy,
int message_id,
std::vector<std::string> replacements,
PolicyErrorPath error_path,
PolicyMap::MessageType level) {
AddError(std::make_unique<PendingError>(
policy, message_id, std::move(replacements), error_path, level));
}
bool PolicyErrorMap::HasError(const std::string& policy) {
if (IsReady()) {
CheckReadyAndConvert();
return base::Contains(map_, policy);
}
return base::Contains(pending_, policy, &PendingError::policy_name);
}
bool PolicyErrorMap::HasFatalError(const std::string& policy) {
std::pair<std::string, PolicyMap::MessageType> fatal_error =
std::make_pair(policy, PolicyMap::MessageType::kError);
if (IsReady()) {
CheckReadyAndConvert();
return base::Contains(
map_, fatal_error, [](const std::pair<std::string, Data>& entry) {
return std::make_pair(entry.first, entry.second.level);
});
}
return base::Contains(pending_, fatal_error, [](const auto& entry) {
return std::make_pair(entry->policy_name(), entry->level());
});
}
std::u16string PolicyErrorMap::GetErrorMessages(const std::string& policy) {
return GetErrorMessages(policy, PolicyMap::MessageType::kError);
}
std::u16string PolicyErrorMap::GetErrorMessages(
const std::string& policy,
PolicyMap::MessageType message_type) {
CheckReadyAndConvert();
std::pair<const_iterator, const_iterator> range = map_.equal_range(policy);
std::vector<std::u16string_view> list;
for (auto it = range.first; it != range.second; ++it) {
if (it->second.level == message_type) {
list.push_back(it->second.message);
}
}
return base::JoinString(list, u"\n");
}
std::vector<PolicyErrorMap::Data> PolicyErrorMap::GetErrors(
const std::string& policy) {
CheckReadyAndConvert();
std::pair<const_iterator, const_iterator> range = map_.equal_range(policy);
std::vector<PolicyErrorMap::Data> list;
for (auto it = range.first; it != range.second; ++it)
list.push_back(it->second);
return list;
}
bool PolicyErrorMap::empty() const {
// This doesn't call CheckReadyAndConvert() to allow code to destroy empty
// PolicyErrorMaps rather than having to wait for ResourceBundle to be ready.
return pending_.empty() && map_.empty();
}
size_t PolicyErrorMap::size() {
CheckReadyAndConvert();
return map_.size();
}
PolicyErrorMap::const_iterator PolicyErrorMap::begin() {
CheckReadyAndConvert();
return map_.begin();
}
PolicyErrorMap::const_iterator PolicyErrorMap::end() {
CheckReadyAndConvert();
return map_.end();
}
void PolicyErrorMap::Clear() {
CheckReadyAndConvert();
map_.clear();
}
void PolicyErrorMap::AddError(std::unique_ptr<PendingError> error) {
if (IsReady()) {
Convert(error.get());
// Allow error to be deleted as it exits function scope.
} else {
pending_.push_back(std::move(error));
}
}
void PolicyErrorMap::Convert(PendingError* error) {
map_.insert(std::make_pair(
error->policy_name(),
Data{.message = error->GetMessage(), .level = error->level()}));
}
void PolicyErrorMap::CheckReadyAndConvert() {
DCHECK(IsReady());
for (size_t i = 0; i < pending_.size(); ++i) {
Convert(pending_[i].get());
}
pending_.clear();
}
} // namespace policy
|