summaryrefslogtreecommitdiff
path: root/chromium/components/policy/core/browser/policy_error_map.cc
blob: 3f21746b719513399893eb5a2ebb13f66a5680db (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
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
// Copyright 2013 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 "components/policy/core/browser/policy_error_map.h"

#include <string>
#include <utility>

#include "base/strings/string_number_conversions.h"
#include "base/strings/string_piece.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "components/strings/grit/components_strings.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/resource/resource_bundle.h"

namespace policy {

class PolicyErrorMap::PendingError {
 public:
  explicit PendingError(const std::string& policy_name)
      : policy_name_(policy_name) {}
  PendingError(const PendingError&) = delete;
  PendingError& operator=(const PendingError&) = delete;
  virtual ~PendingError() = default;

  const std::string& policy_name() const { return policy_name_; }

  virtual std::u16string GetMessage() const = 0;

 private:
  std::string policy_name_;
};

namespace {

class SimplePendingError : public PolicyErrorMap::PendingError {
 public:
  SimplePendingError(const std::string& policy_name, int message_id)
      : SimplePendingError(policy_name,
                           message_id,
                           std::string(),
                           std::string()) {}
  SimplePendingError(const std::string& policy_name,
                     int message_id,
                     const std::string& replacement_a)
      : SimplePendingError(policy_name,
                           message_id,
                           replacement_a,
                           std::string()) {}

  SimplePendingError(const std::string& policy_name,
                     int message_id,
                     const std::string& replacement_a,
                     const std::string& replacement_b)
      : PendingError(policy_name),
        message_id_(message_id),
        replacement_a_(replacement_a),
        replacement_b_(replacement_b) {
    DCHECK(replacement_b.empty() || !replacement_a.empty());
  }
  SimplePendingError(const SimplePendingError&) = delete;
  SimplePendingError& operator=(const SimplePendingError&) = delete;
  ~SimplePendingError() override = default;

  std::u16string GetMessage() const override {
    if (message_id_ >= 0) {
      if (replacement_a_.empty() && replacement_b_.empty())
        return l10n_util::GetStringUTF16(message_id_);
      if (replacement_b_.empty()) {
        return l10n_util::GetStringFUTF16(message_id_,
                                          base::ASCIIToUTF16(replacement_a_));
      }
      return l10n_util::GetStringFUTF16(message_id_,
                                        base::ASCIIToUTF16(replacement_a_),
                                        base::ASCIIToUTF16(replacement_b_));
    }
    return base::ASCIIToUTF16(replacement_a_);
  }

 private:
  int message_id_;
  std::string replacement_a_;
  std::string replacement_b_;
};

class DictSubkeyPendingError : public SimplePendingError {
 public:
  DictSubkeyPendingError(const std::string& policy_name,
                         const std::string& subkey,
                         int message_id,
                         const std::string& replacement)
      : SimplePendingError(policy_name, message_id, replacement),
        subkey_(subkey) {}
  DictSubkeyPendingError(const DictSubkeyPendingError&) = delete;
  DictSubkeyPendingError& operator=(const DictSubkeyPendingError&) = delete;
  ~DictSubkeyPendingError() override = default;

  std::u16string GetMessage() const override {
    return l10n_util::GetStringFUTF16(IDS_POLICY_SUBKEY_ERROR,
                                      base::ASCIIToUTF16(subkey_),
                                      SimplePendingError::GetMessage());
  }

 private:
  std::string subkey_;
};

class ListItemPendingError : public SimplePendingError {
 public:
  ListItemPendingError(const std::string& policy_name,
                       int index,
                       int message_id,
                       const std::string& replacement)
      : SimplePendingError(policy_name, message_id, replacement),
        index_(index) {}
  ListItemPendingError(const ListItemPendingError&) = delete;
  ListItemPendingError& operator=(const ListItemPendingError&) = delete;
  ~ListItemPendingError() override = default;

  std::u16string GetMessage() const override {
    return l10n_util::GetStringFUTF16(IDS_POLICY_LIST_ENTRY_ERROR,
                                      base::NumberToString16(index_),
                                      SimplePendingError::GetMessage());
  }

 private:
  int index_;
};

class SchemaValidatingPendingError : public SimplePendingError {
 public:
  SchemaValidatingPendingError(const std::string& policy_name,
                               const std::string& error_path,
                               const std::string& replacement)
      : SimplePendingError(policy_name, -1, replacement),
        error_path_(error_path) {}
  SchemaValidatingPendingError(const SchemaValidatingPendingError&) = delete;
  SchemaValidatingPendingError& operator=(const SchemaValidatingPendingError&) =
      delete;
  ~SchemaValidatingPendingError() override = default;

  std::u16string GetMessage() const override {
    return l10n_util::GetStringFUTF16(IDS_POLICY_SCHEMA_VALIDATION_ERROR,
                                      base::ASCIIToUTF16(error_path_),
                                      SimplePendingError::GetMessage());
  }

 private:
  std::string error_path_;
};

}  // namespace

PolicyErrorMap::PolicyErrorMap() = default;

PolicyErrorMap::~PolicyErrorMap() = default;

bool PolicyErrorMap::IsReady() const {
  return ui::ResourceBundle::HasSharedInstance();
}

void PolicyErrorMap::AddError(const std::string& policy, int message_id) {
  AddError(std::make_unique<SimplePendingError>(policy, message_id));
}

void PolicyErrorMap::AddError(const std::string& policy,
                              const std::string& subkey,
                              int message_id) {
  AddError(std::make_unique<DictSubkeyPendingError>(policy, subkey, message_id,
                                                    std::string()));
}

void PolicyErrorMap::AddError(const std::string& policy,
                              int index,
                              int message_id) {
  AddError(std::make_unique<ListItemPendingError>(policy, index, message_id,
                                                  std::string()));
}

void PolicyErrorMap::AddError(const std::string& policy,
                              int message_id,
                              const std::string& replacement) {
  AddError(
      std::make_unique<SimplePendingError>(policy, message_id, replacement));
}

void PolicyErrorMap::AddError(const std::string& policy,
                              int message_id,
                              const std::string& replacement_a,
                              const std::string& replacement_b) {
  AddError(std::make_unique<SimplePendingError>(policy, message_id,
                                                replacement_a, replacement_b));
}

void PolicyErrorMap::AddError(const std::string& policy,
                              const std::string& subkey,
                              int message_id,
                              const std::string& replacement) {
  AddError(std::make_unique<DictSubkeyPendingError>(policy, subkey, message_id,
                                                    replacement));
}

void PolicyErrorMap::AddError(const std::string& policy,
                              int index,
                              int message_id,
                              const std::string& replacement) {
  AddError(std::make_unique<ListItemPendingError>(policy, index, message_id,
                                                  replacement));
}

void PolicyErrorMap::AddError(const std::string& policy,
                              const std::string& error_path,
                              const std::string& message) {
  AddError(std::make_unique<SchemaValidatingPendingError>(policy, error_path,
                                                          message));
}

bool PolicyErrorMap::HasError(const std::string& policy) {
  if (IsReady()) {
    CheckReadyAndConvert();
    return map_.find(policy) != map_.end();
  } else {
    return std::find_if(pending_.begin(), pending_.end(),
                        [policy](const auto& error) {
                          return error->policy_name() == policy;
                        }) != pending_.end();
  }
}

std::u16string PolicyErrorMap::GetErrors(const std::string& policy) {
  CheckReadyAndConvert();
  std::pair<const_iterator, const_iterator> range = map_.equal_range(policy);
  std::vector<base::StringPiece16> list;
  for (auto it = range.first; it != range.second; ++it)
    list.push_back(it->second);
  return base::JoinString(list, u"\n");
}

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();
  debug_infos_.clear();
}

void PolicyErrorMap::SetDebugInfo(const std::string& policy,
                                  const std::string& debug_infos) {
  debug_infos_[policy] = debug_infos;
}

const std::string PolicyErrorMap::GetDebugInfo(const std::string& policy) {
  auto it = debug_infos_.find(policy);
  if (it != debug_infos_.end())
    return it->second;
  return std::string();
}

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(), error->GetMessage()));
}

void PolicyErrorMap::CheckReadyAndConvert() {
  DCHECK(IsReady());
  for (size_t i = 0; i < pending_.size(); ++i) {
    Convert(pending_[i].get());
  }
  pending_.clear();
}

}  // namespace policy