blob: 035ab2e5b80a0142b3d6dc506300414fed6665d0 [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
[email protected]770c6d82012-09-06 22:21:327#include <windows.h>
[email protected]fdbea98d2014-05-16 19:29:208#include <psapi.h>
[email protected]770c6d82012-09-06 22:21:329#include <shellapi.h>
10
[email protected]bc38c252011-04-12 21:46:5711#include <algorithm>
12#include <string>
13
14#include "base/base_paths.h"
15#include "base/command_line.h"
[email protected]57999812013-02-24 05:40:5216#include "base/files/file_path.h"
thestig18dfb7a52014-08-26 10:44:0417#include "base/files/file_util.h"
[email protected]bc38c252011-04-12 21:46:5718#include "base/logging.h"
avi6846aef2015-12-26 01:09:3819#include "base/macros.h"
[email protected]bc38c252011-04-12 21:46:5720#include "base/path_service.h"
[email protected]d09a4ce1c2013-07-24 17:37:0221#include "base/process/launch.h"
22#include "base/process/process_handle.h"
[email protected]3ea1b182013-02-08 22:38:4123#include "base/strings/string_number_conversions.h"
[email protected]d8830562013-06-10 22:01:5424#include "base/strings/string_util.h"
25#include "base/strings/stringprintf.h"
[email protected]bc38c252011-04-12 21:46:5726#include "base/win/registry.h"
27#include "base/win/scoped_comptr.h"
[email protected]770c6d82012-09-06 22:21:3228#include "base/win/windows_version.h"
ananta069fc882014-09-13 01:22:1229#include "chrome/browser/browser_process.h"
[email protected]3e087992011-04-14 22:28:1230#include "chrome/browser/first_run/upgrade_util_win.h"
[email protected]3f69d6e612012-08-03 18:52:2731#include "chrome/browser/shell_integration.h"
[email protected]80274b92011-07-15 17:20:3832#include "chrome/common/chrome_switches.h"
ananta069fc882014-09-13 01:22:1233#include "chrome/common/pref_names.h"
[email protected]bc38c252011-04-12 21:46:5734#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"
brettwb1fc1b82016-02-02 00:19:0839#include "components/prefs/pref_service.h"
ananta069fc882014-09-13 01:22:1240#include "ui/base/ui_base_switches.h"
[email protected]bc38c252011-04-12 21:46:5741
grt235b3f092015-05-27 21:42:4842#if defined(GOOGLE_CHROME_BUILD)
43#include "google_update/google_update_idl.h"
44#endif
45
[email protected]bc38c252011-04-12 21:46:5746namespace {
47
[email protected]650b2d52013-02-10 03:41:4548bool GetNewerChromeFile(base::FilePath* path) {
[email protected]bc38c252011-04-12 21:46:5749 if (!PathService::Get(base::DIR_EXE, path))
50 return false;
51 *path = path->Append(installer::kChromeNewExe);
52 return true;
53}
54
55bool InvokeGoogleUpdateForRename() {
grt235b3f092015-05-27 21:42:4856#if defined(GOOGLE_CHROME_BUILD)
[email protected]bc38c252011-04-12 21:46:5757 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 }
grt235b3f092015-05-27 21:42:4875#endif // GOOGLE_CHROME_BUILD
[email protected]bc38c252011-04-12 21:46:5776 return false;
77}
78
79} // namespace
80
[email protected]815856722011-04-13 17:19:1981namespace upgrade_util {
82
scottmg812f1acf2016-03-18 23:09:0583bool RelaunchChromeBrowser(const base::CommandLine& command_line) {
[email protected]650b2d52013-02-10 03:41:4584 base::FilePath chrome_exe;
[email protected]3f69d6e612012-08-03 18:52:2785 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) {
86 NOTREACHED();
87 return false;
88 }
89
[email protected]fdbea98d2014-05-16 19:29:2090 // 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.
avi556c05022014-12-22 23:31:4392 base::CommandLine chrome_exe_command_line = command_line;
[email protected]fdbea98d2014-05-16 19:29:2093 chrome_exe_command_line.SetProgram(
94 chrome_exe.DirName().Append(installer::kChromeExe));
95
scottmg20920cb2016-04-07 16:23:5996 // 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]815856722011-04-13 17:19:19101}
102
[email protected]815856722011-04-13 17:19:19103bool IsUpdatePendingRestart() {
[email protected]650b2d52013-02-10 03:41:45104 base::FilePath new_chrome_exe;
[email protected]815856722011-04-13 17:19:19105 if (!GetNewerChromeFile(&new_chrome_exe))
106 return false;
[email protected]7567484142013-07-11 17:36:07107 return base::PathExists(new_chrome_exe);
[email protected]815856722011-04-13 17:19:19108}
109
[email protected]815856722011-04-13 17:19:19110bool SwapNewChromeExeIfPresent() {
grtb35a90b2016-08-30 05:34:21111 if (!IsUpdatePendingRestart())
[email protected]bc38c252011-04-12 21:46:57112 return false;
[email protected]650b2d52013-02-10 03:41:45113 base::FilePath cur_chrome_exe;
[email protected]bc38c252011-04-12 21:46:57114 if (!PathService::Get(base::FILE_EXE, &cur_chrome_exe))
115 return false;
grtb35a90b2016-08-30 05:34:21116 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]bc38c252011-04-12 21:46:57122
[email protected]80274b92011-07-15 17:20:38123 // Open up the registry key containing current version and rename information.
[email protected]bc38c252011-04-12 21:46:57124 HKEY reg_root = user_install ? HKEY_CURRENT_USER : HKEY_LOCAL_MACHINE;
125 BrowserDistribution *dist = BrowserDistribution::GetDistribution();
126 base::win::RegKey key;
[email protected]80274b92011-07-15 17:20:38127 if (key.Open(reg_root, dist->GetVersionKey().c_str(),
grtb35a90b2016-08-30 05:34:21128 KEY_QUERY_VALUE | KEY_WOW64_32KEY) == ERROR_SUCCESS) {
[email protected]80274b92011-07-15 17:20:38129 // 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]89a56162011-07-18 21:38:02133 base::LaunchOptions options;
134 options.wait = true;
135 options.start_hidden = true;
rvargas61812772014-12-05 03:14:54136 base::Process process = base::LaunchProcess(rename_cmd, options);
137 if (process.IsValid()) {
[email protected]80274b92011-07-15 17:20:38138 DWORD exit_code;
rvargas61812772014-12-05 03:14:54139 ::GetExitCodeProcess(process.Handle(), &exit_code);
[email protected]80274b92011-07-15 17:20:38140 if (exit_code == installer::RENAME_SUCCESSFUL)
141 return true;
142 }
[email protected]bc38c252011-04-12 21:46:57143 }
144 }
145
grtb35a90b2016-08-30 05:34:21146 return false;
[email protected]bc38c252011-04-12 21:46:57147}
148
[email protected]fdbea98d2014-05-16 19:29:20149bool 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
avi556c05022014-12-22 23:31:43168bool DoUpgradeTasks(const base::CommandLine& command_line) {
[email protected]fdbea98d2014-05-16 19:29:20169 if (!SwapNewChromeExeIfPresent() && !IsRunningOldChrome())
[email protected]bc38c252011-04-12 21:46:57170 return false;
171 // At this point the chrome.exe has been swapped with the new one.
[email protected]815856722011-04-13 17:19:19172 if (!RelaunchChromeBrowser(command_line)) {
[email protected]bc38c252011-04-12 21:46:57173 // The re-launch fails. Feel free to panic now.
174 NOTREACHED();
175 }
176 return true;
177}
178
[email protected]815856722011-04-13 17:19:19179} // namespace upgrade_util