1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/policy/policy_error_map.h"
9 #include "base/strings/string_number_conversions.h"
10 #include "base/strings/string_util.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "grit/generated_resources.h"
13 #include "ui/base/l10n/l10n_util.h"
14 #include "ui/base/resource/resource_bundle.h"
18 struct PolicyErrorMap::PendingError
{
19 PendingError(const std::string
& policy
,
20 const std::string
& subkey
,
23 const std::string
& replacement
)
27 message_id(message_id
),
28 has_replacement(true),
29 replacement(replacement
) {}
31 PendingError(const std::string
& policy
,
32 const std::string
& subkey
,
38 message_id(message_id
),
39 has_replacement(false) {}
46 std::string replacement
;
49 PolicyErrorMap::PolicyErrorMap() {
52 PolicyErrorMap::~PolicyErrorMap() {
55 bool PolicyErrorMap::IsReady() const {
56 return ui::ResourceBundle::HasSharedInstance();
59 void PolicyErrorMap::AddError(const std::string
& policy
, int message_id
) {
60 AddError(PendingError(policy
, std::string(), -1, message_id
));
63 void PolicyErrorMap::AddError(const std::string
& policy
,
64 const std::string
& subkey
,
66 AddError(PendingError(policy
, subkey
, -1, message_id
));
69 void PolicyErrorMap::AddError(const std::string
& policy
,
72 AddError(PendingError(policy
, std::string(), index
, message_id
));
75 void PolicyErrorMap::AddError(const std::string
& policy
,
77 const std::string
& replacement
) {
78 AddError(PendingError(policy
, std::string(), -1, message_id
, replacement
));
81 void PolicyErrorMap::AddError(const std::string
& policy
,
82 const std::string
& subkey
,
84 const std::string
& replacement
) {
85 AddError(PendingError(policy
, subkey
, -1, message_id
, replacement
));
88 void PolicyErrorMap::AddError(const std::string
& policy
,
91 const std::string
& replacement
) {
92 AddError(PendingError(policy
, std::string(), index
, message_id
, replacement
));
95 string16
PolicyErrorMap::GetErrors(const std::string
& policy
) {
96 CheckReadyAndConvert();
97 std::pair
<const_iterator
, const_iterator
> range
= map_
.equal_range(policy
);
98 std::vector
<string16
> list
;
99 for (const_iterator it
= range
.first
; it
!= range
.second
; ++it
)
100 list
.push_back(it
->second
);
101 return JoinString(list
, '\n');
104 bool PolicyErrorMap::empty() {
105 CheckReadyAndConvert();
109 size_t PolicyErrorMap::size() {
110 CheckReadyAndConvert();
114 PolicyErrorMap::const_iterator
PolicyErrorMap::begin() {
115 CheckReadyAndConvert();
119 PolicyErrorMap::const_iterator
PolicyErrorMap::end() {
120 CheckReadyAndConvert();
124 void PolicyErrorMap::Clear() {
125 CheckReadyAndConvert();
129 void PolicyErrorMap::AddError(const PendingError
& error
) {
133 pending_
.push_back(error
);
137 void PolicyErrorMap::Convert(const PendingError
& error
) {
139 if (error
.has_replacement
) {
140 submessage
= l10n_util::GetStringFUTF16(error
.message_id
,
141 ASCIIToUTF16(error
.replacement
));
143 submessage
= l10n_util::GetStringUTF16(error
.message_id
);
146 if (!error
.subkey
.empty()) {
147 message
= l10n_util::GetStringFUTF16(IDS_POLICY_SUBKEY_ERROR
,
148 ASCIIToUTF16(error
.subkey
),
150 } else if (error
.index
>= 0) {
151 message
= l10n_util::GetStringFUTF16(IDS_POLICY_LIST_ENTRY_ERROR
,
152 base::IntToString16(error
.index
),
155 message
= submessage
;
157 map_
.insert(std::make_pair(error
.policy
, message
));
160 void PolicyErrorMap::CheckReadyAndConvert() {
162 for (size_t i
= 0; i
< pending_
.size(); ++i
) {
163 Convert(pending_
[i
]);
168 } // namespace policy