1# Copyright 2013 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import base64
6import hashlib
7import os
8import string
9import win32api
10import win32com.client
11from win32com.shell import shell, shellcon
12import win32security
13
14
15def _GetFileVersion(file_path):
16  """Returns the file version of the given file."""
17  return win32com.client.Dispatch('Scripting.FileSystemObject').GetFileVersion(
18      file_path)
19
20
21def _GetProductName(file_path):
22  """Returns the product name of the given file.
23
24  Args:
25    file_path: The absolute or relative path to the file.
26
27  Returns:
28    A string representing the product name of the file, or None if the product
29    name was not found.
30  """
31  language_and_codepage_pairs = win32api.GetFileVersionInfo(
32      file_path, '\\VarFileInfo\\Translation')
33  if not language_and_codepage_pairs:
34    return None
35  product_name_entry = ('\\StringFileInfo\\%04x%04x\\ProductName' %
36                        language_and_codepage_pairs[0])
37  return win32api.GetFileVersionInfo(file_path, product_name_entry)
38
39
40def _GetUserSpecificRegistrySuffix():
41  """Returns '.' plus the unpadded Base32 encoding of the MD5 of the user's SID.
42
43  The result must match the output from the method
44  UserSpecificRegistrySuffix::GetSuffix() in
45  chrome/installer/util/shell_util.cc. It will always be 27 characters long.
46  """
47  token_handle = win32security.OpenProcessToken(win32api.GetCurrentProcess(),
48                                                win32security.TOKEN_QUERY)
49  user_sid, _ = win32security.GetTokenInformation(token_handle,
50                                                  win32security.TokenUser)
51  user_sid_string = win32security.ConvertSidToStringSid(user_sid)
52  md5_digest = hashlib.md5(user_sid_string).digest()
53  return '.' + base64.b32encode(md5_digest).rstrip('=')
54
55
56class VariableExpander:
57  """Expands variables in strings."""
58
59  def __init__(self, mini_installer_path):
60    """Constructor.
61
62    The constructor initializes a variable dictionary that maps variables to
63    their values. These are the only acceptable variables:
64        * $PROGRAM_FILE: the unquoted path to the Program Files folder.
65        * $LOCAL_APPDATA: the unquoted path to the Local Application Data
66            folder.
67        * $MINI_INSTALLER: the unquoted path to the mini_installer.
68        * $MINI_INSTALLER_FILE_VERSION: the file version of the mini_installer.
69        * $CHROME_SHORT_NAME: 'Chrome' (or 'Chromium').
70        * $CHROME_LONG_NAME: 'Google Chrome' (or 'Chromium').
71        * $CHROME_DIR: the directory of Chrome (or Chromium) from the base
72            installation directory.
73        * $CHROME_UPDATE_REGISTRY_SUBKEY: the registry key, excluding the root
74            key, of Chrome for Google Update.
75        * $CHROME_HTML_PROG_ID: 'ChromeHTML' (or 'ChromiumHTM').
76        * $USER_SPECIFIC_REGISTRY_SUFFIX: the output from the function
77            _GetUserSpecificRegistrySuffix().
78        * $WINDOWS_VERSION: a 2-tuple representing the current Windows version.
79        * $VERSION_[XP/SERVER_2003/VISTA/WIN7/WIN8/WIN8_1]: a 2-tuple
80            representing the version of the corresponding OS.
81
82    Args:
83      mini_installer_path: The path to mini_installer.exe.
84    """
85    mini_installer_abspath = os.path.abspath(mini_installer_path)
86    mini_installer_file_version = _GetFileVersion(mini_installer_abspath)
87    mini_installer_product_name = _GetProductName(mini_installer_abspath)
88    program_files_path = shell.SHGetFolderPath(0, shellcon.CSIDL_PROGRAM_FILES,
89                                               None, 0)
90    local_appdata_path = shell.SHGetFolderPath(0, shellcon.CSIDL_LOCAL_APPDATA,
91                                               None, 0)
92    user_specific_registry_suffix = _GetUserSpecificRegistrySuffix()
93    windows_major_ver, windows_minor_ver, _, _, _ = win32api.GetVersionEx()
94    # This string will be converted to a tuple once injected in eval() through
95    # conditional checks. Tuples are compared lexicographically.
96    windows_version = '(%s, %s)' % (windows_major_ver, windows_minor_ver)
97    if mini_installer_product_name == 'Google Chrome Installer':
98      chrome_short_name = 'Chrome'
99      chrome_long_name = 'Google Chrome'
100      chrome_dir = 'Google\\Chrome'
101      chrome_update_registry_subkey = ('Software\\Google\\Update\\Clients\\'
102                                       '{8A69D345-D564-463c-AFF1-A69D9E530F96}')
103      chrome_html_prog_id = 'ChromeHTML'
104    elif mini_installer_product_name == 'Chromium Installer':
105      chrome_short_name = 'Chromium'
106      chrome_long_name = 'Chromium'
107      chrome_dir = 'Chromium'
108      chrome_update_registry_subkey = 'Software\\Chromium'
109      chrome_html_prog_id = 'ChromiumHTM'
110    else:
111      raise KeyError("Unknown mini_installer product name '%s'" %
112                     mini_installer_product_name)
113
114    self._variable_mapping = {
115        'PROGRAM_FILES': program_files_path,
116        'LOCAL_APPDATA': local_appdata_path,
117        'MINI_INSTALLER': mini_installer_abspath,
118        'MINI_INSTALLER_FILE_VERSION': mini_installer_file_version,
119        'CHROME_SHORT_NAME': chrome_short_name,
120        'CHROME_LONG_NAME': chrome_long_name,
121        'CHROME_DIR': chrome_dir,
122        'CHROME_UPDATE_REGISTRY_SUBKEY': chrome_update_registry_subkey,
123        'CHROME_HTML_PROG_ID': chrome_html_prog_id,
124        'USER_SPECIFIC_REGISTRY_SUFFIX': user_specific_registry_suffix,
125        'WINDOWS_VERSION': windows_version,
126        'VERSION_XP': '(5, 1)',
127        'VERSION_SERVER_2003': '(5, 2)',
128        'VERSION_VISTA': '(6, 0)',
129        'VERSION_WIN7': '(6, 1)',
130        'VERSION_WIN8': '(6, 2)',
131        'VERSION_WIN8_1': '(6, 3)',
132    }
133
134  def Expand(self, str):
135    """Expands variables in the given string.
136
137    This method resolves only variables defined in the constructor. It does not
138    resolve environment variables. Any dollar signs that are not part of
139    variables must be escaped with $$, otherwise a KeyError or a ValueError will
140    be raised.
141
142    Args:
143      str: A string.
144
145    Returns:
146      A new string created by replacing variables with their values.
147    """
148    return string.Template(str).substitute(self._variable_mapping)
149