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.
5 #include "chrome/installer/util/google_update_util.h"
7 #include "base/command_line.h"
8 #include "base/files/file_path.h"
9 #include "base/files/file_util.h"
10 #include "base/logging.h"
11 #include "base/path_service.h"
12 #include "base/process/kill.h"
13 #include "base/process/launch.h"
14 #include "base/strings/string16.h"
15 #include "base/time/time.h"
16 #include "base/win/registry.h"
17 #include "base/win/scoped_handle.h"
18 #include "base/win/win_util.h"
19 #include "base/win/windows_version.h"
20 #include "chrome/installer/util/browser_distribution.h"
21 #include "chrome/installer/util/google_update_constants.h"
22 #include "chrome/installer/util/google_update_settings.h"
23 #include "chrome/installer/util/install_util.h"
24 #include "chrome/installer/util/installation_state.h"
25 #include "chrome/installer/util/product.h"
27 using base::win::RegKey
;
29 namespace google_update
{
33 const int kGoogleUpdateTimeoutMs
= 20 * 1000;
35 // Returns true if Google Update is present at the given level.
36 bool IsGoogleUpdatePresent(bool system_install
) {
37 // Using the existence of version key in the registry to decide.
38 return GoogleUpdateSettings::GetGoogleUpdateVersion(system_install
).IsValid();
41 // Returns GoogleUpdateSetup.exe's executable path at specified level.
42 // or an empty path if none is found.
43 base::FilePath
GetGoogleUpdateSetupExe(bool system_install
) {
44 const HKEY root_key
= system_install
? HKEY_LOCAL_MACHINE
: HKEY_CURRENT_USER
;
47 if (update_key
.Open(root_key
, kRegPathGoogleUpdate
, KEY_QUERY_VALUE
) ==
49 base::string16 path_str
;
50 base::string16 version_str
;
51 if ((update_key
.ReadValue(kRegPathField
, &path_str
) == ERROR_SUCCESS
) &&
52 (update_key
.ReadValue(kRegGoogleUpdateVersion
, &version_str
) ==
54 return base::FilePath(path_str
).DirName().Append(version_str
).
55 Append(kGoogleUpdateSetupExe
);
58 return base::FilePath();
61 // If Google Update is present at system-level, sets |cmd_string| to the command
62 // line to install Google Update at user-level and returns true.
63 // Otherwise, clears |cmd_string| and returns false.
64 bool GetUserLevelGoogleUpdateInstallCommandLine(base::string16
* cmd_string
) {
66 base::FilePath
google_update_setup(
67 GetGoogleUpdateSetupExe(true)); // system-level.
68 if (!google_update_setup
.empty()) {
69 base::CommandLine
cmd(google_update_setup
);
70 // Appends "/install runtime=true&needsadmin=false /silent /nomitag".
71 // NB: /nomitag needs to be at the end.
72 // Constants are found in code.google.com/p/omaha/common/const_cmd_line.h.
73 cmd
.AppendArg("/install");
74 // The "&" can be used in base::LaunchProcess() without quotation
75 // (this is problematic only if run from command prompt).
76 cmd
.AppendArg("runtime=true&needsadmin=false");
77 cmd
.AppendArg("/silent");
78 cmd
.AppendArg("/nomitag");
79 *cmd_string
= cmd
.GetCommandLineString();
81 return !cmd_string
->empty();
84 // Launches command |cmd_string|, and waits for |timeout| milliseconds before
85 // timing out. To wait indefinitely, one can set
86 // |timeout| to be base::TimeDelta::FromMilliseconds(INFINITE).
87 // Returns true if this executes successfully.
88 // Returns false if command execution fails to execute, or times out.
89 bool LaunchProcessAndWaitWithTimeout(const base::string16
& cmd_string
,
90 base::TimeDelta timeout
) {
93 VLOG(0) << "Launching: " << cmd_string
;
94 base::Process process
=
95 base::LaunchProcess(cmd_string
, base::LaunchOptions());
96 if (!process
.IsValid()) {
97 PLOG(ERROR
) << "Failed to launch (" << cmd_string
<< ")";
98 } else if (!process
.WaitForExitWithTimeout(timeout
, &exit_code
)) {
99 // The GetExitCodeProcess failed or timed-out.
100 LOG(ERROR
) <<"Command (" << cmd_string
<< ") is taking more than "
101 << timeout
.InMilliseconds() << " milliseconds to complete.";
102 } else if (exit_code
!= 0) {
103 LOG(ERROR
) << "Command (" << cmd_string
<< ") exited with code "
113 bool EnsureUserLevelGoogleUpdatePresent() {
114 VLOG(0) << "Ensuring Google Update is present at user-level.";
116 bool success
= false;
117 if (IsGoogleUpdatePresent(false)) {
120 base::string16 cmd_string
;
121 if (!GetUserLevelGoogleUpdateInstallCommandLine(&cmd_string
)) {
122 LOG(ERROR
) << "Cannot find Google Update at system-level.";
123 // Ideally we should return false. However, this case should not be
124 // encountered by regular users, and developers (who often installs
125 // Chrome without Google Update) may be unduly impeded by this case.
126 // Therefore we return true.
129 success
= LaunchProcessAndWaitWithTimeout(cmd_string
,
130 base::TimeDelta::FromMilliseconds(INFINITE
));
136 bool UninstallGoogleUpdate(bool system_install
) {
137 bool success
= false;
138 base::string16
cmd_string(
139 GoogleUpdateSettings::GetUninstallCommandLine(system_install
));
140 if (cmd_string
.empty()) {
141 success
= true; // Nothing to; vacuous success.
143 success
= LaunchProcessAndWaitWithTimeout(cmd_string
,
144 base::TimeDelta::FromMilliseconds(kGoogleUpdateTimeoutMs
));
149 void ElevateIfNeededToReenableUpdates() {
150 base::FilePath chrome_exe
;
151 if (!PathService::Get(base::FILE_EXE
, &chrome_exe
)) {
155 installer::ProductState product_state
;
156 BrowserDistribution
* dist
= BrowserDistribution::GetDistribution();
157 const bool system_install
= !InstallUtil::IsPerUserInstall(chrome_exe
);
158 if (!product_state
.Initialize(system_install
, dist
))
160 base::FilePath
exe_path(product_state
.GetSetupPath());
161 if (exe_path
.empty() || !base::PathExists(exe_path
)) {
162 LOG(ERROR
) << "Could not find setup.exe to reenable updates.";
166 base::CommandLine
cmd(exe_path
);
167 cmd
.AppendSwitch(installer::switches::kReenableAutoupdates
);
168 installer::Product
product(dist
);
169 product
.InitializeFromUninstallCommand(product_state
.uninstall_command());
170 product
.AppendProductFlags(&cmd
);
172 cmd
.AppendSwitch(installer::switches::kSystemLevel
);
173 if (product_state
.uninstall_command().HasSwitch(
174 installer::switches::kVerboseLogging
)) {
175 cmd
.AppendSwitch(installer::switches::kVerboseLogging
);
178 base::LaunchOptions launch_options
;
179 launch_options
.force_breakaway_from_job_
= true;
181 if (base::win::GetVersion() >= base::win::VERSION_VISTA
&&
182 base::win::UserAccountControlIsEnabled()) {
183 base::LaunchElevatedProcess(cmd
, launch_options
);
185 base::LaunchProcess(cmd
, launch_options
);
189 } // namespace google_update