1 // Copyright 2013 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 "components/policy/core/browser/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/component_strings.h"
13 #include "ui/base/l10n/l10n_util.h"
14 #include "ui/base/resource/resource_bundle.h"
18 class PolicyErrorMap::PendingError
{
20 explicit PendingError(const std::string
& policy_name
)
21 : policy_name_(policy_name
) {}
22 virtual ~PendingError() {}
24 const std::string
& policy_name() const { return policy_name_
; }
26 virtual base::string16
GetMessage() const = 0;
29 std::string policy_name_
;
31 DISALLOW_COPY_AND_ASSIGN(PendingError
);
36 class SimplePendingError
: public PolicyErrorMap::PendingError
{
38 SimplePendingError(const std::string
& policy_name
,
40 const std::string
& replacement
)
41 : PendingError(policy_name
),
42 message_id_(message_id
),
43 replacement_(replacement
) {}
44 virtual ~SimplePendingError() {}
46 virtual base::string16
GetMessage() const OVERRIDE
{
47 if (message_id_
>= 0) {
48 if (replacement_
.empty())
49 return l10n_util::GetStringUTF16(message_id_
);
50 return l10n_util::GetStringFUTF16(message_id_
,
51 base::ASCIIToUTF16(replacement_
));
53 return base::ASCIIToUTF16(replacement_
);
58 std::string replacement_
;
60 DISALLOW_COPY_AND_ASSIGN(SimplePendingError
);
63 class DictSubkeyPendingError
: public SimplePendingError
{
65 DictSubkeyPendingError(const std::string
& policy_name
,
66 const std::string
& subkey
,
68 const std::string
& replacement
)
69 : SimplePendingError(policy_name
, message_id
, replacement
),
71 virtual ~DictSubkeyPendingError() {}
73 virtual base::string16
GetMessage() const OVERRIDE
{
74 return l10n_util::GetStringFUTF16(IDS_POLICY_SUBKEY_ERROR
,
75 base::ASCIIToUTF16(subkey_
),
76 SimplePendingError::GetMessage());
82 DISALLOW_COPY_AND_ASSIGN(DictSubkeyPendingError
);
85 class ListItemPendingError
: public SimplePendingError
{
87 ListItemPendingError(const std::string
& policy_name
,
90 const std::string
& replacement
)
91 : SimplePendingError(policy_name
, message_id
, replacement
),
93 virtual ~ListItemPendingError() {}
95 virtual base::string16
GetMessage() const OVERRIDE
{
96 return l10n_util::GetStringFUTF16(IDS_POLICY_LIST_ENTRY_ERROR
,
97 base::IntToString16(index_
),
98 SimplePendingError::GetMessage());
104 DISALLOW_COPY_AND_ASSIGN(ListItemPendingError
);
107 class SchemaValidatingPendingError
: public SimplePendingError
{
109 SchemaValidatingPendingError(const std::string
& policy_name
,
110 const std::string
& error_path
,
111 const std::string
& replacement
)
112 : SimplePendingError(policy_name
, -1, replacement
),
113 error_path_(error_path
) {};
114 virtual ~SchemaValidatingPendingError() {}
116 virtual base::string16
GetMessage() const OVERRIDE
{
117 return l10n_util::GetStringFUTF16(IDS_POLICY_SCHEMA_VALIDATION_ERROR
,
118 base::ASCIIToUTF16(error_path_
),
119 SimplePendingError::GetMessage());
123 std::string error_path_
;
125 DISALLOW_COPY_AND_ASSIGN(SchemaValidatingPendingError
);
130 PolicyErrorMap::PolicyErrorMap() {
133 PolicyErrorMap::~PolicyErrorMap() {
136 bool PolicyErrorMap::IsReady() const {
137 return ui::ResourceBundle::HasSharedInstance();
140 void PolicyErrorMap::AddError(const std::string
& policy
, int message_id
) {
141 AddError(new SimplePendingError(policy
, message_id
, std::string()));
144 void PolicyErrorMap::AddError(const std::string
& policy
,
145 const std::string
& subkey
,
148 new DictSubkeyPendingError(policy
, subkey
, message_id
, std::string()));
151 void PolicyErrorMap::AddError(const std::string
& policy
,
154 AddError(new ListItemPendingError(policy
, index
, message_id
, std::string()));
157 void PolicyErrorMap::AddError(const std::string
& policy
,
159 const std::string
& replacement
) {
160 AddError(new SimplePendingError(policy
, message_id
, replacement
));
163 void PolicyErrorMap::AddError(const std::string
& policy
,
164 const std::string
& subkey
,
166 const std::string
& replacement
) {
167 AddError(new DictSubkeyPendingError(policy
, subkey
, message_id
, replacement
));
170 void PolicyErrorMap::AddError(const std::string
& policy
,
173 const std::string
& replacement
) {
174 AddError(new ListItemPendingError(policy
, index
, message_id
, replacement
));
177 void PolicyErrorMap::AddError(const std::string
& policy
,
178 const std::string
& error_path
,
179 const std::string
& message
) {
180 AddError(new SchemaValidatingPendingError(policy
, error_path
, message
));
183 base::string16
PolicyErrorMap::GetErrors(const std::string
& policy
) {
184 CheckReadyAndConvert();
185 std::pair
<const_iterator
, const_iterator
> range
= map_
.equal_range(policy
);
186 std::vector
<base::string16
> list
;
187 for (const_iterator it
= range
.first
; it
!= range
.second
; ++it
)
188 list
.push_back(it
->second
);
189 return JoinString(list
, '\n');
192 bool PolicyErrorMap::empty() {
193 CheckReadyAndConvert();
197 size_t PolicyErrorMap::size() {
198 CheckReadyAndConvert();
202 PolicyErrorMap::const_iterator
PolicyErrorMap::begin() {
203 CheckReadyAndConvert();
207 PolicyErrorMap::const_iterator
PolicyErrorMap::end() {
208 CheckReadyAndConvert();
212 void PolicyErrorMap::Clear() {
213 CheckReadyAndConvert();
217 void PolicyErrorMap::AddError(PendingError
* error
) {
222 pending_
.push_back(error
);
226 void PolicyErrorMap::Convert(PendingError
* error
) {
227 map_
.insert(std::make_pair(error
->policy_name(), error
->GetMessage()));
230 void PolicyErrorMap::CheckReadyAndConvert() {
232 for (size_t i
= 0; i
< pending_
.size(); ++i
) {
233 Convert(pending_
[i
]);
238 } // namespace policy