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 "win8/delegate_execute/chrome_util.h"
15 #include "base/files/file_path.h"
16 #include "base/files/file_util.h"
18 #include "base/process/kill.h"
19 #include "base/process/launch.h"
20 #include "base/process/process.h"
21 #include "base/strings/string_util.h"
22 #include "base/strings/utf_string_conversions.h"
23 #include "base/win/registry.h"
24 #include "base/win/scoped_comptr.h"
25 #include "base/win/scoped_handle.h"
26 #include "base/win/win_util.h"
27 #include "chrome/installer/util/browser_distribution.h"
28 #include "chrome/installer/util/install_util.h"
29 #include "chrome/installer/util/util_constants.h"
30 #include "google_update/google_update_idl.h"
34 #if defined(GOOGLE_CHROME_BUILD)
36 // TODO(grt): These constants live in installer_util. Consider moving them
37 // into common_constants to allow for reuse.
38 const base::FilePath::CharType kNewChromeExe
[] =
39 FILE_PATH_LITERAL("new_chrome.exe");
40 const wchar_t kRenameCommandValue
[] = L
"cmd";
41 const wchar_t kRegPathChromeClientBase
[] =
42 L
"Software\\Google\\Update\\Clients\\";
44 // Returns the name of the global event used to detect if |chrome_exe| is in
45 // use by a browser process.
46 // TODO(grt): Move this somewhere central so it can be used by both this
47 // IsBrowserRunning (below) and IsBrowserAlreadyRunning (browser_util_win.cc).
48 base::string16
GetEventName(const base::FilePath
& chrome_exe
) {
49 static wchar_t const kEventPrefix
[] = L
"Global\\";
50 const size_t prefix_len
= arraysize(kEventPrefix
) - 1;
52 name
.reserve(prefix_len
+ chrome_exe
.value().size());
53 name
.assign(kEventPrefix
, prefix_len
);
54 name
.append(chrome_exe
.value());
55 std::replace(name
.begin() + prefix_len
, name
.end(), '\\', '!');
56 std::transform(name
.begin() + prefix_len
, name
.end(),
57 name
.begin() + prefix_len
, tolower
);
61 // Returns true if |chrome_exe| is in use by a browser process. In this case,
62 // "in use" means past ChromeBrowserMainParts::PreMainMessageLoopRunImpl.
63 bool IsBrowserRunning(const base::FilePath
& chrome_exe
) {
64 base::win::ScopedHandle
handle(::OpenEvent(
65 SYNCHRONIZE
, FALSE
, GetEventName(chrome_exe
).c_str()));
68 DWORD last_error
= ::GetLastError();
69 if (last_error
!= ERROR_FILE_NOT_FOUND
) {
70 AtlTrace("%hs. Failed to open browser event; error %u.\n", __FUNCTION__
,
76 // Returns true if the file new_chrome.exe exists in the same directory as
78 bool NewChromeExeExists(const base::FilePath
& chrome_exe
) {
79 base::FilePath
new_chrome_exe(chrome_exe
.DirName().Append(kNewChromeExe
));
80 return base::PathExists(new_chrome_exe
);
83 bool GetUpdateCommand(bool is_per_user
, base::string16
* update_command
) {
84 const HKEY root
= is_per_user
? HKEY_CURRENT_USER
: HKEY_LOCAL_MACHINE
;
85 BrowserDistribution
* dist
= BrowserDistribution::GetDistribution();
86 base::string16 reg_path_chrome_client
= kRegPathChromeClientBase
;
87 reg_path_chrome_client
.append(dist
->GetAppGuid());
88 base::win::RegKey
key(root
, reg_path_chrome_client
.c_str(), KEY_QUERY_VALUE
);
90 return key
.ReadValue(kRenameCommandValue
, update_command
) == ERROR_SUCCESS
;
93 #endif // GOOGLE_CHROME_BUILD
97 namespace delegate_execute
{
99 void UpdateChromeIfNeeded(const base::FilePath
& chrome_exe
) {
100 #if defined(GOOGLE_CHROME_BUILD)
101 // Nothing to do if a browser is already running or if there's no
103 if (IsBrowserRunning(chrome_exe
) || !NewChromeExeExists(chrome_exe
))
106 base::Process process
;
108 if (InstallUtil::IsPerUserInstall(chrome_exe
)) {
109 // Read the update command from the registry.
110 base::string16 update_command
;
111 if (!GetUpdateCommand(true, &update_command
)) {
112 AtlTrace("%hs. Failed to read update command from registry.\n",
115 // Run the update command.
116 base::LaunchOptions launch_options
;
117 launch_options
.start_hidden
= true;
118 process
= base::LaunchProcess(update_command
, launch_options
);
119 if (!process
.IsValid()) {
120 AtlTrace("%hs. Failed to launch command to finalize update; "
121 "error %u.\n", __FUNCTION__
, ::GetLastError());
125 // Run the update command via Google Update.
127 base::win::ScopedComPtr
<IProcessLauncher
> process_launcher
;
128 hr
= process_launcher
.CreateInstance(__uuidof(ProcessLauncherClass
));
130 AtlTrace("%hs. Failed to Create ProcessLauncher; hr=0x%X.\n",
133 ULONG_PTR handle
= 0;
134 BrowserDistribution
* dist
= BrowserDistribution::GetDistribution();
135 hr
= process_launcher
->LaunchCmdElevated(
136 dist
->GetAppGuid().c_str(), kRenameCommandValue
,
137 GetCurrentProcessId(), &handle
);
139 AtlTrace("%hs. Failed to launch command to finalize update; "
140 "hr=0x%X.\n", __FUNCTION__
, hr
);
142 process
= base::Process(reinterpret_cast<base::ProcessHandle
>(handle
));
147 // Wait for the update to complete and report the results.
148 if (process
.IsValid()) {
150 if (!process
.WaitForExitWithTimeout(
151 base::TimeDelta::FromMilliseconds(INFINITE
), &exit_code
)) {
152 AtlTrace("%hs. Failed to get result when finalizing update.\n",
154 } else if (exit_code
!= installer::RENAME_SUCCESSFUL
) {
155 AtlTrace("%hs. Failed to finalize update with exit code %d.\n",
156 __FUNCTION__
, exit_code
);
158 AtlTrace("%hs. Finalized pending update.\n", __FUNCTION__
);
164 } // delegate_execute