Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / installer / util / install_util.h
blob1b256a7512071c54af5f854d7cc4f034d12ccf29
1 // Copyright (c) 2012 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 //
5 // This file declares utility functions for the installer. The original reason
6 // for putting these functions in installer\util library is so that we can
7 // separate out the critical logic and write unit tests for it.
9 #ifndef CHROME_INSTALLER_UTIL_INSTALL_UTIL_H_
10 #define CHROME_INSTALLER_UTIL_INSTALL_UTIL_H_
12 #include <windows.h>
13 #include <tchar.h>
15 #include "base/basictypes.h"
16 #include "base/command_line.h"
17 #include "base/files/file.h"
18 #include "base/files/file_path.h"
19 #include "base/strings/string16.h"
20 #include "base/win/scoped_handle.h"
21 #include "chrome/installer/util/browser_distribution.h"
22 #include "chrome/installer/util/util_constants.h"
24 class WorkItemList;
26 namespace base {
27 class Version;
30 // This is a utility class that provides common installation related
31 // utility methods that can be used by installer and also unit tested
32 // independently.
33 class InstallUtil {
34 public:
35 // Returns true if properties that enable Metro mode on Win8+ should be
36 // installed.
37 static bool ShouldInstallMetroProperties();
39 // Get the path to this distribution's Active Setup registry entries.
40 // e.g. Software\Microsoft\Active Setup\Installed Components\<dist_guid>
41 static base::string16 GetActiveSetupPath(BrowserDistribution* dist);
43 // Attempts to trigger the command that would be run by Active Setup for a
44 // system-level Chrome. For use only when system-level Chrome is installed.
45 static void TriggerActiveSetupCommand();
47 // Launches given exe as admin on Vista.
48 static bool ExecuteExeAsAdmin(const base::CommandLine& cmd, DWORD* exit_code);
50 // Reads the uninstall command for Chromium from registry and returns it.
51 // If system_install is true the command is read from HKLM, otherwise
52 // from HKCU.
53 static base::CommandLine GetChromeUninstallCmd(
54 bool system_install,
55 BrowserDistribution::Type distribution_type);
57 // Find the version of Chrome installed on the system by checking the
58 // Google Update registry key. Fills |version| with the version or a
59 // default-constructed Version if no version is found.
60 // system_install: if true, looks for version number under the HKLM root,
61 // otherwise looks under the HKCU.
62 static void GetChromeVersion(BrowserDistribution* dist,
63 bool system_install,
64 base::Version* version);
66 // Find the last critical update (version) of Chrome. Fills |version| with the
67 // version or a default-constructed Version if no version is found. A critical
68 // update is a specially flagged version (by Google Update) that contains an
69 // important security fix.
70 // system_install: if true, looks for version number under the HKLM root,
71 // otherwise looks under the HKCU.
72 static void GetCriticalUpdateVersion(BrowserDistribution* dist,
73 bool system_install,
74 base::Version* version);
76 // This function checks if the current OS is supported for Chromium.
77 static bool IsOSSupported();
79 // Adds work items to |install_list|, which should be a
80 // NoRollbackWorkItemList, to set installer error information in the registry
81 // for consumption by Google Update. |state_key| must be the full path to an
82 // app's ClientState key. See InstallerState::WriteInstallerResult for more
83 // details.
84 static void AddInstallerResultItems(bool system_install,
85 const base::string16& state_key,
86 installer::InstallStatus status,
87 int string_resource_id,
88 const base::string16* const launch_cmd,
89 WorkItemList* install_list);
91 // Update the installer stage reported by Google Update. |state_key_path|
92 // should be obtained via the state_key method of an InstallerState instance
93 // created before the machine state is modified by the installer.
94 static void UpdateInstallerStage(bool system_install,
95 const base::string16& state_key_path,
96 installer::InstallerStage stage);
98 // Returns true if this installation path is per user, otherwise returns false
99 // (per machine install, meaning: the exe_path contains the path to Program
100 // Files).
101 static bool IsPerUserInstall(const base::FilePath& exe_path);
103 // Returns true if the installation represented by the pair of |dist| and
104 // |system_level| is a multi install.
105 static bool IsMultiInstall(BrowserDistribution* dist, bool system_install);
107 // Returns true if this is running setup process for Chrome SxS (as
108 // indicated by the presence of --chrome-sxs on the command line) or if this
109 // is running Chrome process from the Chrome SxS installation (as indicated
110 // by either --chrome-sxs or the executable path).
111 static bool IsChromeSxSProcess();
113 // Returns true if the sentinel file exists (or the path cannot be obtained).
114 static bool IsFirstRunSentinelPresent();
116 // Populates |path| with EULA sentinel file path. Returns false on error.
117 static bool GetEULASentinelFilePath(base::FilePath* path);
119 // Deletes the registry key at path key_path under the key given by root_key.
120 static bool DeleteRegistryKey(HKEY root_key,
121 const base::string16& key_path,
122 REGSAM wow64_access);
124 // Deletes the registry value named value_name at path key_path under the key
125 // given by reg_root.
126 static bool DeleteRegistryValue(HKEY reg_root, const base::string16& key_path,
127 REGSAM wow64_access,
128 const base::string16& value_name);
130 // An interface to a predicate function for use by DeleteRegistryKeyIf and
131 // DeleteRegistryValueIf.
132 class RegistryValuePredicate {
133 public:
134 virtual ~RegistryValuePredicate() { }
135 virtual bool Evaluate(const base::string16& value) const = 0;
138 // The result of a conditional delete operation (i.e., DeleteFOOIf).
139 enum ConditionalDeleteResult {
140 NOT_FOUND, // The condition was not satisfied.
141 DELETED, // The condition was satisfied and the delete succeeded.
142 DELETE_FAILED // The condition was satisfied but the delete failed.
145 // Deletes the key |key_to_delete_path| under |root_key| iff the value
146 // |value_name| in the key |key_to_test_path| under |root_key| satisfies
147 // |predicate|. |value_name| may be either NULL or an empty string to test
148 // the key's default value.
149 static ConditionalDeleteResult DeleteRegistryKeyIf(
150 HKEY root_key,
151 const base::string16& key_to_delete_path,
152 const base::string16& key_to_test_path,
153 REGSAM wow64_access,
154 const wchar_t* value_name,
155 const RegistryValuePredicate& predicate);
157 // Deletes the value |value_name| in the key |key_path| under |root_key| iff
158 // its current value satisfies |predicate|. |value_name| may be either NULL
159 // or an empty string to test/delete the key's default value.
160 static ConditionalDeleteResult DeleteRegistryValueIf(
161 HKEY root_key,
162 const wchar_t* key_path,
163 REGSAM wow64_access,
164 const wchar_t* value_name,
165 const RegistryValuePredicate& predicate);
167 // A predicate that performs a case-sensitive string comparison.
168 class ValueEquals : public RegistryValuePredicate {
169 public:
170 explicit ValueEquals(const base::string16& value_to_match)
171 : value_to_match_(value_to_match) { }
172 bool Evaluate(const base::string16& value) const override;
173 protected:
174 base::string16 value_to_match_;
175 private:
176 DISALLOW_COPY_AND_ASSIGN(ValueEquals);
179 // Returns zero on install success, or an InstallStatus value otherwise.
180 static int GetInstallReturnCode(installer::InstallStatus install_status);
182 // Composes |program| and |arguments| into |command_line|.
183 static void ComposeCommandLine(const base::string16& program,
184 const base::string16& arguments,
185 base::CommandLine* command_line);
187 // Returns a string in the form YYYYMMDD of the current date.
188 static base::string16 GetCurrentDate();
190 // A predicate that compares the program portion of a command line with a
191 // given file path. First, the file paths are compared directly. If they do
192 // not match, the filesystem is consulted to determine if the paths reference
193 // the same file.
194 class ProgramCompare : public RegistryValuePredicate {
195 public:
196 explicit ProgramCompare(const base::FilePath& path_to_match);
197 ~ProgramCompare() override;
198 bool Evaluate(const base::string16& value) const override;
199 bool EvaluatePath(const base::FilePath& path) const;
201 protected:
202 static bool OpenForInfo(const base::FilePath& path, base::File* file);
203 static bool GetInfo(const base::File& file,
204 BY_HANDLE_FILE_INFORMATION* info);
206 base::FilePath path_to_match_;
207 base::File file_;
208 BY_HANDLE_FILE_INFORMATION file_info_;
210 private:
211 DISALLOW_COPY_AND_ASSIGN(ProgramCompare);
212 }; // class ProgramCompare
214 private:
215 DISALLOW_COPY_AND_ASSIGN(InstallUtil);
219 #endif // CHROME_INSTALLER_UTIL_INSTALL_UTIL_H_