Avi Drissman | 4a8573c | 2022-09-09 19:35:54 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
[email protected] | bc38c25 | 2011-04-12 21:46:57 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Daniel Cheng | 7d9e3d5 | 2022-02-26 09:03:24 | [diff] [blame] | 5 | #include "chrome/browser/first_run/upgrade_util_win.h" |
[email protected] | bc38c25 | 2011-04-12 21:46:57 | [diff] [blame] | 6 | |
Nico Weber | eaa0841 | 2019-08-14 01:24:37 | [diff] [blame] | 7 | // Must be first. |
[email protected] | 770c6d8 | 2012-09-06 22:21:32 | [diff] [blame] | 8 | #include <windows.h> |
Nico Weber | eaa0841 | 2019-08-14 01:24:37 | [diff] [blame] | 9 | |
robliao | a872e99 | 2017-05-18 06:36:19 | [diff] [blame] | 10 | #include <objbase.h> |
[email protected] | fdbea98d | 2014-05-16 19:29:20 | [diff] [blame] | 11 | #include <psapi.h> |
[email protected] | 770c6d8 | 2012-09-06 22:21:32 | [diff] [blame] | 12 | #include <shellapi.h> |
Robert Liao | b2bc703d | 2017-10-17 20:52:35 | [diff] [blame] | 13 | #include <wrl/client.h> |
[email protected] | 770c6d8 | 2012-09-06 22:21:32 | [diff] [blame] | 14 | |
[email protected] | bc38c25 | 2011-04-12 21:46:57 | [diff] [blame] | 15 | #include <algorithm> |
Greg Thompson | a1f9512 | 2019-01-31 22:46:45 | [diff] [blame] | 16 | #include <ios> |
[email protected] | bc38c25 | 2011-04-12 21:46:57 | [diff] [blame] | 17 | #include <string> |
| 18 | |
| 19 | #include "base/base_paths.h" |
| 20 | #include "base/command_line.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 21 | #include "base/files/file_path.h" |
thestig | 18dfb7a5 | 2014-08-26 10:44:04 | [diff] [blame] | 22 | #include "base/files/file_util.h" |
[email protected] | bc38c25 | 2011-04-12 21:46:57 | [diff] [blame] | 23 | #include "base/logging.h" |
Gabriel Charette | 12b58ac | 2021-09-22 18:08:55 | [diff] [blame] | 24 | #include "base/metrics/histogram_macros.h" |
[email protected] | bc38c25 | 2011-04-12 21:46:57 | [diff] [blame] | 25 | #include "base/path_service.h" |
[email protected] | d09a4ce1c | 2013-07-24 17:37:02 | [diff] [blame] | 26 | #include "base/process/launch.h" |
| 27 | #include "base/process/process_handle.h" |
[email protected] | 3ea1b18 | 2013-02-08 22:38:41 | [diff] [blame] | 28 | #include "base/strings/string_number_conversions.h" |
[email protected] | d883056 | 2013-06-10 22:01:54 | [diff] [blame] | 29 | #include "base/strings/string_util.h" |
Gabriel Charette | 12b58ac | 2021-09-22 18:08:55 | [diff] [blame] | 30 | #include "base/time/time.h" |
| 31 | #include "base/trace_event/trace_event.h" |
[email protected] | bc38c25 | 2011-04-12 21:46:57 | [diff] [blame] | 32 | #include "base/win/registry.h" |
[email protected] | 770c6d8 | 2012-09-06 22:21:32 | [diff] [blame] | 33 | #include "base/win/windows_version.h" |
Nico Weber | eaa0841 | 2019-08-14 01:24:37 | [diff] [blame] | 34 | #include "build/branding_buildflags.h" |
ananta | 069fc88 | 2014-09-13 01:22:12 | [diff] [blame] | 35 | #include "chrome/browser/browser_process.h" |
Daniel Cheng | 7d9e3d5 | 2022-02-26 09:03:24 | [diff] [blame] | 36 | #include "chrome/browser/first_run/upgrade_util.h" |
[email protected] | 3f69d6e61 | 2012-08-03 18:52:27 | [diff] [blame] | 37 | #include "chrome/browser/shell_integration.h" |
Javier Flores Assad | 9c26f9b9 | 2022-05-21 01:20:43 | [diff] [blame] | 38 | #include "chrome/browser/win/browser_util.h" |
[email protected] | 80274b9 | 2011-07-15 17:20:38 | [diff] [blame] | 39 | #include "chrome/common/chrome_switches.h" |
ananta | 069fc88 | 2014-09-13 01:22:12 | [diff] [blame] | 40 | #include "chrome/common/pref_names.h" |
grt | 4474dad | 2017-02-27 21:00:46 | [diff] [blame] | 41 | #include "chrome/install_static/install_util.h" |
S. Ganesh | f77e2a5 | 2022-11-22 18:40:19 | [diff] [blame] | 42 | #include "chrome/installer/util/app_command.h" |
[email protected] | bc38c25 | 2011-04-12 21:46:57 | [diff] [blame] | 43 | #include "chrome/installer/util/util_constants.h" |
brettw | b1fc1b8 | 2016-02-02 00:19:08 | [diff] [blame] | 44 | #include "components/prefs/pref_service.h" |
ananta | 069fc88 | 2014-09-13 01:22:12 | [diff] [blame] | 45 | #include "ui/base/ui_base_switches.h" |
[email protected] | bc38c25 | 2011-04-12 21:46:57 | [diff] [blame] | 46 | |
Nico Weber | eaa0841 | 2019-08-14 01:24:37 | [diff] [blame] | 47 | #if BUILDFLAG(GOOGLE_CHROME_BRANDING) |
grt | 235b3f09 | 2015-05-27 21:42:48 | [diff] [blame] | 48 | #include "google_update/google_update_idl.h" |
| 49 | #endif |
| 50 | |
[email protected] | bc38c25 | 2011-04-12 21:46:57 | [diff] [blame] | 51 | namespace { |
| 52 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 53 | bool GetNewerChromeFile(base::FilePath* path) { |
Avi Drissman | 9098f900 | 2018-05-04 00:11:52 | [diff] [blame] | 54 | if (!base::PathService::Get(base::DIR_EXE, path)) |
[email protected] | bc38c25 | 2011-04-12 21:46:57 | [diff] [blame] | 55 | return false; |
| 56 | *path = path->Append(installer::kChromeNewExe); |
| 57 | return true; |
| 58 | } |
| 59 | |
| 60 | bool InvokeGoogleUpdateForRename() { |
Nico Weber | eaa0841 | 2019-08-14 01:24:37 | [diff] [blame] | 61 | #if BUILDFLAG(GOOGLE_CHROME_BRANDING) |
Gabriel Charette | f7e8571 | 2021-12-10 20:46:22 | [diff] [blame] | 62 | // This has been identified as very slow on some startups. Detailed trace |
| 63 | // events below try to shine a light on each steps. crbug.com/1252004 |
Gabriel Charette | 12b58ac | 2021-09-22 18:08:55 | [diff] [blame] | 64 | TRACE_EVENT0("startup", "upgrade_util::InvokeGoogleUpdateForRename"); |
| 65 | |
Robert Liao | b2bc703d | 2017-10-17 20:52:35 | [diff] [blame] | 66 | Microsoft::WRL::ComPtr<IProcessLauncher> ipl; |
Gabriel Charette | f7e8571 | 2021-12-10 20:46:22 | [diff] [blame] | 67 | { |
| 68 | TRACE_EVENT0("startup", "InvokeGoogleUpdateForRename CoCreateInstance"); |
| 69 | HRESULT hr = ::CoCreateInstance(__uuidof(ProcessLauncherClass), nullptr, |
| 70 | CLSCTX_ALL, IID_PPV_ARGS(&ipl)); |
| 71 | if (FAILED(hr)) { |
| 72 | TRACE_EVENT0("startup", |
| 73 | "InvokeGoogleUpdateForRename CoCreateInstance failed"); |
| 74 | LOG(ERROR) << "CoCreate ProcessLauncherClass failed; hr = " << std::hex |
| 75 | << hr; |
| 76 | return false; |
| 77 | } |
[email protected] | bc38c25 | 2011-04-12 21:46:57 | [diff] [blame] | 78 | } |
Greg Thompson | a1f9512 | 2019-01-31 22:46:45 | [diff] [blame] | 79 | |
S. Ganesh | 74432c1 | 2022-11-02 06:35:34 | [diff] [blame] | 80 | ULONG_PTR process_handle = 0; |
Gabriel Charette | f7e8571 | 2021-12-10 20:46:22 | [diff] [blame] | 81 | { |
| 82 | TRACE_EVENT0("startup", "InvokeGoogleUpdateForRename LaunchCmdElevated"); |
Dominique Fauteux-Chapleau | 269cabf | 2021-12-10 22:10:31 | [diff] [blame] | 83 | HRESULT hr = ipl->LaunchCmdElevated( |
S. Ganesh | f77e2a5 | 2022-11-22 18:40:19 | [diff] [blame] | 84 | install_static::GetAppGuid(), installer::kCmdRenameChromeExe, |
Dominique Fauteux-Chapleau | 269cabf | 2021-12-10 22:10:31 | [diff] [blame] | 85 | ::GetCurrentProcessId(), &process_handle); |
Gabriel Charette | f7e8571 | 2021-12-10 20:46:22 | [diff] [blame] | 86 | if (FAILED(hr)) { |
| 87 | TRACE_EVENT0("startup", |
| 88 | "InvokeGoogleUpdateForRename LaunchCmdElevated failed"); |
| 89 | LOG(ERROR) << "IProcessLauncher::LaunchCmdElevated failed; hr = " |
| 90 | << std::hex << hr; |
| 91 | return false; |
| 92 | } |
Greg Thompson | a1f9512 | 2019-01-31 22:46:45 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | base::Process rename_process( |
| 96 | reinterpret_cast<base::ProcessHandle>(process_handle)); |
| 97 | int exit_code; |
Gabriel Charette | f7e8571 | 2021-12-10 20:46:22 | [diff] [blame] | 98 | { |
| 99 | TRACE_EVENT0("startup", "InvokeGoogleUpdateForRename WaitForExit"); |
| 100 | if (!rename_process.WaitForExit(&exit_code)) { |
| 101 | TRACE_EVENT0("startup", "InvokeGoogleUpdateForRename WaitForExit failed"); |
| 102 | PLOG(ERROR) << "WaitForExit of rename process failed"; |
| 103 | return false; |
| 104 | } |
Greg Thompson | a1f9512 | 2019-01-31 22:46:45 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | if (exit_code != installer::RENAME_SUCCESSFUL) { |
Gabriel Charette | f7e8571 | 2021-12-10 20:46:22 | [diff] [blame] | 108 | TRACE_EVENT0("startup", "InvokeGoogleUpdateForRename !RENAME_SUCCESSFUL"); |
Greg Thompson | a1f9512 | 2019-01-31 22:46:45 | [diff] [blame] | 109 | LOG(ERROR) << "Rename process failed with exit code " << exit_code; |
| 110 | return false; |
| 111 | } |
| 112 | |
Gabriel Charette | 334775a | 2021-12-13 18:57:26 | [diff] [blame] | 113 | TRACE_EVENT0("startup", "InvokeGoogleUpdateForRename RENAME_SUCCESSFUL"); |
| 114 | |
Greg Thompson | a1f9512 | 2019-01-31 22:46:45 | [diff] [blame] | 115 | return true; |
Nico Weber | eaa0841 | 2019-08-14 01:24:37 | [diff] [blame] | 116 | #else // BUILDFLAG(GOOGLE_CHROME_BRANDING) |
[email protected] | bc38c25 | 2011-04-12 21:46:57 | [diff] [blame] | 117 | return false; |
Nico Weber | eaa0841 | 2019-08-14 01:24:37 | [diff] [blame] | 118 | #endif // BUILDFLAG(GOOGLE_CHROME_BRANDING) |
[email protected] | bc38c25 | 2011-04-12 21:46:57 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | } // namespace |
| 122 | |
[email protected] | 81585672 | 2011-04-13 17:19:19 | [diff] [blame] | 123 | namespace upgrade_util { |
| 124 | |
Greg Thompson | 87ee38f | 2019-08-09 06:00:23 | [diff] [blame] | 125 | bool RelaunchChromeBrowserImpl(const base::CommandLine& command_line) { |
Gabriel Charette | 12b58ac | 2021-09-22 18:08:55 | [diff] [blame] | 126 | TRACE_EVENT0("startup", "upgrade_util::RelaunchChromeBrowserImpl"); |
| 127 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 128 | base::FilePath chrome_exe; |
Avi Drissman | 9098f900 | 2018-05-04 00:11:52 | [diff] [blame] | 129 | if (!base::PathService::Get(base::FILE_EXE, &chrome_exe)) { |
[email protected] | 3f69d6e61 | 2012-08-03 18:52:27 | [diff] [blame] | 130 | NOTREACHED(); |
| 131 | return false; |
| 132 | } |
| 133 | |
[email protected] | fdbea98d | 2014-05-16 19:29:20 | [diff] [blame] | 134 | // Explicitly make sure to relaunch chrome.exe rather than old_chrome.exe. |
| 135 | // This can happen when old_chrome.exe is launched by a user. |
avi | 556c0502 | 2014-12-22 23:31:43 | [diff] [blame] | 136 | base::CommandLine chrome_exe_command_line = command_line; |
[email protected] | fdbea98d | 2014-05-16 19:29:20 | [diff] [blame] | 137 | chrome_exe_command_line.SetProgram( |
| 138 | chrome_exe.DirName().Append(installer::kChromeExe)); |
| 139 | |
scottmg | 20920cb | 2016-04-07 16:23:59 | [diff] [blame] | 140 | // Set the working directory to the exe's directory. This avoids a handle to |
| 141 | // the version directory being kept open in the relaunched child process. |
| 142 | base::LaunchOptions launch_options; |
| 143 | launch_options.current_directory = chrome_exe.DirName(); |
Greg Thompson | 47faf20 | 2018-05-18 20:59:03 | [diff] [blame] | 144 | // Give the new process the right to bring its windows to the foreground. |
| 145 | launch_options.grant_foreground_privilege = true; |
scottmg | 20920cb | 2016-04-07 16:23:59 | [diff] [blame] | 146 | return base::LaunchProcess(chrome_exe_command_line, launch_options).IsValid(); |
[email protected] | 81585672 | 2011-04-13 17:19:19 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | bool IsUpdatePendingRestart() { |
Gabriel Charette | 12b58ac | 2021-09-22 18:08:55 | [diff] [blame] | 150 | TRACE_EVENT0("startup", "upgrade_util::IsUpdatePendingRestart"); |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 151 | base::FilePath new_chrome_exe; |
[email protected] | 81585672 | 2011-04-13 17:19:19 | [diff] [blame] | 152 | if (!GetNewerChromeFile(&new_chrome_exe)) |
| 153 | return false; |
[email protected] | 756748414 | 2013-07-11 17:36:07 | [diff] [blame] | 154 | return base::PathExists(new_chrome_exe); |
[email protected] | 81585672 | 2011-04-13 17:19:19 | [diff] [blame] | 155 | } |
| 156 | |
[email protected] | 81585672 | 2011-04-13 17:19:19 | [diff] [blame] | 157 | bool SwapNewChromeExeIfPresent() { |
grt | b35a90b | 2016-08-30 05:34:21 | [diff] [blame] | 158 | if (!IsUpdatePendingRestart()) |
[email protected] | bc38c25 | 2011-04-12 21:46:57 | [diff] [blame] | 159 | return false; |
grt | b35a90b | 2016-08-30 05:34:21 | [diff] [blame] | 160 | |
Gabriel Charette | 12b58ac | 2021-09-22 18:08:55 | [diff] [blame] | 161 | TRACE_EVENT0("startup", "upgrade_util::SwapNewChromeExeIfPresent"); |
| 162 | |
fdoray | e9c97a0 | 2016-10-04 12:05:16 | [diff] [blame] | 163 | // If this is a system-level install, ask Google Update to launch an elevated |
| 164 | // process to rename Chrome executables. |
Greg Thompson | d2efb1f | 2018-08-29 06:12:54 | [diff] [blame] | 165 | if (install_static::IsSystemInstall()) |
fdoray | e9c97a0 | 2016-10-04 12:05:16 | [diff] [blame] | 166 | return InvokeGoogleUpdateForRename(); |
[email protected] | bc38c25 | 2011-04-12 21:46:57 | [diff] [blame] | 167 | |
fdoray | e9c97a0 | 2016-10-04 12:05:16 | [diff] [blame] | 168 | // If this is a user-level install, directly launch a process to rename Chrome |
| 169 | // executables. Obtain the command to launch the process from the registry. |
S. Ganesh | f77e2a5 | 2022-11-22 18:40:19 | [diff] [blame] | 170 | installer::AppCommand rename_cmd(installer::kCmdRenameChromeExe, {}); |
| 171 | if (!rename_cmd.Initialize(HKEY_CURRENT_USER)) |
Greg Thompson | a1f9512 | 2019-01-31 22:46:45 | [diff] [blame] | 172 | return false; |
Greg Thompson | a1f9512 | 2019-01-31 22:46:45 | [diff] [blame] | 173 | |
| 174 | base::LaunchOptions options; |
| 175 | options.wait = true; |
| 176 | options.start_hidden = true; |
| 177 | ::SetLastError(ERROR_SUCCESS); |
S. Ganesh | f77e2a5 | 2022-11-22 18:40:19 | [diff] [blame] | 178 | base::Process process = |
| 179 | base::LaunchProcess(rename_cmd.command_line(), options); |
Greg Thompson | a1f9512 | 2019-01-31 22:46:45 | [diff] [blame] | 180 | if (!process.IsValid()) { |
| 181 | PLOG(ERROR) << "Launch rename process failed"; |
| 182 | return false; |
| 183 | } |
| 184 | |
| 185 | DWORD exit_code; |
| 186 | if (!::GetExitCodeProcess(process.Handle(), &exit_code)) { |
| 187 | PLOG(ERROR) << "GetExitCodeProcess of rename process failed"; |
| 188 | return false; |
| 189 | } |
| 190 | |
| 191 | if (exit_code != installer::RENAME_SUCCESSFUL) { |
| 192 | LOG(ERROR) << "Rename process failed with exit code " << exit_code; |
| 193 | return false; |
| 194 | } |
| 195 | |
| 196 | return true; |
[email protected] | bc38c25 | 2011-04-12 21:46:57 | [diff] [blame] | 197 | } |
| 198 | |
[email protected] | fdbea98d | 2014-05-16 19:29:20 | [diff] [blame] | 199 | bool IsRunningOldChrome() { |
Gabriel Charette | 12b58ac | 2021-09-22 18:08:55 | [diff] [blame] | 200 | TRACE_EVENT0("startup", "upgrade_util::IsRunningOldChrome"); |
[email protected] | fdbea98d | 2014-05-16 19:29:20 | [diff] [blame] | 201 | // This figures out the actual file name that the section containing the |
| 202 | // mapped exe refers to. This is used instead of GetModuleFileName because the |
| 203 | // .exe may have been renamed out from under us while we've been running which |
| 204 | // GetModuleFileName won't notice. |
| 205 | wchar_t mapped_file_name[MAX_PATH * 2] = {}; |
| 206 | |
| 207 | if (!::GetMappedFileName(::GetCurrentProcess(), |
| 208 | reinterpret_cast<void*>(::GetModuleHandle(NULL)), |
Daniel Cheng | 7d9e3d5 | 2022-02-26 09:03:24 | [diff] [blame] | 209 | mapped_file_name, std::size(mapped_file_name))) { |
[email protected] | fdbea98d | 2014-05-16 19:29:20 | [diff] [blame] | 210 | return false; |
| 211 | } |
| 212 | |
| 213 | base::FilePath file_name(base::FilePath(mapped_file_name).BaseName()); |
| 214 | return base::FilePath::CompareEqualIgnoreCase(file_name.value(), |
| 215 | installer::kChromeOldExe); |
| 216 | } |
| 217 | |
avi | 556c0502 | 2014-12-22 23:31:43 | [diff] [blame] | 218 | bool DoUpgradeTasks(const base::CommandLine& command_line) { |
Gabriel Charette | 12b58ac | 2021-09-22 18:08:55 | [diff] [blame] | 219 | TRACE_EVENT0("startup", "upgrade_util::DoUpgradeTasks"); |
Javier Flores Assad | 9c26f9b9 | 2022-05-21 01:20:43 | [diff] [blame] | 220 | // If there is no other instance already running then check if there is a |
| 221 | // pending update and complete it by performing the swap and then relaunch. |
| 222 | bool did_swap = false; |
| 223 | if (!browser_util::IsBrowserAlreadyRunning()) |
| 224 | did_swap = SwapNewChromeExeIfPresent(); |
| 225 | |
| 226 | // We don't need to relaunch if we didn't swap and we aren't running stale |
| 227 | // binaries. |
| 228 | if (!did_swap && !IsRunningOldChrome()) { |
[email protected] | bc38c25 | 2011-04-12 21:46:57 | [diff] [blame] | 229 | return false; |
Gabriel Charette | 12b58ac | 2021-09-22 18:08:55 | [diff] [blame] | 230 | } |
Javier Flores Assad | 9c26f9b9 | 2022-05-21 01:20:43 | [diff] [blame] | 231 | |
[email protected] | bc38c25 | 2011-04-12 21:46:57 | [diff] [blame] | 232 | // At this point the chrome.exe has been swapped with the new one. |
Wissem Gamra | 8e67a95d | 2023-03-17 17:41:21 | [diff] [blame^] | 233 | if (!RelaunchChromeBrowser(command_line)) { |
Javier Flores Assad | 9c26f9b9 | 2022-05-21 01:20:43 | [diff] [blame] | 234 | // The relaunch failed. Feel free to panic now. |
[email protected] | bc38c25 | 2011-04-12 21:46:57 | [diff] [blame] | 235 | NOTREACHED(); |
[email protected] | bc38c25 | 2011-04-12 21:46:57 | [diff] [blame] | 236 | } |
|
|