1a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "components/policy/core/browser/policy_error_map.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <utility>
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
92a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/strings/string_number_conversions.h"
10868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/string_util.h"
11868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/utf_string_conversions.h"
12cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "grit/components_strings.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/base/l10n/l10n_util.h"
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/base/resource/resource_bundle.h"
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace policy {
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class PolicyErrorMap::PendingError {
195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) public:
205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  explicit PendingError(const std::string& policy_name)
215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      : policy_name_(policy_name) {}
225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual ~PendingError() {}
235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  const std::string& policy_name() const { return policy_name_; }
255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual base::string16 GetMessage() const = 0;
275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) private:
295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  std::string policy_name_;
305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(PendingError);
325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)};
335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace {
355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class SimplePendingError : public PolicyErrorMap::PendingError {
375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) public:
385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  SimplePendingError(const std::string& policy_name,
395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                     int message_id,
405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                     const std::string& replacement)
415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      : PendingError(policy_name),
425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        message_id_(message_id),
435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        replacement_(replacement) {}
445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual ~SimplePendingError() {}
455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual base::string16 GetMessage() const OVERRIDE {
475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    if (message_id_ >= 0) {
485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      if (replacement_.empty())
495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        return l10n_util::GetStringUTF16(message_id_);
505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      return l10n_util::GetStringFUTF16(message_id_,
515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                        base::ASCIIToUTF16(replacement_));
525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    }
535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return base::ASCIIToUTF16(replacement_);
545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) private:
575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  int message_id_;
585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  std::string replacement_;
595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(SimplePendingError);
615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)};
625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class DictSubkeyPendingError : public SimplePendingError {
645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) public:
655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  DictSubkeyPendingError(const std::string& policy_name,
665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                         const std::string& subkey,
675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                         int message_id,
685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                         const std::string& replacement)
695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      : SimplePendingError(policy_name, message_id, replacement),
705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        subkey_(subkey) {}
715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual ~DictSubkeyPendingError() {}
725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual base::string16 GetMessage() const OVERRIDE {
745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return l10n_util::GetStringFUTF16(IDS_POLICY_SUBKEY_ERROR,
755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                      base::ASCIIToUTF16(subkey_),
765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                      SimplePendingError::GetMessage());
775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) private:
805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  std::string subkey_;
815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(DictSubkeyPendingError);
835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)};
845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class ListItemPendingError : public SimplePendingError {
865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) public:
875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ListItemPendingError(const std::string& policy_name,
885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                       int index,
895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                       int message_id,
905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                       const std::string& replacement)
915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      : SimplePendingError(policy_name, message_id, replacement),
925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        index_(index) {}
935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual ~ListItemPendingError() {}
945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual base::string16 GetMessage() const OVERRIDE {
965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return l10n_util::GetStringFUTF16(IDS_POLICY_LIST_ENTRY_ERROR,
975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                      base::IntToString16(index_),
985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                      SimplePendingError::GetMessage());
995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
1005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) private:
1025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  int index_;
1035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(ListItemPendingError);
1055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)};
1065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class SchemaValidatingPendingError : public SimplePendingError {
1085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) public:
1095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  SchemaValidatingPendingError(const std::string& policy_name,
1105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                               const std::string& error_path,
1115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                               const std::string& replacement)
1125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      : SimplePendingError(policy_name, -1, replacement),
1135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        error_path_(error_path) {};
1145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual ~SchemaValidatingPendingError() {}
1155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual base::string16 GetMessage() const OVERRIDE {
1175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return l10n_util::GetStringFUTF16(IDS_POLICY_SCHEMA_VALIDATION_ERROR,
1185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                      base::ASCIIToUTF16(error_path_),
1195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                      SimplePendingError::GetMessage());
1205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
1215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) private:
1235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  std::string error_path_;
1245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(SchemaValidatingPendingError);
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}  // namespace
1295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)PolicyErrorMap::PolicyErrorMap() {
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)PolicyErrorMap::~PolicyErrorMap() {
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool PolicyErrorMap::IsReady() const {
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return ui::ResourceBundle::HasSharedInstance();
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void PolicyErrorMap::AddError(const std::string& policy, int message_id) {
1415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  AddError(new SimplePendingError(policy, message_id, std::string()));
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void PolicyErrorMap::AddError(const std::string& policy,
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                              const std::string& subkey,
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                              int message_id) {
1475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  AddError(
1485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      new DictSubkeyPendingError(policy, subkey, message_id, std::string()));
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void PolicyErrorMap::AddError(const std::string& policy,
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                              int index,
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                              int message_id) {
1545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  AddError(new ListItemPendingError(policy, index, message_id, std::string()));
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void PolicyErrorMap::AddError(const std::string& policy,
1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                              int message_id,
1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                              const std::string& replacement) {
1605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  AddError(new SimplePendingError(policy, message_id, replacement));
1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void PolicyErrorMap::AddError(const std::string& policy,
1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                              const std::string& subkey,
1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                              int message_id,
1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                              const std::string& replacement) {
1675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  AddError(new DictSubkeyPendingError(policy, subkey, message_id, replacement));
1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void PolicyErrorMap::AddError(const std::string& policy,
1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                              int index,
1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                              int message_id,
1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                              const std::string& replacement) {
1745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  AddError(new ListItemPendingError(policy, index, message_id, replacement));
1755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void PolicyErrorMap::AddError(const std::string& policy,
1785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                              const std::string& error_path,
1795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                              const std::string& message) {
1805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  AddError(new SchemaValidatingPendingError(policy, error_path, message));
1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
183a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)base::string16 PolicyErrorMap::GetErrors(const std::string& policy) {
1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CheckReadyAndConvert();
1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::pair<const_iterator, const_iterator> range = map_.equal_range(policy);
186a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  std::vector<base::string16> list;
1875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  for (const_iterator it = range.first; it != range.second; ++it)
1885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    list.push_back(it->second);
1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return JoinString(list, '\n');
1905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool PolicyErrorMap::empty() {
1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CheckReadyAndConvert();
1945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return map_.empty();
1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)size_t PolicyErrorMap::size() {
1985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CheckReadyAndConvert();
1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return map_.size();
2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)PolicyErrorMap::const_iterator PolicyErrorMap::begin() {
2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CheckReadyAndConvert();
2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return map_.begin();
2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)PolicyErrorMap::const_iterator PolicyErrorMap::end() {
2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CheckReadyAndConvert();
2095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return map_.end();
2105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void PolicyErrorMap::Clear() {
2135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CheckReadyAndConvert();
2145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  map_.clear();
2155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void PolicyErrorMap::AddError(PendingError* error) {
2185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (IsReady()) {
2195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    Convert(error);
2205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    delete error;
2215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  } else {
2225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    pending_.push_back(error);
2235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void PolicyErrorMap::Convert(PendingError* error) {
2275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  map_.insert(std::make_pair(error->policy_name(), error->GetMessage()));
2285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void PolicyErrorMap::CheckReadyAndConvert() {
2315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(IsReady());
2325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  for (size_t i = 0; i < pending_.size(); ++i) {
2335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    Convert(pending_[i]);
2345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  pending_.clear();
2365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace policy
239