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 // Explicitly make sure to relaunch chrome.exe rather than old_chrome.exe. |
140 // we exit. But ShellExecute does not support handle passing to the child | 134 // This can happen when old_chrome.exe is launched by a user. |
141 // process so we create a uniquely named mutex that we aquire and never | 135 CommandLine chrome_exe_command_line = command_line; |
142 // release. So when we exit, Windows marks our mutex as abandoned and the | 136 chrome_exe_command_line.SetProgram( |
143 // wait is satisfied. | 137 chrome_exe.DirName().Append(installer::kChromeExe)); |
144 // The format of the named mutex is important. See DelegateExecuteOperation | 138 |
145 // for more details. | 139 if (base::win::GetVersion() < base::win::VERSION_WIN8) |
| 140 return base::LaunchProcess(chrome_exe_command_line, |
| 141 base::LaunchOptions(), NULL); |
| 142 |
| 143 // On Windows 8 we always use the delegate_execute for re-launching chrome. |
| 144 // |
| 145 // Pass this Chrome's Start Menu shortcut path to the relauncher so it can re- |
| 146 // activate chrome via ShellExecute which will wait until we exit. Since |
| 147 // ShellExecute does not support handle passing to the child process we create |
| 148 // a uniquely named mutex that we aquire and never release. So when we exit, |
| 149 // Windows marks our mutex as abandoned and the wait is satisfied. The format |
| 150 // of the named mutex is important. See DelegateExecuteOperation for more |
| 151 // details. |
146 base::string16 mutex_name = | 152 base::string16 mutex_name = |
147 base::StringPrintf(L"chrome.relaunch.%d", ::GetCurrentProcessId()); | 153 base::StringPrintf(L"chrome.relaunch.%d", ::GetCurrentProcessId()); |
148 HANDLE mutex = ::CreateMutexW(NULL, TRUE, mutex_name.c_str()); | 154 HANDLE mutex = ::CreateMutexW(NULL, TRUE, mutex_name.c_str()); |
149 // The |mutex| handle needs to be leaked. See comment above. | 155 // The |mutex| handle needs to be leaked. See comment above. |
150 if (!mutex) { | 156 if (!mutex) { |
151 NOTREACHED(); | 157 NOTREACHED(); |
152 return false; | 158 return false; |
153 } | 159 } |
154 if (::GetLastError() == ERROR_ALREADY_EXISTS) { | 160 if (::GetLastError() == ERROR_ALREADY_EXISTS) { |
155 NOTREACHED() << "Relaunch mutex already exists"; | 161 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) | 245 if (exit_code == installer::RENAME_SUCCESSFUL) |
240 return true; | 246 return true; |
241 } | 247 } |
242 } | 248 } |
243 } | 249 } |
244 | 250 |
245 // Rename didn't work so try to rename by calling Google Update | 251 // Rename didn't work so try to rename by calling Google Update |
246 return InvokeGoogleUpdateForRename(); | 252 return InvokeGoogleUpdateForRename(); |
247 } | 253 } |
248 | 254 |
| 255 bool IsRunningOldChrome() { |
| 256 // This figures out the actual file name that the section containing the |
| 257 // mapped exe refers to. This is used instead of GetModuleFileName because the |
| 258 // .exe may have been renamed out from under us while we've been running which |
| 259 // GetModuleFileName won't notice. |
| 260 wchar_t mapped_file_name[MAX_PATH * 2] = {}; |
| 261 |
| 262 if (!::GetMappedFileName(::GetCurrentProcess(), |
| 263 reinterpret_cast<void*>(::GetModuleHandle(NULL)), |
| 264 mapped_file_name, |
| 265 arraysize(mapped_file_name))) { |
| 266 return false; |
| 267 } |
| 268 |
| 269 base::FilePath file_name(base::FilePath(mapped_file_name).BaseName()); |
| 270 return base::FilePath::CompareEqualIgnoreCase(file_name.value(), |
| 271 installer::kChromeOldExe); |
| 272 } |
| 273 |
249 bool DoUpgradeTasks(const CommandLine& command_line) { | 274 bool DoUpgradeTasks(const CommandLine& command_line) { |
250 // The DelegateExecute verb handler finalizes pending in-use updates for | 275 // The DelegateExecute verb handler finalizes pending in-use updates for |
251 // metro mode launches, as Chrome cannot be gracefully relaunched when | 276 // metro mode launches, as Chrome cannot be gracefully relaunched when |
252 // running in this mode. | 277 // running in this mode. |
253 if (base::win::IsMetroProcess()) | 278 if (base::win::IsMetroProcess()) |
254 return false; | 279 return false; |
255 if (!SwapNewChromeExeIfPresent()) | 280 if (!SwapNewChromeExeIfPresent() && !IsRunningOldChrome()) |
256 return false; | 281 return false; |
257 // At this point the chrome.exe has been swapped with the new one. | 282 // At this point the chrome.exe has been swapped with the new one. |
258 if (!RelaunchChromeBrowser(command_line)) { | 283 if (!RelaunchChromeBrowser(command_line)) { |
259 // The re-launch fails. Feel free to panic now. | 284 // The re-launch fails. Feel free to panic now. |
260 NOTREACHED(); | 285 NOTREACHED(); |
261 } | 286 } |
262 return true; | 287 return true; |
263 } | 288 } |
264 | 289 |
265 } // namespace upgrade_util | 290 } // namespace upgrade_util |
OLD | NEW |