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 | |
Takuto Ikuta | c8d6b16f | 2024-04-15 16:59:19 | [diff] [blame] | 7 | #include <objbase.h> |
| 8 | |
[email protected] | 770c6d8 | 2012-09-06 22:21:32 | [diff] [blame] | 9 | #include <windows.h> |
Nico Weber | eaa0841 | 2019-08-14 01:24:37 | [diff] [blame] | 10 | |
[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" |
Etienne Bergeron | 7a0ef61 | 2023-09-15 18:28:08 | [diff] [blame] | 20 | #include "base/check.h" |
[email protected] | bc38c25 | 2011-04-12 21:46:57 | [diff] [blame] | 21 | #include "base/command_line.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 22 | #include "base/files/file_path.h" |
thestig | 18dfb7a5 | 2014-08-26 10:44:04 | [diff] [blame] | 23 | #include "base/files/file_util.h" |
S. Ganesh | cad4503 | 2024-07-18 17:43:11 | [diff] [blame] | 24 | #include "base/functional/bind.h" |
[email protected] | bc38c25 | 2011-04-12 21:46:57 | [diff] [blame] | 25 | #include "base/logging.h" |
S. Ganesh | cad4503 | 2024-07-18 17:43:11 | [diff] [blame] | 26 | #include "base/memory/ref_counted.h" |
| 27 | #include "base/memory/scoped_refptr.h" |
S. Ganesh | 4854086 | 2024-08-12 18:09:39 | [diff] [blame^] | 28 | #include "base/metrics/histogram_functions.h" |
[email protected] | bc38c25 | 2011-04-12 21:46:57 | [diff] [blame] | 29 | #include "base/path_service.h" |
[email protected] | d09a4ce1c | 2013-07-24 17:37:02 | [diff] [blame] | 30 | #include "base/process/launch.h" |
| 31 | #include "base/process/process_handle.h" |
[email protected] | 3ea1b18 | 2013-02-08 22:38:41 | [diff] [blame] | 32 | #include "base/strings/string_number_conversions.h" |
[email protected] | d883056 | 2013-06-10 22:01:54 | [diff] [blame] | 33 | #include "base/strings/string_util.h" |
S. Ganesh | cad4503 | 2024-07-18 17:43:11 | [diff] [blame] | 34 | #include "base/synchronization/waitable_event.h" |
| 35 | #include "base/task/sequenced_task_runner.h" |
| 36 | #include "base/task/task_traits.h" |
| 37 | #include "base/task/thread_pool.h" |
Gabriel Charette | 12b58ac | 2021-09-22 18:08:55 | [diff] [blame] | 38 | #include "base/time/time.h" |
S. Ganesh | 4854086 | 2024-08-12 18:09:39 | [diff] [blame^] | 39 | #include "base/timer/elapsed_timer.h" |
Gabriel Charette | 12b58ac | 2021-09-22 18:08:55 | [diff] [blame] | 40 | #include "base/trace_event/trace_event.h" |
S. Ganesh | 4854086 | 2024-08-12 18:09:39 | [diff] [blame^] | 41 | #include "base/values.h" |
[email protected] | bc38c25 | 2011-04-12 21:46:57 | [diff] [blame] | 42 | #include "base/win/registry.h" |
[email protected] | 770c6d8 | 2012-09-06 22:21:32 | [diff] [blame] | 43 | #include "base/win/windows_version.h" |
Nico Weber | eaa0841 | 2019-08-14 01:24:37 | [diff] [blame] | 44 | #include "build/branding_buildflags.h" |
ananta | 069fc88 | 2014-09-13 01:22:12 | [diff] [blame] | 45 | #include "chrome/browser/browser_process.h" |
Etienne Bergeron | 7a0ef61 | 2023-09-15 18:28:08 | [diff] [blame] | 46 | #include "chrome/browser/chrome_process_singleton.h" |
Daniel Cheng | 7d9e3d5 | 2022-02-26 09:03:24 | [diff] [blame] | 47 | #include "chrome/browser/first_run/upgrade_util.h" |
[email protected] | 3f69d6e61 | 2012-08-03 18:52:27 | [diff] [blame] | 48 | #include "chrome/browser/shell_integration.h" |
Javier Flores Assad | 9c26f9b9 | 2022-05-21 01:20:43 | [diff] [blame] | 49 | #include "chrome/browser/win/browser_util.h" |
[email protected] | 80274b9 | 2011-07-15 17:20:38 | [diff] [blame] | 50 | #include "chrome/common/chrome_switches.h" |
ananta | 069fc88 | 2014-09-13 01:22:12 | [diff] [blame] | 51 | #include "chrome/common/pref_names.h" |
grt | 4474dad | 2017-02-27 21:00:46 | [diff] [blame] | 52 | #include "chrome/install_static/install_util.h" |
S. Ganesh | f77e2a5 | 2022-11-22 18:40:19 | [diff] [blame] | 53 | #include "chrome/installer/util/app_command.h" |
S. Ganesh | 4854086 | 2024-08-12 18:09:39 | [diff] [blame^] | 54 | #include "chrome/installer/util/per_install_values.h" |
[email protected] | bc38c25 | 2011-04-12 21:46:57 | [diff] [blame] | 55 | #include "chrome/installer/util/util_constants.h" |
brettw | b1fc1b8 | 2016-02-02 00:19:08 | [diff] [blame] | 56 | #include "components/prefs/pref_service.h" |
S. Ganesh | cad4503 | 2024-07-18 17:43:11 | [diff] [blame] | 57 | #include "third_party/abseil-cpp/absl/cleanup/cleanup.h" |
ananta | 069fc88 | 2014-09-13 01:22:12 | [diff] [blame] | 58 | #include "ui/base/ui_base_switches.h" |
[email protected] | bc38c25 | 2011-04-12 21:46:57 | [diff] [blame] | 59 | |
Nico Weber | eaa0841 | 2019-08-14 01:24:37 | [diff] [blame] | 60 | #if BUILDFLAG(GOOGLE_CHROME_BRANDING) |
S. Ganesh | 6adf04c | 2023-06-07 19:34:55 | [diff] [blame] | 61 | #include "chrome/updater/app/server/win/updater_legacy_idl.h" |
grt | 235b3f09 | 2015-05-27 21:42:48 | [diff] [blame] | 62 | #endif |
| 63 | |
[email protected] | bc38c25 | 2011-04-12 21:46:57 | [diff] [blame] | 64 | namespace { |
| 65 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 66 | bool GetNewerChromeFile(base::FilePath* path) { |
Avi Drissman | 9098f900 | 2018-05-04 00:11:52 | [diff] [blame] | 67 | if (!base::PathService::Get(base::DIR_EXE, path)) |
[email protected] | bc38c25 | 2011-04-12 21:46:57 | [diff] [blame] | 68 | return false; |
| 69 | *path = path->Append(installer::kChromeNewExe); |
| 70 | return true; |
| 71 | } |
| 72 | |
S. Ganesh | cad4503 | 2024-07-18 17:43:11 | [diff] [blame] | 73 | #if BUILDFLAG(GOOGLE_CHROME_BRANDING) |
| 74 | // Holds the result of the IPC to CoCreate the process launcher. |
| 75 | struct CreateProcessLauncherResult |
| 76 | : public base::RefCountedThreadSafe<CreateProcessLauncherResult> { |
| 77 | Microsoft::WRL::ComPtr<IStream> stream; |
| 78 | base::WaitableEvent completion_event; |
| 79 | |
| 80 | private: |
| 81 | friend class base::RefCountedThreadSafe<CreateProcessLauncherResult>; |
| 82 | virtual ~CreateProcessLauncherResult() = default; |
| 83 | }; |
| 84 | |
| 85 | // CoCreates the `ProcessLauncher` class, and if successful, marshals the |
| 86 | // resulting interface into `result->stream`. Signals `result->completion_event` |
| 87 | // on successful or failed completion. |
| 88 | void CreateAndMarshalProcessLauncher( |
| 89 | scoped_refptr<CreateProcessLauncherResult> result) { |
| 90 | const absl::Cleanup signal_completion_event = [&result] { |
| 91 | result->completion_event.Signal(); |
| 92 | }; |
| 93 | |
| 94 | Microsoft::WRL::ComPtr<IUnknown> unknown; |
| 95 | { |
| 96 | TRACE_EVENT0("startup", "InvokeGoogleUpdateForRename CoCreateInstance"); |
| 97 | const HRESULT hr = |
| 98 | ::CoCreateInstance(__uuidof(ProcessLauncherClass), nullptr, CLSCTX_ALL, |
| 99 | IID_PPV_ARGS(&unknown)); |
| 100 | if (FAILED(hr)) { |
| 101 | TRACE_EVENT_INSTANT1( |
| 102 | "startup", "InvokeGoogleUpdateForRename CoCreateInstance failed", |
| 103 | TRACE_EVENT_SCOPE_THREAD, "hr", hr); |
| 104 | LOG(ERROR) << "CoCreate ProcessLauncherClass failed; hr = " << std::hex |
| 105 | << hr; |
| 106 | return; |
| 107 | } |
| 108 | } |
S. Ganesh | cad4503 | 2024-07-18 17:43:11 | [diff] [blame] | 109 | const HRESULT hr = ::CoMarshalInterThreadInterfaceInStream( |
| 110 | __uuidof(IUnknown), unknown.Get(), &result->stream); |
| 111 | if (FAILED(hr)) { |
| 112 | TRACE_EVENT_INSTANT1("startup", |
| 113 | "InvokeGoogleUpdateForRename " |
| 114 | "CoMarshalInterThreadInterfaceInStream failed", |
| 115 | TRACE_EVENT_SCOPE_THREAD, "hr", hr); |
| 116 | LOG(ERROR) << "CoMarshalInterThreadInterfaceInStream " |
| 117 | "ProcessLauncherClass failed; hr = " |
| 118 | << std::hex << hr; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | // CoCreates the Google Update `ProcessLauncherClass` in a `ThreadPool` thread |
S. Ganesh | 4854086 | 2024-08-12 18:09:39 | [diff] [blame^] | 123 | // with a timeout, if the `ThreadPool` is operational. The starting value for |
| 124 | // the timeout is 15 seconds. If the CoCreate times out, the timeout is |
| 125 | // increased by 15 seconds at each failed attempt and persisted for the next |
| 126 | // attempt. |
| 127 | // |
| 128 | // If the `ThreadPool` is not operational, the CoCreate is done |
S. Ganesh | cad4503 | 2024-07-18 17:43:11 | [diff] [blame] | 129 | // without a timeout. |
S. Ganesh | cad4503 | 2024-07-18 17:43:11 | [diff] [blame] | 130 | Microsoft::WRL::ComPtr<IUnknown> CreateProcessLauncher() { |
S. Ganesh | 4854086 | 2024-08-12 18:09:39 | [diff] [blame^] | 131 | constexpr int kDefaultTimeoutIncrementSeconds = 15; |
| 132 | |
S. Ganesh | cad4503 | 2024-07-18 17:43:11 | [diff] [blame] | 133 | auto result = base::MakeRefCounted<CreateProcessLauncherResult>(); |
| 134 | if (base::ThreadPool::CreateCOMSTATaskRunner( |
| 135 | {base::MayBlock(), base::TaskPriority::USER_BLOCKING}) |
| 136 | ->PostTask(FROM_HERE, base::BindOnce(&CreateAndMarshalProcessLauncher, |
| 137 | result))) { |
S. Ganesh | 4854086 | 2024-08-12 18:09:39 | [diff] [blame^] | 138 | installer::PerInstallValue creation_timeout( |
| 139 | L"ProcessLauncherCreationTimeout"); |
| 140 | const base::TimeDelta timeout = base::Seconds( |
| 141 | creation_timeout.Get() |
| 142 | .value_or(base::Value(kDefaultTimeoutIncrementSeconds)) |
| 143 | .GetIfInt() |
| 144 | .value_or(kDefaultTimeoutIncrementSeconds)); |
| 145 | const base::ElapsedTimer timer; |
| 146 | if (!result->completion_event.TimedWait(timeout)) { |
| 147 | base::UmaHistogramMediumTimes( |
| 148 | "Startup.CreateProcessLauncher.TimedWaitFailed", timer.Elapsed()); |
| 149 | creation_timeout.Set(base::Value(static_cast<int>(timeout.InSeconds()) + |
| 150 | kDefaultTimeoutIncrementSeconds)); |
S. Ganesh | cad4503 | 2024-07-18 17:43:11 | [diff] [blame] | 151 | TRACE_EVENT_INSTANT0( |
| 152 | "startup", "InvokeGoogleUpdateForRename CoCreateInstance timed out", |
| 153 | TRACE_EVENT_SCOPE_THREAD); |
| 154 | LOG(ERROR) << "CoCreate ProcessLauncherClass timed out"; |
| 155 | return {}; |
| 156 | } |
| 157 | |
| 158 | if (!result->stream) { |
| 159 | return {}; |
| 160 | } |
S. Ganesh | 4854086 | 2024-08-12 18:09:39 | [diff] [blame^] | 161 | base::UmaHistogramMediumTimes( |
| 162 | "Startup.CreateProcessLauncher.TimedWaitSucceeded", timer.Elapsed()); |
S. Ganesh | cad4503 | 2024-07-18 17:43:11 | [diff] [blame] | 163 | |
| 164 | Microsoft::WRL::ComPtr<IUnknown> unknown; |
| 165 | const HRESULT hr = |
| 166 | ::CoUnmarshalInterface(result->stream.Get(), __uuidof(IUnknown), |
| 167 | IID_PPV_ARGS_Helper(&unknown)); |
| 168 | if (FAILED(hr)) { |
| 169 | TRACE_EVENT_INSTANT1( |
| 170 | "startup", "InvokeGoogleUpdateForRename CoUnmarshalInterface failed", |
| 171 | TRACE_EVENT_SCOPE_THREAD, "hr", hr); |
| 172 | LOG(ERROR) << "CoUnmarshalInterface ProcessLauncherClass failed; hr = " |
| 173 | << std::hex << hr; |
| 174 | return {}; |
| 175 | } |
| 176 | |
| 177 | return unknown; |
| 178 | } |
| 179 | |
| 180 | // The task could not be posted to the task runner, so CoCreate without a |
| 181 | // timeout. This could happen in shutdown, where the `ThreadPool` is not |
| 182 | // operational. |
| 183 | { |
| 184 | TRACE_EVENT0("startup", "InvokeGoogleUpdateForRename CoCreateInstance"); |
| 185 | Microsoft::WRL::ComPtr<IUnknown> unknown; |
| 186 | const HRESULT hr = |
| 187 | ::CoCreateInstance(__uuidof(ProcessLauncherClass), nullptr, CLSCTX_ALL, |
| 188 | IID_PPV_ARGS(&unknown)); |
| 189 | if (FAILED(hr)) { |
| 190 | TRACE_EVENT_INSTANT1( |
| 191 | "startup", "InvokeGoogleUpdateForRename CoCreateInstance failed", |
| 192 | TRACE_EVENT_SCOPE_THREAD, "hr", hr); |
| 193 | LOG(ERROR) << "CoCreate ProcessLauncherClass failed; hr = " << std::hex |
| 194 | << hr; |
| 195 | return {}; |
| 196 | } |
| 197 | |
| 198 | return unknown; |
| 199 | } |
| 200 | } |
| 201 | #endif // BUILDFLAG(GOOGLE_CHROME_BRANDING) |
| 202 | |
[email protected] | bc38c25 | 2011-04-12 21:46:57 | [diff] [blame] | 203 | bool InvokeGoogleUpdateForRename() { |
Nico Weber | eaa0841 | 2019-08-14 01:24:37 | [diff] [blame] | 204 | #if BUILDFLAG(GOOGLE_CHROME_BRANDING) |
Gabriel Charette | f7e8571 | 2021-12-10 20:46:22 | [diff] [blame] | 205 | // This has been identified as very slow on some startups. Detailed trace |
| 206 | // events below try to shine a light on each steps. crbug.com/1252004 |
Gabriel Charette | 12b58ac | 2021-09-22 18:08:55 | [diff] [blame] | 207 | TRACE_EVENT0("startup", "upgrade_util::InvokeGoogleUpdateForRename"); |
| 208 | |
S. Ganesh | cad4503 | 2024-07-18 17:43:11 | [diff] [blame] | 209 | Microsoft::WRL::ComPtr<IUnknown> unknown = CreateProcessLauncher(); |
| 210 | if (!unknown) { |
| 211 | return false; |
| 212 | } |
| 213 | |
S. Ganesh | ea7ef087 | 2024-03-21 16:28:32 | [diff] [blame] | 214 | // Chrome queries for the SxS IIDs first, with a fallback to the legacy IID, |
| 215 | // to make sure that marshaling loads the proxy/stub from the correct (HKLM) |
| 216 | // hive. |
Robert Liao | b2bc703d | 2017-10-17 20:52:35 | [diff] [blame] | 217 | Microsoft::WRL::ComPtr<IProcessLauncher> ipl; |
Gabriel Charette | f7e8571 | 2021-12-10 20:46:22 | [diff] [blame] | 218 | { |
S. Ganesh | cad4503 | 2024-07-18 17:43:11 | [diff] [blame] | 219 | HRESULT hr = unknown.CopyTo(__uuidof(IProcessLauncherSystem), |
| 220 | IID_PPV_ARGS_Helper(&ipl)); |
S. Ganesh | ea7ef087 | 2024-03-21 16:28:32 | [diff] [blame] | 221 | if (FAILED(hr)) { |
| 222 | hr = unknown.As(&ipl); |
| 223 | } |
| 224 | if (FAILED(hr)) { |
| 225 | TRACE_EVENT0("startup", |
| 226 | "InvokeGoogleUpdateForRename QueryInterface failed"); |
| 227 | LOG(ERROR) << "QueryInterface failed; hr = " << std::hex << hr; |
| 228 | return false; |
| 229 | } |
[email protected] | bc38c25 | 2011-04-12 21:46:57 | [diff] [blame] | 230 | } |
Greg Thompson | a1f9512 | 2019-01-31 22:46:45 | [diff] [blame] | 231 | |
S. Ganesh | 74432c1 | 2022-11-02 06:35:34 | [diff] [blame] | 232 | ULONG_PTR process_handle = 0; |
Gabriel Charette | f7e8571 | 2021-12-10 20:46:22 | [diff] [blame] | 233 | { |
| 234 | TRACE_EVENT0("startup", "InvokeGoogleUpdateForRename LaunchCmdElevated"); |
Dominique Fauteux-Chapleau | 269cabf | 2021-12-10 22:10:31 | [diff] [blame] | 235 | HRESULT hr = ipl->LaunchCmdElevated( |
S. Ganesh | f77e2a5 | 2022-11-22 18:40:19 | [diff] [blame] | 236 | install_static::GetAppGuid(), installer::kCmdRenameChromeExe, |
Dominique Fauteux-Chapleau | 269cabf | 2021-12-10 22:10:31 | [diff] [blame] | 237 | ::GetCurrentProcessId(), &process_handle); |
Gabriel Charette | f7e8571 | 2021-12-10 20:46:22 | [diff] [blame] | 238 | if (FAILED(hr)) { |
| 239 | TRACE_EVENT0("startup", |
| 240 | "InvokeGoogleUpdateForRename LaunchCmdElevated failed"); |
| 241 | LOG(ERROR) << "IProcessLauncher::LaunchCmdElevated failed; hr = " |
| 242 | << std::hex << hr; |
| 243 | return false; |
| 244 | } |
Greg Thompson | a1f9512 | 2019-01-31 22:46:45 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | base::Process rename_process( |
| 248 | reinterpret_cast<base::ProcessHandle>(process_handle)); |
| 249 | int exit_code; |
Gabriel Charette | f7e8571 | 2021-12-10 20:46:22 | [diff] [blame] | 250 | { |
| 251 | TRACE_EVENT0("startup", "InvokeGoogleUpdateForRename WaitForExit"); |
| 252 | if (!rename_process.WaitForExit(&exit_code)) { |
| 253 | TRACE_EVENT0("startup", "InvokeGoogleUpdateForRename WaitForExit failed"); |
| 254 | PLOG(ERROR) << "WaitForExit of rename process failed"; |
| 255 | return false; |
| 256 | } |
Greg Thompson | a1f9512 | 2019-01-31 22:46:45 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | if (exit_code != installer::RENAME_SUCCESSFUL) { |
Gabriel Charette | f7e8571 | 2021-12-10 20:46:22 | [diff] [blame] | 260 | TRACE_EVENT0("startup", "InvokeGoogleUpdateForRename !RENAME_SUCCESSFUL"); |
Greg Thompson | a1f9512 | 2019-01-31 22:46:45 | [diff] [blame] | 261 | LOG(ERROR) << "Rename process failed with exit code " << exit_code; |
| 262 | return false; |
| 263 | } |
| 264 | |
Gabriel Charette | 334775a | 2021-12-13 18:57:26 | [diff] [blame] | 265 | TRACE_EVENT0("startup", "InvokeGoogleUpdateForRename RENAME_SUCCESSFUL"); |
| 266 | |
Greg Thompson | a1f9512 | 2019-01-31 22:46:45 | [diff] [blame] | 267 | return true; |
Nico Weber | eaa0841 | 2019-08-14 01:24:37 | [diff] [blame] | 268 | #else // BUILDFLAG(GOOGLE_CHROME_BRANDING) |
[email protected] | bc38c25 | 2011-04-12 21:46:57 | [diff] [blame] | 269 | return false; |
Nico Weber | eaa0841 | 2019-08-14 01:24:37 | [diff] [blame] | 270 | #endif // BUILDFLAG(GOOGLE_CHROME_BRANDING) |
[email protected] | bc38c25 | 2011-04-12 21:46:57 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | } // namespace |
| 274 | |
[email protected] | 81585672 | 2011-04-13 17:19:19 | [diff] [blame] | 275 | namespace upgrade_util { |
| 276 | |
Greg Thompson | 87ee38f | 2019-08-09 06:00:23 | [diff] [blame] | 277 | bool RelaunchChromeBrowserImpl(const base::CommandLine& command_line) { |
Gabriel Charette | 12b58ac | 2021-09-22 18:08:55 | [diff] [blame] | 278 | TRACE_EVENT0("startup", "upgrade_util::RelaunchChromeBrowserImpl"); |
| 279 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 280 | base::FilePath chrome_exe; |
Avi Drissman | 9098f900 | 2018-05-04 00:11:52 | [diff] [blame] | 281 | if (!base::PathService::Get(base::FILE_EXE, &chrome_exe)) { |
Peter Boström | 3e79afa | 2024-05-15 00:49:26 | [diff] [blame] | 282 | NOTREACHED_IN_MIGRATION(); |
[email protected] | 3f69d6e61 | 2012-08-03 18:52:27 | [diff] [blame] | 283 | return false; |
| 284 | } |
| 285 | |
[email protected] | fdbea98d | 2014-05-16 19:29:20 | [diff] [blame] | 286 | // Explicitly make sure to relaunch chrome.exe rather than old_chrome.exe. |
| 287 | // This can happen when old_chrome.exe is launched by a user. |
avi | 556c0502 | 2014-12-22 23:31:43 | [diff] [blame] | 288 | base::CommandLine chrome_exe_command_line = command_line; |
[email protected] | fdbea98d | 2014-05-16 19:29:20 | [diff] [blame] | 289 | chrome_exe_command_line.SetProgram( |
| 290 | chrome_exe.DirName().Append(installer::kChromeExe)); |
| 291 | |
scottmg | 20920cb | 2016-04-07 16:23:59 | [diff] [blame] | 292 | // Set the working directory to the exe's directory. This avoids a handle to |
| 293 | // the version directory being kept open in the relaunched child process. |
| 294 | base::LaunchOptions launch_options; |
| 295 | launch_options.current_directory = chrome_exe.DirName(); |
Greg Thompson | 47faf20 | 2018-05-18 20:59:03 | [diff] [blame] | 296 | // Give the new process the right to bring its windows to the foreground. |
| 297 | launch_options.grant_foreground_privilege = true; |
scottmg | 20920cb | 2016-04-07 16:23:59 | [diff] [blame] | 298 | return base::LaunchProcess(chrome_exe_command_line, launch_options).IsValid(); |
[email protected] | 81585672 | 2011-04-13 17:19:19 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | bool IsUpdatePendingRestart() { |
Gabriel Charette | 12b58ac | 2021-09-22 18:08:55 | [diff] [blame] | 302 | TRACE_EVENT0("startup", "upgrade_util::IsUpdatePendingRestart"); |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 303 | base::FilePath new_chrome_exe; |
[email protected] | 81585672 | 2011-04-13 17:19:19 | [diff] [blame] | 304 | if (!GetNewerChromeFile(&new_chrome_exe)) |
| 305 | return false; |
[email protected] | 756748414 | 2013-07-11 17:36:07 | [diff] [blame] | 306 | return base::PathExists(new_chrome_exe); |
[email protected] | 81585672 | 2011-04-13 17:19:19 | [diff] [blame] | 307 | } |
| 308 | |
[email protected] | 81585672 | 2011-04-13 17:19:19 | [diff] [blame] | 309 | bool SwapNewChromeExeIfPresent() { |
grt | b35a90b | 2016-08-30 05:34:21 | [diff] [blame] | 310 | if (!IsUpdatePendingRestart()) |
[email protected] | bc38c25 | 2011-04-12 21:46:57 | [diff] [blame] | 311 | return false; |
grt | b35a90b | 2016-08-30 05:34:21 | [diff] [blame] | 312 | |
Gabriel Charette | 12b58ac | 2021-09-22 18:08:55 | [diff] [blame] | 313 | TRACE_EVENT0("startup", "upgrade_util::SwapNewChromeExeIfPresent"); |
| 314 | |
Etienne Bergeron | 7a0ef61 | 2023-09-15 18:28:08 | [diff] [blame] | 315 | // Renaming the chrome executable requires the process singleton to avoid |
| 316 | // any race condition. |
| 317 | CHECK(ChromeProcessSingleton::IsSingletonInstance()); |
| 318 | |
fdoray | e9c97a0 | 2016-10-04 12:05:16 | [diff] [blame] | 319 | // If this is a system-level install, ask Google Update to launch an elevated |
| 320 | // process to rename Chrome executables. |
Greg Thompson | d2efb1f | 2018-08-29 06:12:54 | [diff] [blame] | 321 | if (install_static::IsSystemInstall()) |
fdoray | e9c97a0 | 2016-10-04 12:05:16 | [diff] [blame] | 322 | return InvokeGoogleUpdateForRename(); |
[email protected] | bc38c25 | 2011-04-12 21:46:57 | [diff] [blame] | 323 | |
fdoray | e9c97a0 | 2016-10-04 12:05:16 | [diff] [blame] | 324 | // If this is a user-level install, directly launch a process to rename Chrome |
| 325 | // executables. Obtain the command to launch the process from the registry. |
S. Ganesh | f77e2a5 | 2022-11-22 18:40:19 | [diff] [blame] | 326 | installer::AppCommand rename_cmd(installer::kCmdRenameChromeExe, {}); |
| 327 | if (!rename_cmd.Initialize(HKEY_CURRENT_USER)) |
Greg Thompson | a1f9512 | 2019-01-31 22:46:45 | [diff] [blame] | 328 | return false; |
Greg Thompson | a1f9512 | 2019-01-31 22:46:45 | [diff] [blame] | 329 | |
| 330 | base::LaunchOptions options; |
| 331 | options.wait = true; |
| 332 | options.start_hidden = true; |
| 333 | ::SetLastError(ERROR_SUCCESS); |
S. Ganesh | f77e2a5 | 2022-11-22 18:40:19 | [diff] [blame] | 334 | base::Process process = |
| 335 | base::LaunchProcess(rename_cmd.command_line(), options); |
Greg Thompson | a1f9512 | 2019-01-31 22:46:45 | [diff] [blame] | 336 | if (!process.IsValid()) { |
| 337 | PLOG(ERROR) << "Launch rename process failed"; |
| 338 | return false; |
| 339 | } |
| 340 | |
| 341 | DWORD exit_code; |
| 342 | if (!::GetExitCodeProcess(process.Handle(), &exit_code)) { |
| 343 | PLOG(ERROR) << "GetExitCodeProcess of rename process failed"; |
| 344 | return false; |
| 345 | } |
| 346 | |
| 347 | if (exit_code != installer::RENAME_SUCCESSFUL) { |
| 348 | LOG(ERROR) << "Rename process failed with exit code " << exit_code; |
| 349 | return false; |
| 350 | } |
| 351 | |
| 352 | return true; |
[email protected] | bc38c25 | 2011-04-12 21:46:57 | [diff] [blame] | 353 | } |
| 354 | |
[email protected] | fdbea98d | 2014-05-16 19:29:20 | [diff] [blame] | 355 | bool IsRunningOldChrome() { |
Gabriel Charette | 12b58ac | 2021-09-22 18:08:55 | [diff] [blame] | 356 | TRACE_EVENT0("startup", "upgrade_util::IsRunningOldChrome"); |
[email protected] | fdbea98d | 2014-05-16 19:29:20 | [diff] [blame] | 357 | // This figures out the actual file name that the section containing the |
| 358 | // mapped exe refers to. This is used instead of GetModuleFileName because the |
| 359 | // .exe may have been renamed out from under us while we've been running which |
| 360 | // GetModuleFileName won't notice. |
| 361 | wchar_t mapped_file_name[MAX_PATH * 2] = {}; |
| 362 | |
| 363 | if (!::GetMappedFileName(::GetCurrentProcess(), |
| 364 | reinterpret_cast<void*>(::GetModuleHandle(NULL)), |
Daniel Cheng | 7d9e3d5 | 2022-02-26 09:03:24 | [diff] [blame] | 365 | mapped_file_name, std::size(mapped_file_name))) { |
[email protected] | fdbea98d | 2014-05-16 19:29:20 | [diff] [blame] | 366 | return false; |
| 367 | } |
| 368 | |
| 369 | base::FilePath file_name(base::FilePath(mapped_file_name).BaseName()); |
| 370 | return base::FilePath::CompareEqualIgnoreCase(file_name.value(), |
| 371 | installer::kChromeOldExe); |
| 372 | } |
| 373 | |
avi | 556c0502 | 2014-12-22 23:31:43 | [diff] [blame] | 374 | bool DoUpgradeTasks(const base::CommandLine& command_line) { |
Gabriel Charette | 12b58ac | 2021-09-22 18:08:55 | [diff] [blame] | 375 | TRACE_EVENT0("startup", "upgrade_util::DoUpgradeTasks"); |
Javier Flores Assad | 9c26f9b9 | 2022-05-21 01:20:43 | [diff] [blame] | 376 | // If there is no other instance already running then check if there is a |
| 377 | // pending update and complete it by performing the swap and then relaunch. |
Etienne Bergeron | 7a0ef61 | 2023-09-15 18:28:08 | [diff] [blame] | 378 | |
| 379 | // Upgrade tasks require the process singleton to avoid any race condition. |
| 380 | CHECK(ChromeProcessSingleton::IsSingletonInstance()); |
| 381 | |
Javier Flores Assad | 9c26f9b9 | 2022-05-21 01:20:43 | [diff] [blame] | 382 | bool did_swap = false; |
| 383 | if (!browser_util::IsBrowserAlreadyRunning()) |
| 384 | did_swap = SwapNewChromeExeIfPresent(); |
| 385 | |
| 386 | // We don't need to relaunch if we didn't swap and we aren't running stale |
| 387 | // binaries. |
| 388 | if (!did_swap && !IsRunningOldChrome()) { |
[email protected] | bc38c25 | 2011-04-12 21:46:57 | [diff] [blame] | 389 | return false; |
Gabriel Charette | 12b58ac | 2021-09-22 18:08:55 | [diff] [blame] | 390 | } |
Javier Flores Assad | 9c26f9b9 | 2022-05-21 01:20:43 | [diff] [blame] | 391 | |
[email protected] | bc38c25 | 2011-04-12 21:46:57 | [diff] [blame] | 392 | // At this point the chrome.exe has been swapped with the new one. |
Wissem Gamra | 8e67a95d | 2023-03-17 17:41:21 | [diff] [blame] | 393 | if (!RelaunchChromeBrowser(command_line)) { |
Javier Flores Assad | 9c26f9b9 | 2022-05-21 01:20:43 | [diff] [blame] | 394 | // The relaunch failed. Feel free to panic now. |
Peter Boström | 3e79afa | 2024-05-15 00:49:26 | [diff] [blame] | 395 | NOTREACHED_IN_MIGRATION(); |
[email protected] | bc38c25 | 2011-04-12 21:46:57 | [diff] [blame] | 396 | } |
| 397 | return true; |
| 398 | } |
| 399 | |
[email protected] | 81585672 | 2011-04-13 17:19:19 | [diff] [blame] | 400 | } // namespace upgrade_util |