| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/first_run/upgrade_util.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 #include <string> | |
| 9 | |
| 10 #include "base/base_paths.h" | |
| 11 #include "base/command_line.h" | |
| 12 #include "base/environment.h" | |
| 13 #include "base/file_path.h" | |
| 14 #include "base/file_util.h" | |
| 15 #include "base/logging.h" | |
| 16 #include "base/path_service.h" | |
| 17 #include "base/process_util.h" | |
| 18 #include "base/win/registry.h" | |
| 19 #include "base/win/scoped_comptr.h" | |
| 20 #include "chrome/browser/first_run/try_chrome_dialog_view.h" | |
| 21 #include "chrome/browser/process_singleton.h" | |
| 22 #include "chrome/common/chrome_constants.h" | |
| 23 #include "chrome/installer/util/browser_distribution.h" | |
| 24 #include "chrome/installer/util/google_update_constants.h" | |
| 25 #include "chrome/installer/util/install_util.h" | |
| 26 #include "chrome/installer/util/shell_util.h" | |
| 27 #include "chrome/installer/util/util_constants.h" | |
| 28 #include "google_update_idl.h" | |
| 29 | |
| 30 namespace { | |
| 31 | |
| 32 bool GetNewerChromeFile(FilePath* path) { | |
| 33 if (!PathService::Get(base::DIR_EXE, path)) | |
| 34 return false; | |
| 35 *path = path->Append(installer::kChromeNewExe); | |
| 36 return true; | |
| 37 } | |
| 38 | |
| 39 bool InvokeGoogleUpdateForRename() { | |
| 40 base::win::ScopedComPtr<IProcessLauncher> ipl; | |
| 41 if (!FAILED(ipl.CreateInstance(__uuidof(ProcessLauncherClass)))) { | |
| 42 ULONG_PTR phandle = NULL; | |
| 43 DWORD id = GetCurrentProcessId(); | |
| 44 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); | |
| 45 if (!FAILED(ipl->LaunchCmdElevated(dist->GetAppGuid().c_str(), | |
| 46 google_update::kRegRenameCmdField, | |
| 47 id, | |
| 48 &phandle))) { | |
| 49 HANDLE handle = HANDLE(phandle); | |
| 50 WaitForSingleObject(handle, INFINITE); | |
| 51 DWORD exit_code; | |
| 52 ::GetExitCodeProcess(handle, &exit_code); | |
| 53 ::CloseHandle(handle); | |
| 54 if (exit_code == installer::RENAME_SUCCESSFUL) | |
| 55 return true; | |
| 56 } | |
| 57 } | |
| 58 return false; | |
| 59 } | |
| 60 | |
| 61 } // namespace | |
| 62 | |
| 63 | |
| 64 namespace upgrade_util { | |
| 65 | |
| 66 bool RelaunchChromeBrowser(const CommandLine& command_line) { | |
| 67 scoped_ptr<base::Environment> env(base::Environment::Create()); | |
| 68 env->UnSetVar(chrome::kChromeVersionEnvVar); | |
| 69 return base::LaunchApp( | |
| 70 command_line.command_line_string(), false, false, NULL); | |
| 71 } | |
| 72 | |
| 73 bool IsUpdatePendingRestart() { | |
| 74 FilePath new_chrome_exe; | |
| 75 if (!GetNewerChromeFile(&new_chrome_exe)) | |
| 76 return false; | |
| 77 return file_util::PathExists(new_chrome_exe); | |
| 78 } | |
| 79 | |
| 80 bool IsBrowserAlreadyRunning() { | |
| 81 static HANDLE handle = NULL; | |
| 82 FilePath exe_path; | |
| 83 PathService::Get(base::FILE_EXE, &exe_path); | |
| 84 std::wstring exe = exe_path.value(); | |
| 85 std::replace(exe.begin(), exe.end(), '\\', '!'); | |
| 86 std::transform(exe.begin(), exe.end(), exe.begin(), tolower); | |
| 87 exe = L"Global\\" + exe; | |
| 88 if (handle != NULL) | |
| 89 CloseHandle(handle); | |
| 90 handle = CreateEvent(NULL, TRUE, TRUE, exe.c_str()); | |
| 91 int error = GetLastError(); | |
| 92 return (error == ERROR_ALREADY_EXISTS || error == ERROR_ACCESS_DENIED); | |
| 93 } | |
| 94 | |
| 95 bool SwapNewChromeExeIfPresent() { | |
| 96 FilePath new_chrome_exe; | |
| 97 if (!GetNewerChromeFile(&new_chrome_exe)) | |
| 98 return false; | |
| 99 if (!file_util::PathExists(new_chrome_exe)) | |
| 100 return false; | |
| 101 FilePath cur_chrome_exe; | |
| 102 if (!PathService::Get(base::FILE_EXE, &cur_chrome_exe)) | |
| 103 return false; | |
| 104 | |
| 105 // First try to rename exe by launching rename command ourselves. | |
| 106 bool user_install = | |
| 107 InstallUtil::IsPerUserInstall(cur_chrome_exe.value().c_str()); | |
| 108 HKEY reg_root = user_install ? HKEY_CURRENT_USER : HKEY_LOCAL_MACHINE; | |
| 109 BrowserDistribution *dist = BrowserDistribution::GetDistribution(); | |
| 110 base::win::RegKey key; | |
| 111 std::wstring rename_cmd; | |
| 112 if ((key.Open(reg_root, dist->GetVersionKey().c_str(), | |
| 113 KEY_READ) == ERROR_SUCCESS) && | |
| 114 (key.ReadValue(google_update::kRegRenameCmdField, | |
| 115 &rename_cmd) == ERROR_SUCCESS)) { | |
| 116 base::ProcessHandle handle; | |
| 117 if (base::LaunchApp(rename_cmd, true, true, &handle)) { | |
| 118 DWORD exit_code; | |
| 119 ::GetExitCodeProcess(handle, &exit_code); | |
| 120 ::CloseHandle(handle); | |
| 121 if (exit_code == installer::RENAME_SUCCESSFUL) | |
| 122 return true; | |
| 123 } | |
| 124 } | |
| 125 | |
| 126 // Rename didn't work so try to rename by calling Google Update | |
| 127 return InvokeGoogleUpdateForRename(); | |
| 128 } | |
| 129 | |
| 130 bool DoUpgradeTasks(const CommandLine& command_line) { | |
| 131 if (!SwapNewChromeExeIfPresent()) | |
| 132 return false; | |
| 133 // At this point the chrome.exe has been swapped with the new one. | |
| 134 if (!RelaunchChromeBrowser(command_line)) { | |
| 135 // The re-launch fails. Feel free to panic now. | |
| 136 NOTREACHED(); | |
| 137 } | |
| 138 return true; | |
| 139 } | |
| 140 | |
| 141 TryResult ShowTryChromeDialog(size_t version, | |
| 142 ProcessSingleton* process_singleton) { | |
| 143 if (version > 10000) { | |
| 144 // This is a test value. We want to make sure we exercise | |
| 145 // returning this early. See EarlyReturnTest test harness. | |
| 146 return NOT_NOW; | |
| 147 } | |
| 148 TryChromeDialogView dialog(version); | |
| 149 return dialog.ShowModal(process_singleton); | |
| 150 } | |
| 151 | |
| 152 } // namespace upgrade_util | |
| OLD | NEW |