| 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.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 // static | |
| 64 CommandLine* Upgrade::new_command_line_ = NULL; | |
| 65 | |
| 66 // static | |
| 67 bool Upgrade::IsBrowserAlreadyRunning() { | |
| 68 static HANDLE handle = NULL; | |
| 69 FilePath exe_path; | |
| 70 PathService::Get(base::FILE_EXE, &exe_path); | |
| 71 std::wstring exe = exe_path.value(); | |
| 72 std::replace(exe.begin(), exe.end(), '\\', '!'); | |
| 73 std::transform(exe.begin(), exe.end(), exe.begin(), tolower); | |
| 74 exe = L"Global\\" + exe; | |
| 75 if (handle != NULL) | |
| 76 CloseHandle(handle); | |
| 77 handle = CreateEvent(NULL, TRUE, TRUE, exe.c_str()); | |
| 78 int error = GetLastError(); | |
| 79 return (error == ERROR_ALREADY_EXISTS || error == ERROR_ACCESS_DENIED); | |
| 80 } | |
| 81 | |
| 82 // static | |
| 83 bool Upgrade::SwapNewChromeExeIfPresent() { | |
| 84 FilePath new_chrome_exe; | |
| 85 if (!GetNewerChromeFile(&new_chrome_exe)) | |
| 86 return false; | |
| 87 if (!file_util::PathExists(new_chrome_exe)) | |
| 88 return false; | |
| 89 FilePath cur_chrome_exe; | |
| 90 if (!PathService::Get(base::FILE_EXE, &cur_chrome_exe)) | |
| 91 return false; | |
| 92 | |
| 93 // First try to rename exe by launching rename command ourselves. | |
| 94 bool user_install = | |
| 95 InstallUtil::IsPerUserInstall(cur_chrome_exe.value().c_str()); | |
| 96 HKEY reg_root = user_install ? HKEY_CURRENT_USER : HKEY_LOCAL_MACHINE; | |
| 97 BrowserDistribution *dist = BrowserDistribution::GetDistribution(); | |
| 98 base::win::RegKey key; | |
| 99 std::wstring rename_cmd; | |
| 100 if ((key.Open(reg_root, dist->GetVersionKey().c_str(), | |
| 101 KEY_READ) == ERROR_SUCCESS) && | |
| 102 (key.ReadValue(google_update::kRegRenameCmdField, | |
| 103 &rename_cmd) == ERROR_SUCCESS)) { | |
| 104 base::ProcessHandle handle; | |
| 105 if (base::LaunchApp(rename_cmd, true, true, &handle)) { | |
| 106 DWORD exit_code; | |
| 107 ::GetExitCodeProcess(handle, &exit_code); | |
| 108 ::CloseHandle(handle); | |
| 109 if (exit_code == installer::RENAME_SUCCESSFUL) | |
| 110 return true; | |
| 111 } | |
| 112 } | |
| 113 | |
| 114 // Rename didn't work so try to rename by calling Google Update | |
| 115 return InvokeGoogleUpdateForRename(); | |
| 116 } | |
| 117 | |
| 118 // static | |
| 119 bool Upgrade::DoUpgradeTasks(const CommandLine& command_line) { | |
| 120 if (!Upgrade::SwapNewChromeExeIfPresent()) | |
| 121 return false; | |
| 122 // At this point the chrome.exe has been swapped with the new one. | |
| 123 if (!Upgrade::RelaunchChromeBrowser(command_line)) { | |
| 124 // The re-launch fails. Feel free to panic now. | |
| 125 NOTREACHED(); | |
| 126 } | |
| 127 return true; | |
| 128 } | |
| 129 | |
| 130 // static | |
| 131 Upgrade::TryResult Upgrade::ShowTryChromeDialog( | |
| 132 size_t version, | |
| 133 ProcessSingleton* process_singleton) { | |
| 134 if (version > 10000) { | |
| 135 // This is a test value. We want to make sure we exercise | |
| 136 // returning this early. See EarlyReturnTest test harness. | |
| 137 return Upgrade::NOT_NOW; | |
| 138 } | |
| 139 TryChromeDialogView dialog(version); | |
| 140 return dialog.ShowModal(process_singleton); | |
| 141 } | |
| 142 | |
| 143 // static | |
| 144 bool Upgrade::RelaunchChromeBrowser(const CommandLine& command_line) { | |
| 145 scoped_ptr<base::Environment> env(base::Environment::Create()); | |
| 146 env->UnSetVar(chrome::kChromeVersionEnvVar); | |
| 147 return base::LaunchApp( | |
| 148 command_line.command_line_string(), false, false, NULL); | |
| 149 } | |
| 150 | |
| 151 // static | |
| 152 bool Upgrade::IsUpdatePendingRestart() { | |
| 153 FilePath new_chrome_exe; | |
| 154 if (!GetNewerChromeFile(&new_chrome_exe)) | |
| 155 return false; | |
| 156 return file_util::PathExists(new_chrome_exe); | |
| 157 } | |
| OLD | NEW |