| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/first_run/upgrade_util.h" | 5 #include "chrome/browser/first_run/upgrade_util.h" |
| 6 | 6 |
| 7 #include <shellapi.h> |
| 8 |
| 7 #include <algorithm> | 9 #include <algorithm> |
| 8 #include <string> | 10 #include <string> |
| 9 | 11 |
| 10 #include "base/base_paths.h" | 12 #include "base/base_paths.h" |
| 11 #include "base/command_line.h" | 13 #include "base/command_line.h" |
| 12 #include "base/environment.h" | 14 #include "base/environment.h" |
| 13 #include "base/file_path.h" | 15 #include "base/file_path.h" |
| 14 #include "base/file_util.h" | 16 #include "base/file_util.h" |
| 15 #include "base/logging.h" | 17 #include "base/logging.h" |
| 16 #include "base/path_service.h" | 18 #include "base/path_service.h" |
| 17 #include "base/process_util.h" | 19 #include "base/process_util.h" |
| 18 #include "base/string_number_conversions.h" | 20 #include "base/string_number_conversions.h" |
| 19 #include "base/string_util.h" | 21 #include "base/string_util.h" |
| 22 #include "base/stringprintf.h" |
| 20 #include "base/win/metro.h" | 23 #include "base/win/metro.h" |
| 21 #include "base/win/registry.h" | 24 #include "base/win/registry.h" |
| 22 #include "base/win/scoped_comptr.h" | 25 #include "base/win/scoped_comptr.h" |
| 26 #include "base/win/windows_version.h" |
| 23 #include "chrome/browser/first_run/upgrade_util_win.h" | 27 #include "chrome/browser/first_run/upgrade_util_win.h" |
| 24 #include "chrome/browser/shell_integration.h" | 28 #include "chrome/browser/shell_integration.h" |
| 25 #include "chrome/common/chrome_constants.h" | 29 #include "chrome/common/chrome_constants.h" |
| 26 #include "chrome/common/chrome_switches.h" | 30 #include "chrome/common/chrome_switches.h" |
| 27 #include "chrome/installer/util/browser_distribution.h" | 31 #include "chrome/installer/util/browser_distribution.h" |
| 28 #include "chrome/installer/util/google_update_constants.h" | 32 #include "chrome/installer/util/google_update_constants.h" |
| 29 #include "chrome/installer/util/install_util.h" | 33 #include "chrome/installer/util/install_util.h" |
| 30 #include "chrome/installer/util/shell_util.h" | 34 #include "chrome/installer/util/shell_util.h" |
| 31 #include "chrome/installer/util/util_constants.h" | 35 #include "chrome/installer/util/util_constants.h" |
| 32 #include "google_update/google_update_idl.h" | 36 #include "google_update/google_update_idl.h" |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 72 if (!version_str.empty()) | 76 if (!version_str.empty()) |
| 73 path = path.AppendASCII(version_str); | 77 path = path.AppendASCII(version_str); |
| 74 | 78 |
| 75 return path.Append(installer::kDelegateExecuteExe); | 79 return path.Append(installer::kDelegateExecuteExe); |
| 76 } | 80 } |
| 77 | 81 |
| 78 } // namespace | 82 } // namespace |
| 79 | 83 |
| 80 namespace upgrade_util { | 84 namespace upgrade_util { |
| 81 | 85 |
| 82 bool RelaunchChromeBrowser(const CommandLine& command_line) { | 86 bool RelaunchChromehelper(const CommandLine& command_line, bool mode_switch) { |
| 83 scoped_ptr<base::Environment> env(base::Environment::Create()); | 87 scoped_ptr<base::Environment> env(base::Environment::Create()); |
| 84 std::string version_str; | 88 std::string version_str; |
| 85 | 89 |
| 86 // Get the version variable and remove it from the environment. | 90 // Get the version variable and remove it from the environment. |
| 87 if (env->GetVar(chrome::kChromeVersionEnvVar, &version_str)) | 91 if (env->GetVar(chrome::kChromeVersionEnvVar, &version_str)) |
| 88 env->UnSetVar(chrome::kChromeVersionEnvVar); | 92 env->UnSetVar(chrome::kChromeVersionEnvVar); |
| 89 else | 93 else |
| 90 version_str.clear(); | 94 version_str.clear(); |
| 91 | 95 |
| 92 if (!base::win::IsMetroProcess()) | 96 if (base::win::GetVersion() < base::win::VERSION_WIN8) |
| 93 return base::LaunchProcess(command_line, base::LaunchOptions(), NULL); | 97 return base::LaunchProcess(command_line, base::LaunchOptions(), NULL); |
| 94 | 98 |
| 95 // Pass this Chrome's Start Menu shortcut path and handle to the relauncher. | 99 // On Windows 8 we always use the delegate_execute for re-launching chrome. |
| 96 // The relauncher will wait for this Chrome to exit then reactivate it by | 100 // |
| 97 // way of the Start Menu shortcut. | 101 // Pass this Chrome's Start Menu shortcut path to the relauncher so it can |
| 102 // re-activate chrome via ShellExecute. |
| 98 FilePath chrome_exe; | 103 FilePath chrome_exe; |
| 99 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) { | 104 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) { |
| 100 NOTREACHED(); | 105 NOTREACHED(); |
| 101 return false; | 106 return false; |
| 102 } | 107 } |
| 103 | 108 |
| 104 // Get a handle to this process that can be passed to the relauncher. | 109 // We need to use ShellExecute to launch the relauncher, which will wait until |
| 105 HANDLE current_process = GetCurrentProcess(); | 110 // we exit. But ShellExecute does not support handle passing to the child |
| 106 base::win::ScopedHandle process_handle; | 111 // process so we create a uniquely named mutex that we aquire and never |
| 107 if (!::DuplicateHandle(current_process, current_process, current_process, | 112 // release. So when we exit, Windows marks our mutex as abandoned and the |
| 108 process_handle.Receive(), SYNCHRONIZE, TRUE, 0)) { | 113 // wait is satisfied. |
| 109 DPCHECK(false); | 114 // The format of the named mutex is important. See DelegateExecuteOperation |
| 115 // for more details. |
| 116 string16 mutex_name = |
| 117 base::StringPrintf(L"chrome.relaunch.%d", ::GetCurrentProcessId()); |
| 118 HANDLE mutex = ::CreateMutexW(NULL, TRUE, mutex_name.c_str()); |
| 119 // The |mutex| handle needs to be leaked. See comment above. |
| 120 if (!mutex) { |
| 121 NOTREACHED(); |
| 122 return false; |
| 123 } |
| 124 if (::GetLastError() == ERROR_ALREADY_EXISTS) { |
| 125 NOTREACHED() << "Relaunch mutex already exists"; |
| 110 return false; | 126 return false; |
| 111 } | 127 } |
| 112 | 128 |
| 113 // Make the command line for the relauncher. | 129 CommandLine relaunch_cmd(CommandLine::NO_PROGRAM); |
| 114 CommandLine relaunch_cmd(GetMetroRelauncherPath(chrome_exe, version_str)); | |
| 115 relaunch_cmd.AppendSwitchPath(switches::kRelaunchShortcut, | 130 relaunch_cmd.AppendSwitchPath(switches::kRelaunchShortcut, |
| 116 ShellIntegration::GetStartMenuShortcut(chrome_exe)); | 131 ShellIntegration::GetStartMenuShortcut(chrome_exe)); |
| 117 relaunch_cmd.AppendSwitchNative(switches::kWaitForHandle, | 132 relaunch_cmd.AppendSwitchNative(switches::kWaitForMutex, mutex_name); |
| 118 base::UintToString16(reinterpret_cast<uint32>(process_handle.Get()))); | |
| 119 | 133 |
| 120 base::LaunchOptions launch_options; | 134 const char* flag; |
| 121 launch_options.inherit_handles = true; | 135 if (base::win::IsMetroProcess()) |
| 122 if (!base::LaunchProcess(relaunch_cmd, launch_options, NULL)) { | 136 flag = mode_switch ? switches::kForceDesktop : switches::kForceImmersive; |
| 123 DPLOG(ERROR) << "Failed to launch relauncher \"" | 137 else |
| 124 << relaunch_cmd.GetCommandLineString() << "\""; | 138 flag = mode_switch ? switches::kForceImmersive : switches::kForceDesktop; |
| 139 relaunch_cmd.AppendSwitch(flag); |
| 140 |
| 141 string16 params(relaunch_cmd.GetCommandLineString()); |
| 142 string16 path(GetMetroRelauncherPath(chrome_exe, version_str).value()); |
| 143 |
| 144 SHELLEXECUTEINFO sei = { sizeof(sei) }; |
| 145 sei.fMask = SEE_MASK_FLAG_LOG_USAGE | SEE_MASK_NOCLOSEPROCESS; |
| 146 sei.nShow = SW_SHOWNORMAL; |
| 147 sei.lpFile = path.c_str(); |
| 148 sei.lpParameters = params.c_str(); |
| 149 |
| 150 if (!::ShellExecuteExW(&sei)) { |
| 151 NOTREACHED() << "ShellExecute failed with " << GetLastError(); |
| 125 return false; | 152 return false; |
| 126 } | 153 } |
| 154 DWORD pid = ::GetProcessId(sei.hProcess); |
| 155 CloseHandle(sei.hProcess); |
| 156 if (!pid) |
| 157 return false; |
| 158 // The next call appears to be needed if we are relaunching from desktop into |
| 159 // metro mode. The observed effect if not done is that chrome starts in metro |
| 160 // mode but it is not given focus and it gets killed by windows after a few |
| 161 // seconds. |
| 162 ::AllowSetForegroundWindow(pid); |
| 163 return true; |
| 164 } |
| 127 | 165 |
| 128 return true; | 166 bool RelaunchChromeBrowser(const CommandLine& command_line) { |
| 167 return RelaunchChromehelper(command_line, false); |
| 168 } |
| 169 |
| 170 bool RelaunchChromeWithModeSwitch(const CommandLine& command_line) { |
| 171 return RelaunchChromehelper(command_line, true); |
| 129 } | 172 } |
| 130 | 173 |
| 131 bool IsUpdatePendingRestart() { | 174 bool IsUpdatePendingRestart() { |
| 132 FilePath new_chrome_exe; | 175 FilePath new_chrome_exe; |
| 133 if (!GetNewerChromeFile(&new_chrome_exe)) | 176 if (!GetNewerChromeFile(&new_chrome_exe)) |
| 134 return false; | 177 return false; |
| 135 return file_util::PathExists(new_chrome_exe); | 178 return file_util::PathExists(new_chrome_exe); |
| 136 } | 179 } |
| 137 | 180 |
| 138 bool SwapNewChromeExeIfPresent() { | 181 bool SwapNewChromeExeIfPresent() { |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 208 return false; | 251 return false; |
| 209 // At this point the chrome.exe has been swapped with the new one. | 252 // At this point the chrome.exe has been swapped with the new one. |
| 210 if (!RelaunchChromeBrowser(command_line)) { | 253 if (!RelaunchChromeBrowser(command_line)) { |
| 211 // The re-launch fails. Feel free to panic now. | 254 // The re-launch fails. Feel free to panic now. |
| 212 NOTREACHED(); | 255 NOTREACHED(); |
| 213 } | 256 } |
| 214 return true; | 257 return true; |
| 215 } | 258 } |
| 216 | 259 |
| 217 } // namespace upgrade_util | 260 } // namespace upgrade_util |
| OLD | NEW |