summaryrefslogtreecommitdiffstats
path: root/chrome/browser/policy/policy_error_map.cc
blob: ee49b0e362a43da40959f415216a15e5ec84fd81 (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
// Copyright (c) 2012 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/policy/policy_error_map.h"

#include <utility>

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

namespace policy {

struct PolicyErrorMap::PendingError {
  PendingError(const std::string& policy,
               const std::string& subkey,
               int index,
               int message_id,
               const std::string& replacement)
      : policy(policy),
        subkey(subkey),
        index(index),
        message_id(message_id),
        has_replacement(true),
        replacement(replacement) {}

  PendingError(const std::string& policy,
               const std::string& subkey,
               int index,
               int message_id)
      : policy(policy),
        subkey(subkey),
        index(index),
        message_id(message_id),
        has_replacement(false) {}

  std::string policy;
  std::string subkey;
  int index;
  int message_id;
  bool has_replacement;
  std::string replacement;
};

PolicyErrorMap::PolicyErrorMap() {
}

PolicyErrorMap::~PolicyErrorMap() {
}

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

void PolicyErrorMap::AddError(const std::string& policy, int message_id) {
  AddError(PendingError(policy, std::string(), -1, message_id));
}

void PolicyErrorMap::AddError(const std::string& policy,
                              const std::string& subkey,
                              int message_id) {
  AddError(PendingError(policy, subkey, -1, message_id));
}

void PolicyErrorMap::AddError(const std::string& policy,
                              int index,
                              int message_id) {
  AddError(PendingError(policy, std::string(), index, message_id));
}

void PolicyErrorMap::AddError(const std::string& policy,
                              int message_id,
                              const std::string& replacement) {
  AddError(PendingError(policy, std::string(), -1, message_id, replacement));
}

void PolicyErrorMap::AddError(const std::string& policy,
                              const std::string& subkey,
                              int message_id,
                              const std::string& replacement) {
  AddError(PendingError(policy, subkey, -1, message_id, replacement));
}

void PolicyErrorMap::AddError(const std::string& policy,
                              int index,
                              int message_id,
                              const std::string& replacement) {
  AddError(PendingError(policy, std::string(), index, message_id, replacement));
}

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

bool PolicyErrorMap::empty() {
  CheckReadyAndConvert();
  return 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(const PendingError& error) {
  if (IsReady()) {
    Convert(error);
  } else {
    pending_.push_back(error);
  }
}

void PolicyErrorMap::Convert(const PendingError& error) {
  string16 submessage;
  if (error.has_replacement) {
    submessage = l10n_util::GetStringFUTF16(error.message_id,
                                            ASCIIToUTF16(error.replacement));
  } else {
    submessage = l10n_util::GetStringUTF16(error.message_id);
  }
  string16 message;
  if (!error.subkey.empty()) {
    message = l10n_util::GetStringFUTF16(IDS_POLICY_SUBKEY_ERROR,
                                         ASCIIToUTF16(error.subkey),
                                         submessage);
  } else if (error.index >= 0) {
    message = l10n_util::GetStringFUTF16(IDS_POLICY_LIST_ENTRY_ERROR,
                                         base::IntToString16(error.index),
                                         submessage);
  } else {
    message = submessage;
  }
  map_.insert(std::make_pair(error.policy, message));
}

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

}  // namespace policy