blob: 97cd4d1365eda7480c07c3b52c339515ca3acb40 [file] [log] [blame]
[email protected]cec99842012-02-10 03:24:231// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]bc38c252011-04-12 21:46:572// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]815856722011-04-13 17:19:195#include "chrome/browser/first_run/upgrade_util.h"
[email protected]bc38c252011-04-12 21:46:576
Nico Webereaa08412019-08-14 01:24:377// Must be first.
[email protected]770c6d82012-09-06 22:21:328#include <windows.h>
Nico Webereaa08412019-08-14 01:24:379
robliaoa872e992017-05-18 06:36:1910#include <objbase.h>
[email protected]fdbea98d2014-05-16 19:29:2011#include <psapi.h>
[email protected]770c6d82012-09-06 22:21:3212#include <shellapi.h>
Robert Liaob2bc703d2017-10-17 20:52:3513#include <wrl/client.h>
[email protected]770c6d82012-09-06 22:21:3214
[email protected]bc38c252011-04-12 21:46:5715#include <algorithm>
Greg Thompsona1f95122019-01-31 22:46:4516#include <ios>
[email protected]bc38c252011-04-12 21:46:5717#include <string>
18
19#include "base/base_paths.h"
20#include "base/command_line.h"
[email protected]57999812013-02-24 05:40:5221#include "base/files/file_path.h"
thestig18dfb7a52014-08-26 10:44:0422#include "base/files/file_util.h"
[email protected]bc38c252011-04-12 21:46:5723#include "base/logging.h"
[email protected]bc38c252011-04-12 21:46:5724#include "base/path_service.h"
[email protected]d09a4ce1c2013-07-24 17:37:0225#include "base/process/launch.h"
26#include "base/process/process_handle.h"
Avi Drissman5f0fb8c2018-12-25 23:20:4927#include "base/stl_util.h"
[email protected]3ea1b182013-02-08 22:38:4128#include "base/strings/string_number_conversions.h"
[email protected]d8830562013-06-10 22:01:5429#include "base/strings/string_util.h"
[email protected]bc38c252011-04-12 21:46:5730#include "base/win/registry.h"
[email protected]770c6d82012-09-06 22:21:3231#include "base/win/windows_version.h"
Nico Webereaa08412019-08-14 01:24:3732#include "build/branding_buildflags.h"
ananta069fc882014-09-13 01:22:1233#include "chrome/browser/browser_process.h"
[email protected]3e087992011-04-14 22:28:1234#include "chrome/browser/first_run/upgrade_util_win.h"
[email protected]3f69d6e612012-08-03 18:52:2735#include "chrome/browser/shell_integration.h"
[email protected]80274b92011-07-15 17:20:3836#include "chrome/common/chrome_switches.h"
ananta069fc882014-09-13 01:22:1237#include "chrome/common/pref_names.h"
grt4474dad2017-02-27 21:00:4638#include "chrome/install_static/install_util.h"
[email protected]bc38c252011-04-12 21:46:5739#include "chrome/installer/util/google_update_constants.h"
[email protected]bc38c252011-04-12 21:46:5740#include "chrome/installer/util/util_constants.h"
brettwb1fc1b82016-02-02 00:19:0841#include "components/prefs/pref_service.h"
ananta069fc882014-09-13 01:22:1242#include "ui/base/ui_base_switches.h"
[email protected]bc38c252011-04-12 21:46:5743
Nico Webereaa08412019-08-14 01:24:3744#if BUILDFLAG(GOOGLE_CHROME_BRANDING)
grt235b3f092015-05-27 21:42:4845#include "google_update/google_update_idl.h"
46#endif
47
[email protected]bc38c252011-04-12 21:46:5748namespace {
49
[email protected]650b2d52013-02-10 03:41:4550bool GetNewerChromeFile(base::FilePath* path) {
Avi Drissman9098f9002018-05-04 00:11:5251 if (!base::PathService::Get(base::DIR_EXE, path))
[email protected]bc38c252011-04-12 21:46:5752 return false;
53 *path = path->Append(installer::kChromeNewExe);
54 return true;
55}
56
57bool InvokeGoogleUpdateForRename() {
Nico Webereaa08412019-08-14 01:24:3758#if BUILDFLAG(GOOGLE_CHROME_BRANDING)
Robert Liaob2bc703d2017-10-17 20:52:3559 Microsoft::WRL::ComPtr<IProcessLauncher> ipl;
Greg Thompsona1f95122019-01-31 22:46:4560 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]bc38c252011-04-12 21:46:5766 }
Greg Thompsona1f95122019-01-31 22:46:4567
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 Webereaa08412019-08-14 01:24:3792#else // BUILDFLAG(GOOGLE_CHROME_BRANDING)
[email protected]bc38c252011-04-12 21:46:5793 return false;
Nico Webereaa08412019-08-14 01:24:3794#endif // BUILDFLAG(GOOGLE_CHROME_BRANDING)
[email protected]bc38c252011-04-12 21:46:5795}
96
97} // namespace
98
[email protected]815856722011-04-13 17:19:1999namespace upgrade_util {
100
Greg Thompson87ee38f2019-08-09 06:00:23101bool RelaunchChromeBrowserImpl(const base::CommandLine& command_line) {
[email protected]650b2d52013-02-10 03:41:45102 base::FilePath chrome_exe;
Avi Drissman9098f9002018-05-04 00:11:52103 if (!base::PathService::Get(base::FILE_EXE, &chrome_exe)) {
[email protected]3f69d6e612012-08-03 18:52:27104 NOTREACHED();
105 return false;
106 }
107
[email protected]fdbea98d2014-05-16 19:29:20108 // 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.
avi556c05022014-12-22 23:31:43110 base::CommandLine chrome_exe_command_line = command_line;
[email protected]fdbea98d2014-05-16 19:29:20111 chrome_exe_command_line.SetProgram(
112 chrome_exe.DirName().Append(installer::kChromeExe));
113
scottmg20920cb2016-04-07 16:23:59114 // 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 Thompson47faf202018-05-18 20:59:03118 // Give the new process the right to bring its windows to the foreground.
119 launch_options.grant_foreground_privilege = true;
scottmg20920cb2016-04-07 16:23:59120 return base::LaunchProcess(chrome_exe_command_line, launch_options).IsValid();
[email protected]815856722011-04-13 17:19:19121}
122
123bool IsUpdatePendingRestart() {
[email protected]650b2d52013-02-10 03:41:45124 base::FilePath new_chrome_exe;
[email protected]815856722011-04-13 17:19:19125 if (!GetNewerChromeFile(&new_chrome_exe))
126 return false;
[email protected]7567484142013-07-11 17:36:07127 return base::PathExists(new_chrome_exe);
[email protected]815856722011-04-13 17:19:19128}
129
[email protected]815856722011-04-13 17:19:19130bool SwapNewChromeExeIfPresent() {
grtb35a90b2016-08-30 05:34:21131 if (!IsUpdatePendingRestart())
[email protected]bc38c252011-04-12 21:46:57132 return false;
grtb35a90b2016-08-30 05:34:21133
fdoraye9c97a02016-10-04 12:05:16134 // If this is a system-level install, ask Google Update to launch an elevated
135 // process to rename Chrome executables.
Greg Thompsond2efb1f2018-08-29 06:12:54136 if (install_static::IsSystemInstall())
fdoraye9c97a02016-10-04 12:05:16137 return InvokeGoogleUpdateForRename();
[email protected]bc38c252011-04-12 21:46:57138
fdoraye9c97a02016-10-04 12:05:16139 // 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]bc38c252011-04-12 21:46:57141 base::win::RegKey key;
Greg Thompsona1f95122019-01-31 22:46:45142 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]bc38c252011-04-12 21:46:57149 }
150
Greg Thompsona1f95122019-01-31 22:46:45151 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]bc38c252011-04-12 21:46:57181}
182
[email protected]fdbea98d2014-05-16 19:29:20183bool 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 Drissman5f0fb8c2018-12-25 23:20:49192 mapped_file_name, base::size(mapped_file_name))) {
[email protected]fdbea98d2014-05-16 19:29:20193 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
avi556c05022014-12-22 23:31:43201bool DoUpgradeTasks(const base::CommandLine& command_line) {
[email protected]fdbea98d2014-05-16 19:29:20202 if (!SwapNewChromeExeIfPresent() && !IsRunningOldChrome())
[email protected]bc38c252011-04-12 21:46:57203 return false;
204 // At this point the chrome.exe has been swapped with the new one.
[email protected]815856722011-04-13 17:19:19205 if (!RelaunchChromeBrowser(command_line)) {
[email protected]bc38c252011-04-12 21:46:57206 // The re-launch fails. Feel free to panic now.
207 NOTREACHED();
208 }
209 return true;
210}
211
[email protected]815856722011-04-13 17:19:19212} // namespace upgrade_util