Commit d637103c authored by Brian Malcolm's avatar Brian Malcolm Committed by Commit Bot

Refactor policy handlers with policy names to derive from same base class.

By making TypeCheckingPolicyHandler, SchemaValidatingPolicyHandler, and
SimpleJsonStringSchemaValidatingPolicyHandler all derive from the same
class we can allow SimpleDeprecatingPolicyHandler to work with all of them.

BUG=chromium:1112810

Change-Id: I67a75965c123989e227efc7a9fad47bd4b7f405e
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2337118
Commit-Queue: Brian Malcolm <bmalcolm@chromium.org>
Reviewed-by: default avatarOwen Min <zmin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#794702}
parent 8344291b
...@@ -47,19 +47,25 @@ void ConfigurationPolicyHandler::ApplyPolicySettingsWithParameters( ...@@ -47,19 +47,25 @@ void ConfigurationPolicyHandler::ApplyPolicySettingsWithParameters(
ApplyPolicySettings(policies, prefs); ApplyPolicySettings(policies, prefs);
} }
// NamedPolicyHandler implementation -------------------------------------------
NamedPolicyHandler::NamedPolicyHandler(const char* policy_name)
: policy_name_(policy_name) {}
NamedPolicyHandler::~NamedPolicyHandler() = default;
const char* NamedPolicyHandler::policy_name() const {
return policy_name_;
}
// TypeCheckingPolicyHandler implementation ------------------------------------ // TypeCheckingPolicyHandler implementation ------------------------------------
TypeCheckingPolicyHandler::TypeCheckingPolicyHandler( TypeCheckingPolicyHandler::TypeCheckingPolicyHandler(
const char* policy_name, const char* policy_name,
base::Value::Type value_type) base::Value::Type value_type)
: policy_name_(policy_name), value_type_(value_type) {} : NamedPolicyHandler(policy_name), value_type_(value_type) {}
TypeCheckingPolicyHandler::~TypeCheckingPolicyHandler() {} TypeCheckingPolicyHandler::~TypeCheckingPolicyHandler() {}
const char* TypeCheckingPolicyHandler::policy_name() const {
return policy_name_;
}
bool TypeCheckingPolicyHandler::CheckPolicySettings(const PolicyMap& policies, bool TypeCheckingPolicyHandler::CheckPolicySettings(const PolicyMap& policies,
PolicyErrorMap* errors) { PolicyErrorMap* errors) {
const base::Value* value = nullptr; const base::Value* value = nullptr;
...@@ -69,9 +75,9 @@ bool TypeCheckingPolicyHandler::CheckPolicySettings(const PolicyMap& policies, ...@@ -69,9 +75,9 @@ bool TypeCheckingPolicyHandler::CheckPolicySettings(const PolicyMap& policies,
bool TypeCheckingPolicyHandler::CheckAndGetValue(const PolicyMap& policies, bool TypeCheckingPolicyHandler::CheckAndGetValue(const PolicyMap& policies,
PolicyErrorMap* errors, PolicyErrorMap* errors,
const base::Value** value) { const base::Value** value) {
*value = policies.GetValue(policy_name_); *value = policies.GetValue(policy_name());
if (*value && (*value)->type() != value_type_) { if (*value && (*value)->type() != value_type_) {
errors->AddError(policy_name_, IDS_POLICY_TYPE_ERROR, errors->AddError(policy_name(), IDS_POLICY_TYPE_ERROR,
base::Value::GetTypeName(value_type_)); base::Value::GetTypeName(value_type_));
return false; return false;
} }
...@@ -352,16 +358,12 @@ SchemaValidatingPolicyHandler::SchemaValidatingPolicyHandler( ...@@ -352,16 +358,12 @@ SchemaValidatingPolicyHandler::SchemaValidatingPolicyHandler(
const char* policy_name, const char* policy_name,
Schema schema, Schema schema,
SchemaOnErrorStrategy strategy) SchemaOnErrorStrategy strategy)
: policy_name_(policy_name), schema_(schema), strategy_(strategy) { : NamedPolicyHandler(policy_name), schema_(schema), strategy_(strategy) {
DCHECK(schema_.valid()); DCHECK(schema_.valid());
} }
SchemaValidatingPolicyHandler::~SchemaValidatingPolicyHandler() {} SchemaValidatingPolicyHandler::~SchemaValidatingPolicyHandler() {}
const char* SchemaValidatingPolicyHandler::policy_name() const {
return policy_name_;
}
bool SchemaValidatingPolicyHandler::CheckPolicySettings( bool SchemaValidatingPolicyHandler::CheckPolicySettings(
const PolicyMap& policies, const PolicyMap& policies,
PolicyErrorMap* errors) { PolicyErrorMap* errors) {
...@@ -376,7 +378,7 @@ bool SchemaValidatingPolicyHandler::CheckPolicySettings( ...@@ -376,7 +378,7 @@ bool SchemaValidatingPolicyHandler::CheckPolicySettings(
if (errors && !error.empty()) { if (errors && !error.empty()) {
if (error_path.empty()) if (error_path.empty())
error_path = "(ROOT)"; error_path = "(ROOT)";
errors->AddError(policy_name_, error_path, error); errors->AddError(policy_name(), error_path, error);
} }
return result; return result;
...@@ -399,7 +401,7 @@ bool SchemaValidatingPolicyHandler::CheckAndGetValue( ...@@ -399,7 +401,7 @@ bool SchemaValidatingPolicyHandler::CheckAndGetValue(
if (errors && !error.empty()) { if (errors && !error.empty()) {
if (error_path.empty()) if (error_path.empty())
error_path = "(ROOT)"; error_path = "(ROOT)";
errors->AddError(policy_name_, error_path, error); errors->AddError(policy_name(), error_path, error);
} }
return result; return result;
...@@ -462,8 +464,8 @@ SimpleJsonStringSchemaValidatingPolicyHandler:: ...@@ -462,8 +464,8 @@ SimpleJsonStringSchemaValidatingPolicyHandler::
recommended_permission, recommended_permission,
SimpleSchemaValidatingPolicyHandler::MandatoryPermission SimpleSchemaValidatingPolicyHandler::MandatoryPermission
mandatory_permission) mandatory_permission)
: policy_name_(policy_name), : NamedPolicyHandler(policy_name),
schema_(schema.GetKnownProperty(policy_name_)), schema_(schema.GetKnownProperty(policy_name)),
pref_path_(pref_path), pref_path_(pref_path),
allow_recommended_( allow_recommended_(
recommended_permission == recommended_permission ==
...@@ -478,17 +480,17 @@ SimpleJsonStringSchemaValidatingPolicyHandler:: ...@@ -478,17 +480,17 @@ SimpleJsonStringSchemaValidatingPolicyHandler::
bool SimpleJsonStringSchemaValidatingPolicyHandler::CheckPolicySettings( bool SimpleJsonStringSchemaValidatingPolicyHandler::CheckPolicySettings(
const PolicyMap& policies, const PolicyMap& policies,
PolicyErrorMap* errors) { PolicyErrorMap* errors) {
const base::Value* root_value = policies.GetValue(policy_name_); const base::Value* root_value = policies.GetValue(policy_name());
if (!root_value) if (!root_value)
return true; return true;
const PolicyMap::Entry* policy_entry = policies.Get(policy_name_); const PolicyMap::Entry* policy_entry = policies.Get(policy_name());
if ((policy_entry->level == policy::POLICY_LEVEL_MANDATORY && if ((policy_entry->level == policy::POLICY_LEVEL_MANDATORY &&
!allow_mandatory_) || !allow_mandatory_) ||
(policy_entry->level == policy::POLICY_LEVEL_RECOMMENDED && (policy_entry->level == policy::POLICY_LEVEL_RECOMMENDED &&
!allow_recommended_)) { !allow_recommended_)) {
if (errors) if (errors)
errors->AddError(policy_name_, IDS_POLICY_LEVEL_ERROR); errors->AddError(policy_name(), IDS_POLICY_LEVEL_ERROR);
return false; return false;
} }
...@@ -504,7 +506,7 @@ bool SimpleJsonStringSchemaValidatingPolicyHandler::CheckSingleJsonString( ...@@ -504,7 +506,7 @@ bool SimpleJsonStringSchemaValidatingPolicyHandler::CheckSingleJsonString(
// First validate the root value is a string. // First validate the root value is a string.
if (!root_value->is_string()) { if (!root_value->is_string()) {
if (errors) { if (errors) {
errors->AddError(policy_name_, "(ROOT)", IDS_POLICY_TYPE_ERROR, errors->AddError(policy_name(), "(ROOT)", IDS_POLICY_TYPE_ERROR,
base::Value::GetTypeName(base::Value::Type::STRING)); base::Value::GetTypeName(base::Value::Type::STRING));
} }
return false; return false;
...@@ -525,7 +527,7 @@ bool SimpleJsonStringSchemaValidatingPolicyHandler::CheckListOfJsonStrings( ...@@ -525,7 +527,7 @@ bool SimpleJsonStringSchemaValidatingPolicyHandler::CheckListOfJsonStrings(
// First validate the root value is a list. // First validate the root value is a list.
if (!root_value->is_list()) { if (!root_value->is_list()) {
if (errors) { if (errors) {
errors->AddError(policy_name_, "(ROOT)", IDS_POLICY_TYPE_ERROR, errors->AddError(policy_name(), "(ROOT)", IDS_POLICY_TYPE_ERROR,
base::Value::GetTypeName(base::Value::Type::LIST)); base::Value::GetTypeName(base::Value::Type::LIST));
} }
return false; return false;
...@@ -540,7 +542,7 @@ bool SimpleJsonStringSchemaValidatingPolicyHandler::CheckListOfJsonStrings( ...@@ -540,7 +542,7 @@ bool SimpleJsonStringSchemaValidatingPolicyHandler::CheckListOfJsonStrings(
const base::Value& entry = list[index]; const base::Value& entry = list[index];
if (!entry.is_string()) { if (!entry.is_string()) {
if (errors) { if (errors) {
errors->AddError(policy_name_, index, IDS_POLICY_TYPE_ERROR, errors->AddError(policy_name(), index, IDS_POLICY_TYPE_ERROR,
base::Value::GetTypeName(base::Value::Type::STRING)); base::Value::GetTypeName(base::Value::Type::STRING));
} }
continue; continue;
...@@ -567,7 +569,7 @@ bool SimpleJsonStringSchemaValidatingPolicyHandler::ValidateJsonString( ...@@ -567,7 +569,7 @@ bool SimpleJsonStringSchemaValidatingPolicyHandler::ValidateJsonString(
json_string, base::JSONParserOptions::JSON_ALLOW_TRAILING_COMMAS); json_string, base::JSONParserOptions::JSON_ALLOW_TRAILING_COMMAS);
if (!value_with_error.value) { if (!value_with_error.value) {
if (errors) { if (errors) {
errors->AddError(policy_name_, ErrorPath(index, ""), errors->AddError(policy_name(), ErrorPath(index, ""),
IDS_POLICY_INVALID_JSON_ERROR, IDS_POLICY_INVALID_JSON_ERROR,
value_with_error.error_message); value_with_error.error_message);
} }
...@@ -585,7 +587,7 @@ bool SimpleJsonStringSchemaValidatingPolicyHandler::ValidateJsonString( ...@@ -585,7 +587,7 @@ bool SimpleJsonStringSchemaValidatingPolicyHandler::ValidateJsonString(
SCHEMA_ALLOW_UNKNOWN, SCHEMA_ALLOW_UNKNOWN,
&error_path, &schema_error); &error_path, &schema_error);
if (errors && !schema_error.empty()) if (errors && !schema_error.empty())
errors->AddError(policy_name_, ErrorPath(index, error_path), schema_error); errors->AddError(policy_name(), ErrorPath(index, error_path), schema_error);
if (!validated) if (!validated)
return false; return false;
...@@ -609,13 +611,13 @@ void SimpleJsonStringSchemaValidatingPolicyHandler::ApplyPolicySettings( ...@@ -609,13 +611,13 @@ void SimpleJsonStringSchemaValidatingPolicyHandler::ApplyPolicySettings(
PrefValueMap* prefs) { PrefValueMap* prefs) {
if (!pref_path_) if (!pref_path_)
return; return;
const base::Value* value = policies.GetValue(policy_name_); const base::Value* value = policies.GetValue(policy_name());
if (value) if (value)
prefs->SetValue(pref_path_, value->Clone()); prefs->SetValue(pref_path_, value->Clone());
} }
void SimpleJsonStringSchemaValidatingPolicyHandler::RecordJsonError() { void SimpleJsonStringSchemaValidatingPolicyHandler::RecordJsonError() {
const PolicyDetails* details = GetChromePolicyDetails(policy_name_); const PolicyDetails* details = GetChromePolicyDetails(policy_name());
if (details) { if (details) {
base::UmaHistogramSparse("EnterpriseCheck.InvalidJsonPolicies", base::UmaHistogramSparse("EnterpriseCheck.InvalidJsonPolicies",
details->id); details->id);
...@@ -680,8 +682,8 @@ void LegacyPoliciesDeprecatingPolicyHandler::ApplyPolicySettings( ...@@ -680,8 +682,8 @@ void LegacyPoliciesDeprecatingPolicyHandler::ApplyPolicySettings(
// SimpleDeprecatingPolicyHandler implementation ----------------------- // SimpleDeprecatingPolicyHandler implementation -----------------------
SimpleDeprecatingPolicyHandler::SimpleDeprecatingPolicyHandler( SimpleDeprecatingPolicyHandler::SimpleDeprecatingPolicyHandler(
std::unique_ptr<TypeCheckingPolicyHandler> legacy_policy_handler, std::unique_ptr<NamedPolicyHandler> legacy_policy_handler,
std::unique_ptr<TypeCheckingPolicyHandler> new_policy_handler) std::unique_ptr<NamedPolicyHandler> new_policy_handler)
: legacy_policy_handler_(std::move(legacy_policy_handler)), : legacy_policy_handler_(std::move(legacy_policy_handler)),
new_policy_handler_(std::move(new_policy_handler)) {} new_policy_handler_(std::move(new_policy_handler)) {}
......
...@@ -71,10 +71,25 @@ class POLICY_EXPORT ConfigurationPolicyHandler { ...@@ -71,10 +71,25 @@ class POLICY_EXPORT ConfigurationPolicyHandler {
DISALLOW_COPY_AND_ASSIGN(ConfigurationPolicyHandler); DISALLOW_COPY_AND_ASSIGN(ConfigurationPolicyHandler);
}; };
// Abstract class derived from ConfigurationPolicyHandler that should be
// subclassed to handle policies that have a name.
class POLICY_EXPORT NamedPolicyHandler : public ConfigurationPolicyHandler {
public:
explicit NamedPolicyHandler(const char* policy_name);
~NamedPolicyHandler() override;
NamedPolicyHandler(const NamedPolicyHandler&) = delete;
NamedPolicyHandler& operator=(const NamedPolicyHandler&) = delete;
const char* policy_name() const;
private:
// The name of the policy.
const char* policy_name_;
};
// Abstract class derived from ConfigurationPolicyHandler that should be // Abstract class derived from ConfigurationPolicyHandler that should be
// subclassed to handle a single policy (not a combination of policies). // subclassed to handle a single policy (not a combination of policies).
class POLICY_EXPORT TypeCheckingPolicyHandler class POLICY_EXPORT TypeCheckingPolicyHandler : public NamedPolicyHandler {
: public ConfigurationPolicyHandler {
public: public:
TypeCheckingPolicyHandler(const char* policy_name, TypeCheckingPolicyHandler(const char* policy_name,
base::Value::Type value_type); base::Value::Type value_type);
...@@ -84,8 +99,6 @@ class POLICY_EXPORT TypeCheckingPolicyHandler ...@@ -84,8 +99,6 @@ class POLICY_EXPORT TypeCheckingPolicyHandler
bool CheckPolicySettings(const PolicyMap& policies, bool CheckPolicySettings(const PolicyMap& policies,
PolicyErrorMap* errors) override; PolicyErrorMap* errors) override;
const char* policy_name() const;
protected: protected:
// Runs policy checks and returns the policy value if successful. // Runs policy checks and returns the policy value if successful.
bool CheckAndGetValue(const PolicyMap& policies, bool CheckAndGetValue(const PolicyMap& policies,
...@@ -93,9 +106,6 @@ class POLICY_EXPORT TypeCheckingPolicyHandler ...@@ -93,9 +106,6 @@ class POLICY_EXPORT TypeCheckingPolicyHandler
const base::Value** value); const base::Value** value);
private: private:
// The name of the policy.
const char* policy_name_;
// The type the value of the policy should have. // The type the value of the policy should have.
base::Value::Type value_type_; base::Value::Type value_type_;
...@@ -306,8 +316,7 @@ class POLICY_EXPORT IntPercentageToDoublePolicyHandler ...@@ -306,8 +316,7 @@ class POLICY_EXPORT IntPercentageToDoublePolicyHandler
// Like TypeCheckingPolicyHandler, but validates against a schema instead of a // Like TypeCheckingPolicyHandler, but validates against a schema instead of a
// single type. |schema| is the schema used for this policy, and |strategy| is // single type. |schema| is the schema used for this policy, and |strategy| is
// the strategy used for schema validation errors. // the strategy used for schema validation errors.
class POLICY_EXPORT SchemaValidatingPolicyHandler class POLICY_EXPORT SchemaValidatingPolicyHandler : public NamedPolicyHandler {
: public ConfigurationPolicyHandler {
public: public:
SchemaValidatingPolicyHandler(const char* policy_name, SchemaValidatingPolicyHandler(const char* policy_name,
Schema schema, Schema schema,
...@@ -318,8 +327,6 @@ class POLICY_EXPORT SchemaValidatingPolicyHandler ...@@ -318,8 +327,6 @@ class POLICY_EXPORT SchemaValidatingPolicyHandler
bool CheckPolicySettings(const PolicyMap& policies, bool CheckPolicySettings(const PolicyMap& policies,
PolicyErrorMap* errors) override; PolicyErrorMap* errors) override;
const char* policy_name() const;
protected: protected:
// Runs policy checks and returns the policy value if successful. // Runs policy checks and returns the policy value if successful.
bool CheckAndGetValue(const PolicyMap& policies, bool CheckAndGetValue(const PolicyMap& policies,
...@@ -327,7 +334,6 @@ class POLICY_EXPORT SchemaValidatingPolicyHandler ...@@ -327,7 +334,6 @@ class POLICY_EXPORT SchemaValidatingPolicyHandler
std::unique_ptr<base::Value>* output); std::unique_ptr<base::Value>* output);
private: private:
const char* policy_name_;
const Schema schema_; const Schema schema_;
const SchemaOnErrorStrategy strategy_; const SchemaOnErrorStrategy strategy_;
...@@ -380,7 +386,7 @@ class POLICY_EXPORT SimpleSchemaValidatingPolicyHandler ...@@ -380,7 +386,7 @@ class POLICY_EXPORT SimpleSchemaValidatingPolicyHandler
// - You don't have to parse JSON every time you read it from the pref store. // - You don't have to parse JSON every time you read it from the pref store.
// - Nested dicts are simple, but nested JSON strings are complicated. // - Nested dicts are simple, but nested JSON strings are complicated.
class POLICY_EXPORT SimpleJsonStringSchemaValidatingPolicyHandler class POLICY_EXPORT SimpleJsonStringSchemaValidatingPolicyHandler
: public ConfigurationPolicyHandler { : public NamedPolicyHandler {
public: public:
SimpleJsonStringSchemaValidatingPolicyHandler( SimpleJsonStringSchemaValidatingPolicyHandler(
const char* policy_name, const char* policy_name,
...@@ -431,7 +437,6 @@ class POLICY_EXPORT SimpleJsonStringSchemaValidatingPolicyHandler ...@@ -431,7 +437,6 @@ class POLICY_EXPORT SimpleJsonStringSchemaValidatingPolicyHandler
// Returns true if the schema root is a list. // Returns true if the schema root is a list.
bool IsListSchema() const; bool IsListSchema() const;
const char* policy_name_;
const Schema schema_; const Schema schema_;
const char* pref_path_; const char* pref_path_;
const bool allow_recommended_; const bool allow_recommended_;
...@@ -478,8 +483,8 @@ class POLICY_EXPORT SimpleDeprecatingPolicyHandler ...@@ -478,8 +483,8 @@ class POLICY_EXPORT SimpleDeprecatingPolicyHandler
: public ConfigurationPolicyHandler { : public ConfigurationPolicyHandler {
public: public:
SimpleDeprecatingPolicyHandler( SimpleDeprecatingPolicyHandler(
std::unique_ptr<TypeCheckingPolicyHandler> legacy_policy_handler, std::unique_ptr<NamedPolicyHandler> legacy_policy_handler,
std::unique_ptr<TypeCheckingPolicyHandler> new_policy_handler); std::unique_ptr<NamedPolicyHandler> new_policy_handler);
~SimpleDeprecatingPolicyHandler() override; ~SimpleDeprecatingPolicyHandler() override;
SimpleDeprecatingPolicyHandler(const SimpleDeprecatingPolicyHandler&) = SimpleDeprecatingPolicyHandler(const SimpleDeprecatingPolicyHandler&) =
delete; delete;
...@@ -500,8 +505,8 @@ class POLICY_EXPORT SimpleDeprecatingPolicyHandler ...@@ -500,8 +505,8 @@ class POLICY_EXPORT SimpleDeprecatingPolicyHandler
PrefValueMap* prefs) override; PrefValueMap* prefs) override;
private: private:
std::unique_ptr<TypeCheckingPolicyHandler> legacy_policy_handler_; std::unique_ptr<NamedPolicyHandler> legacy_policy_handler_;
std::unique_ptr<TypeCheckingPolicyHandler> new_policy_handler_; std::unique_ptr<NamedPolicyHandler> new_policy_handler_;
}; };
} // namespace policy } // namespace policy
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment