blob: 09dec13fb4e45344742a640131562bb8c2f23aaa [file] [log] [blame]
[email protected]fc14cef2009-01-27 22:17:291// Copyright (c) 2009 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.
4
[email protected]7c47ae3e2009-02-18 00:34:215#include "chrome/browser/process_singleton.h"
[email protected]fc14cef2009-01-27 22:17:296
[email protected]a92b8642009-05-05 23:38:567#include "app/l10n_util.h"
[email protected]4a0765a2009-05-08 23:12:258#include "app/win_util.h"
[email protected]fc14cef2009-01-27 22:17:299#include "base/base_paths.h"
10#include "base/command_line.h"
[email protected]9e9b6e8e2009-12-02 08:45:0111#include "base/path_service.h"
[email protected]fc14cef2009-01-27 22:17:2912#include "base/process_util.h"
[email protected]bbef41f02010-03-04 16:16:1913#include "base/scoped_handle.h"
[email protected]fc14cef2009-01-27 22:17:2914#include "base/win_util.h"
[email protected]fc14cef2009-01-27 22:17:2915#include "chrome/browser/browser_init.h"
16#include "chrome/browser/browser_process.h"
17#include "chrome/browser/profile.h"
18#include "chrome/browser/profile_manager.h"
19#include "chrome/common/chrome_constants.h"
20#include "chrome/common/chrome_paths.h"
[email protected]74d1bb02009-03-03 00:41:2321#include "chrome/common/result_codes.h"
[email protected]bbef41f02010-03-04 16:16:1922#include "chrome/installer/util/browser_distribution.h"
[email protected]34ac8f32009-02-22 23:03:2723#include "grit/chromium_strings.h"
24#include "grit/generated_resources.h"
[email protected]fc14cef2009-01-27 22:17:2925
26namespace {
27
[email protected]f891fb32009-04-08 00:20:3228// Checks the visibility of the enumerated window and signals once a visible
[email protected]fc14cef2009-01-27 22:17:2929// window has been found.
30BOOL CALLBACK BrowserWindowEnumeration(HWND window, LPARAM param) {
31 bool* result = reinterpret_cast<bool*>(param);
32 *result = IsWindowVisible(window) != 0;
33 // Stops enumeration if a visible window has been found.
34 return !*result;
35}
36
37} // namespace
38
[email protected]f891fb32009-04-08 00:20:3239// Look for a Chrome instance that uses the same profile directory.
[email protected]7c47ae3e2009-02-18 00:34:2140ProcessSingleton::ProcessSingleton(const FilePath& user_data_dir)
[email protected]175a7a22009-05-03 15:57:5341 : window_(NULL), locked_(false), foreground_window_(NULL) {
[email protected]bbef41f02010-03-04 16:16:1942 std::wstring user_data_dir_str(user_data_dir.ToWStringHack());
43 remote_window_ = FindWindowEx(HWND_MESSAGE, NULL,
44 chrome::kMessageWindowClass,
45 user_data_dir_str.c_str());
46 if (!remote_window_) {
47 // Make sure we will be the one and only process creating the window.
48 // We use a named Mutex since we are protecting against multi-process
49 // access. As documented, it's clearer to NOT request ownership on creation
50 // since it isn't guaranteed we will get it. It is better to create it
51 // without ownership and explicitly get the ownership afterward.
52 std::wstring mutex_name(L"Local\\ProcessSingletonStartup!");
53 mutex_name += BrowserDistribution::GetDistribution()->GetAppGuid();
54 ScopedHandle only_me(CreateMutex(NULL, FALSE, mutex_name.c_str()));
55 DCHECK(only_me.Get() != NULL) << "GetLastError = " << GetLastError();
56
57 // This is how we acquire the mutex (as opposed to the initial ownership).
58 DWORD result = WaitForSingleObject(only_me, INFINITE);
59 DCHECK(result == WAIT_OBJECT_0) << "Result = " << result <<
60 "GetLastError = " << GetLastError();
61
62 // We now own the mutex so we are the only process that can create the
63 // window at this time, but we must still check if someone created it
64 // between the time where we looked for it above and the time the mutex
65 // was given to us.
66 remote_window_ = FindWindowEx(HWND_MESSAGE, NULL,
67 chrome::kMessageWindowClass,
68 user_data_dir_str.c_str());
69 if (!remote_window_)
70 Create();
71 BOOL success = ReleaseMutex(only_me);
72 DCHECK(success) << "GetLastError = " << GetLastError();
73 }
[email protected]fc14cef2009-01-27 22:17:2974}
75
[email protected]7c47ae3e2009-02-18 00:34:2176ProcessSingleton::~ProcessSingleton() {
[email protected]aef15bd2009-04-27 20:51:5177 if (window_) {
[email protected]fc14cef2009-01-27 22:17:2978 DestroyWindow(window_);
[email protected]aef15bd2009-04-27 20:51:5179 UnregisterClass(chrome::kMessageWindowClass, GetModuleHandle(NULL));
80 }
[email protected]fc14cef2009-01-27 22:17:2981}
82
[email protected]9f20a6d02009-08-21 01:18:3783ProcessSingleton::NotifyResult ProcessSingleton::NotifyOtherProcess() {
[email protected]fc14cef2009-01-27 22:17:2984 if (!remote_window_)
[email protected]9f20a6d02009-08-21 01:18:3785 return PROCESS_NONE;
[email protected]fc14cef2009-01-27 22:17:2986
87 // Found another window, send our command line to it
88 // format is "START\0<<<current directory>>>\0<<<commandline>>>".
89 std::wstring to_send(L"START\0", 6); // want the NULL in the string.
90 std::wstring cur_dir;
91 if (!PathService::Get(base::DIR_CURRENT, &cur_dir))
[email protected]9f20a6d02009-08-21 01:18:3792 return PROCESS_NONE;
[email protected]fc14cef2009-01-27 22:17:2993 to_send.append(cur_dir);
94 to_send.append(L"\0", 1); // Null separator.
95 to_send.append(GetCommandLineW());
96 to_send.append(L"\0", 1); // Null separator.
97
98 // Allow the current running browser window making itself the foreground
99 // window (otherwise it will just flash in the taskbar).
100 DWORD process_id = 0;
101 DWORD thread_id = GetWindowThreadProcessId(remote_window_, &process_id);
[email protected]0815b6d2009-02-11 00:39:37102 // It is possible that the process owning this window may have died by now.
103 if (!thread_id || !process_id) {
104 remote_window_ = NULL;
[email protected]9f20a6d02009-08-21 01:18:37105 return PROCESS_NONE;
[email protected]0815b6d2009-02-11 00:39:37106 }
107
[email protected]fc14cef2009-01-27 22:17:29108 AllowSetForegroundWindow(process_id);
109
[email protected]fc14cef2009-01-27 22:17:29110 COPYDATASTRUCT cds;
111 cds.dwData = 0;
112 cds.cbData = static_cast<DWORD>((to_send.length() + 1) * sizeof(wchar_t));
113 cds.lpData = const_cast<wchar_t*>(to_send.c_str());
114 DWORD_PTR result = 0;
115 if (SendMessageTimeout(remote_window_,
116 WM_COPYDATA,
117 NULL,
118 reinterpret_cast<LPARAM>(&cds),
119 SMTO_ABORTIFHUNG,
[email protected]8b08cbd2009-08-04 05:34:19120 kTimeoutInSeconds * 1000,
[email protected]fc14cef2009-01-27 22:17:29121 &result)) {
[email protected]0815b6d2009-02-11 00:39:37122 // It is possible that the process owning this window may have died by now.
123 if (!result) {
124 remote_window_ = NULL;
[email protected]9f20a6d02009-08-21 01:18:37125 return PROCESS_NONE;
[email protected]0815b6d2009-02-11 00:39:37126 }
[email protected]9f20a6d02009-08-21 01:18:37127 return PROCESS_NOTIFIED;
[email protected]fc14cef2009-01-27 22:17:29128 }
129
[email protected]0815b6d2009-02-11 00:39:37130 // It is possible that the process owning this window may have died by now.
131 if (!IsWindow(remote_window_)) {
132 remote_window_ = NULL;
[email protected]9f20a6d02009-08-21 01:18:37133 return PROCESS_NONE;
[email protected]0815b6d2009-02-11 00:39:37134 }
135
[email protected]fc14cef2009-01-27 22:17:29136 // The window is hung. Scan for every window to find a visible one.
137 bool visible_window = false;
138 EnumThreadWindows(thread_id,
139 &BrowserWindowEnumeration,
140 reinterpret_cast<LPARAM>(&visible_window));
141
142 // If there is a visible browser window, ask the user before killing it.
143 if (visible_window) {
144 std::wstring text = l10n_util::GetString(IDS_BROWSER_HUNGBROWSER_MESSAGE);
145 std::wstring caption = l10n_util::GetString(IDS_PRODUCT_NAME);
146 if (IDYES != win_util::MessageBox(NULL, text, caption,
147 MB_YESNO | MB_ICONSTOP | MB_TOPMOST)) {
148 // The user denied. Quit silently.
[email protected]9f20a6d02009-08-21 01:18:37149 return PROCESS_NOTIFIED;
[email protected]fc14cef2009-01-27 22:17:29150 }
151 }
152
153 // Time to take action. Kill the browser process.
[email protected]cd4fd152009-02-09 19:28:41154 base::KillProcessById(process_id, ResultCodes::HUNG, true);
[email protected]fc14cef2009-01-27 22:17:29155 remote_window_ = NULL;
[email protected]9f20a6d02009-08-21 01:18:37156 return PROCESS_NONE;
[email protected]fc14cef2009-01-27 22:17:29157}
158
[email protected]f891fb32009-04-08 00:20:32159// For windows, there is no need to call Create() since the call is made in
160// the constructor but to avoid having more platform specific code in
161// browser_main.cc we tolerate a second call which will do nothing.
[email protected]4dd42242010-04-07 02:21:15162bool ProcessSingleton::Create() {
[email protected]fc14cef2009-01-27 22:17:29163 DCHECK(!remote_window_);
[email protected]f891fb32009-04-08 00:20:32164 if (window_)
[email protected]4dd42242010-04-07 02:21:15165 return true;
[email protected]f891fb32009-04-08 00:20:32166
[email protected]fc14cef2009-01-27 22:17:29167 HINSTANCE hinst = GetModuleHandle(NULL);
168
169 WNDCLASSEX wc = {0};
170 wc.cbSize = sizeof(wc);
[email protected]7c47ae3e2009-02-18 00:34:21171 wc.lpfnWndProc = ProcessSingleton::WndProcStatic;
[email protected]fc14cef2009-01-27 22:17:29172 wc.hInstance = hinst;
173 wc.lpszClassName = chrome::kMessageWindowClass;
[email protected]aef15bd2009-04-27 20:51:51174 ATOM clazz = RegisterClassEx(&wc);
175 DCHECK(clazz);
[email protected]fc14cef2009-01-27 22:17:29176
[email protected]ef5ff1b2009-09-09 20:00:13177 FilePath user_data_dir;
[email protected]fc14cef2009-01-27 22:17:29178 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir);
179
180 // Set the window's title to the path of our user data directory so other
181 // Chrome instances can decide if they should forward to us or not.
[email protected]ef5ff1b2009-09-09 20:00:13182 window_ = CreateWindow(chrome::kMessageWindowClass,
183 user_data_dir.value().c_str(),
[email protected]fc14cef2009-01-27 22:17:29184 0, 0, 0, 0, 0, HWND_MESSAGE, 0, hinst, 0);
185 DCHECK(window_);
186
187 win_util::SetWindowUserData(window_, this);
[email protected]4dd42242010-04-07 02:21:15188 return true;
[email protected]fc14cef2009-01-27 22:17:29189}
190
[email protected]9f20a6d02009-08-21 01:18:37191void ProcessSingleton::Cleanup() {
192}
193
[email protected]7c47ae3e2009-02-18 00:34:21194LRESULT ProcessSingleton::OnCopyData(HWND hwnd, const COPYDATASTRUCT* cds) {
[email protected]175a7a22009-05-03 15:57:53195 // If locked, it means we are not ready to process this message because
196 // we are probably in a first run critical phase. We must do this before
197 // doing the IsShuttingDown() check since that returns true during first run
198 // (since g_browser_process hasn't been AddRefModule()d yet).
199 if (locked_) {
200 // Attempt to place ourselves in the foreground / flash the task bar.
201 if (IsWindow(foreground_window_))
202 SetForegroundWindow(foreground_window_);
203 return TRUE;
204 }
205
[email protected]fc14cef2009-01-27 22:17:29206 // Ignore the request if the browser process is already in shutdown path.
[email protected]0815b6d2009-02-11 00:39:37207 if (!g_browser_process || g_browser_process->IsShuttingDown()) {
208 LOG(WARNING) << "Not handling WM_COPYDATA as browser is shutting down";
209 return FALSE;
210 }
[email protected]fc14cef2009-01-27 22:17:29211
[email protected]fc14cef2009-01-27 22:17:29212 // We should have enough room for the shortest command (min_message_size)
[email protected]366ffe52009-04-24 22:02:23213 // and also be a multiple of wchar_t bytes. The shortest command
214 // possible is L"START\0\0" (empty current directory and command line).
[email protected]fc14cef2009-01-27 22:17:29215 static const int min_message_size = 7;
[email protected]366ffe52009-04-24 22:02:23216 if (cds->cbData < min_message_size * sizeof(wchar_t) ||
217 cds->cbData % sizeof(wchar_t) != 0) {
[email protected]fc14cef2009-01-27 22:17:29218 LOG(WARNING) << "Invalid WM_COPYDATA, length = " << cds->cbData;
219 return TRUE;
220 }
221
222 // We split the string into 4 parts on NULLs.
[email protected]366ffe52009-04-24 22:02:23223 DCHECK(cds->lpData);
[email protected]fc14cef2009-01-27 22:17:29224 const std::wstring msg(static_cast<wchar_t*>(cds->lpData),
225 cds->cbData / sizeof(wchar_t));
226 const std::wstring::size_type first_null = msg.find_first_of(L'\0');
227 if (first_null == 0 || first_null == std::wstring::npos) {
228 // no NULL byte, don't know what to do
229 LOG(WARNING) << "Invalid WM_COPYDATA, length = " << msg.length() <<
230 ", first null = " << first_null;
231 return TRUE;
232 }
233
234 // Decode the command, which is everything until the first NULL.
235 if (msg.substr(0, first_null) == L"START") {
236 // Another instance is starting parse the command line & do what it would
237 // have done.
238 LOG(INFO) << "Handling STARTUP request from another process";
239 const std::wstring::size_type second_null =
240 msg.find_first_of(L'\0', first_null + 1);
241 if (second_null == std::wstring::npos ||
242 first_null == msg.length() - 1 || second_null == msg.length()) {
243 LOG(WARNING) << "Invalid format for start command, we need a string in 4 "
244 "parts separated by NULLs";
245 return TRUE;
246 }
247
248 // Get current directory.
249 const std::wstring cur_dir =
250 msg.substr(first_null + 1, second_null - first_null);
251
252 const std::wstring::size_type third_null =
253 msg.find_first_of(L'\0', second_null + 1);
254 if (third_null == std::wstring::npos ||
255 third_null == msg.length()) {
256 LOG(WARNING) << "Invalid format for start command, we need a string in 4 "
257 "parts separated by NULLs";
258 }
259
260 // Get command line.
261 const std::wstring cmd_line =
262 msg.substr(second_null + 1, third_null - second_null);
263
[email protected]51343d5a2009-10-26 22:39:33264 CommandLine parsed_command_line = CommandLine::FromString(cmd_line);
[email protected]fc14cef2009-01-27 22:17:29265 PrefService* prefs = g_browser_process->local_state();
266 DCHECK(prefs);
267
[email protected]ddf8a4b02010-03-22 23:08:30268 Profile* profile = ProfileManager::GetDefaultProfile();
[email protected]fc14cef2009-01-27 22:17:29269 if (!profile) {
270 // We should only be able to get here if the profile already exists and
271 // has been created.
272 NOTREACHED();
273 return TRUE;
274 }
[email protected]0303f31c2009-02-02 06:42:05275
276 // Run the browser startup sequence again, with the command line of the
277 // signalling process.
[email protected]7c27203a2009-03-10 21:55:21278 BrowserInit::ProcessCommandLine(parsed_command_line, cur_dir, false,
[email protected]0303f31c2009-02-02 06:42:05279 profile, NULL);
[email protected]fc14cef2009-01-27 22:17:29280 return TRUE;
281 }
282 return TRUE;
283}
284
[email protected]7c47ae3e2009-02-18 00:34:21285LRESULT CALLBACK ProcessSingleton::WndProc(HWND hwnd, UINT message,
[email protected]175a7a22009-05-03 15:57:53286 WPARAM wparam, LPARAM lparam) {
[email protected]fc14cef2009-01-27 22:17:29287 switch (message) {
288 case WM_COPYDATA:
289 return OnCopyData(reinterpret_cast<HWND>(wparam),
290 reinterpret_cast<COPYDATASTRUCT*>(lparam));
291 default:
292 break;
293 }
294
295 return ::DefWindowProc(hwnd, message, wparam, lparam);
296}