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