[email protected] | cec9984 | 2012-02-10 03:24:23 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[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 | |
[email protected] | 81585672 | 2011-04-13 17:19:19 | [diff] [blame] | 5 | #include "chrome/browser/first_run/upgrade_util.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" |
[email protected] | bc38c25 | 2011-04-12 21:46:57 | [diff] [blame] | 24 | #include "base/path_service.h" |
[email protected] | d09a4ce1c | 2013-07-24 17:37:02 | [diff] [blame] | 25 | #include "base/process/launch.h" |
| 26 | #include "base/process/process_handle.h" |
Avi Drissman | 5f0fb8c | 2018-12-25 23:20:49 | [diff] [blame] | 27 | #include "base/stl_util.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" |
[email protected] | bc38c25 | 2011-04-12 21:46:57 | [diff] [blame] | 30 | #include "base/win/registry.h" |
[email protected] | 770c6d8 | 2012-09-06 22:21:32 | [diff] [blame] | 31 | #include "base/win/windows_version.h" |
Nico Weber | eaa0841 | 2019-08-14 01:24:37 | [diff] [blame^] | 32 | #include "build/branding_buildflags.h" |
ananta | 069fc88 | 2014-09-13 01:22:12 | [diff] [blame] | 33 | #include "chrome/browser/browser_process.h" |
[email protected] | 3e08799 | 2011-04-14 22:28:12 | [diff] [blame] | 34 | #include "chrome/browser/first_run/upgrade_util_win.h" |
[email protected] | 3f69d6e61 | 2012-08-03 18:52:27 | [diff] [blame] | 35 | #include "chrome/browser/shell_integration.h" |
[email protected] | 80274b9 | 2011-07-15 17:20:38 | [diff] [blame] | 36 | #include "chrome/common/chrome_switches.h" |
ananta | 069fc88 | 2014-09-13 01:22:12 | [diff] [blame] | 37 | #include "chrome/common/pref_names.h" |
grt | 4474dad | 2017-02-27 21:00:46 | [diff] [blame] | 38 | #include "chrome/install_static/install_util.h" |
[email protected] | bc38c25 | 2011-04-12 21:46:57 | [diff] [blame] | 39 | #include "chrome/installer/util/google_update_constants.h" |
[email protected] | bc38c25 | 2011-04-12 21:46:57 | [diff] [blame] | 40 | #include "chrome/installer/util/util_constants.h" |
brettw | b1fc1b8 | 2016-02-02 00:19:08 | [diff] [blame] | 41 | #include "components/prefs/pref_service.h" |
ananta | 069fc88 | 2014-09-13 01:22:12 | [diff] [blame] | 42 | #include "ui/base/ui_base_switches.h" |
[email protected] | bc38c25 | 2011-04-12 21:46:57 | [diff] [blame] | 43 | |
Nico Weber | eaa0841 | 2019-08-14 01:24:37 | [diff] [blame^] | 44 | #if BUILDFLAG(GOOGLE_CHROME_BRANDING) |
grt | 235b3f09 | 2015-05-27 21:42:48 | [diff] [blame] | 45 | #include "google_update/google_update_idl.h" |
| 46 | #endif |
| 47 | |
[email protected] | bc38c25 | 2011-04-12 21:46:57 | [diff] [blame] | 48 | namespace { |
| 49 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 50 | bool GetNewerChromeFile(base::FilePath* path) { |
Avi Drissman | 9098f900 | 2018-05-04 00:11:52 | [diff] [blame] | 51 | if (!base::PathService::Get(base::DIR_EXE, path)) |
[email protected] | bc38c25 | 2011-04-12 21:46:57 | [diff] [blame] | 52 | return false; |
| 53 | *path = path->Append(installer::kChromeNewExe); |
| 54 | return true; |
| 55 | } |
| 56 | |
| 57 | bool InvokeGoogleUpdateForRename() { |
Nico Weber | eaa0841 | 2019-08-14 01:24:37 | [diff] [blame^] | 58 | #if BUILDFLAG(GOOGLE_CHROME_BRANDING) |
Robert Liao | b2bc703d | 2017-10-17 20:52:35 | [diff] [blame] | 59 | Microsoft::WRL::ComPtr<IProcessLauncher> ipl; |
Greg Thompson | a1f9512 | 2019-01-31 22:46:45 | [diff] [blame] | 60 | HRESULT hr = ::CoCreateInstance(__uuidof(ProcessLauncherClass), nullptr, |
| 61 | CLSCTX_ALL, IID_PPV_ARGS(&ipl)); |
| 62 | if (FAILED(hr)) { |
| 63 | LOG(ERROR) << "CoCreate ProcessLauncherClass failed; hr = " << std::hex |
| 64 | << hr; |
| 65 | return false; |
[email protected] | bc38c25 | 2011-04-12 21:46:57 | [diff] [blame] | 66 | } |
Greg Thompson | a1f9512 | 2019-01-31 22:46:45 | [diff] [blame] | 67 | |
| 68 | ULONG_PTR process_handle; |
| 69 | hr = ipl->LaunchCmdElevated(install_static::GetAppGuid(), |
| 70 | google_update::kRegRenameCmdField, |
| 71 | ::GetCurrentProcessId(), &process_handle); |
| 72 | if (FAILED(hr)) { |
| 73 | LOG(ERROR) << "IProcessLauncher::LaunchCmdElevated failed; hr = " |
| 74 | << std::hex << hr; |
| 75 | return false; |
| 76 | } |
| 77 | |
| 78 | base::Process rename_process( |
| 79 | reinterpret_cast<base::ProcessHandle>(process_handle)); |
| 80 | int exit_code; |
| 81 | if (!rename_process.WaitForExit(&exit_code)) { |
| 82 | PLOG(ERROR) << "WaitForExit of rename process failed"; |
| 83 | return false; |
| 84 | } |
| 85 | |
| 86 | if (exit_code != installer::RENAME_SUCCESSFUL) { |
| 87 | LOG(ERROR) << "Rename process failed with exit code " << exit_code; |
| 88 | return false; |
| 89 | } |
| 90 | |
| 91 | return true; |
Nico Weber | eaa0841 | 2019-08-14 01:24:37 | [diff] [blame^] | 92 | #else // BUILDFLAG(GOOGLE_CHROME_BRANDING) |
[email protected] | bc38c25 | 2011-04-12 21:46:57 | [diff] [blame] | 93 | return false; |
Nico Weber | eaa0841 | 2019-08-14 01:24:37 | [diff] [blame^] | 94 | #endif // BUILDFLAG(GOOGLE_CHROME_BRANDING) |
[email protected] | bc38c25 | 2011-04-12 21:46:57 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | } // namespace |
| 98 | |
[email protected] | 81585672 | 2011-04-13 17:19:19 | [diff] [blame] | 99 | namespace upgrade_util { |
| 100 | |
Greg Thompson | 87ee38f | 2019-08-09 06:00:23 | [diff] [blame] | 101 | bool RelaunchChromeBrowserImpl(const base::CommandLine& command_line) { |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 102 | base::FilePath chrome_exe; |
Avi Drissman | 9098f900 | 2018-05-04 00:11:52 | [diff] [blame] | 103 | if (!base::PathService::Get(base::FILE_EXE, &chrome_exe)) { |
[email protected] | 3f69d6e61 | 2012-08-03 18:52:27 | [diff] [blame] | 104 | NOTREACHED(); |
| 105 | return false; |
| 106 | } |
| 107 | |
[email protected] | fdbea98d | 2014-05-16 19:29:20 | [diff] [blame] | 108 | // Explicitly make sure to relaunch chrome.exe rather than old_chrome.exe. |
| 109 | // This can happen when old_chrome.exe is launched by a user. |
avi | 556c0502 | 2014-12-22 23:31:43 | [diff] [blame] | 110 | base::CommandLine chrome_exe_command_line = command_line; |
[email protected] | fdbea98d | 2014-05-16 19:29:20 | [diff] [blame] | 111 | chrome_exe_command_line.SetProgram( |
| 112 | chrome_exe.DirName().Append(installer::kChromeExe)); |
| 113 | |
scottmg | 20920cb | 2016-04-07 16:23:59 | [diff] [blame] | 114 | // Set the working directory to the exe's directory. This avoids a handle to |
| 115 | // the version directory being kept open in the relaunched child process. |
| 116 | base::LaunchOptions launch_options; |
| 117 | launch_options.current_directory = chrome_exe.DirName(); |
Greg Thompson | 47faf20 | 2018-05-18 20:59:03 | [diff] [blame] | 118 | // Give the new process the right to bring its windows to the foreground. |
| 119 | launch_options.grant_foreground_privilege = true; |
scottmg | 20920cb | 2016-04-07 16:23:59 | [diff] [blame] | 120 | return base::LaunchProcess(chrome_exe_command_line, launch_options).IsValid(); |
[email protected] | 81585672 | 2011-04-13 17:19:19 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | bool IsUpdatePendingRestart() { |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 124 | base::FilePath new_chrome_exe; |
[email protected] | 81585672 | 2011-04-13 17:19:19 | [diff] [blame] | 125 | if (!GetNewerChromeFile(&new_chrome_exe)) |
| 126 | return false; |
[email protected] | 756748414 | 2013-07-11 17:36:07 | [diff] [blame] | 127 | return base::PathExists(new_chrome_exe); |
[email protected] | 81585672 | 2011-04-13 17:19:19 | [diff] [blame] | 128 | } |
| 129 | |
[email protected] | 81585672 | 2011-04-13 17:19:19 | [diff] [blame] | 130 | bool SwapNewChromeExeIfPresent() { |
grt | b35a90b | 2016-08-30 05:34:21 | [diff] [blame] | 131 | if (!IsUpdatePendingRestart()) |
[email protected] | bc38c25 | 2011-04-12 21:46:57 | [diff] [blame] | 132 | return false; |
grt | b35a90b | 2016-08-30 05:34:21 | [diff] [blame] | 133 | |
fdoray | e9c97a0 | 2016-10-04 12:05:16 | [diff] [blame] | 134 | // If this is a system-level install, ask Google Update to launch an elevated |
| 135 | // process to rename Chrome executables. |
Greg Thompson | d2efb1f | 2018-08-29 06:12:54 | [diff] [blame] | 136 | if (install_static::IsSystemInstall()) |
fdoray | e9c97a0 | 2016-10-04 12:05:16 | [diff] [blame] | 137 | return InvokeGoogleUpdateForRename(); |
[email protected] | bc38c25 | 2011-04-12 21:46:57 | [diff] [blame] | 138 | |
fdoray | e9c97a0 | 2016-10-04 12:05:16 | [diff] [blame] | 139 | // If this is a user-level install, directly launch a process to rename Chrome |
| 140 | // executables. Obtain the command to launch the process from the registry. |
[email protected] | bc38c25 | 2011-04-12 21:46:57 | [diff] [blame] | 141 | base::win::RegKey key; |
Greg Thompson | a1f9512 | 2019-01-31 22:46:45 | [diff] [blame] | 142 | auto result = |
| 143 | key.Open(HKEY_CURRENT_USER, install_static::GetClientsKeyPath().c_str(), |
| 144 | KEY_QUERY_VALUE | KEY_WOW64_32KEY); |
| 145 | if (result != ERROR_SUCCESS) { |
| 146 | ::SetLastError(result); |
| 147 | PLOG(ERROR) << "Open Clients key failed"; |
| 148 | return false; |
[email protected] | bc38c25 | 2011-04-12 21:46:57 | [diff] [blame] | 149 | } |
| 150 | |
Greg Thompson | a1f9512 | 2019-01-31 22:46:45 | [diff] [blame] | 151 | std::wstring rename_cmd; |
| 152 | result = key.ReadValue(google_update::kRegRenameCmdField, &rename_cmd); |
| 153 | if (result != ERROR_SUCCESS) { |
| 154 | ::SetLastError(result); |
| 155 | PLOG(ERROR) << "Read rename command failed"; |
| 156 | return false; |
| 157 | } |
| 158 | |
| 159 | base::LaunchOptions options; |
| 160 | options.wait = true; |
| 161 | options.start_hidden = true; |
| 162 | ::SetLastError(ERROR_SUCCESS); |
| 163 | base::Process process = base::LaunchProcess(rename_cmd, options); |
| 164 | if (!process.IsValid()) { |
| 165 | PLOG(ERROR) << "Launch rename process failed"; |
| 166 | return false; |
| 167 | } |
| 168 | |
| 169 | DWORD exit_code; |
| 170 | if (!::GetExitCodeProcess(process.Handle(), &exit_code)) { |
| 171 | PLOG(ERROR) << "GetExitCodeProcess of rename process failed"; |
| 172 | return false; |
| 173 | } |
| 174 | |
| 175 | if (exit_code != installer::RENAME_SUCCESSFUL) { |
| 176 | LOG(ERROR) << "Rename process failed with exit code " << exit_code; |
| 177 | return false; |
| 178 | } |
| 179 | |
| 180 | return true; |
[email protected] | bc38c25 | 2011-04-12 21:46:57 | [diff] [blame] | 181 | } |
| 182 | |
[email protected] | fdbea98d | 2014-05-16 19:29:20 | [diff] [blame] | 183 | bool IsRunningOldChrome() { |
| 184 | // This figures out the actual file name that the section containing the |
| 185 | // mapped exe refers to. This is used instead of GetModuleFileName because the |
| 186 | // .exe may have been renamed out from under us while we've been running which |
| 187 | // GetModuleFileName won't notice. |
| 188 | wchar_t mapped_file_name[MAX_PATH * 2] = {}; |
| 189 | |
| 190 | if (!::GetMappedFileName(::GetCurrentProcess(), |
| 191 | reinterpret_cast<void*>(::GetModuleHandle(NULL)), |
Avi Drissman | 5f0fb8c | 2018-12-25 23:20:49 | [diff] [blame] | 192 | mapped_file_name, base::size(mapped_file_name))) { |
[email protected] | fdbea98d | 2014-05-16 19:29:20 | [diff] [blame] | 193 | return false; |
| 194 | } |
| 195 | |
| 196 | base::FilePath file_name(base::FilePath(mapped_file_name).BaseName()); |
| 197 | return base::FilePath::CompareEqualIgnoreCase(file_name.value(), |
| 198 | installer::kChromeOldExe); |
| 199 | } |
| 200 | |
avi | 556c0502 | 2014-12-22 23:31:43 | [diff] [blame] | 201 | bool DoUpgradeTasks(const base::CommandLine& command_line) { |
[email protected] | fdbea98d | 2014-05-16 19:29:20 | [diff] [blame] | 202 | if (!SwapNewChromeExeIfPresent() && !IsRunningOldChrome()) |
[email protected] | bc38c25 | 2011-04-12 21:46:57 | [diff] [blame] | 203 | return false; |
| 204 | // At this point the chrome.exe has been swapped with the new one. |
[email protected] | 81585672 | 2011-04-13 17:19:19 | [diff] [blame] | 205 | if (!RelaunchChromeBrowser(command_line)) { |
[email protected] | bc38c25 | 2011-04-12 21:46:57 | [diff] [blame] | 206 | // The re-launch fails. Feel free to panic now. |
| 207 | NOTREACHED(); |
| 208 | } |
| 209 | return true; |
| 210 | } |
| 211 | |
[email protected] | 81585672 | 2011-04-13 17:19:19 | [diff] [blame] | 212 | } // namespace upgrade_util |