Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(282)

Unified Diff: components/policy/core/browser/policy_error_map.cc

Issue 144363008: Add support for schema validate error to PolicyErrorMap (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@expand-policy-schema-3.5
Patch Set: rebase; fix ios build Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: components/policy/core/browser/policy_error_map.cc
diff --git a/components/policy/core/browser/policy_error_map.cc b/components/policy/core/browser/policy_error_map.cc
index 611260d3f8378b9850f90457cb857b44660b8cad..9a5f770a3f7e2b23c01afbdaf137092568ea2ad8 100644
--- a/components/policy/core/browser/policy_error_map.cc
+++ b/components/policy/core/browser/policy_error_map.cc
@@ -15,37 +15,118 @@
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;
+class PolicyErrorMap::PendingError {
+ public:
+ PendingError(const std::string& policy_name) : policy_name_(policy_name) {}
Joao da Silva 2014/01/29 09:14:42 explicit
binjin 2014/02/03 14:40:19 Done.
+ virtual ~PendingError() {}
+
+ const std::string& policy_name() const { return policy_name_; }
+
+ virtual base::string16 GetMessage() const = 0;
+
+ private:
+ std::string policy_name_;
+
+ DISALLOW_COPY_AND_ASSIGN(PendingError);
+};
+
+namespace {
+
+class SimplePendingError : public PolicyErrorMap::PendingError {
+ public:
+ SimplePendingError(const std::string& policy_name,
+ int message_id,
+ const std::string& replacement)
+ : PendingError(policy_name),
+ message_id_(message_id),
+ replacement_(replacement) {}
+ virtual ~SimplePendingError() {}
+
+ virtual base::string16 GetMessage() const OVERRIDE {
+ if (message_id_ >= 0) {
+ if (replacement_.empty())
+ return l10n_util::GetStringUTF16(message_id_);
+ return l10n_util::GetStringFUTF16(message_id_,
+ base::ASCIIToUTF16(replacement_));
+ }
+ return base::ASCIIToUTF16(replacement_);
+ }
+
+ private:
+ int message_id_;
+ std::string replacement_;
+
+ DISALLOW_COPY_AND_ASSIGN(SimplePendingError);
+};
+
+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) {}
+ virtual ~DictSubkeyPendingError() {}
+
+ virtual base::string16 GetMessage() const OVERRIDE {
+ return l10n_util::GetStringFUTF16(IDS_POLICY_SUBKEY_ERROR,
+ base::ASCIIToUTF16(subkey_),
+ SimplePendingError::GetMessage());
+ }
+
+ private:
+ std::string subkey_;
+
+ DISALLOW_COPY_AND_ASSIGN(DictSubkeyPendingError);
+};
+
+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) {}
+ virtual ~ListItemPendingError() {}
+
+ virtual base::string16 GetMessage() const OVERRIDE {
+ return l10n_util::GetStringFUTF16(IDS_POLICY_LIST_ENTRY_ERROR,
+ base::IntToString16(index_),
+ SimplePendingError::GetMessage());
+ }
+
+ private:
+ int index_;
+
+ DISALLOW_COPY_AND_ASSIGN(ListItemPendingError);
+};
+
+class SchemaValidatePendingError : public SimplePendingError {
+ public:
+ SchemaValidatePendingError(const std::string& policy_name,
+ const std::string& error_path,
+ int message_id,
+ const std::string& replacement)
+ : SimplePendingError(policy_name, message_id, replacement),
Joao da Silva 2014/01/29 09:14:42 Pass -1 instead of message_id here, and remove the
binjin 2014/02/03 14:40:19 Done.
+ error_path_(error_path) {};
+ virtual ~SchemaValidatePendingError() {}
+
+ virtual base::string16 GetMessage() const OVERRIDE {
+ return l10n_util::GetStringFUTF16(IDS_POLICY_SCHEMA_VALIDATION_ERROR,
+ base::ASCIIToUTF16(error_path_),
+ SimplePendingError::GetMessage());
+ }
+
+ private:
+ std::string error_path_;
+
+ DISALLOW_COPY_AND_ASSIGN(SchemaValidatePendingError);
};
+} // namespace
+
PolicyErrorMap::PolicyErrorMap() {
}
@@ -57,39 +138,46 @@ bool PolicyErrorMap::IsReady() const {
}
void PolicyErrorMap::AddError(const std::string& policy, int message_id) {
- AddError(PendingError(policy, std::string(), -1, message_id));
+ AddError(new SimplePendingError(policy, message_id, std::string()));
}
void PolicyErrorMap::AddError(const std::string& policy,
const std::string& subkey,
int message_id) {
- AddError(PendingError(policy, subkey, -1, message_id));
+ AddError(
+ new DictSubkeyPendingError(policy, subkey, message_id, std::string()));
}
void PolicyErrorMap::AddError(const std::string& policy,
int index,
int message_id) {
- AddError(PendingError(policy, std::string(), index, message_id));
+ AddError(new ListItemPendingError(policy, index, message_id, std::string()));
}
void PolicyErrorMap::AddError(const std::string& policy,
int message_id,
const std::string& replacement) {
- AddError(PendingError(policy, std::string(), -1, message_id, replacement));
+ AddError(new SimplePendingError(policy, 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));
+ AddError(new DictSubkeyPendingError(policy, subkey, 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));
+ AddError(new ListItemPendingError(policy, index, message_id, replacement));
+}
+
+void PolicyErrorMap::AddError(const std::string& policy,
+ const std::string& error_path,
+ const std::string& message) {
+ AddError(new SchemaValidatePendingError(policy, error_path, -1, message));
Joao da Silva 2014/01/29 09:14:42 Remove the -1 argument to SchemaValidatePendingErr
binjin 2014/02/03 14:40:19 Done.
}
base::string16 PolicyErrorMap::GetErrors(const std::string& policy) {
@@ -126,36 +214,17 @@ void PolicyErrorMap::Clear() {
map_.clear();
}
-void PolicyErrorMap::AddError(const PendingError& error) {
+void PolicyErrorMap::AddError(PendingError* error) {
if (IsReady()) {
Convert(error);
+ delete error;
} else {
pending_.push_back(error);
}
}
-void PolicyErrorMap::Convert(const PendingError& error) {
- base::string16 submessage;
- if (error.has_replacement) {
- submessage = l10n_util::GetStringFUTF16(error.message_id,
- base::ASCIIToUTF16(
- error.replacement));
- } else {
- submessage = l10n_util::GetStringUTF16(error.message_id);
- }
- base::string16 message;
- if (!error.subkey.empty()) {
- message = l10n_util::GetStringFUTF16(IDS_POLICY_SUBKEY_ERROR,
- base::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::Convert(PendingError* error) {
+ map_.insert(std::make_pair(error->policy_name(), error->GetMessage()));
}
void PolicyErrorMap::CheckReadyAndConvert() {

Powered by Google App Engine
This is Rietveld 408576698