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

Side by Side Diff: components/policy/tools/generate_policy_source.py

Issue 1940153002: Use std::unique_ptr to express ownership of base::Value in PolicyMap::Entry (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: another-fix Created 4 years, 7 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 unified diff | Download patch
OLDNEW
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be 3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file. 4 # found in the LICENSE file.
5 5
6 '''python %prog [options] platform chromium_os_flag template 6 '''python %prog [options] platform chromium_os_flag template
7 7
8 platform specifies which platform source is being generated for 8 platform specifies which platform source is being generated for
9 and can be one of (win, mac, linux) 9 and can be one of (win, mac, linux)
10 chromium_os_flag should be 1 if this is a Chromium OS build 10 chromium_os_flag should be 1 if this is a Chromium OS build
(...skipping 648 matching lines...) Expand 10 before | Expand all | Expand 10 after
659 self.properties_nodes = map(partial(self.ResolveID, 3), 659 self.properties_nodes = map(partial(self.ResolveID, 3),
660 self.properties_nodes) 660 self.properties_nodes)
661 661
662 def _WritePolicyConstantSource(policies, os, f, riskTags): 662 def _WritePolicyConstantSource(policies, os, f, riskTags):
663 f.write('#include "policy/policy_constants.h"\n' 663 f.write('#include "policy/policy_constants.h"\n'
664 '\n' 664 '\n'
665 '#include <algorithm>\n' 665 '#include <algorithm>\n'
666 '#include <climits>\n' 666 '#include <climits>\n'
667 '\n' 667 '\n'
668 '#include "base/logging.h"\n' 668 '#include "base/logging.h"\n'
669 '#include "base/memory/ptr_util.h"\n'
669 '#include "policy/risk_tag.h"\n' 670 '#include "policy/risk_tag.h"\n'
670 '#include "components/policy/core/common/policy_types.h"\n' 671 '#include "components/policy/core/common/policy_types.h"\n'
671 '#include "components/policy/core/common/schema_internal.h"\n' 672 '#include "components/policy/core/common/schema_internal.h"\n'
672 '\n' 673 '\n'
673 'namespace policy {\n' 674 'namespace policy {\n'
674 '\n') 675 '\n')
675 676
676 # Generate the Chrome schema. 677 # Generate the Chrome schema.
677 chrome_schema = { 678 chrome_schema = {
678 'type': 'object', 679 'type': 'object',
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
746 policy.enterprise_default 747 policy.enterprise_default
747 else: 748 else:
748 raise RuntimeError('Type %s of policy %s is not supported at ' 749 raise RuntimeError('Type %s of policy %s is not supported at '
749 'enterprise defaults' % (policy.policy_type, 750 'enterprise defaults' % (policy.policy_type,
750 policy.name)) 751 policy.name))
751 f.write(' if (!policy_map->Get(key::k%s)) {\n' 752 f.write(' if (!policy_map->Get(key::k%s)) {\n'
752 ' policy_map->Set(key::k%s,\n' 753 ' policy_map->Set(key::k%s,\n'
753 ' POLICY_LEVEL_MANDATORY,\n' 754 ' POLICY_LEVEL_MANDATORY,\n'
754 ' POLICY_SCOPE_USER,\n' 755 ' POLICY_SCOPE_USER,\n'
755 ' POLICY_SOURCE_ENTERPRISE_DEFAULT,\n' 756 ' POLICY_SOURCE_ENTERPRISE_DEFAULT,\n'
756 ' %s,\n' 757 ' base::WrapUnique(%s),\n'
757 ' NULL);\n' 758 ' NULL);\n'
758 ' }\n' % (policy.name, policy.name, creation_expression)) 759 ' }\n' % (policy.name, policy.name, creation_expression))
759 760
760 f.write('}\n' 761 f.write('}\n'
761 '#endif\n\n') 762 '#endif\n\n')
762 763
763 f.write('const PolicyDetails* GetChromePolicyDetails(' 764 f.write('const PolicyDetails* GetChromePolicyDetails('
764 'const std::string& policy) {\n' 765 'const std::string& policy) {\n'
765 ' // First index in kPropertyNodes of the Chrome policies.\n' 766 ' // First index in kPropertyNodes of the Chrome policies.\n'
766 ' static const int begin_index = %s;\n' 767 ' static const int begin_index = %s;\n'
(...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after
996 (policy.policy_protobuf_type, policy.name, 997 (policy.policy_protobuf_type, policy.name,
997 policy.id + RESERVED_IDS)) 998 policy.id + RESERVED_IDS))
998 f.write('}\n\n') 999 f.write('}\n\n')
999 1000
1000 1001
1001 #------------------ protobuf decoder -------------------------------# 1002 #------------------ protobuf decoder -------------------------------#
1002 1003
1003 CPP_HEAD = ''' 1004 CPP_HEAD = '''
1004 #include <limits> 1005 #include <limits>
1005 #include <memory> 1006 #include <memory>
1007 #include <utility>
1006 #include <string> 1008 #include <string>
1007 1009
1008 #include "base/callback.h" 1010 #include "base/callback.h"
1009 #include "base/json/json_reader.h" 1011 #include "base/json/json_reader.h"
1010 #include "base/logging.h" 1012 #include "base/logging.h"
1013 #include "base/memory/ptr_util.h"
1011 #include "base/memory/weak_ptr.h" 1014 #include "base/memory/weak_ptr.h"
1012 #include "base/values.h" 1015 #include "base/values.h"
1013 #include "components/policy/core/common/cloud/cloud_external_data_manager.h" 1016 #include "components/policy/core/common/cloud/cloud_external_data_manager.h"
1014 #include "components/policy/core/common/external_data_fetcher.h" 1017 #include "components/policy/core/common/external_data_fetcher.h"
1015 #include "components/policy/core/common/policy_map.h" 1018 #include "components/policy/core/common/policy_map.h"
1016 #include "components/policy/core/common/policy_types.h" 1019 #include "components/policy/core/common/policy_types.h"
1017 #include "policy/policy_constants.h" 1020 #include "policy/policy_constants.h"
1018 #include "policy/proto/cloud_policy.pb.h" 1021 #include "policy/proto/cloud_policy.pb.h"
1019 1022
1020 using google::protobuf::RepeatedPtrField; 1023 using google::protobuf::RepeatedPtrField;
1021 1024
1022 namespace policy { 1025 namespace policy {
1023 1026
1024 namespace em = enterprise_management; 1027 namespace em = enterprise_management;
1025 1028
1026 base::Value* DecodeIntegerValue(google::protobuf::int64 value) { 1029 std::unique_ptr<base::Value> DecodeIntegerValue(
1030 google::protobuf::int64 value) {
1027 if (value < std::numeric_limits<int>::min() || 1031 if (value < std::numeric_limits<int>::min() ||
1028 value > std::numeric_limits<int>::max()) { 1032 value > std::numeric_limits<int>::max()) {
1029 LOG(WARNING) << "Integer value " << value 1033 LOG(WARNING) << "Integer value " << value
1030 << " out of numeric limits, ignoring."; 1034 << " out of numeric limits, ignoring.";
1031 return nullptr; 1035 return nullptr;
1032 } 1036 }
1033 1037
1034 return new base::FundamentalValue(static_cast<int>(value)); 1038 return base::WrapUnique(
1039 new base::FundamentalValue(static_cast<int>(value)));
1035 } 1040 }
1036 1041
1037 base::ListValue* DecodeStringList(const em::StringList& string_list) { 1042 std::unique_ptr<base::ListValue> DecodeStringList(
1038 base::ListValue* list_value = new base::ListValue; 1043 const em::StringList& string_list) {
1039 RepeatedPtrField<std::string>::const_iterator entry; 1044 std::unique_ptr<base::ListValue> list_value(new base::ListValue);
1040 for (entry = string_list.entries().begin(); 1045 for (const auto& entry : string_list.entries())
1041 entry != string_list.entries().end(); ++entry) { 1046 list_value->AppendString(entry);
1042 list_value->AppendString(*entry);
1043 }
1044 return list_value; 1047 return list_value;
1045 } 1048 }
1046 1049
1047 base::Value* DecodeJson(const std::string& json) { 1050 std::unique_ptr<base::Value> DecodeJson(const std::string& json) {
1048 std::unique_ptr<base::Value> root = 1051 std::unique_ptr<base::Value> root =
1049 base::JSONReader::Read(json, base::JSON_ALLOW_TRAILING_COMMAS); 1052 base::JSONReader::Read(json, base::JSON_ALLOW_TRAILING_COMMAS);
1050 1053
1051 if (!root) 1054 if (!root)
1052 LOG(WARNING) << "Invalid JSON string, ignoring: " << json; 1055 LOG(WARNING) << "Invalid JSON string, ignoring: " << json;
1053 1056
1054 // Accept any Value type that parsed as JSON, and leave it to the handler to 1057 // Accept any Value type that parsed as JSON, and leave it to the handler to
1055 // convert and check the concrete type. 1058 // convert and check the concrete type.
1056 return root.release(); 1059 return root;
1057 } 1060 }
1058 1061
1059 void DecodePolicy(const em::CloudPolicySettings& policy, 1062 void DecodePolicy(const em::CloudPolicySettings& policy,
1060 base::WeakPtr<CloudExternalDataManager> external_data_manager, 1063 base::WeakPtr<CloudExternalDataManager> external_data_manager,
1061 PolicyMap* map) { 1064 PolicyMap* map) {
1062 ''' 1065 '''
1063 1066
1064 1067
1065 CPP_FOOT = '''} 1068 CPP_FOOT = '''}
1066 1069
(...skipping 12 matching lines...) Expand all
1079 return 'DecodeStringList(%s)' % arg 1082 return 'DecodeStringList(%s)' % arg
1080 elif type == 'TYPE_DICTIONARY' or type == 'TYPE_EXTERNAL': 1083 elif type == 'TYPE_DICTIONARY' or type == 'TYPE_EXTERNAL':
1081 return 'DecodeJson(%s)' % arg 1084 return 'DecodeJson(%s)' % arg
1082 else: 1085 else:
1083 raise NotImplementedError('Unknown type %s' % type) 1086 raise NotImplementedError('Unknown type %s' % type)
1084 1087
1085 1088
1086 def _CreateExternalDataFetcher(type, name): 1089 def _CreateExternalDataFetcher(type, name):
1087 if type == 'TYPE_EXTERNAL': 1090 if type == 'TYPE_EXTERNAL':
1088 return 'new ExternalDataFetcher(external_data_manager, key::k%s)' % name 1091 return 'new ExternalDataFetcher(external_data_manager, key::k%s)' % name
1089 return 'NULL' 1092 return 'nullptr'
1090 1093
1091 1094
1092 def _WritePolicyCode(f, policy): 1095 def _WritePolicyCode(f, policy):
1093 membername = policy.name.lower() 1096 membername = policy.name.lower()
1094 proto_type = '%sPolicyProto' % policy.policy_protobuf_type 1097 proto_type = '%sPolicyProto' % policy.policy_protobuf_type
1095 f.write(' if (policy.has_%s()) {\n' % membername) 1098 f.write(' if (policy.has_%s()) {\n' % membername)
1096 f.write(' const em::%s& policy_proto = policy.%s();\n' % 1099 f.write(' const em::%s& policy_proto = policy.%s();\n' %
1097 (proto_type, membername)) 1100 (proto_type, membername))
1098 f.write(' if (policy_proto.has_value()) {\n') 1101 f.write(' if (policy_proto.has_value()) {\n')
1099 f.write(' PolicyLevel level = POLICY_LEVEL_MANDATORY;\n' 1102 f.write(' PolicyLevel level = POLICY_LEVEL_MANDATORY;\n'
1100 ' bool do_set = true;\n' 1103 ' bool do_set = true;\n'
1101 ' if (policy_proto.has_policy_options()) {\n' 1104 ' if (policy_proto.has_policy_options()) {\n'
1102 ' do_set = false;\n' 1105 ' do_set = false;\n'
1103 ' switch(policy_proto.policy_options().mode()) {\n' 1106 ' switch(policy_proto.policy_options().mode()) {\n'
1104 ' case em::PolicyOptions::MANDATORY:\n' 1107 ' case em::PolicyOptions::MANDATORY:\n'
1105 ' do_set = true;\n' 1108 ' do_set = true;\n'
1106 ' level = POLICY_LEVEL_MANDATORY;\n' 1109 ' level = POLICY_LEVEL_MANDATORY;\n'
1107 ' break;\n' 1110 ' break;\n'
1108 ' case em::PolicyOptions::RECOMMENDED:\n' 1111 ' case em::PolicyOptions::RECOMMENDED:\n'
1109 ' do_set = true;\n' 1112 ' do_set = true;\n'
1110 ' level = POLICY_LEVEL_RECOMMENDED;\n' 1113 ' level = POLICY_LEVEL_RECOMMENDED;\n'
1111 ' break;\n' 1114 ' break;\n'
1112 ' case em::PolicyOptions::UNSET:\n' 1115 ' case em::PolicyOptions::UNSET:\n'
1113 ' break;\n' 1116 ' break;\n'
1114 ' }\n' 1117 ' }\n'
1115 ' }\n' 1118 ' }\n'
1116 ' if (do_set) {\n') 1119 ' if (do_set) {\n')
1117 f.write(' base::Value* value = %s;\n' % 1120 f.write(' std::unique_ptr<base::Value> value(%s);\n' %
1118 (_CreateValue(policy.policy_type, 'policy_proto.value()'))) 1121 (_CreateValue(policy.policy_type, 'policy_proto.value()')))
1119 # TODO(bartfab): |value| == NULL indicates that the policy value could not be 1122 # TODO(bartfab): |value| == NULL indicates that the policy value could not be
1120 # parsed successfully. Surface such errors in the UI. 1123 # parsed successfully. Surface such errors in the UI.
1121 f.write(' if (value) {\n') 1124 f.write(' if (value) {\n')
1122 f.write(' ExternalDataFetcher* external_data_fetcher = %s;\n' % 1125 f.write(' std::unique_ptr<ExternalDataFetcher>\n')
1126 f.write(' external_data_fetcher(%s);\n' %
1123 _CreateExternalDataFetcher(policy.policy_type, policy.name)) 1127 _CreateExternalDataFetcher(policy.policy_type, policy.name))
1124 f.write(' map->Set(key::k%s, \n' % policy.name) 1128 f.write(' map->Set(key::k%s, \n' % policy.name)
1125 f.write(' level, \n' 1129 f.write(' level, \n'
1126 ' POLICY_SCOPE_USER, \n' 1130 ' POLICY_SCOPE_USER, \n'
1127 ' POLICY_SOURCE_CLOUD, \n' 1131 ' POLICY_SOURCE_CLOUD, \n'
1128 ' value, \n' 1132 ' std::move(value), \n'
1129 ' external_data_fetcher);\n' 1133 ' std::move(external_data_fetcher));\n'
1130 ' }\n' 1134 ' }\n'
1131 ' }\n' 1135 ' }\n'
1132 ' }\n' 1136 ' }\n'
1133 ' }\n') 1137 ' }\n')
1134 1138
1135 1139
1136 def _WriteCloudPolicyDecoder(policies, os, f, riskTags): 1140 def _WriteCloudPolicyDecoder(policies, os, f, riskTags):
1137 f.write(CPP_HEAD) 1141 f.write(CPP_HEAD)
1138 for policy in policies: 1142 for policy in policies:
1139 if policy.is_supported and not policy.is_device_only: 1143 if policy.is_supported and not policy.is_device_only:
(...skipping 26 matching lines...) Expand all
1166 # _WriteAppRestrictions body 1170 # _WriteAppRestrictions body
1167 f.write('<restrictions xmlns:android="' 1171 f.write('<restrictions xmlns:android="'
1168 'http://schemas.android.com/apk/res/android">\n\n') 1172 'http://schemas.android.com/apk/res/android">\n\n')
1169 for policy in policies: 1173 for policy in policies:
1170 if policy.is_supported and policy.restriction_type != 'invalid': 1174 if policy.is_supported and policy.restriction_type != 'invalid':
1171 WriteAppRestriction(policy) 1175 WriteAppRestriction(policy)
1172 f.write('</restrictions>') 1176 f.write('</restrictions>')
1173 1177
1174 if __name__ == '__main__': 1178 if __name__ == '__main__':
1175 sys.exit(main()) 1179 sys.exit(main())
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698