[email protected] | e3ce40a | 2012-01-31 03:03:03 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 7c47ae3e | 2009-02-18 00:34:21 | [diff] [blame] | 5 | #include "chrome/browser/process_singleton.h" |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 6 | |
manzagop | f071e88 | 2016-04-13 14:36:00 | [diff] [blame] | 7 | #include <windows.h> |
[email protected] | b1d7844b | 2012-08-22 22:37:47 | [diff] [blame] | 8 | #include <shellapi.h> |
avi | b896c71 | 2015-12-26 02:10:43 | [diff] [blame] | 9 | #include <stddef.h> |
[email protected] | b1d7844b | 2012-08-22 22:37:47 | [diff] [blame] | 10 | |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 11 | #include "base/base_paths.h" |
[email protected] | 4cf04bb | 2013-07-11 09:22:38 | [diff] [blame] | 12 | #include "base/bind.h" |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 13 | #include "base/command_line.h" |
aseren | 028ea15 | 2017-05-16 17:22:43 | [diff] [blame] | 14 | #include "base/debug/activity_tracker.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 15 | #include "base/files/file_path.h" |
Hans Wennborg | f6ad69c | 2020-06-18 18:02:32 | [diff] [blame] | 16 | #include "base/logging.h" |
avi | b896c71 | 2015-12-26 02:10:43 | [diff] [blame] | 17 | #include "base/macros.h" |
Ilya Sherman | 982457e6 | 2017-12-13 02:19:36 | [diff] [blame] | 18 | #include "base/metrics/histogram_functions.h" |
gab | 439f54f | 2016-10-07 22:24:22 | [diff] [blame] | 19 | #include "base/metrics/histogram_macros.h" |
rvargas | 4683e5e | 2015-03-04 22:38:20 | [diff] [blame] | 20 | #include "base/process/process.h" |
[email protected] | d09a4ce1c | 2013-07-24 17:37:02 | [diff] [blame] | 21 | #include "base/process/process_info.h" |
[email protected] | 84c58446 | 2013-05-13 05:05:00 | [diff] [blame] | 22 | #include "base/strings/string_number_conversions.h" |
[email protected] | e309f31 | 2013-06-07 21:50:08 | [diff] [blame] | 23 | #include "base/strings/utf_string_conversions.h" |
[email protected] | 8481347 | 2013-06-28 00:25:19 | [diff] [blame] | 24 | #include "base/time/time.h" |
Etienne Bergeron | bacd29d8 | 2021-09-15 20:39:12 | [diff] [blame] | 25 | #include "base/trace_event/trace_event.h" |
[email protected] | b3d791c9 | 2012-10-05 19:01:24 | [diff] [blame] | 26 | #include "base/win/registry.h" |
[email protected] | b90d7e80 | 2011-01-09 16:32:20 | [diff] [blame] | 27 | #include "base/win/scoped_handle.h" |
[email protected] | 6927f2e0 | 2012-09-18 00:13:49 | [diff] [blame] | 28 | #include "base/win/windows_version.h" |
Guido Urdaneta | 73a94302 | 2018-08-24 15:42:27 | [diff] [blame] | 29 | #include "base/win/wmi.h" |
[email protected] | 24590067 | 2012-10-13 02:27:11 | [diff] [blame] | 30 | #include "chrome/browser/shell_integration.h" |
[email protected] | b50892c5f | 2012-05-13 07:34:14 | [diff] [blame] | 31 | #include "chrome/browser/ui/simple_message_box.h" |
pmonette | 23c8fb7e | 2016-06-27 20:45:11 | [diff] [blame] | 32 | #include "chrome/browser/win/chrome_process_finder.h" |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 33 | #include "chrome/common/chrome_constants.h" |
[email protected] | b3d791c9 | 2012-10-05 19:01:24 | [diff] [blame] | 34 | #include "chrome/common/chrome_paths.h" |
| 35 | #include "chrome/common/chrome_paths_internal.h" |
[email protected] | cfa118a | 2012-10-10 22:27:53 | [diff] [blame] | 36 | #include "chrome/common/chrome_switches.h" |
[email protected] | af39f00 | 2014-08-22 10:18:18 | [diff] [blame] | 37 | #include "chrome/grit/chromium_strings.h" |
[email protected] | b39ef1cb | 2011-10-25 04:46:55 | [diff] [blame] | 38 | #include "content/public/common/result_codes.h" |
[email protected] | f394bc6 | 2012-08-22 21:42:29 | [diff] [blame] | 39 | #include "net/base/escape.h" |
[email protected] | c051a1b | 2011-01-21 23:30:17 | [diff] [blame] | 40 | #include "ui/base/l10n/l10n_util.h" |
[email protected] | bde885b | 2013-09-12 20:55:53 | [diff] [blame] | 41 | #include "ui/gfx/win/hwnd_util.h" |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 42 | |
| 43 | namespace { |
| 44 | |
[email protected] | 2b7ff5b9 | 2012-07-17 22:45:18 | [diff] [blame] | 45 | const char kLockfile[] = "lockfile"; |
| 46 | |
[email protected] | 14649ecd | 2012-12-05 01:00:24 | [diff] [blame] | 47 | // A helper class that acquires the given |mutex| while the AutoLockMutex is in |
| 48 | // scope. |
| 49 | class AutoLockMutex { |
| 50 | public: |
| 51 | explicit AutoLockMutex(HANDLE mutex) : mutex_(mutex) { |
[email protected] | b905730 | 2013-03-28 12:40:38 | [diff] [blame] | 52 | DWORD result = ::WaitForSingleObject(mutex_, INFINITE); |
[email protected] | 14649ecd | 2012-12-05 01:00:24 | [diff] [blame] | 53 | DPCHECK(result == WAIT_OBJECT_0) << "Result = " << result; |
| 54 | } |
| 55 | |
Peter Boström | 53c6c595 | 2021-09-17 09:41:26 | [diff] [blame^] | 56 | AutoLockMutex(const AutoLockMutex&) = delete; |
| 57 | AutoLockMutex& operator=(const AutoLockMutex&) = delete; |
| 58 | |
[email protected] | 14649ecd | 2012-12-05 01:00:24 | [diff] [blame] | 59 | ~AutoLockMutex() { |
[email protected] | b905730 | 2013-03-28 12:40:38 | [diff] [blame] | 60 | BOOL released = ::ReleaseMutex(mutex_); |
[email protected] | 14649ecd | 2012-12-05 01:00:24 | [diff] [blame] | 61 | DPCHECK(released); |
| 62 | } |
| 63 | |
| 64 | private: |
| 65 | HANDLE mutex_; |
[email protected] | 14649ecd | 2012-12-05 01:00:24 | [diff] [blame] | 66 | }; |
| 67 | |
| 68 | // A helper class that releases the given |mutex| while the AutoUnlockMutex is |
| 69 | // in scope and immediately re-acquires it when going out of scope. |
| 70 | class AutoUnlockMutex { |
| 71 | public: |
| 72 | explicit AutoUnlockMutex(HANDLE mutex) : mutex_(mutex) { |
[email protected] | b905730 | 2013-03-28 12:40:38 | [diff] [blame] | 73 | BOOL released = ::ReleaseMutex(mutex_); |
[email protected] | 14649ecd | 2012-12-05 01:00:24 | [diff] [blame] | 74 | DPCHECK(released); |
| 75 | } |
| 76 | |
Peter Boström | 53c6c595 | 2021-09-17 09:41:26 | [diff] [blame^] | 77 | AutoUnlockMutex(const AutoUnlockMutex&) = delete; |
| 78 | AutoUnlockMutex& operator=(const AutoUnlockMutex&) = delete; |
| 79 | |
[email protected] | 14649ecd | 2012-12-05 01:00:24 | [diff] [blame] | 80 | ~AutoUnlockMutex() { |
[email protected] | b905730 | 2013-03-28 12:40:38 | [diff] [blame] | 81 | DWORD result = ::WaitForSingleObject(mutex_, INFINITE); |
[email protected] | 14649ecd | 2012-12-05 01:00:24 | [diff] [blame] | 82 | DPCHECK(result == WAIT_OBJECT_0) << "Result = " << result; |
| 83 | } |
| 84 | |
| 85 | private: |
| 86 | HANDLE mutex_; |
[email protected] | 14649ecd | 2012-12-05 01:00:24 | [diff] [blame] | 87 | }; |
[email protected] | b3d791c9 | 2012-10-05 19:01:24 | [diff] [blame] | 88 | |
[email protected] | f891fb3 | 2009-04-08 00:20:32 | [diff] [blame] | 89 | // Checks the visibility of the enumerated window and signals once a visible |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 90 | // window has been found. |
| 91 | BOOL CALLBACK BrowserWindowEnumeration(HWND window, LPARAM param) { |
| 92 | bool* result = reinterpret_cast<bool*>(param); |
[email protected] | b905730 | 2013-03-28 12:40:38 | [diff] [blame] | 93 | *result = ::IsWindowVisible(window) != 0; |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 94 | // Stops enumeration if a visible window has been found. |
| 95 | return !*result; |
| 96 | } |
| 97 | |
[email protected] | edf04b51 | 2012-02-23 09:47:43 | [diff] [blame] | 98 | bool ParseCommandLine(const COPYDATASTRUCT* cds, |
avi | 556c0502 | 2014-12-22 23:31:43 | [diff] [blame] | 99 | base::CommandLine* parsed_command_line, |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 100 | base::FilePath* current_directory) { |
[email protected] | edf04b51 | 2012-02-23 09:47:43 | [diff] [blame] | 101 | // We should have enough room for the shortest command (min_message_size) |
| 102 | // and also be a multiple of wchar_t bytes. The shortest command |
| 103 | // possible is L"START\0\0" (empty current directory and command line). |
| 104 | static const int min_message_size = 7; |
| 105 | if (cds->cbData < min_message_size * sizeof(wchar_t) || |
| 106 | cds->cbData % sizeof(wchar_t) != 0) { |
| 107 | LOG(WARNING) << "Invalid WM_COPYDATA, length = " << cds->cbData; |
| 108 | return false; |
| 109 | } |
| 110 | |
| 111 | // We split the string into 4 parts on NULLs. |
| 112 | DCHECK(cds->lpData); |
| 113 | const std::wstring msg(static_cast<wchar_t*>(cds->lpData), |
| 114 | cds->cbData / sizeof(wchar_t)); |
| 115 | const std::wstring::size_type first_null = msg.find_first_of(L'\0'); |
| 116 | if (first_null == 0 || first_null == std::wstring::npos) { |
| 117 | // no NULL byte, don't know what to do |
| 118 | LOG(WARNING) << "Invalid WM_COPYDATA, length = " << msg.length() << |
| 119 | ", first null = " << first_null; |
| 120 | return false; |
| 121 | } |
| 122 | |
| 123 | // Decode the command, which is everything until the first NULL. |
| 124 | if (msg.substr(0, first_null) == L"START") { |
| 125 | // Another instance is starting parse the command line & do what it would |
| 126 | // have done. |
| 127 | VLOG(1) << "Handling STARTUP request from another process"; |
| 128 | const std::wstring::size_type second_null = |
| 129 | msg.find_first_of(L'\0', first_null + 1); |
| 130 | if (second_null == std::wstring::npos || |
| 131 | first_null == msg.length() - 1 || second_null == msg.length()) { |
| 132 | LOG(WARNING) << "Invalid format for start command, we need a string in 4 " |
| 133 | "parts separated by NULLs"; |
| 134 | return false; |
| 135 | } |
| 136 | |
| 137 | // Get current directory. |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 138 | *current_directory = base::FilePath(msg.substr(first_null + 1, |
[email protected] | b905730 | 2013-03-28 12:40:38 | [diff] [blame] | 139 | second_null - first_null)); |
[email protected] | edf04b51 | 2012-02-23 09:47:43 | [diff] [blame] | 140 | |
| 141 | const std::wstring::size_type third_null = |
| 142 | msg.find_first_of(L'\0', second_null + 1); |
| 143 | if (third_null == std::wstring::npos || |
| 144 | third_null == msg.length()) { |
| 145 | LOG(WARNING) << "Invalid format for start command, we need a string in 4 " |
| 146 | "parts separated by NULLs"; |
| 147 | } |
| 148 | |
| 149 | // Get command line. |
| 150 | const std::wstring cmd_line = |
| 151 | msg.substr(second_null + 1, third_null - second_null); |
avi | 556c0502 | 2014-12-22 23:31:43 | [diff] [blame] | 152 | *parsed_command_line = base::CommandLine::FromString(cmd_line); |
[email protected] | edf04b51 | 2012-02-23 09:47:43 | [diff] [blame] | 153 | return true; |
| 154 | } |
| 155 | return false; |
| 156 | } |
| 157 | |
[email protected] | 4cf04bb | 2013-07-11 09:22:38 | [diff] [blame] | 158 | bool ProcessLaunchNotification( |
| 159 | const ProcessSingleton::NotificationCallback& notification_callback, |
| 160 | UINT message, |
| 161 | WPARAM wparam, |
| 162 | LPARAM lparam, |
| 163 | LRESULT* result) { |
| 164 | if (message != WM_COPYDATA) |
| 165 | return false; |
| 166 | |
| 167 | // Handle the WM_COPYDATA message from another process. |
[email protected] | 4cf04bb | 2013-07-11 09:22:38 | [diff] [blame] | 168 | const COPYDATASTRUCT* cds = reinterpret_cast<COPYDATASTRUCT*>(lparam); |
| 169 | |
avi | 556c0502 | 2014-12-22 23:31:43 | [diff] [blame] | 170 | base::CommandLine parsed_command_line(base::CommandLine::NO_PROGRAM); |
[email protected] | 4cf04bb | 2013-07-11 09:22:38 | [diff] [blame] | 171 | base::FilePath current_directory; |
| 172 | if (!ParseCommandLine(cds, &parsed_command_line, ¤t_directory)) { |
| 173 | *result = TRUE; |
| 174 | return true; |
| 175 | } |
| 176 | |
| 177 | *result = notification_callback.Run(parsed_command_line, current_directory) ? |
| 178 | TRUE : FALSE; |
| 179 | return true; |
| 180 | } |
| 181 | |
siggi | 2a0214b | 2015-03-12 14:50:27 | [diff] [blame] | 182 | bool DisplayShouldKillMessageBox() { |
Collin Baker | 58ae65c | 2021-02-05 21:12:29 | [diff] [blame] | 183 | return chrome::ShowQuestionMessageBoxSync( |
siggi | 2a0214b | 2015-03-12 14:50:27 | [diff] [blame] | 184 | NULL, l10n_util::GetStringUTF16(IDS_PRODUCT_NAME), |
estade | e16f96ed | 2016-03-07 22:57:05 | [diff] [blame] | 185 | l10n_util::GetStringUTF16(IDS_BROWSER_HUNGBROWSER_MESSAGE)) != |
siggi | 2a0214b | 2015-03-12 14:50:27 | [diff] [blame] | 186 | chrome::MESSAGE_BOX_RESULT_NO; |
| 187 | } |
| 188 | |
aseren | 028ea15 | 2017-05-16 17:22:43 | [diff] [blame] | 189 | void SendRemoteProcessInteractionResultHistogram( |
| 190 | ProcessSingleton::RemoteProcessInteractionResult result) { |
| 191 | UMA_HISTOGRAM_ENUMERATION( |
| 192 | "Chrome.ProcessSingleton.RemoteProcessInteractionResult", result, |
| 193 | ProcessSingleton::REMOTE_PROCESS_INTERACTION_RESULT_COUNT); |
| 194 | } |
| 195 | |
| 196 | // Function was copied from Process::Terminate. |
| 197 | void TerminateProcessWithHistograms(const base::Process& process, |
| 198 | int exit_code) { |
| 199 | DCHECK(process.IsValid()); |
| 200 | base::TimeTicks start_time = base::TimeTicks::Now(); |
| 201 | bool result = (::TerminateProcess(process.Handle(), exit_code) != FALSE); |
| 202 | DWORD terminate_error = 0; |
| 203 | if (result) { |
| 204 | DWORD wait_error = 0; |
| 205 | // The process may not end immediately due to pending I/O |
| 206 | DWORD wait_result = ::WaitForSingleObject(process.Handle(), 60 * 1000); |
| 207 | if (wait_result != WAIT_OBJECT_0) { |
| 208 | if (wait_result == WAIT_FAILED) |
| 209 | wait_error = ::GetLastError(); |
| 210 | SendRemoteProcessInteractionResultHistogram( |
| 211 | ProcessSingleton::TERMINATE_WAIT_TIMEOUT); |
| 212 | DPLOG(ERROR) << "Error waiting for process exit"; |
| 213 | } else { |
| 214 | SendRemoteProcessInteractionResultHistogram( |
| 215 | ProcessSingleton::TERMINATE_SUCCEEDED); |
| 216 | } |
| 217 | base::debug::GlobalActivityTracker::RecordProcessExitIfEnabled( |
| 218 | process.Pid(), exit_code); |
| 219 | UMA_HISTOGRAM_TIMES("Chrome.ProcessSingleton.TerminateProcessTime", |
| 220 | base::TimeTicks::Now() - start_time); |
Ilya Sherman | 982457e6 | 2017-12-13 02:19:36 | [diff] [blame] | 221 | base::UmaHistogramSparse( |
aseren | 028ea15 | 2017-05-16 17:22:43 | [diff] [blame] | 222 | "Chrome.ProcessSingleton.TerminationWaitErrorCode.Windows", wait_error); |
| 223 | } else { |
| 224 | terminate_error = ::GetLastError(); |
| 225 | SendRemoteProcessInteractionResultHistogram( |
| 226 | ProcessSingleton::TERMINATE_FAILED); |
| 227 | DPLOG(ERROR) << "Unable to terminate process"; |
| 228 | } |
Ilya Sherman | 982457e6 | 2017-12-13 02:19:36 | [diff] [blame] | 229 | base::UmaHistogramSparse( |
Aleksei Seren | 75591e73 | 2017-12-01 21:47:45 | [diff] [blame] | 230 | "Chrome.ProcessSingleton.TerminateProcessErrorCode.Windows", |
aseren | 028ea15 | 2017-05-16 17:22:43 | [diff] [blame] | 231 | terminate_error); |
| 232 | } |
| 233 | |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 234 | } // namespace |
| 235 | |
[email protected] | 0a19455 | 2011-09-14 17:53:35 | [diff] [blame] | 236 | // Microsoft's Softricity virtualization breaks the sandbox processes. |
| 237 | // So, if we detect the Softricity DLL we use WMI Win32_Process.Create to |
| 238 | // break out of the virtualization environment. |
| 239 | // http://code.google.com/p/chromium/issues/detail?id=43650 |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 240 | bool ProcessSingleton::EscapeVirtualization( |
| 241 | const base::FilePath& user_data_dir) { |
[email protected] | 0a19455 | 2011-09-14 17:53:35 | [diff] [blame] | 242 | if (::GetModuleHandle(L"sftldr_wow64.dll") || |
| 243 | ::GetModuleHandle(L"sftldr.dll")) { |
| 244 | int process_id; |
Guido Urdaneta | 73a94302 | 2018-08-24 15:42:27 | [diff] [blame] | 245 | if (!base::win::WmiLaunchProcess(::GetCommandLineW(), &process_id)) |
[email protected] | 0a19455 | 2011-09-14 17:53:35 | [diff] [blame] | 246 | return false; |
| 247 | is_virtualized_ = true; |
| 248 | // The new window was spawned from WMI, and won't be in the foreground. |
| 249 | // So, first we sleep while the new chrome.exe instance starts (because |
| 250 | // WaitForInputIdle doesn't work here). Then we poll for up to two more |
| 251 | // seconds and make the window foreground if we find it (or we give up). |
| 252 | HWND hwnd = 0; |
| 253 | ::Sleep(90); |
| 254 | for (int tries = 200; tries; --tries) { |
[email protected] | 1011816 | 2013-06-05 00:24:45 | [diff] [blame] | 255 | hwnd = chrome::FindRunningChromeWindow(user_data_dir); |
[email protected] | 0a19455 | 2011-09-14 17:53:35 | [diff] [blame] | 256 | if (hwnd) { |
| 257 | ::SetForegroundWindow(hwnd); |
| 258 | break; |
| 259 | } |
| 260 | ::Sleep(10); |
| 261 | } |
| 262 | return true; |
| 263 | } |
| 264 | return false; |
| 265 | } |
| 266 | |
[email protected] | dd85d45 | 2013-03-28 12:39:59 | [diff] [blame] | 267 | ProcessSingleton::ProcessSingleton( |
| 268 | const base::FilePath& user_data_dir, |
| 269 | const NotificationCallback& notification_callback) |
[email protected] | 4cf04bb | 2013-07-11 09:22:38 | [diff] [blame] | 270 | : notification_callback_(notification_callback), |
siggi | 2a0214b | 2015-03-12 14:50:27 | [diff] [blame] | 271 | is_virtualized_(false), |
| 272 | lock_file_(INVALID_HANDLE_VALUE), |
| 273 | user_data_dir_(user_data_dir), |
| 274 | should_kill_remote_process_callback_( |
Alexander Cooper | 4bcb0ce | 2020-07-16 23:10:38 | [diff] [blame] | 275 | base::BindRepeating(&DisplayShouldKillMessageBox)) {} |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 276 | |
[email protected] | 7c47ae3e | 2009-02-18 00:34:21 | [diff] [blame] | 277 | ProcessSingleton::~ProcessSingleton() { |
gab | 25894fe | 2017-05-30 03:40:36 | [diff] [blame] | 278 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
[email protected] | 2b7ff5b9 | 2012-07-17 22:45:18 | [diff] [blame] | 279 | if (lock_file_ != INVALID_HANDLE_VALUE) |
[email protected] | b905730 | 2013-03-28 12:40:38 | [diff] [blame] | 280 | ::CloseHandle(lock_file_); |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 281 | } |
| 282 | |
[email protected] | 14649ecd | 2012-12-05 01:00:24 | [diff] [blame] | 283 | // Code roughly based on Mozilla. |
[email protected] | 9f20a6d0 | 2009-08-21 01:18:37 | [diff] [blame] | 284 | ProcessSingleton::NotifyResult ProcessSingleton::NotifyOtherProcess() { |
[email protected] | 0a19455 | 2011-09-14 17:53:35 | [diff] [blame] | 285 | if (is_virtualized_) |
| 286 | return PROCESS_NOTIFIED; // We already spawned the process in this case. |
[email protected] | e9613b5 | 2012-11-27 22:35:13 | [diff] [blame] | 287 | if (lock_file_ == INVALID_HANDLE_VALUE && !remote_window_) { |
[email protected] | 2b7ff5b9 | 2012-07-17 22:45:18 | [diff] [blame] | 288 | return LOCK_ERROR; |
[email protected] | e9613b5 | 2012-11-27 22:35:13 | [diff] [blame] | 289 | } else if (!remote_window_) { |
[email protected] | 9f20a6d0 | 2009-08-21 01:18:37 | [diff] [blame] | 290 | return PROCESS_NONE; |
[email protected] | e9613b5 | 2012-11-27 22:35:13 | [diff] [blame] | 291 | } |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 292 | |
Francois Doray | 1aaf5a2 | 2020-01-22 16:04:45 | [diff] [blame] | 293 | switch (chrome::AttemptToNotifyRunningChrome(remote_window_)) { |
[email protected] | 1011816 | 2013-06-05 00:24:45 | [diff] [blame] | 294 | case chrome::NOTIFY_SUCCESS: |
| 295 | return PROCESS_NOTIFIED; |
| 296 | case chrome::NOTIFY_FAILED: |
[email protected] | 0815b6d | 2009-02-11 00:39:37 | [diff] [blame] | 297 | remote_window_ = NULL; |
aseren | 028ea15 | 2017-05-16 17:22:43 | [diff] [blame] | 298 | SendRemoteProcessInteractionResultHistogram(RUNNING_PROCESS_NOTIFY_ERROR); |
[email protected] | 9f20a6d0 | 2009-08-21 01:18:37 | [diff] [blame] | 299 | return PROCESS_NONE; |
[email protected] | 1011816 | 2013-06-05 00:24:45 | [diff] [blame] | 300 | case chrome::NOTIFY_WINDOW_HUNG: |
siggi | d896f9a | 2015-10-07 17:19:27 | [diff] [blame] | 301 | // Fall through and potentially terminate the hung browser. |
[email protected] | 1011816 | 2013-06-05 00:24:45 | [diff] [blame] | 302 | break; |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 303 | } |
| 304 | |
manzagop | f071e88 | 2016-04-13 14:36:00 | [diff] [blame] | 305 | // The window is hung. |
[email protected] | 1011816 | 2013-06-05 00:24:45 | [diff] [blame] | 306 | DWORD process_id = 0; |
| 307 | DWORD thread_id = ::GetWindowThreadProcessId(remote_window_, &process_id); |
| 308 | if (!thread_id || !process_id) { |
[email protected] | 0815b6d | 2009-02-11 00:39:37 | [diff] [blame] | 309 | remote_window_ = NULL; |
aseren | 028ea15 | 2017-05-16 17:22:43 | [diff] [blame] | 310 | SendRemoteProcessInteractionResultHistogram(REMOTE_PROCESS_NOT_FOUND); |
[email protected] | 9f20a6d0 | 2009-08-21 01:18:37 | [diff] [blame] | 311 | return PROCESS_NONE; |
[email protected] | 0815b6d | 2009-02-11 00:39:37 | [diff] [blame] | 312 | } |
manzagop | f071e88 | 2016-04-13 14:36:00 | [diff] [blame] | 313 | |
| 314 | // Get a handle to the process that created the window. |
rvargas | 4683e5e | 2015-03-04 22:38:20 | [diff] [blame] | 315 | base::Process process = base::Process::Open(process_id); |
[email protected] | 0815b6d | 2009-02-11 00:39:37 | [diff] [blame] | 316 | |
manzagop | f071e88 | 2016-04-13 14:36:00 | [diff] [blame] | 317 | // Scan for every window to find a visible one. |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 318 | bool visible_window = false; |
[email protected] | b905730 | 2013-03-28 12:40:38 | [diff] [blame] | 319 | ::EnumThreadWindows(thread_id, |
| 320 | &BrowserWindowEnumeration, |
| 321 | reinterpret_cast<LPARAM>(&visible_window)); |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 322 | |
| 323 | // If there is a visible browser window, ask the user before killing it. |
siggi | 2a0214b | 2015-03-12 14:50:27 | [diff] [blame] | 324 | if (visible_window && !should_kill_remote_process_callback_.Run()) { |
Mikhail Atuchin | a6e8bcd3 | 2018-07-06 12:14:03 | [diff] [blame] | 325 | SendRemoteProcessInteractionResultHistogram(USER_REFUSED_TERMINATION); |
[email protected] | 5da155e | 2012-05-26 16:31:16 | [diff] [blame] | 326 | // The user denied. Quit silently. |
| 327 | return PROCESS_NOTIFIED; |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 328 | } |
aseren | 028ea15 | 2017-05-16 17:22:43 | [diff] [blame] | 329 | UMA_HISTOGRAM_ENUMERATION( |
| 330 | "Chrome.ProcessSingleton.RemoteHungProcessTerminateReason", |
| 331 | visible_window ? USER_ACCEPTED_TERMINATION : NO_VISIBLE_WINDOW_FOUND, |
| 332 | REMOTE_HUNG_PROCESS_TERMINATE_REASON_COUNT); |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 333 | |
| 334 | // Time to take action. Kill the browser process. |
aseren | 028ea15 | 2017-05-16 17:22:43 | [diff] [blame] | 335 | TerminateProcessWithHistograms(process, content::RESULT_CODE_HUNG); |
| 336 | |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 337 | remote_window_ = NULL; |
[email protected] | 9f20a6d0 | 2009-08-21 01:18:37 | [diff] [blame] | 338 | return PROCESS_NONE; |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 339 | } |
| 340 | |
[email protected] | 1011816 | 2013-06-05 00:24:45 | [diff] [blame] | 341 | ProcessSingleton::NotifyResult |
| 342 | ProcessSingleton::NotifyOtherProcessOrCreate() { |
Etienne Bergeron | bacd29d8 | 2021-09-15 20:39:12 | [diff] [blame] | 343 | TRACE_EVENT0("startup", "ProcessSingleton::NotifyOtherProcessOrCreate"); |
gab | 439f54f | 2016-10-07 22:24:22 | [diff] [blame] | 344 | const base::TimeTicks begin_ticks = base::TimeTicks::Now(); |
gcomanici | 6681567 | 2016-08-30 04:54:10 | [diff] [blame] | 345 | for (int i = 0; i < 2; ++i) { |
| 346 | if (Create()) { |
gab | 439f54f | 2016-10-07 22:24:22 | [diff] [blame] | 347 | UMA_HISTOGRAM_MEDIUM_TIMES("Chrome.ProcessSingleton.TimeToCreate", |
| 348 | base::TimeTicks::Now() - begin_ticks); |
gcomanici | 6681567 | 2016-08-30 04:54:10 | [diff] [blame] | 349 | return PROCESS_NONE; // This is the single browser process. |
| 350 | } |
| 351 | ProcessSingleton::NotifyResult result = NotifyOtherProcess(); |
| 352 | if (result == PROCESS_NOTIFIED || result == LOCK_ERROR) { |
gab | 439f54f | 2016-10-07 22:24:22 | [diff] [blame] | 353 | if (result == PROCESS_NOTIFIED) { |
| 354 | UMA_HISTOGRAM_MEDIUM_TIMES("Chrome.ProcessSingleton.TimeToNotify", |
| 355 | base::TimeTicks::Now() - begin_ticks); |
| 356 | } else { |
| 357 | UMA_HISTOGRAM_MEDIUM_TIMES("Chrome.ProcessSingleton.TimeToFailure", |
| 358 | base::TimeTicks::Now() - begin_ticks); |
| 359 | } |
gcomanici | 6681567 | 2016-08-30 04:54:10 | [diff] [blame] | 360 | // The single browser process was notified, the user chose not to |
| 361 | // terminate a hung browser, or the lock file could not be created. |
| 362 | // Nothing more to do. |
| 363 | return result; |
| 364 | } |
| 365 | DCHECK_EQ(PROCESS_NONE, result); |
| 366 | // The process could not be notified for some reason, or it was hung and |
| 367 | // terminated. Retry once if this is the first time; otherwise, fall through |
| 368 | // to report that the process must exit because the profile is in use. |
[email protected] | 14649ecd | 2012-12-05 01:00:24 | [diff] [blame] | 369 | } |
gab | 439f54f | 2016-10-07 22:24:22 | [diff] [blame] | 370 | UMA_HISTOGRAM_MEDIUM_TIMES("Chrome.ProcessSingleton.TimeToFailure", |
| 371 | base::TimeTicks::Now() - begin_ticks); |
gcomanici | 6681567 | 2016-08-30 04:54:10 | [diff] [blame] | 372 | return PROFILE_IN_USE; |
[email protected] | 4a44bc3 | 2010-05-28 22:22:44 | [diff] [blame] | 373 | } |
| 374 | |
[email protected] | 14649ecd | 2012-12-05 01:00:24 | [diff] [blame] | 375 | // Look for a Chrome instance that uses the same profile directory. If there |
| 376 | // isn't one, create a message window with its title set to the profile |
| 377 | // directory path. |
[email protected] | dd85d45 | 2013-03-28 12:39:59 | [diff] [blame] | 378 | bool ProcessSingleton::Create() { |
[email protected] | 14649ecd | 2012-12-05 01:00:24 | [diff] [blame] | 379 | static const wchar_t kMutexName[] = L"Local\\ChromeProcessSingletonStartup!"; |
[email protected] | 14649ecd | 2012-12-05 01:00:24 | [diff] [blame] | 380 | |
[email protected] | 1011816 | 2013-06-05 00:24:45 | [diff] [blame] | 381 | remote_window_ = chrome::FindRunningChromeWindow(user_data_dir_); |
[email protected] | 14649ecd | 2012-12-05 01:00:24 | [diff] [blame] | 382 | if (!remote_window_ && !EscapeVirtualization(user_data_dir_)) { |
| 383 | // Make sure we will be the one and only process creating the window. |
| 384 | // We use a named Mutex since we are protecting against multi-process |
| 385 | // access. As documented, it's clearer to NOT request ownership on creation |
| 386 | // since it isn't guaranteed we will get it. It is better to create it |
| 387 | // without ownership and explicitly get the ownership afterward. |
[email protected] | b905730 | 2013-03-28 12:40:38 | [diff] [blame] | 388 | base::win::ScopedHandle only_me(::CreateMutex(NULL, FALSE, kMutexName)); |
rvargas | d7743ba | 2014-09-25 01:35:28 | [diff] [blame] | 389 | if (!only_me.IsValid()) { |
| 390 | DPLOG(FATAL) << "CreateMutex failed"; |
| 391 | return false; |
| 392 | } |
[email protected] | 14649ecd | 2012-12-05 01:00:24 | [diff] [blame] | 393 | |
rvargas | d7743ba | 2014-09-25 01:35:28 | [diff] [blame] | 394 | AutoLockMutex auto_lock_only_me(only_me.Get()); |
[email protected] | 14649ecd | 2012-12-05 01:00:24 | [diff] [blame] | 395 | |
| 396 | // We now own the mutex so we are the only process that can create the |
| 397 | // window at this time, but we must still check if someone created it |
| 398 | // between the time where we looked for it above and the time the mutex |
| 399 | // was given to us. |
[email protected] | 1011816 | 2013-06-05 00:24:45 | [diff] [blame] | 400 | remote_window_ = chrome::FindRunningChromeWindow(user_data_dir_); |
[email protected] | 14649ecd | 2012-12-05 01:00:24 | [diff] [blame] | 401 | |
[email protected] | 14649ecd | 2012-12-05 01:00:24 | [diff] [blame] | 402 | if (!remote_window_) { |
| 403 | // We have to make sure there is no Chrome instance running on another |
| 404 | // machine that uses the same profile. |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 405 | base::FilePath lock_file_path = user_data_dir_.AppendASCII(kLockfile); |
[email protected] | b905730 | 2013-03-28 12:40:38 | [diff] [blame] | 406 | lock_file_ = ::CreateFile(lock_file_path.value().c_str(), |
| 407 | GENERIC_WRITE, |
| 408 | FILE_SHARE_READ, |
| 409 | NULL, |
| 410 | CREATE_ALWAYS, |
| 411 | FILE_ATTRIBUTE_NORMAL | |
| 412 | FILE_FLAG_DELETE_ON_CLOSE, |
| 413 | NULL); |
| 414 | DWORD error = ::GetLastError(); |
[email protected] | 14649ecd | 2012-12-05 01:00:24 | [diff] [blame] | 415 | LOG_IF(WARNING, lock_file_ != INVALID_HANDLE_VALUE && |
| 416 | error == ERROR_ALREADY_EXISTS) << "Lock file exists but is writable."; |
| 417 | LOG_IF(ERROR, lock_file_ == INVALID_HANDLE_VALUE) |
| 418 | << "Lock file can not be created! Error code: " << error; |
| 419 | |
| 420 | if (lock_file_ != INVALID_HANDLE_VALUE) { |
[email protected] | 14649ecd | 2012-12-05 01:00:24 | [diff] [blame] | 421 | // Set the window's title to the path of our user data directory so |
| 422 | // other Chrome instances can decide if they should forward to us. |
Alexander Cooper | 4bcb0ce | 2020-07-16 23:10:38 | [diff] [blame] | 423 | bool result = |
| 424 | window_.CreateNamed(base::BindRepeating(&ProcessLaunchNotification, |
| 425 | notification_callback_), |
| 426 | user_data_dir_.value()); |
[email protected] | 4cf04bb | 2013-07-11 09:22:38 | [diff] [blame] | 427 | CHECK(result && window_.hwnd()); |
[email protected] | 14649ecd | 2012-12-05 01:00:24 | [diff] [blame] | 428 | } |
[email protected] | 14649ecd | 2012-12-05 01:00:24 | [diff] [blame] | 429 | } |
| 430 | } |
| 431 | |
[email protected] | 4cf04bb | 2013-07-11 09:22:38 | [diff] [blame] | 432 | return window_.hwnd() != NULL; |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 433 | } |
| 434 | |
[email protected] | 9f20a6d0 | 2009-08-21 01:18:37 | [diff] [blame] | 435 | void ProcessSingleton::Cleanup() { |
| 436 | } |
siggi | 2a0214b | 2015-03-12 14:50:27 | [diff] [blame] | 437 | |
| 438 | void ProcessSingleton::OverrideShouldKillRemoteProcessCallbackForTesting( |
| 439 | const ShouldKillRemoteProcessCallback& display_dialog_callback) { |
| 440 | should_kill_remote_process_callback_ = display_dialog_callback; |
| 441 | } |