[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 | |
[email protected] | 770c6d8 | 2012-09-06 22:21:32 | [diff] [blame] | 7 | #include <windows.h> |
robliao | a872e99 | 2017-05-18 06:36:19 | [diff] [blame] | 8 | #include <objbase.h> |
[email protected] | fdbea98d | 2014-05-16 19:29:20 | [diff] [blame] | 9 | #include <psapi.h> |
[email protected] | 770c6d8 | 2012-09-06 22:21:32 | [diff] [blame] | 10 | #include <shellapi.h> |
Robert Liao | b2bc703d | 2017-10-17 20:52:35 | [diff] [blame] | 11 | #include <wrl/client.h> |
[email protected] | 770c6d8 | 2012-09-06 22:21:32 | [diff] [blame] | 12 | |
[email protected] | bc38c25 | 2011-04-12 21:46:57 | [diff] [blame] | 13 | #include <algorithm> |
| 14 | #include <string> |
| 15 | |
| 16 | #include "base/base_paths.h" |
| 17 | #include "base/command_line.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 18 | #include "base/files/file_path.h" |
thestig | 18dfb7a5 | 2014-08-26 10:44:04 | [diff] [blame] | 19 | #include "base/files/file_util.h" |
[email protected] | bc38c25 | 2011-04-12 21:46:57 | [diff] [blame] | 20 | #include "base/logging.h" |
avi | 6846aef | 2015-12-26 01:09:38 | [diff] [blame] | 21 | #include "base/macros.h" |
[email protected] | bc38c25 | 2011-04-12 21:46:57 | [diff] [blame] | 22 | #include "base/path_service.h" |
[email protected] | d09a4ce1c | 2013-07-24 17:37:02 | [diff] [blame] | 23 | #include "base/process/launch.h" |
| 24 | #include "base/process/process_handle.h" |
[email protected] | 3ea1b18 | 2013-02-08 22:38:41 | [diff] [blame] | 25 | #include "base/strings/string_number_conversions.h" |
[email protected] | d883056 | 2013-06-10 22:01:54 | [diff] [blame] | 26 | #include "base/strings/string_util.h" |
[email protected] | bc38c25 | 2011-04-12 21:46:57 | [diff] [blame] | 27 | #include "base/win/registry.h" |
[email protected] | 770c6d8 | 2012-09-06 22:21:32 | [diff] [blame] | 28 | #include "base/win/windows_version.h" |
ananta | 069fc88 | 2014-09-13 01:22:12 | [diff] [blame] | 29 | #include "chrome/browser/browser_process.h" |
[email protected] | 3e08799 | 2011-04-14 22:28:12 | [diff] [blame] | 30 | #include "chrome/browser/first_run/upgrade_util_win.h" |
[email protected] | 3f69d6e61 | 2012-08-03 18:52:27 | [diff] [blame] | 31 | #include "chrome/browser/shell_integration.h" |
[email protected] | 80274b9 | 2011-07-15 17:20:38 | [diff] [blame] | 32 | #include "chrome/common/chrome_switches.h" |
ananta | 069fc88 | 2014-09-13 01:22:12 | [diff] [blame] | 33 | #include "chrome/common/pref_names.h" |
grt | 4474dad | 2017-02-27 21:00:46 | [diff] [blame] | 34 | #include "chrome/install_static/install_util.h" |
[email protected] | bc38c25 | 2011-04-12 21:46:57 | [diff] [blame] | 35 | #include "chrome/installer/util/browser_distribution.h" |
| 36 | #include "chrome/installer/util/google_update_constants.h" |
| 37 | #include "chrome/installer/util/install_util.h" |
[email protected] | bc38c25 | 2011-04-12 21:46:57 | [diff] [blame] | 38 | #include "chrome/installer/util/util_constants.h" |
brettw | b1fc1b8 | 2016-02-02 00:19:08 | [diff] [blame] | 39 | #include "components/prefs/pref_service.h" |
ananta | 069fc88 | 2014-09-13 01:22:12 | [diff] [blame] | 40 | #include "ui/base/ui_base_switches.h" |
[email protected] | bc38c25 | 2011-04-12 21:46:57 | [diff] [blame] | 41 | |
grt | 235b3f09 | 2015-05-27 21:42:48 | [diff] [blame] | 42 | #if defined(GOOGLE_CHROME_BUILD) |
| 43 | #include "google_update/google_update_idl.h" |
| 44 | #endif |
| 45 | |
[email protected] | bc38c25 | 2011-04-12 21:46:57 | [diff] [blame] | 46 | namespace { |
| 47 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 48 | bool GetNewerChromeFile(base::FilePath* path) { |
Avi Drissman | 9098f900 | 2018-05-04 00:11:52 | [diff] [blame^] | 49 | if (!base::PathService::Get(base::DIR_EXE, path)) |
[email protected] | bc38c25 | 2011-04-12 21:46:57 | [diff] [blame] | 50 | return false; |
| 51 | *path = path->Append(installer::kChromeNewExe); |
| 52 | return true; |
| 53 | } |
| 54 | |
| 55 | bool InvokeGoogleUpdateForRename() { |
grt | 235b3f09 | 2015-05-27 21:42:48 | [diff] [blame] | 56 | #if defined(GOOGLE_CHROME_BUILD) |
Robert Liao | b2bc703d | 2017-10-17 20:52:35 | [diff] [blame] | 57 | Microsoft::WRL::ComPtr<IProcessLauncher> ipl; |
robliao | a872e99 | 2017-05-18 06:36:19 | [diff] [blame] | 58 | if (!FAILED(::CoCreateInstance(__uuidof(ProcessLauncherClass), nullptr, |
| 59 | CLSCTX_ALL, IID_PPV_ARGS(&ipl)))) { |
[email protected] | bc38c25 | 2011-04-12 21:46:57 | [diff] [blame] | 60 | ULONG_PTR phandle = NULL; |
| 61 | DWORD id = GetCurrentProcessId(); |
grt | 4474dad | 2017-02-27 21:00:46 | [diff] [blame] | 62 | if (!FAILED(ipl->LaunchCmdElevated(install_static::GetAppGuid(), |
| 63 | google_update::kRegRenameCmdField, id, |
[email protected] | bc38c25 | 2011-04-12 21:46:57 | [diff] [blame] | 64 | &phandle))) { |
| 65 | HANDLE handle = HANDLE(phandle); |
| 66 | WaitForSingleObject(handle, INFINITE); |
| 67 | DWORD exit_code; |
| 68 | ::GetExitCodeProcess(handle, &exit_code); |
| 69 | ::CloseHandle(handle); |
| 70 | if (exit_code == installer::RENAME_SUCCESSFUL) |
| 71 | return true; |
| 72 | } |
| 73 | } |
grt | 235b3f09 | 2015-05-27 21:42:48 | [diff] [blame] | 74 | #endif // GOOGLE_CHROME_BUILD |
[email protected] | bc38c25 | 2011-04-12 21:46:57 | [diff] [blame] | 75 | return false; |
| 76 | } |
| 77 | |
| 78 | } // namespace |
| 79 | |
[email protected] | 81585672 | 2011-04-13 17:19:19 | [diff] [blame] | 80 | namespace upgrade_util { |
| 81 | |
scottmg | 812f1acf | 2016-03-18 23:09:05 | [diff] [blame] | 82 | bool RelaunchChromeBrowser(const base::CommandLine& command_line) { |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 83 | base::FilePath chrome_exe; |
Avi Drissman | 9098f900 | 2018-05-04 00:11:52 | [diff] [blame^] | 84 | if (!base::PathService::Get(base::FILE_EXE, &chrome_exe)) { |
[email protected] | 3f69d6e61 | 2012-08-03 18:52:27 | [diff] [blame] | 85 | NOTREACHED(); |
| 86 | return false; |
| 87 | } |
| 88 | |
[email protected] | fdbea98d | 2014-05-16 19:29:20 | [diff] [blame] | 89 | // Explicitly make sure to relaunch chrome.exe rather than old_chrome.exe. |
| 90 | // This can happen when old_chrome.exe is launched by a user. |
avi | 556c0502 | 2014-12-22 23:31:43 | [diff] [blame] | 91 | base::CommandLine chrome_exe_command_line = command_line; |
[email protected] | fdbea98d | 2014-05-16 19:29:20 | [diff] [blame] | 92 | chrome_exe_command_line.SetProgram( |
| 93 | chrome_exe.DirName().Append(installer::kChromeExe)); |
| 94 | |
scottmg | 20920cb | 2016-04-07 16:23:59 | [diff] [blame] | 95 | // Set the working directory to the exe's directory. This avoids a handle to |
| 96 | // the version directory being kept open in the relaunched child process. |
| 97 | base::LaunchOptions launch_options; |
| 98 | launch_options.current_directory = chrome_exe.DirName(); |
| 99 | return base::LaunchProcess(chrome_exe_command_line, launch_options).IsValid(); |
[email protected] | 81585672 | 2011-04-13 17:19:19 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | bool IsUpdatePendingRestart() { |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 103 | base::FilePath new_chrome_exe; |
[email protected] | 81585672 | 2011-04-13 17:19:19 | [diff] [blame] | 104 | if (!GetNewerChromeFile(&new_chrome_exe)) |
| 105 | return false; |
[email protected] | 756748414 | 2013-07-11 17:36:07 | [diff] [blame] | 106 | return base::PathExists(new_chrome_exe); |
[email protected] | 81585672 | 2011-04-13 17:19:19 | [diff] [blame] | 107 | } |
| 108 | |
[email protected] | 81585672 | 2011-04-13 17:19:19 | [diff] [blame] | 109 | bool SwapNewChromeExeIfPresent() { |
grt | b35a90b | 2016-08-30 05:34:21 | [diff] [blame] | 110 | if (!IsUpdatePendingRestart()) |
[email protected] | bc38c25 | 2011-04-12 21:46:57 | [diff] [blame] | 111 | return false; |
grt | b35a90b | 2016-08-30 05:34:21 | [diff] [blame] | 112 | |
fdoray | e9c97a0 | 2016-10-04 12:05:16 | [diff] [blame] | 113 | // If this is a system-level install, ask Google Update to launch an elevated |
| 114 | // process to rename Chrome executables. |
grt | 973412f | 2017-02-25 08:04:21 | [diff] [blame] | 115 | if (!InstallUtil::IsPerUserInstall()) |
fdoray | e9c97a0 | 2016-10-04 12:05:16 | [diff] [blame] | 116 | return InvokeGoogleUpdateForRename(); |
[email protected] | bc38c25 | 2011-04-12 21:46:57 | [diff] [blame] | 117 | |
fdoray | e9c97a0 | 2016-10-04 12:05:16 | [diff] [blame] | 118 | // If this is a user-level install, directly launch a process to rename Chrome |
| 119 | // executables. Obtain the command to launch the process from the registry. |
[email protected] | bc38c25 | 2011-04-12 21:46:57 | [diff] [blame] | 120 | base::win::RegKey key; |
fdoray | e9c97a0 | 2016-10-04 12:05:16 | [diff] [blame] | 121 | if (key.Open(HKEY_CURRENT_USER, |
| 122 | BrowserDistribution::GetDistribution()->GetVersionKey().c_str(), |
grt | b35a90b | 2016-08-30 05:34:21 | [diff] [blame] | 123 | KEY_QUERY_VALUE | KEY_WOW64_32KEY) == ERROR_SUCCESS) { |
[email protected] | 80274b9 | 2011-07-15 17:20:38 | [diff] [blame] | 124 | std::wstring rename_cmd; |
| 125 | if (key.ReadValue(google_update::kRegRenameCmdField, |
| 126 | &rename_cmd) == ERROR_SUCCESS) { |
[email protected] | 89a5616 | 2011-07-18 21:38:02 | [diff] [blame] | 127 | base::LaunchOptions options; |
| 128 | options.wait = true; |
| 129 | options.start_hidden = true; |
rvargas | 6181277 | 2014-12-05 03:14:54 | [diff] [blame] | 130 | base::Process process = base::LaunchProcess(rename_cmd, options); |
| 131 | if (process.IsValid()) { |
[email protected] | 80274b9 | 2011-07-15 17:20:38 | [diff] [blame] | 132 | DWORD exit_code; |
rvargas | 6181277 | 2014-12-05 03:14:54 | [diff] [blame] | 133 | ::GetExitCodeProcess(process.Handle(), &exit_code); |
[email protected] | 80274b9 | 2011-07-15 17:20:38 | [diff] [blame] | 134 | if (exit_code == installer::RENAME_SUCCESSFUL) |
| 135 | return true; |
| 136 | } |
[email protected] | bc38c25 | 2011-04-12 21:46:57 | [diff] [blame] | 137 | } |
| 138 | } |
| 139 | |
grt | b35a90b | 2016-08-30 05:34:21 | [diff] [blame] | 140 | return false; |
[email protected] | bc38c25 | 2011-04-12 21:46:57 | [diff] [blame] | 141 | } |
| 142 | |
[email protected] | fdbea98d | 2014-05-16 19:29:20 | [diff] [blame] | 143 | bool IsRunningOldChrome() { |
| 144 | // This figures out the actual file name that the section containing the |
| 145 | // mapped exe refers to. This is used instead of GetModuleFileName because the |
| 146 | // .exe may have been renamed out from under us while we've been running which |
| 147 | // GetModuleFileName won't notice. |
| 148 | wchar_t mapped_file_name[MAX_PATH * 2] = {}; |
| 149 | |
| 150 | if (!::GetMappedFileName(::GetCurrentProcess(), |
| 151 | reinterpret_cast<void*>(::GetModuleHandle(NULL)), |
| 152 | mapped_file_name, |
| 153 | arraysize(mapped_file_name))) { |
| 154 | return false; |
| 155 | } |
| 156 | |
| 157 | base::FilePath file_name(base::FilePath(mapped_file_name).BaseName()); |
| 158 | return base::FilePath::CompareEqualIgnoreCase(file_name.value(), |
| 159 | installer::kChromeOldExe); |
| 160 | } |
| 161 | |
avi | 556c0502 | 2014-12-22 23:31:43 | [diff] [blame] | 162 | bool DoUpgradeTasks(const base::CommandLine& command_line) { |
[email protected] | fdbea98d | 2014-05-16 19:29:20 | [diff] [blame] | 163 | if (!SwapNewChromeExeIfPresent() && !IsRunningOldChrome()) |
[email protected] | bc38c25 | 2011-04-12 21:46:57 | [diff] [blame] | 164 | return false; |
| 165 | // At this point the chrome.exe has been swapped with the new one. |
[email protected] | 81585672 | 2011-04-13 17:19:19 | [diff] [blame] | 166 | if (!RelaunchChromeBrowser(command_line)) { |
[email protected] | bc38c25 | 2011-04-12 21:46:57 | [diff] [blame] | 167 | // The re-launch fails. Feel free to panic now. |
| 168 | NOTREACHED(); |
| 169 | } |
| 170 | return true; |
| 171 | } |
| 172 | |
[email protected] | 81585672 | 2011-04-13 17:19:19 | [diff] [blame] | 173 | } // namespace upgrade_util |