OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/installer/launcher_support/chrome_launcher_support.h" | 5 #include "chrome/installer/launcher_support/chrome_launcher_support.h" |
6 | 6 |
7 #include <windows.h> | 7 #include <windows.h> |
| 8 #include <tchar.h> |
8 | 9 |
| 10 #include "base/command_line.h" |
9 #include "base/file_util.h" | 11 #include "base/file_util.h" |
10 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
| 13 #include "base/logging.h" |
| 14 #include "base/process/launch.h" |
11 #include "base/strings/string16.h" | 15 #include "base/strings/string16.h" |
12 #include "base/win/registry.h" | 16 #include "base/win/registry.h" |
13 | 17 |
| 18 #ifndef OFFICIAL_BUILD |
| 19 #include "base/path_service.h" |
| 20 #endif |
| 21 |
14 namespace chrome_launcher_support { | 22 namespace chrome_launcher_support { |
15 | 23 |
16 namespace { | 24 namespace { |
17 | 25 |
18 // TODO(huangs) Refactor the constants: http://crbug.com/148538 | 26 // TODO(huangs) Refactor the constants: http://crbug.com/148538 |
19 const wchar_t kGoogleRegClientStateKey[] = | 27 const wchar_t kGoogleRegClientStateKey[] = |
20 L"Software\\Google\\Update\\ClientState"; | 28 L"Software\\Google\\Update\\ClientState"; |
| 29 const wchar_t kGoogleRegClientsKey[] = L"Software\\Google\\Update\\Clients"; |
| 30 const wchar_t kRegVersionField[] = L"pv"; |
21 | 31 |
22 // Copied from chrome_appid.cc. | 32 // Copied from chrome_appid.cc. |
23 const wchar_t kBinariesAppGuid[] = L"{4DC8B4CA-1BDA-483e-B5FA-D3C12E15B62D}"; | 33 const wchar_t kBinariesAppGuid[] = L"{4DC8B4CA-1BDA-483e-B5FA-D3C12E15B62D}"; |
24 | 34 |
25 // Copied from google_chrome_distribution.cc. | 35 // Copied from google_chrome_distribution.cc. |
26 const wchar_t kBrowserAppGuid[] = L"{8A69D345-D564-463c-AFF1-A69D9E530F96}"; | 36 const wchar_t kBrowserAppGuid[] = L"{8A69D345-D564-463c-AFF1-A69D9E530F96}"; |
27 | 37 |
| 38 // Copied frome google_chrome_sxs_distribution.cc. |
| 39 const wchar_t kSxSBrowserAppGuid[] = L"{4ea16ac7-fd5a-47c3-875b-dbf4a2008c20}"; |
| 40 |
28 // Copied from util_constants.cc. | 41 // Copied from util_constants.cc. |
| 42 const wchar_t kChromeAppHostExe[] = L"app_host.exe"; |
| 43 const char kChromeAppLauncher[] = "app-launcher"; |
29 const wchar_t kChromeExe[] = L"chrome.exe"; | 44 const wchar_t kChromeExe[] = L"chrome.exe"; |
| 45 const wchar_t kUninstallArgumentsField[] = L"UninstallArguments"; |
30 const wchar_t kUninstallStringField[] = L"UninstallString"; | 46 const wchar_t kUninstallStringField[] = L"UninstallString"; |
31 | 47 |
32 // Reads a string value from the specified product's "ClientState" registry key. | 48 // Reads a string value from the specified product's "ClientState" registry key. |
33 // Returns true iff the value is present and successfully read. | 49 // Returns true iff the value is present and successfully read. |
34 bool GetClientStateValue(InstallationLevel level, | 50 bool GetClientStateValue(InstallationLevel level, |
35 const wchar_t* app_guid, | 51 const wchar_t* app_guid, |
36 const wchar_t* value_name, | 52 const wchar_t* value_name, |
37 base::string16* value) { | 53 base::string16* value) { |
38 HKEY root_key = (level == USER_LEVEL_INSTALLATION) ? | 54 HKEY root_key = (level == USER_LEVEL_INSTALLATION) ? |
39 HKEY_CURRENT_USER : HKEY_LOCAL_MACHINE; | 55 HKEY_CURRENT_USER : HKEY_LOCAL_MACHINE; |
40 base::string16 subkey(kGoogleRegClientStateKey); | 56 base::string16 subkey(kGoogleRegClientStateKey); |
41 subkey.append(1, L'\\').append(app_guid); | 57 subkey.append(1, L'\\').append(app_guid); |
42 base::win::RegKey reg_key; | 58 base::win::RegKey reg_key; |
43 // Google Update always uses 32bit hive. | 59 // Google Update always uses 32bit hive. |
44 if (reg_key.Open(root_key, subkey.c_str(), | 60 if (reg_key.Open(root_key, subkey.c_str(), |
45 KEY_QUERY_VALUE | KEY_WOW64_32KEY) == ERROR_SUCCESS) { | 61 KEY_QUERY_VALUE | KEY_WOW64_32KEY) == ERROR_SUCCESS) { |
46 if (reg_key.ReadValue(value_name, value) == ERROR_SUCCESS) { | 62 if (reg_key.ReadValue(value_name, value) == ERROR_SUCCESS) { |
47 return true; | 63 return true; |
48 } | 64 } |
49 } | 65 } |
50 return false; | 66 return false; |
51 } | 67 } |
52 | 68 |
| 69 // Determines whether the specified product has a key in "Clients". This |
| 70 // indicates whether the product is installed at the given level. |
| 71 bool IsProductInstalled(InstallationLevel level, const wchar_t* app_guid) { |
| 72 HKEY root_key = (level == USER_LEVEL_INSTALLATION) ? |
| 73 HKEY_CURRENT_USER : HKEY_LOCAL_MACHINE; |
| 74 base::string16 subkey(kGoogleRegClientsKey); |
| 75 subkey.append(1, L'\\').append(app_guid); |
| 76 base::win::RegKey reg_key; |
| 77 // Google Update always uses 32bit hive. |
| 78 return reg_key.Open(root_key, subkey.c_str(), |
| 79 KEY_QUERY_VALUE | KEY_WOW64_32KEY) == ERROR_SUCCESS && |
| 80 reg_key.HasValue(kRegVersionField); |
| 81 } |
| 82 |
| 83 bool IsAppLauncherEnabledAtLevel(InstallationLevel level) { |
| 84 base::string16 uninstall_arguments; |
| 85 if (GetClientStateValue(level, |
| 86 kAppLauncherGuid, |
| 87 kUninstallArgumentsField, |
| 88 &uninstall_arguments)) { |
| 89 return CommandLine::FromString(L"dummy.exe " + uninstall_arguments) |
| 90 .HasSwitch(kChromeAppLauncher) && |
| 91 !GetAppHostPathForInstallationLevel(level).empty(); |
| 92 } |
| 93 return false; |
| 94 } |
| 95 |
53 // Reads the path to setup.exe from the value "UninstallString" within the | 96 // Reads the path to setup.exe from the value "UninstallString" within the |
54 // specified product's "ClientState" registry key. Returns an empty FilePath if | 97 // specified product's "ClientState" registry key. Returns an empty FilePath if |
55 // an error occurs or the product is not installed at the specified level. | 98 // an error occurs or the product is not installed at the specified level. |
56 base::FilePath GetSetupExeFromRegistry(InstallationLevel level, | 99 base::FilePath GetSetupExeFromRegistry(InstallationLevel level, |
57 const wchar_t* app_guid) { | 100 const wchar_t* app_guid) { |
58 base::string16 uninstall; | 101 base::string16 uninstall; |
59 if (GetClientStateValue(level, app_guid, kUninstallStringField, &uninstall)) { | 102 if (GetClientStateValue(level, app_guid, kUninstallStringField, &uninstall)) { |
60 base::FilePath setup_exe_path(uninstall); | 103 base::FilePath setup_exe_path(uninstall); |
61 if (base::PathExists(setup_exe_path)) | 104 if (base::PathExists(setup_exe_path)) |
62 return setup_exe_path; | 105 return setup_exe_path; |
63 } | 106 } |
64 return base::FilePath(); | 107 return base::FilePath(); |
65 } | 108 } |
66 | 109 |
67 // Returns the path to an existing setup.exe at the specified level, if it can | 110 // Returns the path to an installed |exe_file| (e.g. chrome.exe, app_host.exe) |
68 // be found via Omaha client state. | 111 // at the specified level, given |setup_exe_path| from Omaha client state. |
69 base::FilePath GetSetupExeForInstallationLevel(InstallationLevel level) { | 112 // Returns empty base::FilePath if none found, or if |setup_exe_path| is empty. |
70 // Look in the registry for Chrome Binaries first. | |
71 base::FilePath setup_exe_path( | |
72 GetSetupExeFromRegistry(level, kBinariesAppGuid)); | |
73 // If the above fails, look in the registry for Chrome next. | |
74 if (setup_exe_path.empty()) | |
75 setup_exe_path = GetSetupExeFromRegistry(level, kBrowserAppGuid); | |
76 // If we fail again, then setup_exe_path would be empty. | |
77 return setup_exe_path; | |
78 } | |
79 | |
80 // Returns the path to an installed |exe_file| (e.g. chrome.exe) at the | |
81 // specified level, given |setup_exe_path| from Omaha client state. Returns | |
82 // empty base::FilePath if none found, or if |setup_exe_path| is empty. | |
83 base::FilePath FindExeRelativeToSetupExe(const base::FilePath setup_exe_path, | 113 base::FilePath FindExeRelativeToSetupExe(const base::FilePath setup_exe_path, |
84 const wchar_t* exe_file) { | 114 const wchar_t* exe_file) { |
85 if (!setup_exe_path.empty()) { | 115 if (!setup_exe_path.empty()) { |
86 // The uninstall path contains the path to setup.exe, which is two levels | 116 // The uninstall path contains the path to setup.exe, which is two levels |
87 // down from |exe_file|. Move up two levels (plus one to drop the file | 117 // down from |exe_file|. Move up two levels (plus one to drop the file |
88 // name) and look for chrome.exe from there. | 118 // name) and look for chrome.exe from there. |
89 base::FilePath exe_path( | 119 base::FilePath exe_path( |
90 setup_exe_path.DirName().DirName().DirName().Append(exe_file)); | 120 setup_exe_path.DirName().DirName().DirName().Append(exe_file)); |
91 if (base::PathExists(exe_path)) | 121 if (base::PathExists(exe_path)) |
92 return exe_path; | 122 return exe_path; |
93 // By way of mild future proofing, look up one to see if there's a | 123 // By way of mild future proofing, look up one to see if there's a |
94 // |exe_file| in the version directory | 124 // |exe_file| in the version directory |
95 exe_path = setup_exe_path.DirName().DirName().Append(exe_file); | 125 exe_path = setup_exe_path.DirName().DirName().Append(exe_file); |
96 if (base::PathExists(exe_path)) | 126 if (base::PathExists(exe_path)) |
97 return exe_path; | 127 return exe_path; |
98 } | 128 } |
99 return base::FilePath(); | 129 return base::FilePath(); |
100 } | 130 } |
101 | 131 |
102 } // namespace | 132 } // namespace |
103 | 133 |
| 134 const wchar_t kAppLauncherGuid[] = L"{FDA71E6F-AC4C-4a00-8B70-9958A68906BF}"; |
| 135 |
| 136 void UninstallLegacyAppLauncher(InstallationLevel level) { |
| 137 base::FilePath setup_exe(GetSetupExeFromRegistry(level, kAppLauncherGuid)); |
| 138 if (setup_exe.empty()) |
| 139 return; |
| 140 base::string16 uninstall_arguments; |
| 141 if (GetClientStateValue(level, |
| 142 kAppLauncherGuid, |
| 143 kUninstallArgumentsField, |
| 144 &uninstall_arguments)) { |
| 145 CommandLine uninstall_cmd = CommandLine::FromString( |
| 146 L"\"" + setup_exe.value() + L"\" " + uninstall_arguments); |
| 147 |
| 148 VLOG(1) << "Uninstalling legacy app launcher with command line: " |
| 149 << uninstall_cmd.GetCommandLineString(); |
| 150 base::LaunchProcess(uninstall_cmd, base::LaunchOptions(), NULL); |
| 151 } |
| 152 } |
| 153 |
| 154 base::FilePath GetSetupExeForInstallationLevel(InstallationLevel level) { |
| 155 // Look in the registry for Chrome Binaries first. |
| 156 base::FilePath setup_exe_path( |
| 157 GetSetupExeFromRegistry(level, kBinariesAppGuid)); |
| 158 // If the above fails, look in the registry for Chrome next. |
| 159 if (setup_exe_path.empty()) |
| 160 setup_exe_path = GetSetupExeFromRegistry(level, kBrowserAppGuid); |
| 161 // If we fail again, then setup_exe_path would be empty. |
| 162 return setup_exe_path; |
| 163 } |
| 164 |
104 base::FilePath GetChromePathForInstallationLevel(InstallationLevel level) { | 165 base::FilePath GetChromePathForInstallationLevel(InstallationLevel level) { |
105 return FindExeRelativeToSetupExe( | 166 return FindExeRelativeToSetupExe( |
106 GetSetupExeForInstallationLevel(level), kChromeExe); | 167 GetSetupExeForInstallationLevel(level), kChromeExe); |
107 } | 168 } |
108 | 169 |
| 170 base::FilePath GetAppHostPathForInstallationLevel(InstallationLevel level) { |
| 171 return FindExeRelativeToSetupExe( |
| 172 GetSetupExeFromRegistry(level, kAppLauncherGuid), kChromeAppHostExe); |
| 173 } |
| 174 |
| 175 base::FilePath GetChromeSxSPathForInstallationLevel(InstallationLevel level) { |
| 176 return FindExeRelativeToSetupExe( |
| 177 GetSetupExeFromRegistry(level, kSxSBrowserAppGuid), kChromeExe); |
| 178 } |
| 179 |
109 base::FilePath GetAnyChromePath() { | 180 base::FilePath GetAnyChromePath() { |
110 base::FilePath chrome_path; | 181 base::FilePath chrome_path; |
111 if (chrome_path.empty()) | 182 if (chrome_path.empty()) |
112 chrome_path = GetChromePathForInstallationLevel(SYSTEM_LEVEL_INSTALLATION); | 183 chrome_path = GetChromePathForInstallationLevel(SYSTEM_LEVEL_INSTALLATION); |
113 if (chrome_path.empty()) | 184 if (chrome_path.empty()) |
114 chrome_path = GetChromePathForInstallationLevel(USER_LEVEL_INSTALLATION); | 185 chrome_path = GetChromePathForInstallationLevel(USER_LEVEL_INSTALLATION); |
115 return chrome_path; | 186 return chrome_path; |
116 } | 187 } |
117 | 188 |
| 189 base::FilePath GetAnyAppHostPath() { |
| 190 base::FilePath app_host_path; |
| 191 if (app_host_path.empty()) { |
| 192 app_host_path = GetAppHostPathForInstallationLevel( |
| 193 SYSTEM_LEVEL_INSTALLATION); |
| 194 } |
| 195 if (app_host_path.empty()) |
| 196 app_host_path = GetAppHostPathForInstallationLevel(USER_LEVEL_INSTALLATION); |
| 197 return app_host_path; |
| 198 } |
| 199 |
| 200 base::FilePath GetAnyChromeSxSPath() { |
| 201 base::FilePath path = |
| 202 GetChromeSxSPathForInstallationLevel(USER_LEVEL_INSTALLATION); |
| 203 if (path.empty()) |
| 204 path = GetChromeSxSPathForInstallationLevel(SYSTEM_LEVEL_INSTALLATION); |
| 205 return path; |
| 206 } |
| 207 |
| 208 bool IsAppHostPresent() { |
| 209 base::FilePath app_host_exe = GetAnyAppHostPath(); |
| 210 return !app_host_exe.empty(); |
| 211 } |
| 212 |
| 213 InstallationState GetAppLauncherInstallationState() { |
| 214 if (IsAppLauncherEnabledAtLevel(SYSTEM_LEVEL_INSTALLATION)) |
| 215 return INSTALLED_AT_SYSTEM_LEVEL; |
| 216 |
| 217 if (IsAppLauncherEnabledAtLevel(USER_LEVEL_INSTALLATION)) |
| 218 return INSTALLED_AT_USER_LEVEL; |
| 219 |
| 220 return NOT_INSTALLED; |
| 221 } |
| 222 |
| 223 bool IsAppLauncherPresent() { |
| 224 return GetAppLauncherInstallationState() != NOT_INSTALLED; |
| 225 } |
| 226 |
| 227 bool IsChromeBrowserPresent() { |
| 228 return IsProductInstalled(USER_LEVEL_INSTALLATION, kBrowserAppGuid) || |
| 229 IsProductInstalled(SYSTEM_LEVEL_INSTALLATION, kBrowserAppGuid); |
| 230 } |
| 231 |
118 } // namespace chrome_launcher_support | 232 } // namespace chrome_launcher_support |
OLD | NEW |