OLD | NEW |
| (Empty) |
1 #!/usr/bin/env python | |
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 | |
4 # found in the LICENSE file. | |
5 | |
6 from xml.dom import minidom | |
7 from grit.format.policy_templates.writers import xml_formatted_writer | |
8 | |
9 | |
10 def GetWriter(config): | |
11 '''Factory method for instanciating the ADMLWriter. Every Writer needs a | |
12 GetWriter method because the TemplateFormatter uses this method to | |
13 instantiate a Writer. | |
14 ''' | |
15 return ADMLWriter(['win'], config) | |
16 | |
17 | |
18 class ADMLWriter(xml_formatted_writer.XMLFormattedWriter): | |
19 ''' Class for generating an ADML policy template. It is used by the | |
20 PolicyTemplateGenerator to write the ADML file. | |
21 ''' | |
22 | |
23 # DOM root node of the generated ADML document. | |
24 _doc = None | |
25 | |
26 # The string-table contains all ADML "string" elements. | |
27 _string_table_elem = None | |
28 | |
29 # The presentation-table is the container for presentation elements, that | |
30 # describe the presentation of Policy-Groups and Policies. | |
31 _presentation_table_elem = None | |
32 | |
33 def _AddString(self, id, text): | |
34 ''' Adds an ADML "string" element to _string_table_elem. The following | |
35 ADML snippet contains an example: | |
36 | |
37 <string id="$(id)">$(text)</string> | |
38 | |
39 Args: | |
40 id: ID of the newly created "string" element. | |
41 text: Value of the newly created "string" element. | |
42 ''' | |
43 id = id.replace('.', '_') | |
44 if id in self.strings_seen: | |
45 assert text == self.strings_seen[id] | |
46 else: | |
47 self.strings_seen[id] = text | |
48 string_elem = self.AddElement( | |
49 self._string_table_elem, 'string', {'id': id}) | |
50 string_elem.appendChild(self._doc.createTextNode(text)) | |
51 | |
52 def WritePolicy(self, policy): | |
53 '''Generates the ADML elements for a Policy. | |
54 <stringTable> | |
55 ... | |
56 <string id="$(policy_group_name)">$(caption)</string> | |
57 <string id="$(policy_group_name)_Explain">$(description)</string> | |
58 </stringTable> | |
59 | |
60 <presentationTables> | |
61 ... | |
62 <presentation id=$(policy_group_name)/> | |
63 </presentationTables> | |
64 | |
65 Args: | |
66 policy: The Policy to generate ADML elements for. | |
67 ''' | |
68 policy_type = policy['type'] | |
69 policy_name = policy['name'] | |
70 if 'caption' in policy: | |
71 policy_caption = policy['caption'] | |
72 else: | |
73 policy_caption = policy_name | |
74 if 'desc' in policy: | |
75 policy_description = policy['desc'] | |
76 else: | |
77 policy_description = policy_name | |
78 if 'label' in policy: | |
79 policy_label = policy['label'] | |
80 else: | |
81 policy_label = policy_name | |
82 | |
83 self._AddString(policy_name, policy_caption) | |
84 self._AddString(policy_name + '_Explain', policy_description) | |
85 presentation_elem = self.AddElement( | |
86 self._presentation_table_elem, 'presentation', {'id': policy_name}) | |
87 | |
88 if policy_type == 'main': | |
89 pass | |
90 elif policy_type in ('string', 'dict'): | |
91 # 'dict' policies are configured as JSON-encoded strings on Windows. | |
92 textbox_elem = self.AddElement(presentation_elem, 'textBox', | |
93 {'refId': policy_name}) | |
94 label_elem = self.AddElement(textbox_elem, 'label') | |
95 label_elem.appendChild(self._doc.createTextNode(policy_label)) | |
96 elif policy_type == 'int': | |
97 textbox_elem = self.AddElement(presentation_elem, 'decimalTextBox', | |
98 {'refId': policy_name}) | |
99 textbox_elem.appendChild(self._doc.createTextNode(policy_label + ':')) | |
100 elif policy_type in ('int-enum', 'string-enum'): | |
101 for item in policy['items']: | |
102 self._AddString(item['name'], item['caption']) | |
103 dropdownlist_elem = self.AddElement(presentation_elem, 'dropdownList', | |
104 {'refId': policy_name}) | |
105 dropdownlist_elem.appendChild(self._doc.createTextNode(policy_label)) | |
106 elif policy_type in ('list', 'string-enum-list'): | |
107 self._AddString(policy_name + 'Desc', policy_caption) | |
108 listbox_elem = self.AddElement(presentation_elem, 'listBox', | |
109 {'refId': policy_name + 'Desc'}) | |
110 listbox_elem.appendChild(self._doc.createTextNode(policy_label)) | |
111 elif policy_type == 'group': | |
112 pass | |
113 elif policy_type == 'external': | |
114 # This type can only be set through cloud policy. | |
115 pass | |
116 else: | |
117 raise Exception('Unknown policy type %s.' % policy_type) | |
118 | |
119 def BeginPolicyGroup(self, group): | |
120 '''Generates ADML elements for a Policy-Group. For each Policy-Group two | |
121 ADML "string" elements are added to the string-table. One contains the | |
122 caption of the Policy-Group and the other a description. A Policy-Group also | |
123 requires an ADML "presentation" element that must be added to the | |
124 presentation-table. The "presentation" element is the container for the | |
125 elements that define the visual presentation of the Policy-Goup's Policies. | |
126 The following ADML snippet shows an example: | |
127 | |
128 Args: | |
129 group: The Policy-Group to generate ADML elements for. | |
130 ''' | |
131 # Add ADML "string" elements to the string-table that are required by a | |
132 # Policy-Group. | |
133 self._AddString(group['name'] + '_group', group['caption']) | |
134 | |
135 def _AddBaseStrings(self, build): | |
136 ''' Adds ADML "string" elements to the string-table that are referenced by | |
137 the ADMX file but not related to any specific Policy-Group or Policy. | |
138 ''' | |
139 self._AddString(self.config['win_supported_os'], | |
140 self.messages['win_supported_winxpsp2']['text']) | |
141 recommended_name = '%s - %s' % \ | |
142 (self.config['app_name'], self.messages['doc_recommended']['text']) | |
143 if build == 'chrome': | |
144 self._AddString(self.config['win_mandatory_category_path'][0], | |
145 'Google') | |
146 self._AddString(self.config['win_mandatory_category_path'][1], | |
147 self.config['app_name']) | |
148 self._AddString(self.config['win_recommended_category_path'][1], | |
149 recommended_name) | |
150 elif build == 'chromium': | |
151 self._AddString(self.config['win_mandatory_category_path'][0], | |
152 self.config['app_name']) | |
153 self._AddString(self.config['win_recommended_category_path'][0], | |
154 recommended_name) | |
155 | |
156 def BeginTemplate(self): | |
157 dom_impl = minidom.getDOMImplementation('') | |
158 self._doc = dom_impl.createDocument(None, 'policyDefinitionResources', | |
159 None) | |
160 if self._GetChromiumVersionString() is not None: | |
161 self.AddComment(self._doc.documentElement, self.config['build'] + \ | |
162 ' version: ' + self._GetChromiumVersionString()) | |
163 policy_definitions_resources_elem = self._doc.documentElement | |
164 policy_definitions_resources_elem.attributes['revision'] = '1.0' | |
165 policy_definitions_resources_elem.attributes['schemaVersion'] = '1.0' | |
166 | |
167 self.AddElement(policy_definitions_resources_elem, 'displayName') | |
168 self.AddElement(policy_definitions_resources_elem, 'description') | |
169 resources_elem = self.AddElement(policy_definitions_resources_elem, | |
170 'resources') | |
171 self._string_table_elem = self.AddElement(resources_elem, 'stringTable') | |
172 self._AddBaseStrings(self.config['build']) | |
173 self._presentation_table_elem = self.AddElement(resources_elem, | |
174 'presentationTable') | |
175 | |
176 def Init(self): | |
177 # Map of all strings seen. | |
178 self.strings_seen = {} | |
179 | |
180 def GetTemplateText(self): | |
181 # Using "toprettyxml()" confuses the Windows Group Policy Editor | |
182 # (gpedit.msc) because it interprets whitespace characters in text between | |
183 # the "string" tags. This prevents gpedit.msc from displaying the category | |
184 # names correctly. | |
185 # TODO(markusheintz): Find a better formatting that works with gpedit. | |
186 return self._doc.toxml() | |
OLD | NEW |