Don't show supervised user as "already on this device" while they're being imported.
[chromium-blink-merge.git] / win8 / delegate_execute / chrome_util.cc
blob858a2ca5265621205169ea7b1af1e2b11de09dc3
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"
7 #include <windows.h>
8 #include <atlbase.h>
9 #include <shlobj.h>
11 #include <algorithm>
12 #include <limits>
13 #include <string>
15 #include "base/files/file_path.h"
16 #include "base/files/file_util.h"
17 #include "base/md5.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"
31 #if defined(GOOGLE_CHROME_BUILD)
32 #include "google_update/google_update_idl.h"
33 #endif
35 namespace {
37 #if defined(GOOGLE_CHROME_BUILD)
39 // TODO(grt): These constants live in installer_util. Consider moving them
40 // into common_constants to allow for reuse.
41 const base::FilePath::CharType kNewChromeExe[] =
42 FILE_PATH_LITERAL("new_chrome.exe");
43 const wchar_t kRenameCommandValue[] = L"cmd";
44 const wchar_t kRegPathChromeClientBase[] =
45 L"Software\\Google\\Update\\Clients\\";
47 // Returns the name of the global event used to detect if |chrome_exe| is in
48 // use by a browser process.
49 // TODO(grt): Move this somewhere central so it can be used by both this
50 // IsBrowserRunning (below) and IsBrowserAlreadyRunning (browser_util_win.cc).
51 base::string16 GetEventName(const base::FilePath& chrome_exe) {
52 static wchar_t const kEventPrefix[] = L"Global\\";
53 const size_t prefix_len = arraysize(kEventPrefix) - 1;
54 base::string16 name;
55 name.reserve(prefix_len + chrome_exe.value().size());
56 name.assign(kEventPrefix, prefix_len);
57 name.append(chrome_exe.value());
58 std::replace(name.begin() + prefix_len, name.end(), '\\', '!');
59 std::transform(name.begin() + prefix_len, name.end(),
60 name.begin() + prefix_len, tolower);
61 return name;
64 // Returns true if |chrome_exe| is in use by a browser process. In this case,
65 // "in use" means past ChromeBrowserMainParts::PreMainMessageLoopRunImpl.
66 bool IsBrowserRunning(const base::FilePath& chrome_exe) {
67 base::win::ScopedHandle handle(::OpenEvent(
68 SYNCHRONIZE, FALSE, GetEventName(chrome_exe).c_str()));
69 if (handle.IsValid())
70 return true;
71 DWORD last_error = ::GetLastError();
72 if (last_error != ERROR_FILE_NOT_FOUND) {
73 AtlTrace("%hs. Failed to open browser event; error %u.\n", __FUNCTION__,
74 last_error);
76 return false;
79 // Returns true if the file new_chrome.exe exists in the same directory as
80 // |chrome_exe|.
81 bool NewChromeExeExists(const base::FilePath& chrome_exe) {
82 base::FilePath new_chrome_exe(chrome_exe.DirName().Append(kNewChromeExe));
83 return base::PathExists(new_chrome_exe);
86 bool GetUpdateCommand(bool is_per_user, base::string16* update_command) {
87 const HKEY root = is_per_user ? HKEY_CURRENT_USER : HKEY_LOCAL_MACHINE;
88 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
89 base::string16 reg_path_chrome_client = kRegPathChromeClientBase;
90 reg_path_chrome_client.append(dist->GetAppGuid());
91 base::win::RegKey key(root, reg_path_chrome_client.c_str(), KEY_QUERY_VALUE);
93 return key.ReadValue(kRenameCommandValue, update_command) == ERROR_SUCCESS;
96 #endif // GOOGLE_CHROME_BUILD
98 } // namespace
100 namespace delegate_execute {
102 void UpdateChromeIfNeeded(const base::FilePath& chrome_exe) {
103 #if defined(GOOGLE_CHROME_BUILD)
104 // Nothing to do if a browser is already running or if there's no
105 // new_chrome.exe.
106 if (IsBrowserRunning(chrome_exe) || !NewChromeExeExists(chrome_exe))
107 return;
109 base::Process process;
111 if (InstallUtil::IsPerUserInstall(chrome_exe)) {
112 // Read the update command from the registry.
113 base::string16 update_command;
114 if (!GetUpdateCommand(true, &update_command)) {
115 AtlTrace("%hs. Failed to read update command from registry.\n",
116 __FUNCTION__);
117 } else {
118 // Run the update command.
119 base::LaunchOptions launch_options;
120 launch_options.start_hidden = true;
121 process = base::LaunchProcess(update_command, launch_options);
122 if (!process.IsValid()) {
123 AtlTrace("%hs. Failed to launch command to finalize update; "
124 "error %u.\n", __FUNCTION__, ::GetLastError());
127 } else {
128 // Run the update command via Google Update.
129 HRESULT hr = S_OK;
130 base::win::ScopedComPtr<IProcessLauncher> process_launcher;
131 hr = process_launcher.CreateInstance(__uuidof(ProcessLauncherClass));
132 if (FAILED(hr)) {
133 AtlTrace("%hs. Failed to Create ProcessLauncher; hr=0x%X.\n",
134 __FUNCTION__, hr);
135 } else {
136 ULONG_PTR handle = 0;
137 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
138 hr = process_launcher->LaunchCmdElevated(
139 dist->GetAppGuid().c_str(), kRenameCommandValue,
140 GetCurrentProcessId(), &handle);
141 if (FAILED(hr)) {
142 AtlTrace("%hs. Failed to launch command to finalize update; "
143 "hr=0x%X.\n", __FUNCTION__, hr);
144 } else {
145 process = base::Process(reinterpret_cast<base::ProcessHandle>(handle));
150 // Wait for the update to complete and report the results.
151 if (process.IsValid()) {
152 int exit_code = 0;
153 if (!process.WaitForExitWithTimeout(
154 base::TimeDelta::FromMilliseconds(INFINITE), &exit_code)) {
155 AtlTrace("%hs. Failed to get result when finalizing update.\n",
156 __FUNCTION__);
157 } else if (exit_code != installer::RENAME_SUCCESSFUL) {
158 AtlTrace("%hs. Failed to finalize update with exit code %d.\n",
159 __FUNCTION__, exit_code);
160 } else {
161 AtlTrace("%hs. Finalized pending update.\n", __FUNCTION__);
164 #endif
167 } // delegate_execute