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.
6 from xml.dom import minidom
7 from grit.format.policy_templates.writers import xml_formatted_writer
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
15 return ADMLWriter(['win'], config)
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.
23 # DOM root node of the generated ADML document.
26 # The string-table contains all ADML "string" elements.
27 _string_table_elem = None
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
33 def _AddString(self, id, text):
34 ''' Adds an ADML "string" element to _string_table_elem. The following
35 ADML snippet contains an example:
37 <string id="$(id)">$(text)</string>
40 id: ID of the newly created "string" element.
41 text: Value of the newly created "string" element.
43 id = id.replace('.', '_')
44 if id in self.strings_seen:
45 assert text == self.strings_seen[id]
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))
52 def WritePolicy(self, policy):
53 '''Generates the ADML elements for a Policy.
56 <string id="$(policy_group_name)">$(caption)</string>
57 <string id="$(policy_group_name)_Explain">$(description)</string>
62 <presentation id=$(policy_group_name)/>
66 policy: The Policy to generate ADML elements for.
68 policy_type = policy['type']
69 policy_name = policy['name']
70 if 'caption' in policy:
71 policy_caption = policy['caption']
73 policy_caption = policy_name
75 policy_description = policy['desc']
77 policy_description = policy_name
79 policy_label = policy['label']
81 policy_label = policy_name
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})
88 if policy_type == 'main':
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':
113 elif policy_type == 'external':
114 # This type can only be set through cloud policy.
117 raise Exception('Unknown policy type %s.' % policy_type)
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:
129 group: The Policy-Group to generate ADML elements for.
131 # Add ADML "string" elements to the string-table that are required by a
133 self._AddString(group['name'] + '_group', group['caption'])
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.
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],
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],
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],
156 def BeginTemplate(self):
157 dom_impl = minidom.getDOMImplementation('')
158 self._doc = dom_impl.createDocument(None, 'policyDefinitionResources',
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'
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,
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,
177 # Map of all strings seen.
178 self.strings_seen = {}
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
185 # TODO(markusheintz): Find a better formatting that works with gpedit.
186 return self._doc.toxml()