blob: 42a76597ff34eb3476f888cc3156a65298de8419 [file] [log] [blame]
[email protected]b90d7e802011-01-09 16:32:201// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]fc14cef2009-01-27 22:17:292// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]7c47ae3e2009-02-18 00:34:215#include "chrome/browser/process_singleton.h"
[email protected]fc14cef2009-01-27 22:17:296
7#include "base/base_paths.h"
8#include "base/command_line.h"
[email protected]f805fe82010-08-03 22:47:109#include "base/file_path.h"
[email protected]9e9b6e8e2009-12-02 08:45:0110#include "base/path_service.h"
[email protected]fc14cef2009-01-27 22:17:2911#include "base/process_util.h"
[email protected]0f26d7b2011-01-05 19:10:4412#include "base/utf_string_conversions.h"
[email protected]b90d7e802011-01-09 16:32:2013#include "base/win/scoped_handle.h"
[email protected]ecb924c2011-03-17 00:34:0914#include "base/win/wrapped_window_proc.h"
[email protected]fc14cef2009-01-27 22:17:2915#include "chrome/browser/browser_process.h"
[email protected]6aeac8342010-10-01 20:21:1816#include "chrome/browser/extensions/extensions_startup.h"
[email protected]8ecad5e2010-12-02 21:18:3317#include "chrome/browser/profiles/profile.h"
18#include "chrome/browser/profiles/profile_manager.h"
[email protected]80772ed2011-08-09 21:11:3819#include "chrome/browser/simple_message_box.h"
[email protected]f7002802010-11-12 19:50:2820#include "chrome/browser/ui/browser_init.h"
[email protected]fc14cef2009-01-27 22:17:2921#include "chrome/common/chrome_constants.h"
22#include "chrome/common/chrome_paths.h"
[email protected]6aeac8342010-10-01 20:21:1823#include "chrome/common/chrome_switches.h"
[email protected]0a194552011-09-14 17:53:3524#include "chrome/installer/util/wmi.h"
[email protected]4dd57932011-03-17 06:06:1225#include "content/common/result_codes.h"
[email protected]34ac8f32009-02-22 23:03:2726#include "grit/chromium_strings.h"
27#include "grit/generated_resources.h"
[email protected]c051a1b2011-01-21 23:30:1728#include "ui/base/l10n/l10n_util.h"
[email protected]e7661062011-01-19 22:16:5329#include "ui/base/win/hwnd_util.h"
[email protected]fc14cef2009-01-27 22:17:2930
31namespace {
32
[email protected]f891fb32009-04-08 00:20:3233// Checks the visibility of the enumerated window and signals once a visible
[email protected]fc14cef2009-01-27 22:17:2934// window has been found.
35BOOL CALLBACK BrowserWindowEnumeration(HWND window, LPARAM param) {
36 bool* result = reinterpret_cast<bool*>(param);
37 *result = IsWindowVisible(window) != 0;
38 // Stops enumeration if a visible window has been found.
39 return !*result;
40}
41
42} // namespace
43
[email protected]0a194552011-09-14 17:53:3544// Microsoft's Softricity virtualization breaks the sandbox processes.
45// So, if we detect the Softricity DLL we use WMI Win32_Process.Create to
46// break out of the virtualization environment.
47// http://code.google.com/p/chromium/issues/detail?id=43650
48bool ProcessSingleton::EscapeVirtualization(const FilePath& user_data_dir) {
49 if (::GetModuleHandle(L"sftldr_wow64.dll") ||
50 ::GetModuleHandle(L"sftldr.dll")) {
51 int process_id;
52 if (!installer::WMIProcess::Launch(GetCommandLineW(), &process_id))
53 return false;
54 is_virtualized_ = true;
55 // The new window was spawned from WMI, and won't be in the foreground.
56 // So, first we sleep while the new chrome.exe instance starts (because
57 // WaitForInputIdle doesn't work here). Then we poll for up to two more
58 // seconds and make the window foreground if we find it (or we give up).
59 HWND hwnd = 0;
60 ::Sleep(90);
61 for (int tries = 200; tries; --tries) {
62 hwnd = FindWindowEx(HWND_MESSAGE, NULL, chrome::kMessageWindowClass,
63 user_data_dir.value().c_str());
64 if (hwnd) {
65 ::SetForegroundWindow(hwnd);
66 break;
67 }
68 ::Sleep(10);
69 }
70 return true;
71 }
72 return false;
73}
74
[email protected]f891fb32009-04-08 00:20:3275// Look for a Chrome instance that uses the same profile directory.
[email protected]7c47ae3e2009-02-18 00:34:2176ProcessSingleton::ProcessSingleton(const FilePath& user_data_dir)
[email protected]0a194552011-09-14 17:53:3577 : window_(NULL), locked_(false), foreground_window_(NULL),
78 is_virtualized_(false) {
[email protected]bbef41f02010-03-04 16:16:1979 remote_window_ = FindWindowEx(HWND_MESSAGE, NULL,
80 chrome::kMessageWindowClass,
[email protected]8a205c02011-02-04 20:41:3381 user_data_dir.value().c_str());
[email protected]0a194552011-09-14 17:53:3582 if (!remote_window_ && !EscapeVirtualization(user_data_dir)) {
[email protected]bbef41f02010-03-04 16:16:1983 // Make sure we will be the one and only process creating the window.
84 // We use a named Mutex since we are protecting against multi-process
85 // access. As documented, it's clearer to NOT request ownership on creation
86 // since it isn't guaranteed we will get it. It is better to create it
87 // without ownership and explicitly get the ownership afterward.
[email protected]e132804b2010-12-22 12:48:2588 std::wstring mutex_name(L"Local\\ChromeProcessSingletonStartup!");
[email protected]b90d7e802011-01-09 16:32:2089 base::win::ScopedHandle only_me(
90 CreateMutex(NULL, FALSE, mutex_name.c_str()));
[email protected]bbef41f02010-03-04 16:16:1991 DCHECK(only_me.Get() != NULL) << "GetLastError = " << GetLastError();
92
93 // This is how we acquire the mutex (as opposed to the initial ownership).
94 DWORD result = WaitForSingleObject(only_me, INFINITE);
95 DCHECK(result == WAIT_OBJECT_0) << "Result = " << result <<
96 "GetLastError = " << GetLastError();
97
98 // We now own the mutex so we are the only process that can create the
99 // window at this time, but we must still check if someone created it
100 // between the time where we looked for it above and the time the mutex
101 // was given to us.
102 remote_window_ = FindWindowEx(HWND_MESSAGE, NULL,
103 chrome::kMessageWindowClass,
[email protected]8a205c02011-02-04 20:41:33104 user_data_dir.value().c_str());
[email protected]bbef41f02010-03-04 16:16:19105 if (!remote_window_)
106 Create();
107 BOOL success = ReleaseMutex(only_me);
108 DCHECK(success) << "GetLastError = " << GetLastError();
109 }
[email protected]fc14cef2009-01-27 22:17:29110}
111
[email protected]7c47ae3e2009-02-18 00:34:21112ProcessSingleton::~ProcessSingleton() {
[email protected]aef15bd2009-04-27 20:51:51113 if (window_) {
[email protected]fc14cef2009-01-27 22:17:29114 DestroyWindow(window_);
[email protected]aef15bd2009-04-27 20:51:51115 UnregisterClass(chrome::kMessageWindowClass, GetModuleHandle(NULL));
116 }
[email protected]fc14cef2009-01-27 22:17:29117}
118
[email protected]9f20a6d02009-08-21 01:18:37119ProcessSingleton::NotifyResult ProcessSingleton::NotifyOtherProcess() {
[email protected]0a194552011-09-14 17:53:35120 if (is_virtualized_)
121 return PROCESS_NOTIFIED; // We already spawned the process in this case.
122 else if (!remote_window_)
[email protected]9f20a6d02009-08-21 01:18:37123 return PROCESS_NONE;
[email protected]fc14cef2009-01-27 22:17:29124
125 // Found another window, send our command line to it
126 // format is "START\0<<<current directory>>>\0<<<commandline>>>".
127 std::wstring to_send(L"START\0", 6); // want the NULL in the string.
[email protected]b9696482010-11-30 23:56:18128 FilePath cur_dir;
[email protected]fc14cef2009-01-27 22:17:29129 if (!PathService::Get(base::DIR_CURRENT, &cur_dir))
[email protected]9f20a6d02009-08-21 01:18:37130 return PROCESS_NONE;
[email protected]b9696482010-11-30 23:56:18131 to_send.append(cur_dir.value());
[email protected]fc14cef2009-01-27 22:17:29132 to_send.append(L"\0", 1); // Null separator.
133 to_send.append(GetCommandLineW());
134 to_send.append(L"\0", 1); // Null separator.
135
136 // Allow the current running browser window making itself the foreground
137 // window (otherwise it will just flash in the taskbar).
138 DWORD process_id = 0;
139 DWORD thread_id = GetWindowThreadProcessId(remote_window_, &process_id);
[email protected]0815b6d2009-02-11 00:39:37140 // It is possible that the process owning this window may have died by now.
141 if (!thread_id || !process_id) {
142 remote_window_ = NULL;
[email protected]9f20a6d02009-08-21 01:18:37143 return PROCESS_NONE;
[email protected]0815b6d2009-02-11 00:39:37144 }
145
[email protected]fc14cef2009-01-27 22:17:29146 AllowSetForegroundWindow(process_id);
147
[email protected]fc14cef2009-01-27 22:17:29148 COPYDATASTRUCT cds;
149 cds.dwData = 0;
150 cds.cbData = static_cast<DWORD>((to_send.length() + 1) * sizeof(wchar_t));
151 cds.lpData = const_cast<wchar_t*>(to_send.c_str());
152 DWORD_PTR result = 0;
153 if (SendMessageTimeout(remote_window_,
154 WM_COPYDATA,
155 NULL,
156 reinterpret_cast<LPARAM>(&cds),
157 SMTO_ABORTIFHUNG,
[email protected]8b08cbd2009-08-04 05:34:19158 kTimeoutInSeconds * 1000,
[email protected]fc14cef2009-01-27 22:17:29159 &result)) {
[email protected]0815b6d2009-02-11 00:39:37160 // It is possible that the process owning this window may have died by now.
161 if (!result) {
162 remote_window_ = NULL;
[email protected]9f20a6d02009-08-21 01:18:37163 return PROCESS_NONE;
[email protected]0815b6d2009-02-11 00:39:37164 }
[email protected]9f20a6d02009-08-21 01:18:37165 return PROCESS_NOTIFIED;
[email protected]fc14cef2009-01-27 22:17:29166 }
167
[email protected]0815b6d2009-02-11 00:39:37168 // It is possible that the process owning this window may have died by now.
169 if (!IsWindow(remote_window_)) {
170 remote_window_ = NULL;
[email protected]9f20a6d02009-08-21 01:18:37171 return PROCESS_NONE;
[email protected]0815b6d2009-02-11 00:39:37172 }
173
[email protected]fc14cef2009-01-27 22:17:29174 // The window is hung. Scan for every window to find a visible one.
175 bool visible_window = false;
176 EnumThreadWindows(thread_id,
177 &BrowserWindowEnumeration,
178 reinterpret_cast<LPARAM>(&visible_window));
179
180 // If there is a visible browser window, ask the user before killing it.
181 if (visible_window) {
[email protected]80772ed2011-08-09 21:11:38182 string16 text = l10n_util::GetStringUTF16(IDS_BROWSER_HUNGBROWSER_MESSAGE);
183 string16 caption = l10n_util::GetStringUTF16(IDS_PRODUCT_NAME);
184 if (!browser::ShowYesNoBox(NULL, caption, text)) {
[email protected]fc14cef2009-01-27 22:17:29185 // The user denied. Quit silently.
[email protected]9f20a6d02009-08-21 01:18:37186 return PROCESS_NOTIFIED;
[email protected]fc14cef2009-01-27 22:17:29187 }
188 }
189
190 // Time to take action. Kill the browser process.
[email protected]1fcfb202011-07-19 19:53:14191 base::KillProcessById(process_id, content::RESULT_CODE_HUNG, true);
[email protected]fc14cef2009-01-27 22:17:29192 remote_window_ = NULL;
[email protected]9f20a6d02009-08-21 01:18:37193 return PROCESS_NONE;
[email protected]fc14cef2009-01-27 22:17:29194}
195
[email protected]4a44bc32010-05-28 22:22:44196ProcessSingleton::NotifyResult ProcessSingleton::NotifyOtherProcessOrCreate() {
197 NotifyResult result = NotifyOtherProcess();
198 if (result != PROCESS_NONE)
199 return result;
200 return Create() ? PROCESS_NONE : PROFILE_IN_USE;
201}
202
[email protected]f891fb32009-04-08 00:20:32203// For windows, there is no need to call Create() since the call is made in
204// the constructor but to avoid having more platform specific code in
205// browser_main.cc we tolerate a second call which will do nothing.
[email protected]4dd42242010-04-07 02:21:15206bool ProcessSingleton::Create() {
[email protected]fc14cef2009-01-27 22:17:29207 DCHECK(!remote_window_);
[email protected]f891fb32009-04-08 00:20:32208 if (window_)
[email protected]4dd42242010-04-07 02:21:15209 return true;
[email protected]f891fb32009-04-08 00:20:32210
[email protected]fc14cef2009-01-27 22:17:29211 HINSTANCE hinst = GetModuleHandle(NULL);
212
213 WNDCLASSEX wc = {0};
214 wc.cbSize = sizeof(wc);
[email protected]ecb924c2011-03-17 00:34:09215 wc.lpfnWndProc =
216 base::win::WrappedWindowProc<ProcessSingleton::WndProcStatic>;
[email protected]fc14cef2009-01-27 22:17:29217 wc.hInstance = hinst;
218 wc.lpszClassName = chrome::kMessageWindowClass;
[email protected]aef15bd2009-04-27 20:51:51219 ATOM clazz = RegisterClassEx(&wc);
220 DCHECK(clazz);
[email protected]fc14cef2009-01-27 22:17:29221
[email protected]ef5ff1b2009-09-09 20:00:13222 FilePath user_data_dir;
[email protected]fc14cef2009-01-27 22:17:29223 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir);
224
225 // Set the window's title to the path of our user data directory so other
226 // Chrome instances can decide if they should forward to us or not.
[email protected]ef5ff1b2009-09-09 20:00:13227 window_ = CreateWindow(chrome::kMessageWindowClass,
228 user_data_dir.value().c_str(),
[email protected]fc14cef2009-01-27 22:17:29229 0, 0, 0, 0, 0, HWND_MESSAGE, 0, hinst, 0);
[email protected]a62d09f2011-04-19 19:06:33230 ui::CheckWindowCreated(window_);
[email protected]e7661062011-01-19 22:16:53231 ui::SetWindowUserData(window_, this);
[email protected]4dd42242010-04-07 02:21:15232 return true;
[email protected]fc14cef2009-01-27 22:17:29233}
234
[email protected]9f20a6d02009-08-21 01:18:37235void ProcessSingleton::Cleanup() {
236}
237
[email protected]7c47ae3e2009-02-18 00:34:21238LRESULT ProcessSingleton::OnCopyData(HWND hwnd, const COPYDATASTRUCT* cds) {
[email protected]175a7a22009-05-03 15:57:53239 // If locked, it means we are not ready to process this message because
[email protected]afd20c022010-06-10 00:48:20240 // we are probably in a first run critical phase.
[email protected]175a7a22009-05-03 15:57:53241 if (locked_) {
242 // Attempt to place ourselves in the foreground / flash the task bar.
243 if (IsWindow(foreground_window_))
244 SetForegroundWindow(foreground_window_);
245 return TRUE;
246 }
247
[email protected]fc14cef2009-01-27 22:17:29248 // Ignore the request if the browser process is already in shutdown path.
[email protected]0815b6d2009-02-11 00:39:37249 if (!g_browser_process || g_browser_process->IsShuttingDown()) {
250 LOG(WARNING) << "Not handling WM_COPYDATA as browser is shutting down";
251 return FALSE;
252 }
[email protected]fc14cef2009-01-27 22:17:29253
[email protected]fc14cef2009-01-27 22:17:29254 // We should have enough room for the shortest command (min_message_size)
[email protected]366ffe52009-04-24 22:02:23255 // and also be a multiple of wchar_t bytes. The shortest command
256 // possible is L"START\0\0" (empty current directory and command line).
[email protected]fc14cef2009-01-27 22:17:29257 static const int min_message_size = 7;
[email protected]366ffe52009-04-24 22:02:23258 if (cds->cbData < min_message_size * sizeof(wchar_t) ||
259 cds->cbData % sizeof(wchar_t) != 0) {
[email protected]fc14cef2009-01-27 22:17:29260 LOG(WARNING) << "Invalid WM_COPYDATA, length = " << cds->cbData;
261 return TRUE;
262 }
263
264 // We split the string into 4 parts on NULLs.
[email protected]366ffe52009-04-24 22:02:23265 DCHECK(cds->lpData);
[email protected]fc14cef2009-01-27 22:17:29266 const std::wstring msg(static_cast<wchar_t*>(cds->lpData),
267 cds->cbData / sizeof(wchar_t));
268 const std::wstring::size_type first_null = msg.find_first_of(L'\0');
269 if (first_null == 0 || first_null == std::wstring::npos) {
270 // no NULL byte, don't know what to do
271 LOG(WARNING) << "Invalid WM_COPYDATA, length = " << msg.length() <<
272 ", first null = " << first_null;
273 return TRUE;
274 }
275
276 // Decode the command, which is everything until the first NULL.
277 if (msg.substr(0, first_null) == L"START") {
278 // Another instance is starting parse the command line & do what it would
279 // have done.
[email protected]8e96e502010-10-21 20:57:12280 VLOG(1) << "Handling STARTUP request from another process";
[email protected]fc14cef2009-01-27 22:17:29281 const std::wstring::size_type second_null =
282 msg.find_first_of(L'\0', first_null + 1);
283 if (second_null == std::wstring::npos ||
284 first_null == msg.length() - 1 || second_null == msg.length()) {
285 LOG(WARNING) << "Invalid format for start command, we need a string in 4 "
286 "parts separated by NULLs";
287 return TRUE;
288 }
289
290 // Get current directory.
[email protected]f805fe82010-08-03 22:47:10291 const FilePath cur_dir(msg.substr(first_null + 1,
292 second_null - first_null));
[email protected]fc14cef2009-01-27 22:17:29293
294 const std::wstring::size_type third_null =
295 msg.find_first_of(L'\0', second_null + 1);
296 if (third_null == std::wstring::npos ||
297 third_null == msg.length()) {
298 LOG(WARNING) << "Invalid format for start command, we need a string in 4 "
299 "parts separated by NULLs";
300 }
301
302 // Get command line.
303 const std::wstring cmd_line =
304 msg.substr(second_null + 1, third_null - second_null);
305
[email protected]51343d5a2009-10-26 22:39:33306 CommandLine parsed_command_line = CommandLine::FromString(cmd_line);
[email protected]fc14cef2009-01-27 22:17:29307 PrefService* prefs = g_browser_process->local_state();
308 DCHECK(prefs);
309
[email protected]ddf8a4b02010-03-22 23:08:30310 Profile* profile = ProfileManager::GetDefaultProfile();
[email protected]fc14cef2009-01-27 22:17:29311 if (!profile) {
312 // We should only be able to get here if the profile already exists and
313 // has been created.
314 NOTREACHED();
315 return TRUE;
316 }
[email protected]0303f31c2009-02-02 06:42:05317
[email protected]6aeac8342010-10-01 20:21:18318 // Handle the --uninstall-extension startup action. This needs to done here
319 // in the process that is running with the target profile, otherwise the
320 // uninstall will fail to unload and remove all components.
321 if (parsed_command_line.HasSwitch(switches::kUninstallExtension)) {
[email protected]ccd875e72010-12-13 23:49:02322 ExtensionsStartupUtil ext_startup_util;
323 ext_startup_util.UninstallExtension(parsed_command_line, profile);
[email protected]6aeac8342010-10-01 20:21:18324 return TRUE;
325 }
326
[email protected]0303f31c2009-02-02 06:42:05327 // Run the browser startup sequence again, with the command line of the
328 // signalling process.
[email protected]7c27203a2009-03-10 21:55:21329 BrowserInit::ProcessCommandLine(parsed_command_line, cur_dir, false,
[email protected]0303f31c2009-02-02 06:42:05330 profile, NULL);
[email protected]fc14cef2009-01-27 22:17:29331 return TRUE;
332 }
333 return TRUE;
334}
335
[email protected]7c47ae3e2009-02-18 00:34:21336LRESULT CALLBACK ProcessSingleton::WndProc(HWND hwnd, UINT message,
[email protected]175a7a22009-05-03 15:57:53337 WPARAM wparam, LPARAM lparam) {
[email protected]fc14cef2009-01-27 22:17:29338 switch (message) {
339 case WM_COPYDATA:
340 return OnCopyData(reinterpret_cast<HWND>(wparam),
341 reinterpret_cast<COPYDATASTRUCT*>(lparam));
342 default:
343 break;
344 }
345
346 return ::DefWindowProc(hwnd, message, wparam, lparam);
347}