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