Chromium Code Reviews| 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 <windows.h> | 7 #include <windows.h> |
| 8 #include <psapi.h> | |
| 8 #include <shellapi.h> | 9 #include <shellapi.h> |
| 9 | 10 |
| 10 #include <algorithm> | 11 #include <algorithm> |
| 11 #include <string> | 12 #include <string> |
| 12 | 13 |
| 13 #include "base/base_paths.h" | 14 #include "base/base_paths.h" |
| 14 #include "base/command_line.h" | 15 #include "base/command_line.h" |
| 15 #include "base/environment.h" | 16 #include "base/environment.h" |
| 16 #include "base/file_util.h" | 17 #include "base/file_util.h" |
| 17 #include "base/files/file_path.h" | 18 #include "base/files/file_path.h" |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 116 const RelaunchMode& relaunch_mode) { | 117 const RelaunchMode& relaunch_mode) { |
| 117 scoped_ptr<base::Environment> env(base::Environment::Create()); | 118 scoped_ptr<base::Environment> env(base::Environment::Create()); |
| 118 std::string version_str; | 119 std::string version_str; |
| 119 | 120 |
| 120 // Get the version variable and remove it from the environment. | 121 // Get the version variable and remove it from the environment. |
| 121 if (env->GetVar(chrome::kChromeVersionEnvVar, &version_str)) | 122 if (env->GetVar(chrome::kChromeVersionEnvVar, &version_str)) |
| 122 env->UnSetVar(chrome::kChromeVersionEnvVar); | 123 env->UnSetVar(chrome::kChromeVersionEnvVar); |
| 123 else | 124 else |
| 124 version_str.clear(); | 125 version_str.clear(); |
| 125 | 126 |
| 126 if (base::win::GetVersion() < base::win::VERSION_WIN8) | |
| 127 return base::LaunchProcess(command_line, base::LaunchOptions(), NULL); | |
| 128 | |
| 129 // On Windows 8 we always use the delegate_execute for re-launching chrome. | |
| 130 // | |
| 131 // Pass this Chrome's Start Menu shortcut path to the relauncher so it can | |
| 132 // re-activate chrome via ShellExecute. | |
| 133 base::FilePath chrome_exe; | 127 base::FilePath chrome_exe; |
| 134 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) { | 128 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) { |
| 135 NOTREACHED(); | 129 NOTREACHED(); |
| 136 return false; | 130 return false; |
| 137 } | 131 } |
| 138 | 132 |
| 139 // We need to use ShellExecute to launch the relauncher, which will wait until | 133 CommandLine chrome_exe_command_line = command_line; |
|
grt (UTC plus 2)
2014/05/15 17:47:38
consider:
// Take care to relaunch chrome.exe ra
robertshield
2014/05/15 18:42:30
Done.
| |
| 140 // we exit. But ShellExecute does not support handle passing to the child | 134 base::FilePath current_exe_path(chrome_exe.DirName()); |
|
grt (UTC plus 2)
2014/05/15 17:47:38
current_exe_path is a bit misleading. how about do
robertshield
2014/05/15 18:42:30
Done.
| |
| 141 // process so we create a uniquely named mutex that we aquire and never | 135 current_exe_path = current_exe_path.Append(installer::kChromeExe); |
| 142 // release. So when we exit, Windows marks our mutex as abandoned and the | 136 chrome_exe_command_line.SetProgram(current_exe_path); |
| 143 // wait is satisfied. | 137 |
| 144 // The format of the named mutex is important. See DelegateExecuteOperation | 138 if (base::win::GetVersion() < base::win::VERSION_WIN8) |
| 145 // for more details. | 139 return base::LaunchProcess(chrome_exe_command_line, |
| 140 base::LaunchOptions(), NULL); | |
| 141 | |
| 142 // On Windows 8 we always use the delegate_execute for re-launching chrome. | |
| 143 // | |
| 144 // Pass this Chrome's Start Menu shortcut path to the relauncher so it can re- | |
| 145 // activate chrome via ShellExecute which will wait until we exit. Since | |
| 146 // ShellExecute does not support handle passing to the child process we create | |
| 147 // a uniquely named mutex that we aquire and never release. So when we exit, | |
| 148 // Windows marks our mutex as abandoned and the wait is satisfied. The format | |
| 149 // of the named mutex is important. See DelegateExecuteOperation for more | |
| 150 // details. | |
| 146 base::string16 mutex_name = | 151 base::string16 mutex_name = |
| 147 base::StringPrintf(L"chrome.relaunch.%d", ::GetCurrentProcessId()); | 152 base::StringPrintf(L"chrome.relaunch.%d", ::GetCurrentProcessId()); |
| 148 HANDLE mutex = ::CreateMutexW(NULL, TRUE, mutex_name.c_str()); | 153 HANDLE mutex = ::CreateMutexW(NULL, TRUE, mutex_name.c_str()); |
| 149 // The |mutex| handle needs to be leaked. See comment above. | 154 // The |mutex| handle needs to be leaked. See comment above. |
| 150 if (!mutex) { | 155 if (!mutex) { |
| 151 NOTREACHED(); | 156 NOTREACHED(); |
| 152 return false; | 157 return false; |
| 153 } | 158 } |
| 154 if (::GetLastError() == ERROR_ALREADY_EXISTS) { | 159 if (::GetLastError() == ERROR_ALREADY_EXISTS) { |
| 155 NOTREACHED() << "Relaunch mutex already exists"; | 160 NOTREACHED() << "Relaunch mutex already exists"; |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 239 if (exit_code == installer::RENAME_SUCCESSFUL) | 244 if (exit_code == installer::RENAME_SUCCESSFUL) |
| 240 return true; | 245 return true; |
| 241 } | 246 } |
| 242 } | 247 } |
| 243 } | 248 } |
| 244 | 249 |
| 245 // Rename didn't work so try to rename by calling Google Update | 250 // Rename didn't work so try to rename by calling Google Update |
| 246 return InvokeGoogleUpdateForRename(); | 251 return InvokeGoogleUpdateForRename(); |
| 247 } | 252 } |
| 248 | 253 |
| 254 bool IsRunningOldChrome() { | |
| 255 // This figures out the actual file name that the section containing the | |
| 256 // mapped exe refers to. This is used instead of GetModuleFileName because the | |
| 257 // .exe may have been renamed out from under us while we've been running which | |
| 258 // GetModuleFileName won't notice. | |
| 259 wchar_t mapped_file_name[MAX_PATH * 2] = {}; | |
| 260 | |
| 261 if (!::GetMappedFileName(::GetCurrentProcess(), | |
| 262 reinterpret_cast<void*>(::GetModuleHandle(NULL)), | |
| 263 mapped_file_name, | |
| 264 arraysize(mapped_file_name))) { | |
| 265 return false; | |
| 266 } | |
| 267 | |
| 268 base::FilePath file_name(base::FilePath(mapped_file_name).BaseName()); | |
| 269 return base::FilePath::CompareEqualIgnoreCase(file_name.value(), | |
| 270 installer::kChromeOldExe); | |
| 271 } | |
| 272 | |
| 249 bool DoUpgradeTasks(const CommandLine& command_line) { | 273 bool DoUpgradeTasks(const CommandLine& command_line) { |
| 250 // The DelegateExecute verb handler finalizes pending in-use updates for | 274 // The DelegateExecute verb handler finalizes pending in-use updates for |
| 251 // metro mode launches, as Chrome cannot be gracefully relaunched when | 275 // metro mode launches, as Chrome cannot be gracefully relaunched when |
| 252 // running in this mode. | 276 // running in this mode. |
| 253 if (base::win::IsMetroProcess()) | 277 if (base::win::IsMetroProcess()) |
| 254 return false; | 278 return false; |
| 255 if (!SwapNewChromeExeIfPresent()) | 279 if (!SwapNewChromeExeIfPresent() && !IsRunningOldChrome()) |
| 256 return false; | 280 return false; |
| 257 // At this point the chrome.exe has been swapped with the new one. | 281 // At this point the chrome.exe has been swapped with the new one. |
| 258 if (!RelaunchChromeBrowser(command_line)) { | 282 if (!RelaunchChromeBrowser(command_line)) { |
| 259 // The re-launch fails. Feel free to panic now. | 283 // The re-launch fails. Feel free to panic now. |
| 260 NOTREACHED(); | 284 NOTREACHED(); |
| 261 } | 285 } |
| 262 return true; | 286 return true; |
| 263 } | 287 } |
| 264 | 288 |
| 265 } // namespace upgrade_util | 289 } // namespace upgrade_util |
| OLD | NEW |