Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/installer/util/google_update_util.h" | |
| 6 | |
| 7 #include "base/command_line.h" | |
| 8 #include "base/file_path.h" | |
| 9 #include "base/file_util.h" | |
| 10 #include "base/logging.h" | |
| 11 #include "base/process.h" | |
| 12 #include "base/process_util.h" | |
| 13 #include "base/string16.h" | |
| 14 #include "base/time.h" | |
| 15 #include "base/win/registry.h" | |
| 16 #include "base/win/scoped_handle.h" | |
| 17 #include "chrome/installer/launcher_support/chrome_launcher_support.h" | |
| 18 #include "chrome/installer/util/google_update_constants.h" | |
| 19 #include "chrome/installer/util/google_update_settings.h" | |
| 20 | |
| 21 using base::win::RegKey; | |
| 22 | |
| 23 namespace google_update { | |
| 24 | |
| 25 namespace { | |
| 26 | |
| 27 const int kGoogleUpdateTimeoutMs = 20 * 1000; | |
| 28 | |
| 29 // Returns true if and only if Google Update is present at the given level. | |
|
erikwright (departed)
2012/09/21 19:10:06
if and only if -> iff
huangs
2012/09/21 19:45:17
Done.
| |
| 30 bool IsGoogleUpdatePresent(bool system_install) { | |
| 31 // Using the existence of version key in the registry to decide. | |
| 32 return GoogleUpdateSettings::GetGoogleUpdateVersion(system_install).IsValid(); | |
| 33 } | |
| 34 | |
| 35 // Returns GoogleUpdateSetup.exe's executable path at |system_level|, | |
|
erikwright (departed)
2012/09/21 19:10:06
|system_level| -> the specified level
huangs
2012/09/21 19:45:17
Done.
| |
| 36 // or an empty path if none is found. | |
| 37 FilePath GetGoogleUpdateSetupExe(bool system_install) { | |
| 38 const HKEY root_key = system_install ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER; | |
| 39 RegKey update_key; | |
| 40 | |
| 41 if (update_key.Open(root_key, kRegPathGoogleUpdate, KEY_QUERY_VALUE) == | |
| 42 ERROR_SUCCESS) { | |
| 43 string16 path_str; | |
| 44 string16 version_str; | |
| 45 if ((update_key.ReadValue(kRegPathField, &path_str) == ERROR_SUCCESS) && | |
| 46 (update_key.ReadValue(kRegGoogleUpdateVersion, &version_str) == | |
| 47 ERROR_SUCCESS)) { | |
| 48 return FilePath(path_str).DirName().Append(version_str). | |
| 49 Append(kGoogleUpdateSetupExe); | |
| 50 } | |
| 51 } | |
| 52 return FilePath(); | |
| 53 } | |
| 54 | |
| 55 // Assign |cmd_string| as command line to install Google Update at user-level, | |
|
erikwright (departed)
2012/09/21 19:10:06
"If Google Update is present at system-level, sets
huangs
2012/09/21 19:45:17
Done.
| |
| 56 // if Google Update is present at system-level, else empty command line. | |
| 57 // Returns true if and only if command line is successfully generated. | |
| 58 bool GetUserLevelGoogleUpdateInstallCommandLine(string16* cmd_string) { | |
| 59 cmd_string->clear(); | |
| 60 CommandLine cmd(CommandLine::NO_PROGRAM); | |
| 61 FilePath google_update_setup(GetGoogleUpdateSetupExe(true)); // system-level. | |
| 62 if (google_update_setup.empty()) { | |
|
erikwright (departed)
2012/09/21 19:10:06
if (!google_update_setup.empty()) {
...
}
retur
huangs
2012/09/21 19:45:17
Done.
| |
| 63 return false; | |
| 64 } else { | |
| 65 cmd.SetProgram(google_update_setup); | |
|
erikwright (departed)
2012/09/21 19:10:06
Declare cmd here, and use the constructor that tak
huangs
2012/09/21 19:45:17
Done.
| |
| 66 // Appends parameter "/install runtime=true&needsadmin=false /silent" | |
| 67 // Constants are found in code.google.com/p/omaha/common/const_cmd_line.h. | |
| 68 cmd.AppendArg("/install"); | |
| 69 // The "&" can be used in base::LaunchProcess() without quotation | |
| 70 // (this is problematic only if run from command prompt). | |
| 71 cmd.AppendArg("runtime=true&needsadmin=false"); | |
| 72 cmd.AppendArg("/silent"); | |
| 73 *cmd_string = cmd.GetCommandLineString(); | |
| 74 return true; | |
| 75 } | |
| 76 } | |
| 77 | |
| 78 } // namespace | |
| 79 | |
| 80 bool EnsureUserLevelGoogleUpdatePresent() { | |
| 81 LOG(INFO) << "Ensuring Google Update is present at user-level."; | |
| 82 | |
| 83 bool success = false; | |
| 84 if (IsGoogleUpdatePresent(false)) { | |
| 85 success = true; | |
| 86 } else { | |
| 87 string16 cmd_string; | |
| 88 if (!GetUserLevelGoogleUpdateInstallCommandLine(&cmd_string)) { | |
| 89 LOG(ERROR) << "Cannot find Google Update at system-level."; | |
| 90 } else { | |
| 91 // WaitForExitCode() will releases handle for us, so using ProcessHandle. | |
|
erikwright (departed)
2012/09/21 19:10:06
Use a ScopedHandle and call Take() when passing it
huangs
2012/09/21 19:45:17
Done. Now we can extract into common code!
| |
| 92 base::ProcessHandle process_handle; | |
| 93 int exit_code = 0; | |
| 94 LOG(INFO) << "Launching: " << cmd_string; | |
| 95 if (!base::LaunchProcess(cmd_string, base::LaunchOptions(), | |
| 96 &process_handle)) { | |
| 97 PLOG(ERROR) << "Failed to launch (" << cmd_string << ")"; | |
| 98 } else if (!base::WaitForExitCode(process_handle, &exit_code)) { | |
| 99 // The process didn't finish in time, or GetExitCodeProcess failed. | |
|
erikwright (departed)
2012/09/21 19:10:06
Remove "The process didn't finish in time, or "
Fi
huangs
2012/09/21 19:45:17
Moot, as this is moved to common code.
| |
| 100 LOG(ERROR) <<"Command (" << cmd_string << ") is taking more than " | |
| 101 << kGoogleUpdateTimeoutMs << " milliseconds to complete."; | |
| 102 } else if (exit_code != 0) { | |
| 103 LOG(ERROR) << "Command (" << cmd_string << ") exited with code " | |
| 104 << exit_code; | |
| 105 } else { | |
| 106 success = true; | |
| 107 } | |
| 108 } | |
| 109 } | |
| 110 return success; | |
| 111 } | |
| 112 | |
| 113 bool UninstallGoogleUpdate(bool system_install) { | |
| 114 bool success = false; | |
| 115 string16 cmd_string( | |
| 116 GoogleUpdateSettings::GetUninstallCommandLine(system_install)); | |
| 117 if (cmd_string.empty()) { | |
| 118 success = true; // Nothing to; vacuous success. | |
| 119 } else { | |
| 120 // WaitForExitCodeWithTimeout() will not releases handle for us. | |
|
erikwright (departed)
2012/09/21 19:10:06
Remove this comment.
huangs
2012/09/21 19:45:17
Done.
| |
| 121 base::win::ScopedHandle process; | |
| 122 int exit_code = 0; | |
| 123 LOG(INFO) << "Launching: " << cmd_string; | |
| 124 if (!base::LaunchProcess(cmd_string, base::LaunchOptions(), | |
| 125 process.Receive())) { | |
| 126 PLOG(ERROR) << "Failed to launch (" << cmd_string << ")"; | |
| 127 } else if (!base::WaitForExitCodeWithTimeout(process, &exit_code, | |
| 128 base::TimeDelta::FromMilliseconds(kGoogleUpdateTimeoutMs))) { | |
| 129 // The process didn't finish in time, or GetExitCodeProcess failed. | |
| 130 LOG(ERROR) <<"Command (" << cmd_string << ") is taking more than " | |
| 131 << kGoogleUpdateTimeoutMs << " milliseconds to complete."; | |
| 132 } else if (exit_code != 0) { | |
| 133 LOG(ERROR) << "Command (" << cmd_string << ") exited with code " | |
| 134 << exit_code; | |
| 135 } else { | |
| 136 success = true; | |
| 137 } | |
| 138 } | |
| 139 return success; | |
| 140 } | |
| 141 | |
| 142 } // namespace google_update | |
| OLD | NEW |