summaryrefslogtreecommitdiffstats
path: root/chromium/components/policy/tools/template_writers/writers/reg_writer.py
blob: 3fbd0a6b3583b7ddb6c3c0072ba982d2adc725dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/env python3
# Copyright (c) 2012 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import json

from writers import template_writer


def GetWriter(config):
  '''Factory method for creating RegWriter objects.
  See the constructor of TemplateWriter for description of
  arguments.
  '''
  return RegWriter(['win', 'win7'], config)


class RegWriter(template_writer.TemplateWriter):
  '''Class for generating policy example files in .reg format (for Windows).
  The generated files will define all the supported policies with example
  values  set for them. This class is used by PolicyTemplateGenerator to
  write .reg  files.
  '''

  NEWLINE = '\r\n'

  def _QuoteAndEscapeString(self, string):
    assert isinstance(string, str)
    return json.dumps(string)

  def _StartBlock(self, key, suffix, list):
    key = 'HKEY_LOCAL_MACHINE\\' + key
    if suffix:
      key = key + '\\' + suffix
    if key != self._last_key.get(id(list), None):
      list.append('')
      list.append('[%s]' % key)
      self._last_key[id(list)] = key

  def PreprocessPolicies(self, policy_list):
    return self.FlattenGroupsAndSortPolicies(policy_list,
                                             self.GetPolicySortingKey)

  def GetPolicySortingKey(self, policy):
    '''Extracts a sorting key from a policy. These keys can be used for
    list.sort() methods to sort policies.
    See TemplateWriter.SortPoliciesGroupsFirst for usage.
    '''
    is_list = policy['type'] in ('list', 'string-enum-list')
    # Lists come after regular policies.
    return (is_list, policy['name'])

  def _WritePolicy(self, policy, key, list):
    example_value = policy['example_value']

    if policy['type'] in ('list', 'string-enum-list'):
      self._StartBlock(key, policy['name'], list)
      i = 1
      for item in example_value:
        list.append('"%d"=%s' % (i, self._QuoteAndEscapeString(item)))
        i = i + 1
    else:
      self._StartBlock(key, None, list)
      if policy['type'] in ('string', 'string-enum'):
        example_value_str = self._QuoteAndEscapeString(example_value)
      elif policy['type'] in ('dict', 'external'):
        example_value_str = self._QuoteAndEscapeString(
            json.dumps(example_value, sort_keys=True))
      elif policy['type'] in ('main', 'int', 'int-enum'):
        example_value_str = 'dword:%08x' % int(example_value)
      else:
        raise Exception('unknown policy type %s:' % policy['type'])

      list.append('"%s"=%s' % (policy['name'], example_value_str))

  def WriteComment(self, comment):
    self._prefix.append('; ' + comment)

  def WritePolicy(self, policy):
    if self.CanBeMandatory(policy):
      self._WritePolicy(policy, self._winconfig['reg_mandatory_key_name'],
                        self._mandatory)

  def WriteRecommendedPolicy(self, policy):
    self._WritePolicy(policy, self._winconfig['reg_recommended_key_name'],
                      self._recommended)

  def BeginTemplate(self):
    pass

  def EndTemplate(self):
    pass

  def Init(self):
    self._mandatory = []
    self._recommended = []
    self._last_key = {}
    self._prefix = []
    self._winconfig = self.config['win_config']['win']

  def GetTemplateText(self):
    self._prefix.append('Windows Registry Editor Version 5.00')
    if self._GetChromiumVersionString() is not None:
      self.WriteComment(self.config['build'] + ' version: ' + \
          self._GetChromiumVersionString())
    all = self._prefix + self._mandatory + self._recommended
    return self.NEWLINE.join(all)