Commit e181733b authored by A Olsen's avatar A Olsen Committed by Commit Bot

Refactor boilerplate in cloud_policy_generated.cc

cloud_policy_generated.cc has lots of boilerplate - the same code
repeated for each policy. This change moves most of the boilerplate
into a function, GetPolicyLevel. Because the file is generated, the
change must happen in the generator - generate_policy_source.py

The diff in the generated file is visible internally at
go/getpolicyleveldiff

Bug: 852366
Change-Id: I9d6f4d3c211b79ad271504c9f32e7b11b7399a32
Reviewed-on: https://chromium-review.googlesource.com/1097318
Commit-Queue: A Olsen <olsen@chromium.org>
Reviewed-by: default avatarMaksim Ivanov <emaxx@chromium.org>
Reviewed-by: default avatarRoman Sorokin <rsorokin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#566863}
parent 60298939
......@@ -1150,6 +1150,8 @@ namespace policy {
namespace em = enterprise_management;
namespace {
std::unique_ptr<base::Value> DecodeIntegerValue(
google::protobuf::int64 value) {
if (value < std::numeric_limits<int>::min() ||
......@@ -1183,6 +1185,32 @@ std::unique_ptr<base::Value> DecodeJson(const std::string& json) {
return root;
}
// Returns true and sets |level| to a PolicyLevel if the policy has been set
// at that level. Returns false if the policy is not set, or has been set at
// the level of PolicyOptions::UNSET.
template <class AnyPolicyProto>
bool GetPolicyLevel(const AnyPolicyProto& policy_proto, PolicyLevel* level) {
if (!policy_proto.has_value()) {
return false;
}
if (!policy_proto.has_policy_options()) {
*level = POLICY_LEVEL_MANDATORY; // Default level.
return true;
}
switch (policy_proto.policy_options().mode()) {
case em::PolicyOptions::MANDATORY:
*level = POLICY_LEVEL_MANDATORY;
return true;
case em::PolicyOptions::RECOMMENDED:
*level = POLICY_LEVEL_RECOMMENDED;
return true;
case em::PolicyOptions::UNSET:
return false;
}
}
} // namespace
void DecodePolicy(const em::CloudPolicySettings& policy,
base::WeakPtr<CloudExternalDataManager> external_data_manager,
PolicyMap* map,
......@@ -1223,40 +1251,22 @@ def _WriteCloudPolicyDecoderCode(f, policy):
f.write(' if (policy.has_%s()) {\n' % membername)
f.write(' const em::%s& policy_proto = policy.%s();\n' %
(proto_type, membername))
f.write(' if (policy_proto.has_value()) {\n')
f.write(' PolicyLevel level = POLICY_LEVEL_MANDATORY;\n'
' bool do_set = true;\n'
' if (policy_proto.has_policy_options()) {\n'
' do_set = false;\n'
' switch(policy_proto.policy_options().mode()) {\n'
' case em::PolicyOptions::MANDATORY:\n'
' do_set = true;\n'
' level = POLICY_LEVEL_MANDATORY;\n'
' break;\n'
' case em::PolicyOptions::RECOMMENDED:\n'
' do_set = true;\n'
' level = POLICY_LEVEL_RECOMMENDED;\n'
' break;\n'
' case em::PolicyOptions::UNSET:\n'
' break;\n'
' }\n'
' }\n'
' if (do_set) {\n')
f.write(' std::unique_ptr<base::Value> value(%s);\n' %
f.write(' PolicyLevel level;\n'
' if (GetPolicyLevel(policy_proto, &level)) {\n')
f.write(' std::unique_ptr<base::Value> value(%s);\n' %
(_CreateValue(policy.policy_type, 'policy_proto.value()')))
# TODO(bartfab): |value| == NULL indicates that the policy value could not be
# parsed successfully. Surface such errors in the UI.
f.write(' if (value) {\n')
f.write(' std::unique_ptr<ExternalDataFetcher>\n')
f.write(' external_data_fetcher(%s);\n' %
f.write(' if (value) {\n')
f.write(' std::unique_ptr<ExternalDataFetcher>\n')
f.write(' external_data_fetcher(%s);\n' %
_CreateExternalDataFetcher(policy.policy_type, policy.name))
f.write(' map->Set(key::k%s, \n' % policy.name)
f.write(' level, \n'
' scope, \n'
' POLICY_SOURCE_CLOUD, \n'
' std::move(value), \n'
' std::move(external_data_fetcher));\n'
' }\n'
f.write(' map->Set(key::k%s, \n' % policy.name)
f.write(' level, \n'
' scope, \n'
' POLICY_SOURCE_CLOUD, \n'
' std::move(value), \n'
' std::move(external_data_fetcher));\n'
' }\n'
' }\n'
' }\n')
......
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